Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 92409d0 | 2021-09-29 15:17:24 -0700 | [diff] [blame] | 3 | #include <human_sort.hpp> |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 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 | /** |
Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 14 | * @brief Populate the collection "Members" from a GetSubTreePaths search of |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 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 |
Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 21 | * @param[in] subtree D-Bus base path to constrain search to. |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 22 | * |
| 23 | * @return void |
| 24 | */ |
Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 25 | inline void |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 26 | getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> aResp, |
Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 27 | const std::string& collectionPath, |
| 28 | const std::vector<const char*>& interfaces, |
| 29 | const char* subtree = "/xyz/openbmc_project/inventory") |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 30 | { |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 31 | BMCWEB_LOG_DEBUG << "Get collection members for: " << collectionPath; |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 32 | crow::connections::systemBus->async_method_call( |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame^] | 33 | [collectionPath, aResp{std::move(aResp)}]( |
| 34 | const boost::system::error_code ec, |
| 35 | const dbus::utility::MapperGetSubTreePathsResponse& objects) { |
Ed Tanous | 27ea7db | 2021-10-01 12:05:44 -0700 | [diff] [blame] | 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 | |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 43 | if (ec) |
| 44 | { |
Ed Tanous | 27ea7db | 2021-10-01 12:05:44 -0700 | [diff] [blame] | 45 | BMCWEB_LOG_DEBUG << "DBUS response error " << ec.value(); |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 46 | messages::internalError(aResp->res); |
| 47 | return; |
| 48 | } |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 49 | |
Ed Tanous | 92409d0 | 2021-09-29 15:17:24 -0700 | [diff] [blame] | 50 | std::vector<std::string> pathNames; |
Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 51 | for (const auto& object : objects) |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 52 | { |
Ed Tanous | 2dfd18e | 2020-12-18 00:41:31 +0000 | [diff] [blame] | 53 | sdbusplus::message::object_path path(object); |
| 54 | std::string leaf = path.filename(); |
| 55 | if (leaf.empty()) |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 56 | { |
Ed Tanous | 2dfd18e | 2020-12-18 00:41:31 +0000 | [diff] [blame] | 57 | continue; |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 58 | } |
Ed Tanous | 92409d0 | 2021-09-29 15:17:24 -0700 | [diff] [blame] | 59 | pathNames.push_back(leaf); |
| 60 | } |
| 61 | std::sort(pathNames.begin(), pathNames.end(), |
| 62 | AlphanumLess<std::string>()); |
| 63 | |
| 64 | nlohmann::json& members = aResp->res.jsonValue["Members"]; |
| 65 | members = nlohmann::json::array(); |
| 66 | for (const std::string& leaf : pathNames) |
| 67 | { |
Ed Tanous | 2dfd18e | 2020-12-18 00:41:31 +0000 | [diff] [blame] | 68 | std::string newPath = collectionPath; |
| 69 | newPath += '/'; |
| 70 | newPath += leaf; |
| 71 | members.push_back({{"@odata.id", std::move(newPath)}}); |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 72 | } |
| 73 | aResp->res.jsonValue["Members@odata.count"] = members.size(); |
| 74 | }, |
| 75 | "xyz.openbmc_project.ObjectMapper", |
| 76 | "/xyz/openbmc_project/object_mapper", |
Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 77 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", subtree, 0, |
| 78 | interfaces); |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } // namespace collection_util |
| 82 | } // namespace redfish |