Add function to find assocs based on endpoint
The helper function findAssociations can find all
associations based on an endpoint path and return
enough information to recreate those associations
later.
For example, searching for something like "endpointA"
could return:
owner: "ownerA"
Association{"typeA", "typeB", "endpointB"}
Which implies the association:
endpointA/typeA -> endpointB/typeB
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I0fbcf5397435f10b3072dd2640b342ee47d52f9b
diff --git a/src/associations.cpp b/src/associations.cpp
index f9aa6dc..1393ed9 100644
--- a/src/associations.cpp
+++ b/src/associations.cpp
@@ -1,5 +1,6 @@
#include "associations.hpp"
+#include <boost/algorithm/string/predicate.hpp>
#include <iostream>
void removeAssociation(const std::string& sourcePath, const std::string& owner,
@@ -413,3 +414,58 @@
assocMaps.pending.erase(objectPath);
}
}
+
+void findAssociations(const std::string& endpointPath,
+ AssociationMaps& assocMaps,
+ FindAssocResults& associationData)
+{
+ for (const auto& [sourcePath, owners] : assocMaps.owners)
+ {
+ for (const auto& [owner, assocs] : owners)
+ {
+ for (const auto& [assocPath, endpoints] : assocs)
+ {
+ if (std::find(endpoints.begin(), endpoints.end(),
+ endpointPath) != endpoints.end())
+ {
+ // assocPath is <path>/<type> which tells us what is on the
+ // other side of the association.
+ auto pos = assocPath.rfind('/');
+ auto otherPath = assocPath.substr(0, pos);
+ auto otherType = assocPath.substr(pos + 1);
+
+ // Now we need to find the endpointPath/<type> ->
+ // [otherPath] entry so that we can get the type for
+ // endpointPath's side of the assoc. Do this by finding
+ // otherPath as an endpoint, and also checking for
+ // 'endpointPath/*' as the key.
+ auto a = std::find_if(
+ assocs.begin(), assocs.end(),
+ [&endpointPath, &otherPath](const auto& ap) {
+ const auto& endpoints = ap.second;
+ auto endpoint = std::find(
+ endpoints.begin(), endpoints.end(), otherPath);
+ if (endpoint != endpoints.end())
+ {
+ return boost::starts_with(ap.first,
+ endpointPath + '/');
+ }
+ return false;
+ });
+
+ if (a != assocs.end())
+ {
+ // Pull out the type from endpointPath/<type>
+ pos = a->first.rfind('/');
+ auto thisType = a->first.substr(pos + 1);
+
+ // Now we know the full association:
+ // endpointPath/thisType -> otherPath/otherType
+ Association association{thisType, otherType, otherPath};
+ associationData.emplace_back(owner, association);
+ }
+ }
+ }
+ }
+ }
+}