Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 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 | |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 13 | /** |
| 14 | * @brief Populate the collection "Members" from a GetSubTree 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 | * |
| 22 | * @return void |
| 23 | */ |
| 24 | inline void getCollectionMembers(std::shared_ptr<AsyncResp> aResp, |
| 25 | const std::string& collectionPath, |
| 26 | const std::vector<const char*>& interfaces) |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 27 | { |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 28 | BMCWEB_LOG_DEBUG << "Get collection members for: " << collectionPath; |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 29 | crow::connections::systemBus->async_method_call( |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 30 | [collectionPath, aResp{std::move(aResp)}]( |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 31 | const boost::system::error_code ec, |
| 32 | const boost::container::flat_map< |
| 33 | std::string, boost::container::flat_map< |
| 34 | std::string, std::vector<std::string>>>& |
| 35 | subtree) { |
| 36 | if (ec) |
| 37 | { |
| 38 | BMCWEB_LOG_DEBUG << "DBUS response error"; |
| 39 | messages::internalError(aResp->res); |
| 40 | return; |
| 41 | } |
| 42 | nlohmann::json& members = aResp->res.jsonValue["Members"]; |
| 43 | members = nlohmann::json::array(); |
| 44 | |
| 45 | for (const auto& object : subtree) |
| 46 | { |
| 47 | auto iter = object.first.rfind("/"); |
| 48 | if ((iter != std::string::npos) && (iter < object.first.size())) |
| 49 | { |
| 50 | members.push_back( |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 51 | {{"@odata.id", collectionPath + "/" + |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 52 | object.first.substr(iter + 1)}}); |
| 53 | } |
| 54 | } |
| 55 | aResp->res.jsonValue["Members@odata.count"] = members.size(); |
| 56 | }, |
| 57 | "xyz.openbmc_project.ObjectMapper", |
| 58 | "/xyz/openbmc_project/object_mapper", |
| 59 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 60 | "/xyz/openbmc_project/inventory", 0, interfaces); |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | } // namespace collection_util |
| 64 | } // namespace redfish |