xref: /openbmc/bmcweb/features/redfish/include/utils/collection.hpp (revision 8d1b46d7f8d39db2ba048f9e9007106ca3a28c9b)
1116bcc5cSGunnar Mills #pragma once
2116bcc5cSGunnar Mills 
3116bcc5cSGunnar Mills #include <boost/container/flat_map.hpp>
4116bcc5cSGunnar Mills 
5116bcc5cSGunnar Mills #include <string>
6116bcc5cSGunnar Mills #include <vector>
7116bcc5cSGunnar Mills 
8116bcc5cSGunnar Mills namespace redfish
9116bcc5cSGunnar Mills {
10116bcc5cSGunnar Mills namespace collection_util
11116bcc5cSGunnar Mills {
12116bcc5cSGunnar Mills 
1305030b8eSGunnar Mills /**
14dea43dd4SJonathan Doman  * @brief Populate the collection "Members" from a GetSubTreePaths search of
1505030b8eSGunnar Mills  *        inventory
1605030b8eSGunnar Mills  *
1705030b8eSGunnar Mills  * @param[i,o] aResp  Async response object
1805030b8eSGunnar Mills  * @param[i]   collectionPath  Redfish collection path which is used for the
1905030b8eSGunnar Mills  *             Members Redfish Path
2005030b8eSGunnar Mills  * @param[i]   interfaces  List of interfaces to constrain the GetSubTree search
21dea43dd4SJonathan Doman  * @param[in]  subtree     D-Bus base path to constrain search to.
2205030b8eSGunnar Mills  *
2305030b8eSGunnar Mills  * @return void
2405030b8eSGunnar Mills  */
25dea43dd4SJonathan Doman inline void
26*8d1b46d7Szhanghch05     getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> aResp,
2705030b8eSGunnar Mills                          const std::string& collectionPath,
28dea43dd4SJonathan Doman                          const std::vector<const char*>& interfaces,
29dea43dd4SJonathan Doman                          const char* subtree = "/xyz/openbmc_project/inventory")
30116bcc5cSGunnar Mills {
3105030b8eSGunnar Mills     BMCWEB_LOG_DEBUG << "Get collection members for: " << collectionPath;
32116bcc5cSGunnar Mills     crow::connections::systemBus->async_method_call(
33dea43dd4SJonathan Doman         [collectionPath,
34dea43dd4SJonathan Doman          aResp{std::move(aResp)}](const boost::system::error_code ec,
35dea43dd4SJonathan Doman                                   const std::vector<std::string>& objects) {
36116bcc5cSGunnar Mills             if (ec)
37116bcc5cSGunnar Mills             {
38116bcc5cSGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
39116bcc5cSGunnar Mills                 messages::internalError(aResp->res);
40116bcc5cSGunnar Mills                 return;
41116bcc5cSGunnar Mills             }
42116bcc5cSGunnar Mills             nlohmann::json& members = aResp->res.jsonValue["Members"];
43116bcc5cSGunnar Mills             members = nlohmann::json::array();
44116bcc5cSGunnar Mills 
45dea43dd4SJonathan Doman             for (const auto& object : objects)
46116bcc5cSGunnar Mills             {
472dfd18efSEd Tanous                 sdbusplus::message::object_path path(object);
482dfd18efSEd Tanous                 std::string leaf = path.filename();
492dfd18efSEd Tanous                 if (leaf.empty())
50116bcc5cSGunnar Mills                 {
512dfd18efSEd Tanous                     continue;
52116bcc5cSGunnar Mills                 }
532dfd18efSEd Tanous                 std::string newPath = collectionPath;
542dfd18efSEd Tanous                 newPath += '/';
552dfd18efSEd Tanous                 newPath += leaf;
562dfd18efSEd Tanous                 members.push_back({{"@odata.id", std::move(newPath)}});
57116bcc5cSGunnar Mills             }
58116bcc5cSGunnar Mills             aResp->res.jsonValue["Members@odata.count"] = members.size();
59116bcc5cSGunnar Mills         },
60116bcc5cSGunnar Mills         "xyz.openbmc_project.ObjectMapper",
61116bcc5cSGunnar Mills         "/xyz/openbmc_project/object_mapper",
62dea43dd4SJonathan Doman         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", subtree, 0,
63dea43dd4SJonathan Doman         interfaces);
64116bcc5cSGunnar Mills }
65116bcc5cSGunnar Mills 
66116bcc5cSGunnar Mills } // namespace collection_util
67116bcc5cSGunnar Mills } // namespace redfish
68