blob: 7d28c0f72b7036b5beafd85ee138fda82515bd90 [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,
25 const boost::urls::url& collectionPath, const boost::system::error_code& ec,
26 const dbus::utility::MapperGetSubTreePathsResponse& objects)
27{
28 if (ec == boost::system::errc::io_error)
29 {
30 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
31 asyncResp->res.jsonValue["Members@odata.count"] = 0;
32 return;
33 }
34
35 if (ec)
36 {
37 BMCWEB_LOG_DEBUG("DBUS response error {}", ec.value());
38 messages::internalError(asyncResp->res);
39 return;
40 }
41
42 std::vector<std::string> pathNames;
43 for (const auto& object : objects)
44 {
45 sdbusplus::message::object_path path(object);
46 std::string leaf = path.filename();
47 if (leaf.empty())
48 {
49 continue;
50 }
51 pathNames.push_back(leaf);
52 }
53 std::ranges::sort(pathNames, AlphanumLess<std::string>());
54
55 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
56 members = nlohmann::json::array();
57 for (const std::string& leaf : pathNames)
58 {
59 boost::urls::url url = collectionPath;
60 crow::utility::appendUrlPieces(url, leaf);
61 nlohmann::json::object_t member;
62 member["@odata.id"] = std::move(url);
63 members.emplace_back(std::move(member));
64 }
65 asyncResp->res.jsonValue["Members@odata.count"] = members.size();
66}
67
Gunnar Mills05030b82020-10-14 15:51:31 -050068/**
Jonathan Domandea43dd2020-12-02 15:21:24 -080069 * @brief Populate the collection "Members" from a GetSubTreePaths search of
Gunnar Mills05030b82020-10-14 15:51:31 -050070 * inventory
71 *
Ed Tanousac106bf2023-06-07 09:24:59 -070072 * @param[i,o] asyncResp Async response object
Gunnar Mills05030b82020-10-14 15:51:31 -050073 * @param[i] collectionPath Redfish collection path which is used for the
74 * Members Redfish Path
75 * @param[i] interfaces List of interfaces to constrain the GetSubTree search
Jonathan Domandea43dd2020-12-02 15:21:24 -080076 * @param[in] subtree D-Bus base path to constrain search to.
Gunnar Mills05030b82020-10-14 15:51:31 -050077 *
78 * @return void
79 */
Jonathan Domandea43dd2020-12-02 15:21:24 -080080inline void
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050081 getCollectionMembers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Willy Tuae9031f2022-09-27 05:48:07 +000082 const boost::urls::url& collectionPath,
George Liu7a1dbc42022-12-07 16:03:22 +080083 std::span<const std::string_view> interfaces,
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050084 const std::string& subtree)
Gunnar Mills116bcc52020-10-14 15:23:42 -050085{
Ed Tanous62598e32023-07-17 17:06:25 -070086 BMCWEB_LOG_DEBUG("Get collection members for: {}", collectionPath.buffer());
George Liu7a1dbc42022-12-07 16:03:22 +080087 dbus::utility::getSubTreePaths(
88 subtree, 0, interfaces,
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -050089 std::bind_front(handleCollectionMembers, asyncResp, collectionPath));
Gunnar Mills116bcc52020-10-14 15:23:42 -050090}
91
92} // namespace collection_util
93} // namespace redfish