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