blob: 97f622d8f442f0662b9f4bbe5d4bb7e7d62bf2bb [file] [log] [blame]
Gunnar Mills116bcc52020-10-14 15:23:42 -05001#pragma once
2
Ed Tanous92409d02021-09-29 15:17:24 -07003#include <human_sort.hpp>
Gunnar Mills116bcc52020-10-14 15:23:42 -05004
5#include <string>
6#include <vector>
7
8namespace redfish
9{
10namespace collection_util
11{
12
Gunnar Mills05030b82020-10-14 15:51:31 -050013/**
Jonathan Domandea43dd2020-12-02 15:21:24 -080014 * @brief Populate the collection "Members" from a GetSubTreePaths search of
Gunnar Mills05030b82020-10-14 15:51:31 -050015 * inventory
16 *
17 * @param[i,o] aResp Async response object
18 * @param[i] collectionPath Redfish collection path which is used for the
19 * Members Redfish Path
20 * @param[i] interfaces List of interfaces to constrain the GetSubTree search
Jonathan Domandea43dd2020-12-02 15:21:24 -080021 * @param[in] subtree D-Bus base path to constrain search to.
Gunnar Mills05030b82020-10-14 15:51:31 -050022 *
23 * @return void
24 */
Jonathan Domandea43dd2020-12-02 15:21:24 -080025inline void
zhanghch058d1b46d2021-04-01 11:18:24 +080026 getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> aResp,
Willy Tuae9031f2022-09-27 05:48:07 +000027 const boost::urls::url& collectionPath,
Jonathan Domandea43dd2020-12-02 15:21:24 -080028 const std::vector<const char*>& interfaces,
29 const char* subtree = "/xyz/openbmc_project/inventory")
Gunnar Mills116bcc52020-10-14 15:23:42 -050030{
Willy Tuae9031f2022-09-27 05:48:07 +000031 BMCWEB_LOG_DEBUG << "Get collection members for: "
32 << collectionPath.string();
Gunnar Mills116bcc52020-10-14 15:23:42 -050033 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -080034 [collectionPath, aResp{std::move(aResp)}](
35 const boost::system::error_code ec,
36 const dbus::utility::MapperGetSubTreePathsResponse& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -070037 if (ec == boost::system::errc::io_error)
38 {
39 aResp->res.jsonValue["Members"] = nlohmann::json::array();
40 aResp->res.jsonValue["Members@odata.count"] = 0;
41 return;
42 }
Ed Tanous27ea7db2021-10-01 12:05:44 -070043
Ed Tanous002d39b2022-05-31 08:59:27 -070044 if (ec)
45 {
46 BMCWEB_LOG_DEBUG << "DBUS response error " << ec.value();
47 messages::internalError(aResp->res);
48 return;
49 }
Gunnar Mills116bcc52020-10-14 15:23:42 -050050
Ed Tanous002d39b2022-05-31 08:59:27 -070051 std::vector<std::string> pathNames;
52 for (const auto& object : objects)
53 {
54 sdbusplus::message::object_path path(object);
55 std::string leaf = path.filename();
56 if (leaf.empty())
Gunnar Mills116bcc52020-10-14 15:23:42 -050057 {
Ed Tanous002d39b2022-05-31 08:59:27 -070058 continue;
Ed Tanous92409d02021-09-29 15:17:24 -070059 }
Ed Tanous002d39b2022-05-31 08:59:27 -070060 pathNames.push_back(leaf);
61 }
62 std::sort(pathNames.begin(), pathNames.end(),
63 AlphanumLess<std::string>());
Ed Tanous92409d02021-09-29 15:17:24 -070064
Ed Tanous002d39b2022-05-31 08:59:27 -070065 nlohmann::json& members = aResp->res.jsonValue["Members"];
66 members = nlohmann::json::array();
67 for (const std::string& leaf : pathNames)
68 {
Willy Tuae9031f2022-09-27 05:48:07 +000069 boost::urls::url url = collectionPath;
70 crow::utility::appendUrlPieces(url, leaf);
Ed Tanous002d39b2022-05-31 08:59:27 -070071 nlohmann::json::object_t member;
Willy Tuae9031f2022-09-27 05:48:07 +000072 member["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -070073 members.push_back(std::move(member));
74 }
75 aResp->res.jsonValue["Members@odata.count"] = members.size();
Gunnar Mills116bcc52020-10-14 15:23:42 -050076 },
77 "xyz.openbmc_project.ObjectMapper",
78 "/xyz/openbmc_project/object_mapper",
Jonathan Domandea43dd2020-12-02 15:21:24 -080079 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", subtree, 0,
80 interfaces);
Gunnar Mills116bcc52020-10-14 15:23:42 -050081}
82
83} // namespace collection_util
84} // namespace redfish