Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 3 | #include "dbus_utility.hpp" |
| 4 | |
Ed Tanous | 92409d0 | 2021-09-29 15:17:24 -0700 | [diff] [blame] | 5 | #include <human_sort.hpp> |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 6 | |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 7 | #include <span> |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 8 | #include <string> |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 9 | #include <string_view> |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
| 12 | namespace redfish |
| 13 | { |
| 14 | namespace collection_util |
| 15 | { |
| 16 | |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 17 | /** |
Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 18 | * @brief Populate the collection "Members" from a GetSubTreePaths search of |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 19 | * inventory |
| 20 | * |
| 21 | * @param[i,o] aResp Async response object |
| 22 | * @param[i] collectionPath Redfish collection path which is used for the |
| 23 | * Members Redfish Path |
| 24 | * @param[i] interfaces List of interfaces to constrain the GetSubTree search |
Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 25 | * @param[in] subtree D-Bus base path to constrain search to. |
Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 26 | * |
| 27 | * @return void |
| 28 | */ |
Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 29 | inline void |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 30 | getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> aResp, |
Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 31 | const boost::urls::url& collectionPath, |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 32 | std::span<const std::string_view> interfaces, |
Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 33 | const char* subtree = "/xyz/openbmc_project/inventory") |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 34 | { |
Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 35 | BMCWEB_LOG_DEBUG << "Get collection members for: " |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 36 | << collectionPath.buffer(); |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 37 | dbus::utility::getSubTreePaths( |
| 38 | subtree, 0, interfaces, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 39 | [collectionPath, aResp{std::move(aResp)}]( |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 40 | const boost::system::error_code& ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 41 | const dbus::utility::MapperGetSubTreePathsResponse& objects) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 42 | if (ec == boost::system::errc::io_error) |
| 43 | { |
| 44 | aResp->res.jsonValue["Members"] = nlohmann::json::array(); |
| 45 | aResp->res.jsonValue["Members@odata.count"] = 0; |
| 46 | return; |
| 47 | } |
Ed Tanous | 27ea7db | 2021-10-01 12:05:44 -0700 | [diff] [blame] | 48 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 49 | if (ec) |
| 50 | { |
| 51 | BMCWEB_LOG_DEBUG << "DBUS response error " << ec.value(); |
| 52 | messages::internalError(aResp->res); |
| 53 | return; |
| 54 | } |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 55 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 56 | std::vector<std::string> pathNames; |
| 57 | for (const auto& object : objects) |
| 58 | { |
| 59 | sdbusplus::message::object_path path(object); |
| 60 | std::string leaf = path.filename(); |
| 61 | if (leaf.empty()) |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 62 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 63 | continue; |
Ed Tanous | 92409d0 | 2021-09-29 15:17:24 -0700 | [diff] [blame] | 64 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 65 | pathNames.push_back(leaf); |
| 66 | } |
| 67 | std::sort(pathNames.begin(), pathNames.end(), |
| 68 | AlphanumLess<std::string>()); |
Ed Tanous | 92409d0 | 2021-09-29 15:17:24 -0700 | [diff] [blame] | 69 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 70 | nlohmann::json& members = aResp->res.jsonValue["Members"]; |
| 71 | members = nlohmann::json::array(); |
| 72 | for (const std::string& leaf : pathNames) |
| 73 | { |
Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 74 | boost::urls::url url = collectionPath; |
| 75 | crow::utility::appendUrlPieces(url, leaf); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 76 | nlohmann::json::object_t member; |
Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 77 | member["@odata.id"] = std::move(url); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 78 | members.push_back(std::move(member)); |
| 79 | } |
| 80 | aResp->res.jsonValue["Members@odata.count"] = members.size(); |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 81 | }); |
Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } // namespace collection_util |
| 85 | } // namespace redfish |