unit-test: Move processing of interfaces added

Make it easier to unit test and continue reduction of main.cpp

Change-Id: Id360255e1546eda026e5e6ef9f15d29dcc82caaa
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/src/processing.cpp b/src/processing.cpp
index 3cd348b..6dd6493 100644
--- a/src/processing.cpp
+++ b/src/processing.cpp
@@ -1,6 +1,7 @@
 #include "processing.hpp"
 
 #include <boost/algorithm/string/predicate.hpp>
+#include <iostream>
 
 bool getWellKnown(
     const boost::container::flat_map<std::string, std::string>& owners,
@@ -82,3 +83,88 @@
         pathIt++;
     }
 }
+
+void processInterfaceAdded(interface_map_type& interfaceMap,
+                           const sdbusplus::message::object_path& objPath,
+                           const InterfacesAdded& intfAdded,
+                           const std::string& wellKnown,
+                           AssociationOwnersType& assocOwners,
+                           AssociationInterfaces& assocInterfaces,
+                           sdbusplus::asio::object_server& server)
+{
+    auto& ifaceList = interfaceMap[objPath.str];
+
+    for (const auto& interfacePair : intfAdded)
+    {
+        ifaceList[wellKnown].emplace(interfacePair.first);
+
+        if (interfacePair.first == ASSOCIATIONS_INTERFACE)
+        {
+            const sdbusplus::message::variant<std::vector<Association>>*
+                variantAssociations = nullptr;
+            for (const auto& interface : interfacePair.second)
+            {
+                if (interface.first == "associations")
+                {
+                    variantAssociations = &(interface.second);
+                }
+            }
+            if (variantAssociations == nullptr)
+            {
+                std::cerr << "Illegal association found on " << wellKnown
+                          << "\n";
+                continue;
+            }
+            std::vector<Association> associations =
+                sdbusplus::message::variant_ns::get<std::vector<Association>>(
+                    *variantAssociations);
+            associationChanged(server, associations, objPath.str, wellKnown,
+                               assocOwners, assocInterfaces);
+        }
+    }
+
+    // To handle the case where an object path is being created
+    // with 2 or more new path segments, check if the parent paths
+    // of this path are already in the interface map, and add them
+    // if they aren't with just the default freedesktop interfaces.
+    // This would be done via introspection if they would have
+    // already existed at startup.  While we could also introspect
+    // them now to do the work, we know there aren't any other
+    // interfaces or we would have gotten signals for them as well,
+    // so take a shortcut to speed things up.
+    //
+    // This is all needed so that mapper operations can be done
+    // on the new parent paths.
+    using iface_map_iterator = interface_map_type::iterator;
+    using iface_map_value_type =
+        boost::container::flat_map<std::string,
+                                   boost::container::flat_set<std::string>>;
+    using name_map_iterator = iface_map_value_type::iterator;
+
+    static const boost::container::flat_set<std::string> defaultIfaces{
+        "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
+        "org.freedesktop.DBus.Properties"};
+
+    std::string parent = objPath.str;
+    auto pos = parent.find_last_of('/');
+
+    while (pos != std::string::npos)
+    {
+        parent = parent.substr(0, pos);
+
+        std::pair<iface_map_iterator, bool> parentEntry =
+            interfaceMap.insert(std::make_pair(parent, iface_map_value_type{}));
+
+        std::pair<name_map_iterator, bool> ifaceEntry =
+            parentEntry.first->second.insert(
+                std::make_pair(wellKnown, defaultIfaces));
+
+        if (!ifaceEntry.second)
+        {
+            // Entry was already there for this name so done.
+            break;
+        }
+
+        pos = parent.find_last_of('/');
+    }
+}