xref: /openbmc/bmcweb/features/redfish/include/utils/collection.hpp (revision 27ea7db1c2a40bdb3402a9ffda335d2876f604fb)
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
268d1b46d7Szhanghch05     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) {
36*27ea7db1SEd Tanous             if (ec == boost::system::errc::io_error)
37*27ea7db1SEd Tanous             {
38*27ea7db1SEd Tanous                 aResp->res.jsonValue["Members"] = nlohmann::json::array();
39*27ea7db1SEd Tanous                 aResp->res.jsonValue["Members@odata.count"] = 0;
40*27ea7db1SEd Tanous                 return;
41*27ea7db1SEd Tanous             }
42*27ea7db1SEd Tanous 
43116bcc5cSGunnar Mills             if (ec)
44116bcc5cSGunnar Mills             {
45*27ea7db1SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec.value();
46116bcc5cSGunnar Mills                 messages::internalError(aResp->res);
47116bcc5cSGunnar Mills                 return;
48116bcc5cSGunnar Mills             }
49116bcc5cSGunnar Mills             nlohmann::json& members = aResp->res.jsonValue["Members"];
50116bcc5cSGunnar Mills             members = nlohmann::json::array();
51116bcc5cSGunnar Mills 
52dea43dd4SJonathan Doman             for (const auto& object : objects)
53116bcc5cSGunnar Mills             {
542dfd18efSEd Tanous                 sdbusplus::message::object_path path(object);
552dfd18efSEd Tanous                 std::string leaf = path.filename();
562dfd18efSEd Tanous                 if (leaf.empty())
57116bcc5cSGunnar Mills                 {
582dfd18efSEd Tanous                     continue;
59116bcc5cSGunnar Mills                 }
602dfd18efSEd Tanous                 std::string newPath = collectionPath;
612dfd18efSEd Tanous                 newPath += '/';
622dfd18efSEd Tanous                 newPath += leaf;
632dfd18efSEd Tanous                 members.push_back({{"@odata.id", std::move(newPath)}});
64116bcc5cSGunnar Mills             }
65116bcc5cSGunnar Mills             aResp->res.jsonValue["Members@odata.count"] = members.size();
66116bcc5cSGunnar Mills         },
67116bcc5cSGunnar Mills         "xyz.openbmc_project.ObjectMapper",
68116bcc5cSGunnar Mills         "/xyz/openbmc_project/object_mapper",
69dea43dd4SJonathan Doman         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", subtree, 0,
70dea43dd4SJonathan Doman         interfaces);
71116bcc5cSGunnar Mills }
72116bcc5cSGunnar Mills 
73116bcc5cSGunnar Mills } // namespace collection_util
74116bcc5cSGunnar Mills } // namespace redfish
75