blob: b66ab5d05c91bdfad3557c38a84640ecd1791dd9 [file] [log] [blame]
Gunnar Mills116bcc52020-10-14 15:23:42 -05001#pragma once
2
George Liu7a1dbc42022-12-07 16:03:22 +08003#include "dbus_utility.hpp"
4
Ed Tanous92409d02021-09-29 15:17:24 -07005#include <human_sort.hpp>
Gunnar Mills116bcc52020-10-14 15:23:42 -05006
George Liu7a1dbc42022-12-07 16:03:22 +08007#include <span>
Gunnar Mills116bcc52020-10-14 15:23:42 -05008#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +08009#include <string_view>
Gunnar Mills116bcc52020-10-14 15:23:42 -050010#include <vector>
11
12namespace redfish
13{
14namespace collection_util
15{
16
Gunnar Mills05030b82020-10-14 15:51:31 -050017/**
Jonathan Domandea43dd2020-12-02 15:21:24 -080018 * @brief Populate the collection "Members" from a GetSubTreePaths search of
Gunnar Mills05030b82020-10-14 15:51:31 -050019 * inventory
20 *
21 * @param[i,o] aResp Async response object
22 * @param[i] collectionPath Redfish collection path which is used for the
23 * Members Redfish Path
24 * @param[i] interfaces List of interfaces to constrain the GetSubTree search
Jonathan Domandea43dd2020-12-02 15:21:24 -080025 * @param[in] subtree D-Bus base path to constrain search to.
Gunnar Mills05030b82020-10-14 15:51:31 -050026 *
27 * @return void
28 */
Jonathan Domandea43dd2020-12-02 15:21:24 -080029inline void
zhanghch058d1b46d2021-04-01 11:18:24 +080030 getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> aResp,
Willy Tuae9031f2022-09-27 05:48:07 +000031 const boost::urls::url& collectionPath,
George Liu7a1dbc42022-12-07 16:03:22 +080032 std::span<const std::string_view> interfaces,
Jonathan Domandea43dd2020-12-02 15:21:24 -080033 const char* subtree = "/xyz/openbmc_project/inventory")
Gunnar Mills116bcc52020-10-14 15:23:42 -050034{
Willy Tuae9031f2022-09-27 05:48:07 +000035 BMCWEB_LOG_DEBUG << "Get collection members for: "
Ed Tanous079360a2022-06-29 10:05:19 -070036 << collectionPath.buffer();
George Liu7a1dbc42022-12-07 16:03:22 +080037 dbus::utility::getSubTreePaths(
38 subtree, 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -080039 [collectionPath, aResp{std::move(aResp)}](
George Liu7a1dbc42022-12-07 16:03:22 +080040 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -080041 const dbus::utility::MapperGetSubTreePathsResponse& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -070042 if (ec == boost::system::errc::io_error)
43 {
44 aResp->res.jsonValue["Members"] = nlohmann::json::array();
45 aResp->res.jsonValue["Members@odata.count"] = 0;
46 return;
47 }
Ed Tanous27ea7db2021-10-01 12:05:44 -070048
Ed Tanous002d39b2022-05-31 08:59:27 -070049 if (ec)
50 {
51 BMCWEB_LOG_DEBUG << "DBUS response error " << ec.value();
52 messages::internalError(aResp->res);
53 return;
54 }
Gunnar Mills116bcc52020-10-14 15:23:42 -050055
Ed Tanous002d39b2022-05-31 08:59:27 -070056 std::vector<std::string> pathNames;
57 for (const auto& object : objects)
58 {
59 sdbusplus::message::object_path path(object);
60 std::string leaf = path.filename();
61 if (leaf.empty())
Gunnar Mills116bcc52020-10-14 15:23:42 -050062 {
Ed Tanous002d39b2022-05-31 08:59:27 -070063 continue;
Ed Tanous92409d02021-09-29 15:17:24 -070064 }
Ed Tanous002d39b2022-05-31 08:59:27 -070065 pathNames.push_back(leaf);
66 }
67 std::sort(pathNames.begin(), pathNames.end(),
68 AlphanumLess<std::string>());
Ed Tanous92409d02021-09-29 15:17:24 -070069
Ed Tanous002d39b2022-05-31 08:59:27 -070070 nlohmann::json& members = aResp->res.jsonValue["Members"];
71 members = nlohmann::json::array();
72 for (const std::string& leaf : pathNames)
73 {
Willy Tuae9031f2022-09-27 05:48:07 +000074 boost::urls::url url = collectionPath;
75 crow::utility::appendUrlPieces(url, leaf);
Ed Tanous002d39b2022-05-31 08:59:27 -070076 nlohmann::json::object_t member;
Willy Tuae9031f2022-09-27 05:48:07 +000077 member["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -070078 members.push_back(std::move(member));
79 }
80 aResp->res.jsonValue["Members@odata.count"] = members.size();
George Liu7a1dbc42022-12-07 16:03:22 +080081 });
Gunnar Mills116bcc52020-10-14 15:23:42 -050082}
83
84} // namespace collection_util
85} // namespace redfish