blob: c5d603ad21f177e34b108c7cc327b9eb22a5fd45 [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,
Jonathan Domandea43dd2020-12-02 15:21:24 -080027 const std::string& collectionPath,
28 const std::vector<const char*>& interfaces,
29 const char* subtree = "/xyz/openbmc_project/inventory")
Gunnar Mills116bcc52020-10-14 15:23:42 -050030{
Gunnar Mills05030b82020-10-14 15:51:31 -050031 BMCWEB_LOG_DEBUG << "Get collection members for: " << collectionPath;
Gunnar Mills116bcc52020-10-14 15:23:42 -050032 crow::connections::systemBus->async_method_call(
Jonathan Domandea43dd2020-12-02 15:21:24 -080033 [collectionPath,
34 aResp{std::move(aResp)}](const boost::system::error_code ec,
35 const std::vector<std::string>& objects) {
Ed Tanous27ea7db2021-10-01 12:05:44 -070036 if (ec == boost::system::errc::io_error)
37 {
38 aResp->res.jsonValue["Members"] = nlohmann::json::array();
39 aResp->res.jsonValue["Members@odata.count"] = 0;
40 return;
41 }
42
Gunnar Mills116bcc52020-10-14 15:23:42 -050043 if (ec)
44 {
Ed Tanous27ea7db2021-10-01 12:05:44 -070045 BMCWEB_LOG_DEBUG << "DBUS response error " << ec.value();
Gunnar Mills116bcc52020-10-14 15:23:42 -050046 messages::internalError(aResp->res);
47 return;
48 }
Gunnar Mills116bcc52020-10-14 15:23:42 -050049
Ed Tanous92409d02021-09-29 15:17:24 -070050 std::vector<std::string> pathNames;
Jonathan Domandea43dd2020-12-02 15:21:24 -080051 for (const auto& object : objects)
Gunnar Mills116bcc52020-10-14 15:23:42 -050052 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +000053 sdbusplus::message::object_path path(object);
54 std::string leaf = path.filename();
55 if (leaf.empty())
Gunnar Mills116bcc52020-10-14 15:23:42 -050056 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +000057 continue;
Gunnar Mills116bcc52020-10-14 15:23:42 -050058 }
Ed Tanous92409d02021-09-29 15:17:24 -070059 pathNames.push_back(leaf);
60 }
61 std::sort(pathNames.begin(), pathNames.end(),
62 AlphanumLess<std::string>());
63
64 nlohmann::json& members = aResp->res.jsonValue["Members"];
65 members = nlohmann::json::array();
66 for (const std::string& leaf : pathNames)
67 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +000068 std::string newPath = collectionPath;
69 newPath += '/';
70 newPath += leaf;
71 members.push_back({{"@odata.id", std::move(newPath)}});
Gunnar Mills116bcc52020-10-14 15:23:42 -050072 }
73 aResp->res.jsonValue["Members@odata.count"] = members.size();
74 },
75 "xyz.openbmc_project.ObjectMapper",
76 "/xyz/openbmc_project/object_mapper",
Jonathan Domandea43dd2020-12-02 15:21:24 -080077 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", subtree, 0,
78 interfaces);
Gunnar Mills116bcc52020-10-14 15:23:42 -050079}
80
81} // namespace collection_util
82} // namespace redfish