control: Set owner state for all service entries

When the owner state of a service for an object is set, set the
specific object's service owner state and then update all other objects'
owner state that share the same service name and interface.

Also fixed nameOwnerChanged signal handler setOwner() parameter order.

Change-Id: I1e23193770d9880101b540f1c3abe7f517797b85
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/manager.cpp b/control/json/manager.cpp
index 3653300..37b8244 100644
--- a/control/json/manager.cpp
+++ b/control/json/manager.cpp
@@ -167,29 +167,40 @@
 void Manager::setOwner(const std::string& path, const std::string& serv,
                        const std::string& intf, bool isOwned)
 {
-    auto itServ = _servTree.find(path);
-    if (itServ == _servTree.end())
+    // Set owner state for specific object given
+    auto& ownIntf = _servTree[path][serv];
+    ownIntf.first = isOwned;
+    auto itIntf = std::find_if(
+        ownIntf.second.begin(), ownIntf.second.end(),
+        [&intf](const auto& interface) { return intf == interface; });
+    if (itIntf == std::end(ownIntf.second))
     {
-        auto intfs = {intf};
-        _servTree[path] = {{serv, std::make_pair(isOwned, intfs)}};
-        return;
+        ownIntf.second.emplace_back(intf);
     }
-    for (auto& service : itServ->second)
+
+    // Update owner state on all entries of the same `serv` & `intf`
+    for (auto& itPath : _servTree)
     {
-        auto itIntf = std::find_if(
-            service.second.second.begin(), service.second.second.end(),
-            [&intf](const auto& interface) { return intf == interface; });
-        if (itIntf != std::end(service.second.second))
+        if (itPath.first == path)
         {
-            if (service.first == serv)
+            // Already set/updated owner on this path for `serv` & `intf`
+            continue;
+        }
+        for (auto& itServ : itPath.second)
+        {
+            if (itServ.first != serv)
             {
-                service.second.first = isOwned;
-                return;
+                continue;
+            }
+            auto itIntf = std::find_if(
+                itServ.second.second.begin(), itServ.second.second.end(),
+                [&intf](const auto& interface) { return intf == interface; });
+            if (itIntf != std::end(itServ.second.second))
+            {
+                itServ.second.first = isOwned;
             }
         }
     }
-    auto intfs = {intf};
-    itServ->second[serv] = std::make_pair(isOwned, intfs);
 }
 
 const std::string& Manager::findService(const std::string& path,
diff --git a/control/json/triggers/handlers.hpp b/control/json/triggers/handlers.hpp
index f3b1a23..1ad4766 100644
--- a/control/json/triggers/handlers.hpp
+++ b/control/json/triggers/handlers.hpp
@@ -151,7 +151,7 @@
             hasOwner = true;
         }
 
-        mgr.setOwner(std::get<Path>(obj), std::get<Intf>(obj), serv, hasOwner);
+        mgr.setOwner(std::get<Path>(obj), serv, std::get<Intf>(obj), hasOwner);
         return true;
     }
 };