blob: f2f0669d11c304c620ffca6648659623d5a019e2 [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 *
26 * @param[i,o] aResp Async response object
27 * @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
zhanghch058d1b46d2021-04-01 11:18:24 +080035 getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> aResp,
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 Tanousb9d36b42022-02-26 21:42:46 -080044 [collectionPath, aResp{std::move(aResp)}](
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 {
49 aResp->res.jsonValue["Members"] = nlohmann::json::array();
50 aResp->res.jsonValue["Members@odata.count"] = 0;
51 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();
57 messages::internalError(aResp->res);
58 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 Tanous002d39b2022-05-31 08:59:27 -070075 nlohmann::json& members = aResp->res.jsonValue["Members"];
76 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);
Ed Tanous002d39b2022-05-31 08:59:27 -070083 members.push_back(std::move(member));
84 }
85 aResp->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