1116bcc5cSGunnar Mills #pragma once 2116bcc5cSGunnar Mills 3*3ccb3adbSEd Tanous #include "async_resp.hpp" 47a1dbc48SGeorge Liu #include "dbus_utility.hpp" 5*3ccb3adbSEd Tanous #include "error_messages.hpp" 6*3ccb3adbSEd Tanous #include "http/utility.hpp" 7*3ccb3adbSEd Tanous #include "human_sort.hpp" 87a1dbc48SGeorge Liu 9*3ccb3adbSEd Tanous #include <boost/url/url.hpp> 10*3ccb3adbSEd 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 * 2605030b8eSGunnar Mills * @param[i,o] aResp 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 358d1b46d7Szhanghch05 getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> aResp, 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 { 40ae9031f0SWilly Tu BMCWEB_LOG_DEBUG << "Get collection members for: " 41079360aeSEd Tanous << collectionPath.buffer(); 427a1dbc48SGeorge Liu dbus::utility::getSubTreePaths( 437a1dbc48SGeorge Liu subtree, 0, interfaces, 44b9d36b47SEd Tanous [collectionPath, aResp{std::move(aResp)}]( 457a1dbc48SGeorge Liu const boost::system::error_code& ec, 46b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& objects) { 4727ea7db1SEd Tanous if (ec == boost::system::errc::io_error) 4827ea7db1SEd Tanous { 4927ea7db1SEd Tanous aResp->res.jsonValue["Members"] = nlohmann::json::array(); 5027ea7db1SEd Tanous aResp->res.jsonValue["Members@odata.count"] = 0; 5127ea7db1SEd Tanous return; 5227ea7db1SEd Tanous } 5327ea7db1SEd Tanous 54116bcc5cSGunnar Mills if (ec) 55116bcc5cSGunnar Mills { 5627ea7db1SEd Tanous BMCWEB_LOG_DEBUG << "DBUS response error " << ec.value(); 57116bcc5cSGunnar Mills messages::internalError(aResp->res); 58116bcc5cSGunnar Mills return; 59116bcc5cSGunnar Mills } 60116bcc5cSGunnar Mills 6192409d0eSEd Tanous std::vector<std::string> pathNames; 62dea43dd4SJonathan Doman for (const auto& object : objects) 63116bcc5cSGunnar Mills { 642dfd18efSEd Tanous sdbusplus::message::object_path path(object); 652dfd18efSEd Tanous std::string leaf = path.filename(); 662dfd18efSEd Tanous if (leaf.empty()) 67116bcc5cSGunnar Mills { 682dfd18efSEd Tanous continue; 69116bcc5cSGunnar Mills } 7092409d0eSEd Tanous pathNames.push_back(leaf); 7192409d0eSEd Tanous } 7292409d0eSEd Tanous std::sort(pathNames.begin(), pathNames.end(), 7392409d0eSEd Tanous AlphanumLess<std::string>()); 7492409d0eSEd Tanous 7592409d0eSEd Tanous nlohmann::json& members = aResp->res.jsonValue["Members"]; 7692409d0eSEd Tanous members = nlohmann::json::array(); 7792409d0eSEd Tanous for (const std::string& leaf : pathNames) 7892409d0eSEd Tanous { 79ae9031f0SWilly Tu boost::urls::url url = collectionPath; 80ae9031f0SWilly Tu crow::utility::appendUrlPieces(url, leaf); 811476687dSEd Tanous nlohmann::json::object_t member; 82ae9031f0SWilly Tu member["@odata.id"] = std::move(url); 831476687dSEd Tanous members.push_back(std::move(member)); 84116bcc5cSGunnar Mills } 85116bcc5cSGunnar Mills aResp->res.jsonValue["Members@odata.count"] = members.size(); 867a1dbc48SGeorge Liu }); 87116bcc5cSGunnar Mills } 88116bcc5cSGunnar Mills 89116bcc5cSGunnar Mills } // namespace collection_util 90116bcc5cSGunnar Mills } // namespace redfish 91