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