blob: 0801cd5f4a71ff2075dee4ba0ef744902e7e86e6 [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
Gunnar Mills05030b82020-10-14 15:51:31 -050023/**
Jonathan Domandea43dd2020-12-02 15:21:24 -080024 * @brief Populate the collection "Members" from a GetSubTreePaths search of
Gunnar Mills05030b82020-10-14 15:51:31 -050025 * inventory
26 *
Ed Tanousac106bf2023-06-07 09:24:59 -070027 * @param[i,o] asyncResp Async response object
Gunnar Mills05030b82020-10-14 15:51:31 -050028 * @param[i] collectionPath Redfish collection path which is used for the
29 * Members Redfish Path
30 * @param[i] interfaces List of interfaces to constrain the GetSubTree search
Jonathan Domandea43dd2020-12-02 15:21:24 -080031 * @param[in] subtree D-Bus base path to constrain search to.
Gunnar Mills05030b82020-10-14 15:51:31 -050032 *
33 * @return void
34 */
Jonathan Domandea43dd2020-12-02 15:21:24 -080035inline void
Ed Tanousac106bf2023-06-07 09:24:59 -070036 getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Willy Tuae9031f2022-09-27 05:48:07 +000037 const boost::urls::url& collectionPath,
George Liu7a1dbc42022-12-07 16:03:22 +080038 std::span<const std::string_view> interfaces,
Jonathan Domandea43dd2020-12-02 15:21:24 -080039 const char* subtree = "/xyz/openbmc_project/inventory")
Gunnar Mills116bcc52020-10-14 15:23:42 -050040{
Ed Tanous62598e32023-07-17 17:06:25 -070041 BMCWEB_LOG_DEBUG("Get collection members for: {}", collectionPath.buffer());
George Liu7a1dbc42022-12-07 16:03:22 +080042 dbus::utility::getSubTreePaths(
43 subtree, 0, interfaces,
Ed Tanousac106bf2023-06-07 09:24:59 -070044 [collectionPath, asyncResp{std::move(asyncResp)}](
George Liu7a1dbc42022-12-07 16:03:22 +080045 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -080046 const dbus::utility::MapperGetSubTreePathsResponse& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -070047 if (ec == boost::system::errc::io_error)
48 {
Ed Tanousac106bf2023-06-07 09:24:59 -070049 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
50 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -070051 return;
52 }
Ed Tanous27ea7db2021-10-01 12:05:44 -070053
Ed Tanous002d39b2022-05-31 08:59:27 -070054 if (ec)
55 {
Ed Tanous62598e32023-07-17 17:06:25 -070056 BMCWEB_LOG_DEBUG("DBUS response error {}", ec.value());
Ed Tanousac106bf2023-06-07 09:24:59 -070057 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -070058 return;
59 }
Gunnar Mills116bcc52020-10-14 15:23:42 -050060
Ed Tanous002d39b2022-05-31 08:59:27 -070061 std::vector<std::string> pathNames;
62 for (const auto& object : objects)
63 {
64 sdbusplus::message::object_path path(object);
65 std::string leaf = path.filename();
66 if (leaf.empty())
Gunnar Mills116bcc52020-10-14 15:23:42 -050067 {
Ed Tanous002d39b2022-05-31 08:59:27 -070068 continue;
Ed Tanous92409d02021-09-29 15:17:24 -070069 }
Ed Tanous002d39b2022-05-31 08:59:27 -070070 pathNames.push_back(leaf);
71 }
Ed Tanous3544d2a2023-08-06 18:12:20 -070072 std::ranges::sort(pathNames, AlphanumLess<std::string>());
Ed Tanous92409d02021-09-29 15:17:24 -070073
Ed Tanousac106bf2023-06-07 09:24:59 -070074 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
Ed Tanous002d39b2022-05-31 08:59:27 -070075 members = nlohmann::json::array();
76 for (const std::string& leaf : pathNames)
77 {
Willy Tuae9031f2022-09-27 05:48:07 +000078 boost::urls::url url = collectionPath;
79 crow::utility::appendUrlPieces(url, leaf);
Ed Tanous002d39b2022-05-31 08:59:27 -070080 nlohmann::json::object_t member;
Willy Tuae9031f2022-09-27 05:48:07 +000081 member["@odata.id"] = std::move(url);
Patrick Williamsb2ba3072023-05-12 10:27:39 -050082 members.emplace_back(std::move(member));
Ed Tanous002d39b2022-05-31 08:59:27 -070083 }
Ed Tanousac106bf2023-06-07 09:24:59 -070084 asyncResp->res.jsonValue["Members@odata.count"] = members.size();
George Liu7a1dbc42022-12-07 16:03:22 +080085 });
Gunnar Mills116bcc52020-10-14 15:23:42 -050086}
87
88} // namespace collection_util
89} // namespace redfish