From 4511b33fe27dbb2cc8839f7e2ee365b12c398f20 Mon Sep 17 00:00:00 2001 From: Andrew Geissler Date: Thu, 21 Feb 2019 15:40:40 -0600 Subject: unit-test: Move associationChanged() Make it easier to unit test and continue reduction of main.cpp Change-Id: Ic549e096343e7a2fb11985f1c48879ed4486e40b Signed-off-by: Andrew Geissler --- src/associations.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++ src/associations.hpp | 27 ++++++++++++++ src/main.cpp | 103 +++------------------------------------------------ 3 files changed, 125 insertions(+), 97 deletions(-) diff --git a/src/associations.cpp b/src/associations.cpp index 352db9b..6982b52 100644 --- a/src/associations.cpp +++ b/src/associations.cpp @@ -1,5 +1,7 @@ #include "associations.hpp" +#include + void removeAssociation(const std::string& sourcePath, const std::string& owner, sdbusplus::asio::object_server& server, AssociationOwnersType& assocOwners, @@ -138,3 +140,93 @@ void checkAssociationEndpointRemoves( } } } + +void associationChanged(sdbusplus::asio::object_server& objectServer, + const std::vector& associations, + const std::string& path, const std::string& owner, + AssociationOwnersType& assocOwners, + AssociationInterfaces& assocInterfaces) +{ + AssociationPaths objects; + + for (const Association& association : associations) + { + std::string forward; + std::string reverse; + std::string endpoint; + std::tie(forward, reverse, endpoint) = association; + + if (forward.size()) + { + objects[path + "/" + forward].emplace(endpoint); + } + if (reverse.size()) + { + if (endpoint.empty()) + { + std::cerr << "Found invalid association on path " << path + << "\n"; + continue; + } + objects[endpoint + "/" + reverse].emplace(path); + } + } + for (const auto& object : objects) + { + // the mapper exposes the new association interface but intakes + // the old + + auto& iface = assocInterfaces[object.first]; + auto& i = std::get(iface); + auto& endpoints = std::get(iface); + + // Only add new endpoints + for (auto& e : object.second) + { + if (std::find(endpoints.begin(), endpoints.end(), e) == + endpoints.end()) + { + endpoints.push_back(e); + } + } + + // If the interface already exists, only need to update + // the property value, otherwise create it + if (i) + { + i->set_property("endpoints", endpoints); + } + else + { + i = objectServer.add_interface(object.first, + XYZ_ASSOCIATION_INTERFACE); + i->register_property("endpoints", endpoints); + i->initialize(); + } + } + + // Check for endpoints being removed instead of added + checkAssociationEndpointRemoves(path, owner, objects, objectServer, + assocOwners, assocInterfaces); + + // Update associationOwners with the latest info + auto a = assocOwners.find(path); + if (a != assocOwners.end()) + { + auto o = a->second.find(owner); + if (o != a->second.end()) + { + o->second = std::move(objects); + } + else + { + a->second.emplace(owner, std::move(objects)); + } + } + else + { + boost::container::flat_map owners; + owners.emplace(owner, std::move(objects)); + assocOwners.emplace(path, owners); + } +} diff --git a/src/associations.hpp b/src/associations.hpp index 7fa15ab..7cd3842 100644 --- a/src/associations.hpp +++ b/src/associations.hpp @@ -8,6 +8,9 @@ #include #include +constexpr const char* XYZ_ASSOCIATION_INTERFACE = + "xyz.openbmc_project.Association"; + // Associations and some metadata are stored in associationInterfaces. // The fields are: // * ifacePos - holds the D-Bus interface object @@ -42,6 +45,8 @@ using AssociationPaths = using AssociationOwnersType = boost::container::flat_map< std::string, boost::container::flat_map>; +using Association = std::tuple; + /** @brief Remove input association * * @param[in] sourcePath - Path of the object that contains the @@ -102,3 +107,25 @@ void checkAssociationEndpointRemoves( const AssociationPaths& newAssociations, sdbusplus::asio::object_server& objectServer, AssociationOwnersType& assocOwners, AssociationInterfaces& assocInterfaces); + +/** @brief Handle new or changed association interfaces + * + * Called when either a new org.openbmc.Associations interface was + * created, or the associations property on that interface changed + * + * @param[in,out] objectServer - sdbus system object + * @param[in] associations - New associations to look at for change + * @param[in] path - Path of the object that contains the + * org.openbmc.Associations + * @param[in] owner - The Dbus service having it's associatons + * changed + * @param[in,out] assocOwners - Owners of associations + * @param[in,out] assocInterfaces - Associations endpoints + * + * @return Void, objectServer and assocOwners updated if needed + */ +void associationChanged(sdbusplus::asio::object_server& objectServer, + const std::vector& associations, + const std::string& path, const std::string& owner, + AssociationOwnersType& assocOwners, + AssociationInterfaces& assocInterfaces); diff --git a/src/main.cpp b/src/main.cpp index 781e690..3208a17 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,10 +15,6 @@ constexpr const char* OBJECT_MAPPER_DBUS_NAME = "xyz.openbmc_project.ObjectMapper"; -constexpr const char* XYZ_ASSOCIATION_INTERFACE = - "xyz.openbmc_project.Association"; - -using Association = std::tuple; AssociationInterfaces associationInterfaces; AssociationOwnersType associationOwners; @@ -130,96 +126,6 @@ struct InProgressIntrospect #endif }; -// Called when either a new org.openbmc.Associations interface was -// created, or the associations property on that interface changed. -void associationChanged(sdbusplus::asio::object_server& objectServer, - const std::vector& associations, - const std::string& path, const std::string& owner) -{ - AssociationPaths objects; - - for (const Association& association : associations) - { - std::string forward; - std::string reverse; - std::string endpoint; - std::tie(forward, reverse, endpoint) = association; - - if (forward.size()) - { - objects[path + "/" + forward].emplace(endpoint); - } - if (reverse.size()) - { - if (endpoint.empty()) - { - std::cerr << "Found invalid association on path " << path - << "\n"; - continue; - } - objects[endpoint + "/" + reverse].emplace(path); - } - } - for (const auto& object : objects) - { - // the mapper exposes the new association interface but intakes - // the old - - auto& iface = associationInterfaces[object.first]; - auto& i = std::get(iface); - auto& endpoints = std::get(iface); - - // Only add new endpoints - for (auto& e : object.second) - { - if (std::find(endpoints.begin(), endpoints.end(), e) == - endpoints.end()) - { - endpoints.push_back(e); - } - } - - // If the interface already exists, only need to update - // the property value, otherwise create it - if (i) - { - i->set_property("endpoints", endpoints); - } - else - { - i = objectServer.add_interface(object.first, - XYZ_ASSOCIATION_INTERFACE); - i->register_property("endpoints", endpoints); - i->initialize(); - } - } - - // Check for endpoints being removed instead of added - checkAssociationEndpointRemoves(path, owner, objects, objectServer, - associationOwners, associationInterfaces); - - // Update associationOwners with the latest info - auto a = associationOwners.find(path); - if (a != associationOwners.end()) - { - auto o = a->second.find(owner); - if (o != a->second.end()) - { - o->second = std::move(objects); - } - else - { - a->second.emplace(owner, std::move(objects)); - } - } - else - { - boost::container::flat_map owners; - owners.emplace(owner, std::move(objects)); - associationOwners.emplace(path, owners); - } -} - void do_associations(sdbusplus::asio::connection* system_bus, sdbusplus::asio::object_server& objectServer, const std::string& processName, const std::string& path) @@ -236,7 +142,8 @@ void do_associations(sdbusplus::asio::connection* system_bus, std::vector associations = sdbusplus::message::variant_ns::get>( variantAssociations); - associationChanged(objectServer, associations, path, processName); + associationChanged(objectServer, associations, path, processName, + associationOwners, associationInterfaces); }, processName, path, "org.freedesktop.DBus.Properties", "Get", ASSOCIATIONS_INTERFACE, "associations"); @@ -632,7 +539,8 @@ int main(int argc, char** argv) sdbusplus::message::variant_ns::get< std::vector>(*variantAssociations); associationChanged(server, associations, obj_path.str, - well_known); + well_known, associationOwners, + associationInterfaces); } } @@ -766,7 +674,8 @@ int main(int argc, char** argv) return; } associationChanged(server, associations, message.get_path(), - well_known); + well_known, associationOwners, + associationInterfaces); } }; sdbusplus::bus::match::match associationChanged( -- cgit v1.2.1