xref: /openbmc/bmcweb/features/redfish/include/utils/collection.hpp (revision 116bcc5c254d6dc8af09ba30f36c90c47b269a82)
1*116bcc5cSGunnar Mills #pragma once
2*116bcc5cSGunnar Mills 
3*116bcc5cSGunnar Mills #include <boost/container/flat_map.hpp>
4*116bcc5cSGunnar Mills 
5*116bcc5cSGunnar Mills #include <string>
6*116bcc5cSGunnar Mills #include <vector>
7*116bcc5cSGunnar Mills 
8*116bcc5cSGunnar Mills namespace redfish
9*116bcc5cSGunnar Mills {
10*116bcc5cSGunnar Mills namespace collection_util
11*116bcc5cSGunnar Mills {
12*116bcc5cSGunnar Mills 
13*116bcc5cSGunnar Mills inline void getResourceList(std::shared_ptr<AsyncResp> aResp,
14*116bcc5cSGunnar Mills                             const std::string& subclass,
15*116bcc5cSGunnar Mills                             const std::vector<const char*>& collectionName)
16*116bcc5cSGunnar Mills {
17*116bcc5cSGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system cpu/mem resources.";
18*116bcc5cSGunnar Mills     crow::connections::systemBus->async_method_call(
19*116bcc5cSGunnar Mills         [subclass, aResp{std::move(aResp)}](
20*116bcc5cSGunnar Mills             const boost::system::error_code ec,
21*116bcc5cSGunnar Mills             const boost::container::flat_map<
22*116bcc5cSGunnar Mills                 std::string, boost::container::flat_map<
23*116bcc5cSGunnar Mills                                  std::string, std::vector<std::string>>>&
24*116bcc5cSGunnar Mills                 subtree) {
25*116bcc5cSGunnar Mills             if (ec)
26*116bcc5cSGunnar Mills             {
27*116bcc5cSGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
28*116bcc5cSGunnar Mills                 messages::internalError(aResp->res);
29*116bcc5cSGunnar Mills                 return;
30*116bcc5cSGunnar Mills             }
31*116bcc5cSGunnar Mills             nlohmann::json& members = aResp->res.jsonValue["Members"];
32*116bcc5cSGunnar Mills             members = nlohmann::json::array();
33*116bcc5cSGunnar Mills 
34*116bcc5cSGunnar Mills             for (const auto& object : subtree)
35*116bcc5cSGunnar Mills             {
36*116bcc5cSGunnar Mills                 auto iter = object.first.rfind("/");
37*116bcc5cSGunnar Mills                 if ((iter != std::string::npos) && (iter < object.first.size()))
38*116bcc5cSGunnar Mills                 {
39*116bcc5cSGunnar Mills                     members.push_back(
40*116bcc5cSGunnar Mills                         {{"@odata.id", "/redfish/v1/Systems/system/" +
41*116bcc5cSGunnar Mills                                            subclass + "/" +
42*116bcc5cSGunnar Mills                                            object.first.substr(iter + 1)}});
43*116bcc5cSGunnar Mills                 }
44*116bcc5cSGunnar Mills             }
45*116bcc5cSGunnar Mills             aResp->res.jsonValue["Members@odata.count"] = members.size();
46*116bcc5cSGunnar Mills         },
47*116bcc5cSGunnar Mills         "xyz.openbmc_project.ObjectMapper",
48*116bcc5cSGunnar Mills         "/xyz/openbmc_project/object_mapper",
49*116bcc5cSGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
50*116bcc5cSGunnar Mills         "/xyz/openbmc_project/inventory", 0, collectionName);
51*116bcc5cSGunnar Mills }
52*116bcc5cSGunnar Mills 
53*116bcc5cSGunnar Mills } // namespace collection_util
54*116bcc5cSGunnar Mills } // namespace redfish
55