blob: 2e5563b4c99c551cdc7a0e42d6a8b5ec2b3b05f6 [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{
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050030 nlohmann::json::json_pointer jsonCountKeyName = jsonKeyName;
31 std::string back = jsonCountKeyName.back();
32 jsonCountKeyName.pop_back();
Myung Baec48377d2023-12-19 10:50:32 -050033 jsonCountKeyName /= back + "@odata.count";
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050034
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050035 if (ec == boost::system::errc::io_error)
36 {
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050037 asyncResp->res.jsonValue[jsonKeyName] = nlohmann::json::array();
38 asyncResp->res.jsonValue[jsonCountKeyName] = 0;
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050039 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 Yadlapati70c4d542023-06-08 04:37:18 -050062 nlohmann::json& members = asyncResp->res.jsonValue[jsonKeyName];
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050063 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 Yadlapati70c4d542023-06-08 04:37:18 -050072 asyncResp->res.jsonValue[jsonCountKeyName] = members.size();
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050073}
74
Gunnar Mills05030b82020-10-14 15:51:31 -050075/**
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050076 * @brief Populate the collection members from a GetSubTreePaths search of
Gunnar Mills05030b82020-10-14 15:51:31 -050077 * inventory
78 *
Ed Tanousac106bf2023-06-07 09:24:59 -070079 * @param[i,o] asyncResp Async response object
Gunnar Mills05030b82020-10-14 15:51:31 -050080 * @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 Domandea43dd2020-12-02 15:21:24 -080083 * @param[in] subtree D-Bus base path to constrain search to.
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050084 * @param[in] jsonKeyName Key name in which the collection members will be
85 * stored.
Gunnar Mills05030b82020-10-14 15:51:31 -050086 *
87 * @return void
88 */
Jonathan Domandea43dd2020-12-02 15:21:24 -080089inline void
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -050090 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}
102inline void
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -0500103 getCollectionMembers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Willy Tuae9031f2022-09-27 05:48:07 +0000104 const boost::urls::url& collectionPath,
George Liu7a1dbc42022-12-07 16:03:22 +0800105 std::span<const std::string_view> interfaces,
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -0500106 const std::string& subtree)
Gunnar Mills116bcc52020-10-14 15:23:42 -0500107{
Lakshmi Yadlapati70c4d542023-06-08 04:37:18 -0500108 getCollectionToKey(asyncResp, collectionPath, interfaces, subtree,
109 nlohmann::json::json_pointer("/Members"));
Gunnar Mills116bcc52020-10-14 15:23:42 -0500110}
111
112} // namespace collection_util
113} // namespace redfish