blob: 500749cd5477be7445136ac37ef3e48a63fafe80 [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
George Liu7a1dbc42022-12-07 16:03:22 +080012#include <span>
Gunnar Mills116bcc52020-10-14 15:23:42 -050013#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080014#include <string_view>
Gunnar Mills116bcc52020-10-14 15:23:42 -050015#include <vector>
16
17namespace redfish
18{
19namespace collection_util
20{
21
Gunnar Mills05030b82020-10-14 15:51:31 -050022/**
Jonathan Domandea43dd2020-12-02 15:21:24 -080023 * @brief Populate the collection "Members" from a GetSubTreePaths search of
Gunnar Mills05030b82020-10-14 15:51:31 -050024 * inventory
25 *
Ed Tanousac106bf2023-06-07 09:24:59 -070026 * @param[i,o] asyncResp Async response object
Gunnar Mills05030b82020-10-14 15:51:31 -050027 * @param[i] collectionPath Redfish collection path which is used for the
28 * Members Redfish Path
29 * @param[i] interfaces List of interfaces to constrain the GetSubTree search
Jonathan Domandea43dd2020-12-02 15:21:24 -080030 * @param[in] subtree D-Bus base path to constrain search to.
Gunnar Mills05030b82020-10-14 15:51:31 -050031 *
32 * @return void
33 */
Jonathan Domandea43dd2020-12-02 15:21:24 -080034inline void
Ed Tanousac106bf2023-06-07 09:24:59 -070035 getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Willy Tuae9031f2022-09-27 05:48:07 +000036 const boost::urls::url& collectionPath,
George Liu7a1dbc42022-12-07 16:03:22 +080037 std::span<const std::string_view> interfaces,
Jonathan Domandea43dd2020-12-02 15:21:24 -080038 const char* subtree = "/xyz/openbmc_project/inventory")
Gunnar Mills116bcc52020-10-14 15:23:42 -050039{
Willy Tuae9031f2022-09-27 05:48:07 +000040 BMCWEB_LOG_DEBUG << "Get collection members for: "
Ed Tanous079360a2022-06-29 10:05:19 -070041 << 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 {
56 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 }
72 std::sort(pathNames.begin(), pathNames.end(),
73 AlphanumLess<std::string>());
Ed Tanous92409d02021-09-29 15:17:24 -070074
Ed Tanousac106bf2023-06-07 09:24:59 -070075 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
Ed Tanous002d39b2022-05-31 08:59:27 -070076 members = nlohmann::json::array();
77 for (const std::string& leaf : pathNames)
78 {
Willy Tuae9031f2022-09-27 05:48:07 +000079 boost::urls::url url = collectionPath;
80 crow::utility::appendUrlPieces(url, leaf);
Ed Tanous002d39b2022-05-31 08:59:27 -070081 nlohmann::json::object_t member;
Willy Tuae9031f2022-09-27 05:48:07 +000082 member["@odata.id"] = std::move(url);
Patrick Williamsb2ba3072023-05-12 10:27:39 -050083 members.emplace_back(std::move(member));
Ed Tanous002d39b2022-05-31 08:59:27 -070084 }
Ed Tanousac106bf2023-06-07 09:24:59 -070085 asyncResp->res.jsonValue["Members@odata.count"] = members.size();
George Liu7a1dbc42022-12-07 16:03:22 +080086 });
Gunnar Mills116bcc52020-10-14 15:23:42 -050087}
88
89} // namespace collection_util
90} // namespace redfish