| Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 1 | #pragma once | 
|  | 2 |  | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "async_resp.hpp" | 
| George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 4 | #include "dbus_utility.hpp" | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "error_messages.hpp" | 
|  | 6 | #include "http/utility.hpp" | 
|  | 7 | #include "human_sort.hpp" | 
| George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 8 |  | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 9 | #include <boost/url/url.hpp> | 
|  | 10 | #include <nlohmann/json.hpp> | 
| Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 11 |  | 
| George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 12 | #include <span> | 
| Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 13 | #include <string> | 
| George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 14 | #include <string_view> | 
| Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 15 | #include <vector> | 
|  | 16 |  | 
|  | 17 | namespace redfish | 
|  | 18 | { | 
|  | 19 | namespace collection_util | 
|  | 20 | { | 
|  | 21 |  | 
| Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 22 | /** | 
| Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 23 | * @brief Populate the collection "Members" from a GetSubTreePaths search of | 
| Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 24 | *        inventory | 
|  | 25 | * | 
| Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 26 | * @param[i,o] asyncResp  Async response object | 
| Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 27 | * @param[i]   collectionPath  Redfish collection path which is used for the | 
|  | 28 | *             Members Redfish Path | 
|  | 29 | * @param[i]   interfaces  List of interfaces to constrain the GetSubTree search | 
| Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 30 | * @param[in]  subtree     D-Bus base path to constrain search to. | 
| Gunnar Mills | 05030b8 | 2020-10-14 15:51:31 -0500 | [diff] [blame] | 31 | * | 
|  | 32 | * @return void | 
|  | 33 | */ | 
| Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 34 | inline void | 
| Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 35 | getCollectionMembers(std::shared_ptr<bmcweb::AsyncResp> asyncResp, | 
| Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 36 | const boost::urls::url& collectionPath, | 
| George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 37 | std::span<const std::string_view> interfaces, | 
| Jonathan Doman | dea43dd | 2020-12-02 15:21:24 -0800 | [diff] [blame] | 38 | const char* subtree = "/xyz/openbmc_project/inventory") | 
| Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 39 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 40 | BMCWEB_LOG_DEBUG("Get collection members for: {}", collectionPath.buffer()); | 
| George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 41 | dbus::utility::getSubTreePaths( | 
|  | 42 | subtree, 0, interfaces, | 
| Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 43 | [collectionPath, asyncResp{std::move(asyncResp)}]( | 
| George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 44 | const boost::system::error_code& ec, | 
| Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 45 | const dbus::utility::MapperGetSubTreePathsResponse& objects) { | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 46 | if (ec == boost::system::errc::io_error) | 
|  | 47 | { | 
| Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 48 | asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); | 
|  | 49 | asyncResp->res.jsonValue["Members@odata.count"] = 0; | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 50 | return; | 
|  | 51 | } | 
| Ed Tanous | 27ea7db | 2021-10-01 12:05:44 -0700 | [diff] [blame] | 52 |  | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 53 | if (ec) | 
|  | 54 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 55 | BMCWEB_LOG_DEBUG("DBUS response error {}", ec.value()); | 
| Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 56 | messages::internalError(asyncResp->res); | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 57 | return; | 
|  | 58 | } | 
| Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 59 |  | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 60 | std::vector<std::string> pathNames; | 
|  | 61 | for (const auto& object : objects) | 
|  | 62 | { | 
|  | 63 | sdbusplus::message::object_path path(object); | 
|  | 64 | std::string leaf = path.filename(); | 
|  | 65 | if (leaf.empty()) | 
| Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 66 | { | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 67 | continue; | 
| Ed Tanous | 92409d0 | 2021-09-29 15:17:24 -0700 | [diff] [blame] | 68 | } | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 69 | pathNames.push_back(leaf); | 
|  | 70 | } | 
|  | 71 | std::sort(pathNames.begin(), pathNames.end(), | 
|  | 72 | AlphanumLess<std::string>()); | 
| Ed Tanous | 92409d0 | 2021-09-29 15:17:24 -0700 | [diff] [blame] | 73 |  | 
| Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 74 | nlohmann::json& members = asyncResp->res.jsonValue["Members"]; | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 75 | members = nlohmann::json::array(); | 
|  | 76 | for (const std::string& leaf : pathNames) | 
|  | 77 | { | 
| Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 78 | boost::urls::url url = collectionPath; | 
|  | 79 | crow::utility::appendUrlPieces(url, leaf); | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 80 | nlohmann::json::object_t member; | 
| Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 81 | member["@odata.id"] = std::move(url); | 
| Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 82 | members.emplace_back(std::move(member)); | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 83 | } | 
| Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 84 | asyncResp->res.jsonValue["Members@odata.count"] = members.size(); | 
| George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 85 | }); | 
| Gunnar Mills | 116bcc5 | 2020-10-14 15:23:42 -0500 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
|  | 88 | } // namespace collection_util | 
|  | 89 | } // namespace redfish |