blob: bcf5c168653cd7d92a707c3c204f818bb0e00ed1 [file] [log] [blame]
Gunnar Mills116bcc52020-10-14 15:23:42 -05001#pragma once
2
3#include <boost/container/flat_map.hpp>
4
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
26 getCollectionMembers(std::shared_ptr<AsyncResp> aResp,
27 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) {
Gunnar Mills116bcc52020-10-14 15:23:42 -050036 if (ec)
37 {
38 BMCWEB_LOG_DEBUG << "DBUS response error";
39 messages::internalError(aResp->res);
40 return;
41 }
42 nlohmann::json& members = aResp->res.jsonValue["Members"];
43 members = nlohmann::json::array();
44
Jonathan Domandea43dd2020-12-02 15:21:24 -080045 for (const auto& object : objects)
Gunnar Mills116bcc52020-10-14 15:23:42 -050046 {
Jonathan Domandea43dd2020-12-02 15:21:24 -080047 auto pos = object.rfind('/');
48 if ((pos != std::string::npos) && (pos < (object.size() - 1)))
Gunnar Mills116bcc52020-10-14 15:23:42 -050049 {
50 members.push_back(
Jonathan Domandea43dd2020-12-02 15:21:24 -080051 {{"@odata.id",
52 collectionPath + "/" + object.substr(pos + 1)}});
Gunnar Mills116bcc52020-10-14 15:23:42 -050053 }
54 }
55 aResp->res.jsonValue["Members@odata.count"] = members.size();
56 },
57 "xyz.openbmc_project.ObjectMapper",
58 "/xyz/openbmc_project/object_mapper",
Jonathan Domandea43dd2020-12-02 15:21:24 -080059 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", subtree, 0,
60 interfaces);
Gunnar Mills116bcc52020-10-14 15:23:42 -050061}
62
63} // namespace collection_util
64} // namespace redfish