xref: /openbmc/bmcweb/features/redfish/include/utils/collection.hpp (revision 079360ae6e04d3f2245e00d70f83d15c5cad3630)
1116bcc5cSGunnar Mills #pragma once
2116bcc5cSGunnar Mills 
392409d0eSEd Tanous #include <human_sort.hpp>
4116bcc5cSGunnar Mills 
5116bcc5cSGunnar Mills #include <string>
6116bcc5cSGunnar Mills #include <vector>
7116bcc5cSGunnar Mills 
8116bcc5cSGunnar Mills namespace redfish
9116bcc5cSGunnar Mills {
10116bcc5cSGunnar Mills namespace collection_util
11116bcc5cSGunnar Mills {
12116bcc5cSGunnar Mills 
1305030b8eSGunnar Mills /**
14dea43dd4SJonathan Doman  * @brief Populate the collection "Members" from a GetSubTreePaths search of
1505030b8eSGunnar Mills  *        inventory
1605030b8eSGunnar Mills  *
1705030b8eSGunnar Mills  * @param[i,o] aResp  Async response object
1805030b8eSGunnar Mills  * @param[i]   collectionPath  Redfish collection path which is used for the
1905030b8eSGunnar Mills  *             Members Redfish Path
2005030b8eSGunnar Mills  * @param[i]   interfaces  List of interfaces to constrain the GetSubTree search
21dea43dd4SJonathan Doman  * @param[in]  subtree     D-Bus base path to constrain search to.
2205030b8eSGunnar Mills  *
2305030b8eSGunnar Mills  * @return void
2405030b8eSGunnar Mills  */
25dea43dd4SJonathan Doman inline void
268d1b46d7Szhanghch05     getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> aResp,
27ae9031f0SWilly Tu                          const boost::urls::url& collectionPath,
28dea43dd4SJonathan Doman                          const std::vector<const char*>& interfaces,
29dea43dd4SJonathan Doman                          const char* subtree = "/xyz/openbmc_project/inventory")
30116bcc5cSGunnar Mills {
31ae9031f0SWilly Tu     BMCWEB_LOG_DEBUG << "Get collection members for: "
32*079360aeSEd Tanous                      << collectionPath.buffer();
33116bcc5cSGunnar Mills     crow::connections::systemBus->async_method_call(
34b9d36b47SEd Tanous         [collectionPath, aResp{std::move(aResp)}](
35b9d36b47SEd Tanous             const boost::system::error_code ec,
36b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& objects) {
3727ea7db1SEd Tanous         if (ec == boost::system::errc::io_error)
3827ea7db1SEd Tanous         {
3927ea7db1SEd Tanous             aResp->res.jsonValue["Members"] = nlohmann::json::array();
4027ea7db1SEd Tanous             aResp->res.jsonValue["Members@odata.count"] = 0;
4127ea7db1SEd Tanous             return;
4227ea7db1SEd Tanous         }
4327ea7db1SEd Tanous 
44116bcc5cSGunnar Mills         if (ec)
45116bcc5cSGunnar Mills         {
4627ea7db1SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error " << ec.value();
47116bcc5cSGunnar Mills             messages::internalError(aResp->res);
48116bcc5cSGunnar Mills             return;
49116bcc5cSGunnar Mills         }
50116bcc5cSGunnar Mills 
5192409d0eSEd Tanous         std::vector<std::string> pathNames;
52dea43dd4SJonathan Doman         for (const auto& object : objects)
53116bcc5cSGunnar Mills         {
542dfd18efSEd Tanous             sdbusplus::message::object_path path(object);
552dfd18efSEd Tanous             std::string leaf = path.filename();
562dfd18efSEd Tanous             if (leaf.empty())
57116bcc5cSGunnar Mills             {
582dfd18efSEd Tanous                 continue;
59116bcc5cSGunnar Mills             }
6092409d0eSEd Tanous             pathNames.push_back(leaf);
6192409d0eSEd Tanous         }
6292409d0eSEd Tanous         std::sort(pathNames.begin(), pathNames.end(),
6392409d0eSEd Tanous                   AlphanumLess<std::string>());
6492409d0eSEd Tanous 
6592409d0eSEd Tanous         nlohmann::json& members = aResp->res.jsonValue["Members"];
6692409d0eSEd Tanous         members = nlohmann::json::array();
6792409d0eSEd Tanous         for (const std::string& leaf : pathNames)
6892409d0eSEd Tanous         {
69ae9031f0SWilly Tu             boost::urls::url url = collectionPath;
70ae9031f0SWilly Tu             crow::utility::appendUrlPieces(url, leaf);
711476687dSEd Tanous             nlohmann::json::object_t member;
72ae9031f0SWilly Tu             member["@odata.id"] = std::move(url);
731476687dSEd Tanous             members.push_back(std::move(member));
74116bcc5cSGunnar Mills         }
75116bcc5cSGunnar Mills         aResp->res.jsonValue["Members@odata.count"] = members.size();
76116bcc5cSGunnar Mills         },
77116bcc5cSGunnar Mills         "xyz.openbmc_project.ObjectMapper",
78116bcc5cSGunnar Mills         "/xyz/openbmc_project/object_mapper",
79dea43dd4SJonathan Doman         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", subtree, 0,
80dea43dd4SJonathan Doman         interfaces);
81116bcc5cSGunnar Mills }
82116bcc5cSGunnar Mills 
83116bcc5cSGunnar Mills } // namespace collection_util
84116bcc5cSGunnar Mills } // namespace redfish
85