blob: 9efa7d3ef3063d731af428c1e484979167069a6c [file] [log] [blame]
Gunnar Mills116bcc52020-10-14 15:23:42 -05001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "async_resp.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +08004#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "error_messages.hpp"
6#include "http/utility.hpp"
7#include "human_sort.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +08008
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08009#include <boost/url/url.hpp>
10#include <nlohmann/json.hpp>
Gunnar Mills116bcc52020-10-14 15:23:42 -050011
Ed Tanous3544d2a2023-08-06 18:12:20 -070012#include <ranges>
George Liu7a1dbc42022-12-07 16:03:22 +080013#include <span>
Gunnar Mills116bcc52020-10-14 15:23:42 -050014#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080015#include <string_view>
Gunnar Mills116bcc52020-10-14 15:23:42 -050016#include <vector>
17
18namespace redfish
19{
20namespace collection_util
21{
22
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050023inline void handleCollectionMembers(
24 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050025 const boost::urls::url& collectionPath,
26 const nlohmann::json::json_pointer& jsonKeyName,
27 const boost::system::error_code& ec,
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050028 const dbus::utility::MapperGetSubTreePathsResponse& objects)
29{
Ed Tanous1aa375b2024-04-13 11:51:10 -070030 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 Yadlapati70c4d542023-06-08 04:37:18 -050036 nlohmann::json::json_pointer jsonCountKeyName = jsonKeyName;
37 std::string back = jsonCountKeyName.back();
38 jsonCountKeyName.pop_back();
Myung Baec48377d2023-12-19 10:50:32 -050039 jsonCountKeyName /= back + "@odata.count";
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050040
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050041 if (ec == boost::system::errc::io_error)
42 {
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050043 asyncResp->res.jsonValue[jsonKeyName] = nlohmann::json::array();
44 asyncResp->res.jsonValue[jsonCountKeyName] = 0;
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050045 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 Yadlapati70c4d542023-06-08 04:37:18 -050068 nlohmann::json& members = asyncResp->res.jsonValue[jsonKeyName];
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050069 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 Yadlapati70c4d542023-06-08 04:37:18 -050078 asyncResp->res.jsonValue[jsonCountKeyName] = members.size();
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050079}
80
Gunnar Mills05030b82020-10-14 15:51:31 -050081/**
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050082 * @brief Populate the collection members from a GetSubTreePaths search of
Gunnar Mills05030b82020-10-14 15:51:31 -050083 * inventory
84 *
Ed Tanousac106bf2023-06-07 09:24:59 -070085 * @param[i,o] asyncResp Async response object
Gunnar Mills05030b82020-10-14 15:51:31 -050086 * @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 Domandea43dd2020-12-02 15:21:24 -080089 * @param[in] subtree D-Bus base path to constrain search to.
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050090 * @param[in] jsonKeyName Key name in which the collection members will be
91 * stored.
Gunnar Mills05030b82020-10-14 15:51:31 -050092 *
93 * @return void
94 */
Jonathan Domandea43dd2020-12-02 15:21:24 -080095inline void
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050096 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}
108inline void
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -0500109 getCollectionMembers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Willy Tuae9031f2022-09-27 05:48:07 +0000110 const boost::urls::url& collectionPath,
George Liu7a1dbc42022-12-07 16:03:22 +0800111 std::span<const std::string_view> interfaces,
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -0500112 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