xref: /openbmc/bmcweb/features/redfish/include/utils/collection.hpp (revision 62598e31d0988d589506d5091bd38f72d61faf5e)
1116bcc5cSGunnar Mills #pragma once
2116bcc5cSGunnar Mills 
33ccb3adbSEd Tanous #include "async_resp.hpp"
47a1dbc48SGeorge Liu #include "dbus_utility.hpp"
53ccb3adbSEd Tanous #include "error_messages.hpp"
63ccb3adbSEd Tanous #include "http/utility.hpp"
73ccb3adbSEd Tanous #include "human_sort.hpp"
87a1dbc48SGeorge Liu 
93ccb3adbSEd Tanous #include <boost/url/url.hpp>
103ccb3adbSEd Tanous #include <nlohmann/json.hpp>
11116bcc5cSGunnar Mills 
127a1dbc48SGeorge Liu #include <span>
13116bcc5cSGunnar Mills #include <string>
147a1dbc48SGeorge Liu #include <string_view>
15116bcc5cSGunnar Mills #include <vector>
16116bcc5cSGunnar Mills 
17116bcc5cSGunnar Mills namespace redfish
18116bcc5cSGunnar Mills {
19116bcc5cSGunnar Mills namespace collection_util
20116bcc5cSGunnar Mills {
21116bcc5cSGunnar Mills 
2205030b8eSGunnar Mills /**
23dea43dd4SJonathan Doman  * @brief Populate the collection "Members" from a GetSubTreePaths search of
2405030b8eSGunnar Mills  *        inventory
2505030b8eSGunnar Mills  *
26ac106bf6SEd Tanous  * @param[i,o] asyncResp  Async response object
2705030b8eSGunnar Mills  * @param[i]   collectionPath  Redfish collection path which is used for the
2805030b8eSGunnar Mills  *             Members Redfish Path
2905030b8eSGunnar Mills  * @param[i]   interfaces  List of interfaces to constrain the GetSubTree search
30dea43dd4SJonathan Doman  * @param[in]  subtree     D-Bus base path to constrain search to.
3105030b8eSGunnar Mills  *
3205030b8eSGunnar Mills  * @return void
3305030b8eSGunnar Mills  */
34dea43dd4SJonathan Doman inline void
35ac106bf6SEd Tanous     getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
36ae9031f0SWilly Tu                          const boost::urls::url& collectionPath,
377a1dbc48SGeorge Liu                          std::span<const std::string_view> interfaces,
38dea43dd4SJonathan Doman                          const char* subtree = "/xyz/openbmc_project/inventory")
39116bcc5cSGunnar Mills {
40*62598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get collection members for: {}", collectionPath.buffer());
417a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
427a1dbc48SGeorge Liu         subtree, 0, interfaces,
43ac106bf6SEd Tanous         [collectionPath, asyncResp{std::move(asyncResp)}](
447a1dbc48SGeorge Liu             const boost::system::error_code& ec,
45b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& objects) {
4627ea7db1SEd Tanous         if (ec == boost::system::errc::io_error)
4727ea7db1SEd Tanous         {
48ac106bf6SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
49ac106bf6SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = 0;
5027ea7db1SEd Tanous             return;
5127ea7db1SEd Tanous         }
5227ea7db1SEd Tanous 
53116bcc5cSGunnar Mills         if (ec)
54116bcc5cSGunnar Mills         {
55*62598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error {}", ec.value());
56ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
57116bcc5cSGunnar Mills             return;
58116bcc5cSGunnar Mills         }
59116bcc5cSGunnar Mills 
6092409d0eSEd Tanous         std::vector<std::string> pathNames;
61dea43dd4SJonathan Doman         for (const auto& object : objects)
62116bcc5cSGunnar Mills         {
632dfd18efSEd Tanous             sdbusplus::message::object_path path(object);
642dfd18efSEd Tanous             std::string leaf = path.filename();
652dfd18efSEd Tanous             if (leaf.empty())
66116bcc5cSGunnar Mills             {
672dfd18efSEd Tanous                 continue;
68116bcc5cSGunnar Mills             }
6992409d0eSEd Tanous             pathNames.push_back(leaf);
7092409d0eSEd Tanous         }
7192409d0eSEd Tanous         std::sort(pathNames.begin(), pathNames.end(),
7292409d0eSEd Tanous                   AlphanumLess<std::string>());
7392409d0eSEd Tanous 
74ac106bf6SEd Tanous         nlohmann::json& members = asyncResp->res.jsonValue["Members"];
7592409d0eSEd Tanous         members = nlohmann::json::array();
7692409d0eSEd Tanous         for (const std::string& leaf : pathNames)
7792409d0eSEd Tanous         {
78ae9031f0SWilly Tu             boost::urls::url url = collectionPath;
79ae9031f0SWilly Tu             crow::utility::appendUrlPieces(url, leaf);
801476687dSEd Tanous             nlohmann::json::object_t member;
81ae9031f0SWilly Tu             member["@odata.id"] = std::move(url);
82b2ba3072SPatrick Williams             members.emplace_back(std::move(member));
83116bcc5cSGunnar Mills         }
84ac106bf6SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = members.size();
857a1dbc48SGeorge Liu         });
86116bcc5cSGunnar Mills }
87116bcc5cSGunnar Mills 
88116bcc5cSGunnar Mills } // namespace collection_util
89116bcc5cSGunnar Mills } // namespace redfish
90