xref: /openbmc/bmcweb/features/redfish/include/utils/collection.hpp (revision 05030b8edfeeb351c902bc5d36c5041796b8df6d)
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 
13*05030b8eSGunnar Mills /**
14*05030b8eSGunnar Mills  * @brief Populate the collection "Members" from a GetSubTree search of
15*05030b8eSGunnar Mills  *        inventory
16*05030b8eSGunnar Mills  *
17*05030b8eSGunnar Mills  * @param[i,o] aResp  Async response object
18*05030b8eSGunnar Mills  * @param[i]   collectionPath  Redfish collection path which is used for the
19*05030b8eSGunnar Mills  *             Members Redfish Path
20*05030b8eSGunnar Mills  * @param[i]   interfaces  List of interfaces to constrain the GetSubTree search
21*05030b8eSGunnar Mills  *
22*05030b8eSGunnar Mills  * @return void
23*05030b8eSGunnar Mills  */
24*05030b8eSGunnar Mills inline void getCollectionMembers(std::shared_ptr<AsyncResp> aResp,
25*05030b8eSGunnar Mills                                  const std::string& collectionPath,
26*05030b8eSGunnar Mills                                  const std::vector<const char*>& interfaces)
27116bcc5cSGunnar Mills {
28*05030b8eSGunnar Mills     BMCWEB_LOG_DEBUG << "Get collection members for: " << collectionPath;
29116bcc5cSGunnar Mills     crow::connections::systemBus->async_method_call(
30*05030b8eSGunnar Mills         [collectionPath, aResp{std::move(aResp)}](
31116bcc5cSGunnar Mills             const boost::system::error_code ec,
32116bcc5cSGunnar Mills             const boost::container::flat_map<
33116bcc5cSGunnar Mills                 std::string, boost::container::flat_map<
34116bcc5cSGunnar Mills                                  std::string, std::vector<std::string>>>&
35116bcc5cSGunnar Mills                 subtree) {
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 
45116bcc5cSGunnar Mills             for (const auto& object : subtree)
46116bcc5cSGunnar Mills             {
47116bcc5cSGunnar Mills                 auto iter = object.first.rfind("/");
48116bcc5cSGunnar Mills                 if ((iter != std::string::npos) && (iter < object.first.size()))
49116bcc5cSGunnar Mills                 {
50116bcc5cSGunnar Mills                     members.push_back(
51*05030b8eSGunnar Mills                         {{"@odata.id", collectionPath + "/" +
52116bcc5cSGunnar Mills                                            object.first.substr(iter + 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",
59116bcc5cSGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
60*05030b8eSGunnar Mills         "/xyz/openbmc_project/inventory", 0, interfaces);
61116bcc5cSGunnar Mills }
62116bcc5cSGunnar Mills 
63116bcc5cSGunnar Mills } // namespace collection_util
64116bcc5cSGunnar Mills } // namespace redfish
65