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