blob: e34aab783bf2c59aebc8b8f4c8e067956d87a548 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Gunnar Mills116bcc52020-10-14 15:23:42 -05003#pragma once
4
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "async_resp.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +08006#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08007#include "error_messages.hpp"
8#include "http/utility.hpp"
9#include "human_sort.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080010
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080011#include <boost/url/url.hpp>
12#include <nlohmann/json.hpp>
Gunnar Mills116bcc52020-10-14 15:23:42 -050013
Ed Tanous3544d2a2023-08-06 18:12:20 -070014#include <ranges>
George Liu7a1dbc42022-12-07 16:03:22 +080015#include <span>
Gunnar Mills116bcc52020-10-14 15:23:42 -050016#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080017#include <string_view>
Gunnar Mills116bcc52020-10-14 15:23:42 -050018#include <vector>
19
20namespace redfish
21{
22namespace collection_util
23{
24
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050025inline void handleCollectionMembers(
26 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050027 const boost::urls::url& collectionPath,
28 const nlohmann::json::json_pointer& jsonKeyName,
29 const boost::system::error_code& ec,
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050030 const dbus::utility::MapperGetSubTreePathsResponse& objects)
31{
Ed Tanous1aa375b2024-04-13 11:51:10 -070032 if (jsonKeyName.empty())
33 {
34 messages::internalError(asyncResp->res);
35 BMCWEB_LOG_ERROR("Json Key called empty. Did you mean /Members?");
36 return;
37 }
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050038 nlohmann::json::json_pointer jsonCountKeyName = jsonKeyName;
39 std::string back = jsonCountKeyName.back();
40 jsonCountKeyName.pop_back();
Myung Baec48377d2023-12-19 10:50:32 -050041 jsonCountKeyName /= back + "@odata.count";
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050042
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050043 if (ec == boost::system::errc::io_error)
44 {
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050045 asyncResp->res.jsonValue[jsonKeyName] = nlohmann::json::array();
46 asyncResp->res.jsonValue[jsonCountKeyName] = 0;
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050047 return;
48 }
49
50 if (ec)
51 {
52 BMCWEB_LOG_DEBUG("DBUS response error {}", ec.value());
53 messages::internalError(asyncResp->res);
54 return;
55 }
56
57 std::vector<std::string> pathNames;
58 for (const auto& object : objects)
59 {
60 sdbusplus::message::object_path path(object);
61 std::string leaf = path.filename();
62 if (leaf.empty())
63 {
64 continue;
65 }
66 pathNames.push_back(leaf);
67 }
68 std::ranges::sort(pathNames, AlphanumLess<std::string>());
69
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050070 nlohmann::json& members = asyncResp->res.jsonValue[jsonKeyName];
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050071 members = nlohmann::json::array();
72 for (const std::string& leaf : pathNames)
73 {
74 boost::urls::url url = collectionPath;
75 crow::utility::appendUrlPieces(url, leaf);
76 nlohmann::json::object_t member;
77 member["@odata.id"] = std::move(url);
78 members.emplace_back(std::move(member));
79 }
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050080 asyncResp->res.jsonValue[jsonCountKeyName] = members.size();
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050081}
82
Gunnar Mills05030b82020-10-14 15:51:31 -050083/**
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050084 * @brief Populate the collection members from a GetSubTreePaths search of
Gunnar Mills05030b82020-10-14 15:51:31 -050085 * inventory
86 *
Ed Tanousac106bf2023-06-07 09:24:59 -070087 * @param[i,o] asyncResp Async response object
Gunnar Mills05030b82020-10-14 15:51:31 -050088 * @param[i] collectionPath Redfish collection path which is used for the
89 * Members Redfish Path
90 * @param[i] interfaces List of interfaces to constrain the GetSubTree search
Jonathan Domandea43dd2020-12-02 15:21:24 -080091 * @param[in] subtree D-Bus base path to constrain search to.
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050092 * @param[in] jsonKeyName Key name in which the collection members will be
93 * stored.
Gunnar Mills05030b82020-10-14 15:51:31 -050094 *
95 * @return void
96 */
Patrick Williamsbd79bce2024-08-16 15:22:20 -040097inline void getCollectionToKey(
98 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
99 const boost::urls::url& collectionPath,
100 std::span<const std::string_view> interfaces, const std::string& subtree,
101 const nlohmann::json::json_pointer& jsonKeyName)
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -0500102{
103 BMCWEB_LOG_DEBUG("Get collection members for: {}", collectionPath.buffer());
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400104 dbus::utility::getSubTreePaths(
105 subtree, 0, interfaces,
106 std::bind_front(handleCollectionMembers, asyncResp, collectionPath,
107 jsonKeyName));
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -0500108}
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400109inline void getCollectionMembers(
110 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
111 const boost::urls::url& collectionPath,
112 std::span<const std::string_view> interfaces, const std::string& subtree)
Gunnar Mills116bcc52020-10-14 15:23:42 -0500113{
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -0500114 getCollectionToKey(asyncResp, collectionPath, interfaces, subtree,
115 nlohmann::json::json_pointer("/Members"));
Gunnar Mills116bcc52020-10-14 15:23:42 -0500116}
117
118} // namespace collection_util
119} // namespace redfish