blob: 862be90efdc8fc8a60e720a28eab29e9d0e48ab3 [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{
Ed Tanous62598e32023-07-17 17:06:25 -070040 BMCWEB_LOG_DEBUG("Get collection members for: {}", collectionPath.buffer());
George Liu7a1dbc42022-12-07 16:03:22 +080041 dbus::utility::getSubTreePaths(
42 subtree, 0, interfaces,
Ed Tanousac106bf2023-06-07 09:24:59 -070043 [collectionPath, asyncResp{std::move(asyncResp)}](
George Liu7a1dbc42022-12-07 16:03:22 +080044 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -080045 const dbus::utility::MapperGetSubTreePathsResponse& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -070046 if (ec == boost::system::errc::io_error)
47 {
Ed Tanousac106bf2023-06-07 09:24:59 -070048 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
49 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -070050 return;
51 }
Ed Tanous27ea7db2021-10-01 12:05:44 -070052
Ed Tanous002d39b2022-05-31 08:59:27 -070053 if (ec)
54 {
Ed Tanous62598e32023-07-17 17:06:25 -070055 BMCWEB_LOG_DEBUG("DBUS response error {}", ec.value());
Ed Tanousac106bf2023-06-07 09:24:59 -070056 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -070057 return;
58 }
Gunnar Mills116bcc52020-10-14 15:23:42 -050059
Ed Tanous002d39b2022-05-31 08:59:27 -070060 std::vector<std::string> pathNames;
61 for (const auto& object : objects)
62 {
63 sdbusplus::message::object_path path(object);
64 std::string leaf = path.filename();
65 if (leaf.empty())
Gunnar Mills116bcc52020-10-14 15:23:42 -050066 {
Ed Tanous002d39b2022-05-31 08:59:27 -070067 continue;
Ed Tanous92409d02021-09-29 15:17:24 -070068 }
Ed Tanous002d39b2022-05-31 08:59:27 -070069 pathNames.push_back(leaf);
70 }
71 std::sort(pathNames.begin(), pathNames.end(),
72 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