Refactor getCollectionMembers
This commit refactors the getCollectionMembers function into smaller
functions. Additionally, the 'subtree' parameter is no longer a
default parameter but is explicitly required in the function. All
calls to getCollectionMembers have been updated to pass the 'subtree'
parameter.
Tested: Validator passed
'''
curl -k https://$bmc/redfish/v1/Systems/system/Storage
{
"@odata.id": "/redfish/v1/Systems/system/Storage",
"@odata.type": "#StorageCollection.StorageCollection",
"Members": [
{
"@odata.id": "/redfish/v1/Systems/system/Storage/1"
}
],
"Members@odata.count": 1,
"Name": "Storage Collection"
}
curl -k https://$bmc/redfish/v1/Cables
{
"@odata.id": "/redfish/v1/Cables",
"@odata.type": "#CableCollection.CableCollection",
"Description": "Collection of Cable Entries",
"Members": [
{
"@odata.id": "/redfish/v1/Cables/dp0_cable0"
},
{
"@odata.id": "/redfish/v1/Cables/dp0_cable1"
},
{
"@odata.id": "/redfish/v1/Cables/dp0_cable2"
},
{
"@odata.id": "/redfish/v1/Cables/dp0_cable3"
}
],
"Members@odata.count": 4,
"Name": "Cable Collection"
}
curl -k https://$bmc/redfish/v1/Chassis
{
"@odata.id": "/redfish/v1/Chassis",
"@odata.type": "#ChassisCollection.ChassisCollection",
"Members": [
{
"@odata.id": "/redfish/v1/Chassis/chassis"
}
],
"Members@odata.count": 1,
"Name": "Chassis Collection"
}
curl -k https://$bmc/redfish/v1/Systems/system/Memory
{
"@odata.id": "/redfish/v1/Systems/system/Memory",
"@odata.type": "#MemoryCollection.MemoryCollection",
"Members": [
{
"@odata.id": "/redfish/v1/Systems/system/Memory/dimm0"
},
{
"@odata.id": "/redfish/v1/Systems/system/Memory/dimm1"
},
......
{
"@odata.id": "/redfish/v1/Systems/system/Memory/dimm31"
}
],
"Members@odata.count": 32,
"Name": "Memory Module Collection"
}
'''
Change-Id: If5091431b548f371bff03b2897fd0aaf8b0ef203
Signed-off-by: Lakshmi Yadlapati <lakshmiy@us.ibm.com>
diff --git a/redfish-core/include/utils/collection.hpp b/redfish-core/include/utils/collection.hpp
index 0801cd5..7d28c0f 100644
--- a/redfish-core/include/utils/collection.hpp
+++ b/redfish-core/include/utils/collection.hpp
@@ -20,6 +20,51 @@
namespace collection_util
{
+inline void handleCollectionMembers(
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const boost::urls::url& collectionPath, const boost::system::error_code& ec,
+ const dbus::utility::MapperGetSubTreePathsResponse& objects)
+{
+ if (ec == boost::system::errc::io_error)
+ {
+ asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
+ asyncResp->res.jsonValue["Members@odata.count"] = 0;
+ return;
+ }
+
+ if (ec)
+ {
+ BMCWEB_LOG_DEBUG("DBUS response error {}", ec.value());
+ messages::internalError(asyncResp->res);
+ return;
+ }
+
+ std::vector<std::string> pathNames;
+ for (const auto& object : objects)
+ {
+ sdbusplus::message::object_path path(object);
+ std::string leaf = path.filename();
+ if (leaf.empty())
+ {
+ continue;
+ }
+ pathNames.push_back(leaf);
+ }
+ std::ranges::sort(pathNames, AlphanumLess<std::string>());
+
+ nlohmann::json& members = asyncResp->res.jsonValue["Members"];
+ members = nlohmann::json::array();
+ for (const std::string& leaf : pathNames)
+ {
+ boost::urls::url url = collectionPath;
+ crow::utility::appendUrlPieces(url, leaf);
+ nlohmann::json::object_t member;
+ member["@odata.id"] = std::move(url);
+ members.emplace_back(std::move(member));
+ }
+ asyncResp->res.jsonValue["Members@odata.count"] = members.size();
+}
+
/**
* @brief Populate the collection "Members" from a GetSubTreePaths search of
* inventory
@@ -33,56 +78,15 @@
* @return void
*/
inline void
- getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
+ getCollectionMembers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const boost::urls::url& collectionPath,
std::span<const std::string_view> interfaces,
- const char* subtree = "/xyz/openbmc_project/inventory")
+ const std::string& subtree)
{
BMCWEB_LOG_DEBUG("Get collection members for: {}", collectionPath.buffer());
dbus::utility::getSubTreePaths(
subtree, 0, interfaces,
- [collectionPath, asyncResp{std::move(asyncResp)}](
- const boost::system::error_code& ec,
- const dbus::utility::MapperGetSubTreePathsResponse& objects) {
- if (ec == boost::system::errc::io_error)
- {
- asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
- asyncResp->res.jsonValue["Members@odata.count"] = 0;
- return;
- }
-
- if (ec)
- {
- BMCWEB_LOG_DEBUG("DBUS response error {}", ec.value());
- messages::internalError(asyncResp->res);
- return;
- }
-
- std::vector<std::string> pathNames;
- for (const auto& object : objects)
- {
- sdbusplus::message::object_path path(object);
- std::string leaf = path.filename();
- if (leaf.empty())
- {
- continue;
- }
- pathNames.push_back(leaf);
- }
- std::ranges::sort(pathNames, AlphanumLess<std::string>());
-
- nlohmann::json& members = asyncResp->res.jsonValue["Members"];
- members = nlohmann::json::array();
- for (const std::string& leaf : pathNames)
- {
- boost::urls::url url = collectionPath;
- crow::utility::appendUrlPieces(url, leaf);
- nlohmann::json::object_t member;
- member["@odata.id"] = std::move(url);
- members.emplace_back(std::move(member));
- }
- asyncResp->res.jsonValue["Members@odata.count"] = members.size();
- });
+ std::bind_front(handleCollectionMembers, asyncResp, collectionPath));
}
} // namespace collection_util
diff --git a/redfish-core/lib/cable.hpp b/redfish-core/lib/cable.hpp
index 28031e1..a39d00a 100644
--- a/redfish-core/lib/cable.hpp
+++ b/redfish-core/lib/cable.hpp
@@ -210,7 +210,8 @@
constexpr std::array<std::string_view, 1> interfaces{
"xyz.openbmc_project.Inventory.Item.Cable"};
collection_util::getCollectionMembers(
- asyncResp, boost::urls::url("/redfish/v1/Cables"), interfaces);
+ asyncResp, boost::urls::url("/redfish/v1/Cables"), interfaces,
+ "/xyz/openbmc_project/inventory");
});
}
diff --git a/redfish-core/lib/chassis.hpp b/redfish-core/lib/chassis.hpp
index 969973f..0d14d4e 100644
--- a/redfish-core/lib/chassis.hpp
+++ b/redfish-core/lib/chassis.hpp
@@ -209,7 +209,8 @@
"xyz.openbmc_project.Inventory.Item.Board",
"xyz.openbmc_project.Inventory.Item.Chassis"};
collection_util::getCollectionMembers(
- asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces);
+ asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces,
+ "/xyz/openbmc_project/inventory");
}
inline void getChassisContainedBy(
diff --git a/redfish-core/lib/fabric_adapters.hpp b/redfish-core/lib/fabric_adapters.hpp
index 2a85f42..317348f 100644
--- a/redfish-core/lib/fabric_adapters.hpp
+++ b/redfish-core/lib/fabric_adapters.hpp
@@ -309,7 +309,7 @@
collection_util::getCollectionMembers(
asyncResp,
boost::urls::url("/redfish/v1/Systems/system/FabricAdapters"),
- interfaces);
+ interfaces, "/xyz/openbmc_project/inventory");
}
inline void handleFabricAdapterCollectionHead(
diff --git a/redfish-core/lib/memory.hpp b/redfish-core/lib/memory.hpp
index 551a579..45a3789 100644
--- a/redfish-core/lib/memory.hpp
+++ b/redfish-core/lib/memory.hpp
@@ -810,7 +810,7 @@
"xyz.openbmc_project.Inventory.Item.Dimm"};
collection_util::getCollectionMembers(
asyncResp, boost::urls::url("/redfish/v1/Systems/system/Memory"),
- interfaces);
+ interfaces, "/xyz/openbmc_project/inventory");
});
}
diff --git a/redfish-core/lib/processor.hpp b/redfish-core/lib/processor.hpp
index 78f25d4..988d5cc 100644
--- a/redfish-core/lib/processor.hpp
+++ b/redfish-core/lib/processor.hpp
@@ -1232,7 +1232,7 @@
boost::urls::format(
"/redfish/v1/Systems/system/Processors/{}/OperatingConfigs",
cpuName),
- interface, object.c_str());
+ interface, object);
return;
}
});
@@ -1362,7 +1362,7 @@
collection_util::getCollectionMembers(
asyncResp,
boost::urls::url("/redfish/v1/Systems/system/Processors"),
- processorInterfaces);
+ processorInterfaces, "/xyz/openbmc_project/inventory");
});
}
diff --git a/redfish-core/lib/storage.hpp b/redfish-core/lib/storage.hpp
index 389a20b..74c88f7 100644
--- a/redfish-core/lib/storage.hpp
+++ b/redfish-core/lib/storage.hpp
@@ -69,7 +69,7 @@
};
collection_util::getCollectionMembers(
asyncResp, boost::urls::format("/redfish/v1/Systems/system/Storage"),
- interface);
+ interface, "/xyz/openbmc_project/inventory");
}
inline void handleStorageCollectionGet(
@@ -88,7 +88,8 @@
"xyz.openbmc_project.Inventory.Item.Storage"
};
collection_util::getCollectionMembers(
- asyncResp, boost::urls::format("/redfish/v1/Storage"), interface);
+ asyncResp, boost::urls::format("/redfish/v1/Storage"), interface,
+ "/xyz/openbmc_project/inventory");
}
inline void requestRoutesStorageCollection(App& app)