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 /**
14*dea43dd4SJonathan 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
21*dea43dd4SJonathan Doman  * @param[in]  subtree     D-Bus base path to constrain search to.
2205030b8eSGunnar Mills  *
2305030b8eSGunnar Mills  * @return void
2405030b8eSGunnar Mills  */
25*dea43dd4SJonathan Doman inline void
26*dea43dd4SJonathan Doman     getCollectionMembers(std::shared_ptr<AsyncResp> aResp,
2705030b8eSGunnar Mills                          const std::string& collectionPath,
28*dea43dd4SJonathan Doman                          const std::vector<const char*>& interfaces,
29*dea43dd4SJonathan 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(
33*dea43dd4SJonathan Doman         [collectionPath,
34*dea43dd4SJonathan Doman          aResp{std::move(aResp)}](const boost::system::error_code ec,
35*dea43dd4SJonathan 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 
45*dea43dd4SJonathan Doman             for (const auto& object : objects)
46116bcc5cSGunnar Mills             {
47*dea43dd4SJonathan Doman                 auto pos = object.rfind('/');
48*dea43dd4SJonathan Doman                 if ((pos != std::string::npos) && (pos < (object.size() - 1)))
49116bcc5cSGunnar Mills                 {
50116bcc5cSGunnar Mills                     members.push_back(
51*dea43dd4SJonathan Doman                         {{"@odata.id",
52*dea43dd4SJonathan Doman                           collectionPath + "/" + object.substr(pos + 1)}});
53116bcc5cSGunnar Mills                 }
54116bcc5cSGunnar Mills             }
55116bcc5cSGunnar Mills             aResp->res.jsonValue["Members@odata.count"] = members.size();
56116bcc5cSGunnar Mills         },
57116bcc5cSGunnar Mills         "xyz.openbmc_project.ObjectMapper",
58116bcc5cSGunnar Mills         "/xyz/openbmc_project/object_mapper",
59*dea43dd4SJonathan Doman         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", subtree, 0,
60*dea43dd4SJonathan Doman         interfaces);
61116bcc5cSGunnar Mills }
62116bcc5cSGunnar Mills 
63116bcc5cSGunnar Mills } // namespace collection_util
64116bcc5cSGunnar Mills } // namespace redfish
65