1 #pragma once
2 
3 #include <boost/container/flat_map.hpp>
4 
5 #include <string>
6 #include <vector>
7 
8 namespace redfish
9 {
10 namespace collection_util
11 {
12 
13 /**
14  * @brief Populate the collection "Members" from a GetSubTreePaths search of
15  *        inventory
16  *
17  * @param[i,o] aResp  Async response object
18  * @param[i]   collectionPath  Redfish collection path which is used for the
19  *             Members Redfish Path
20  * @param[i]   interfaces  List of interfaces to constrain the GetSubTree search
21  * @param[in]  subtree     D-Bus base path to constrain search to.
22  *
23  * @return void
24  */
25 inline void
26     getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> aResp,
27                          const std::string& collectionPath,
28                          const std::vector<const char*>& interfaces,
29                          const char* subtree = "/xyz/openbmc_project/inventory")
30 {
31     BMCWEB_LOG_DEBUG << "Get collection members for: " << collectionPath;
32     crow::connections::systemBus->async_method_call(
33         [collectionPath,
34          aResp{std::move(aResp)}](const boost::system::error_code ec,
35                                   const std::vector<std::string>& objects) {
36             if (ec == boost::system::errc::io_error)
37             {
38                 aResp->res.jsonValue["Members"] = nlohmann::json::array();
39                 aResp->res.jsonValue["Members@odata.count"] = 0;
40                 return;
41             }
42 
43             if (ec)
44             {
45                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec.value();
46                 messages::internalError(aResp->res);
47                 return;
48             }
49             nlohmann::json& members = aResp->res.jsonValue["Members"];
50             members = nlohmann::json::array();
51 
52             for (const auto& object : objects)
53             {
54                 sdbusplus::message::object_path path(object);
55                 std::string leaf = path.filename();
56                 if (leaf.empty())
57                 {
58                     continue;
59                 }
60                 std::string newPath = collectionPath;
61                 newPath += '/';
62                 newPath += leaf;
63                 members.push_back({{"@odata.id", std::move(newPath)}});
64             }
65             aResp->res.jsonValue["Members@odata.count"] = members.size();
66         },
67         "xyz.openbmc_project.ObjectMapper",
68         "/xyz/openbmc_project/object_mapper",
69         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", subtree, 0,
70         interfaces);
71 }
72 
73 } // namespace collection_util
74 } // namespace redfish
75