xref: /openbmc/bmcweb/features/redfish/include/utils/collection.hpp (revision b9d36b4791d77a47e1f3c5c4564fcdf7cc68c115)
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,
2705030b8eSGunnar Mills                          const std::string& collectionPath,
28dea43dd4SJonathan Doman                          const std::vector<const char*>& interfaces,
29dea43dd4SJonathan Doman                          const char* subtree = "/xyz/openbmc_project/inventory")
30116bcc5cSGunnar Mills {
3105030b8eSGunnar Mills     BMCWEB_LOG_DEBUG << "Get collection members for: " << collectionPath;
32116bcc5cSGunnar Mills     crow::connections::systemBus->async_method_call(
33*b9d36b47SEd Tanous         [collectionPath, aResp{std::move(aResp)}](
34*b9d36b47SEd Tanous             const boost::system::error_code ec,
35*b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& objects) {
3627ea7db1SEd Tanous             if (ec == boost::system::errc::io_error)
3727ea7db1SEd Tanous             {
3827ea7db1SEd Tanous                 aResp->res.jsonValue["Members"] = nlohmann::json::array();
3927ea7db1SEd Tanous                 aResp->res.jsonValue["Members@odata.count"] = 0;
4027ea7db1SEd Tanous                 return;
4127ea7db1SEd Tanous             }
4227ea7db1SEd Tanous 
43116bcc5cSGunnar Mills             if (ec)
44116bcc5cSGunnar Mills             {
4527ea7db1SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec.value();
46116bcc5cSGunnar Mills                 messages::internalError(aResp->res);
47116bcc5cSGunnar Mills                 return;
48116bcc5cSGunnar Mills             }
49116bcc5cSGunnar Mills 
5092409d0eSEd Tanous             std::vector<std::string> pathNames;
51dea43dd4SJonathan Doman             for (const auto& object : objects)
52116bcc5cSGunnar Mills             {
532dfd18efSEd Tanous                 sdbusplus::message::object_path path(object);
542dfd18efSEd Tanous                 std::string leaf = path.filename();
552dfd18efSEd Tanous                 if (leaf.empty())
56116bcc5cSGunnar Mills                 {
572dfd18efSEd Tanous                     continue;
58116bcc5cSGunnar Mills                 }
5992409d0eSEd Tanous                 pathNames.push_back(leaf);
6092409d0eSEd Tanous             }
6192409d0eSEd Tanous             std::sort(pathNames.begin(), pathNames.end(),
6292409d0eSEd Tanous                       AlphanumLess<std::string>());
6392409d0eSEd Tanous 
6492409d0eSEd Tanous             nlohmann::json& members = aResp->res.jsonValue["Members"];
6592409d0eSEd Tanous             members = nlohmann::json::array();
6692409d0eSEd Tanous             for (const std::string& leaf : pathNames)
6792409d0eSEd Tanous             {
682dfd18efSEd Tanous                 std::string newPath = collectionPath;
692dfd18efSEd Tanous                 newPath += '/';
702dfd18efSEd Tanous                 newPath += leaf;
712dfd18efSEd Tanous                 members.push_back({{"@odata.id", std::move(newPath)}});
72116bcc5cSGunnar Mills             }
73116bcc5cSGunnar Mills             aResp->res.jsonValue["Members@odata.count"] = members.size();
74116bcc5cSGunnar Mills         },
75116bcc5cSGunnar Mills         "xyz.openbmc_project.ObjectMapper",
76116bcc5cSGunnar Mills         "/xyz/openbmc_project/object_mapper",
77dea43dd4SJonathan Doman         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", subtree, 0,
78dea43dd4SJonathan Doman         interfaces);
79116bcc5cSGunnar Mills }
80116bcc5cSGunnar Mills 
81116bcc5cSGunnar Mills } // namespace collection_util
82116bcc5cSGunnar Mills } // namespace redfish
83