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