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