1 #pragma once
2 
3 #include "async_resp.hpp"
4 #include "dbus_utility.hpp"
5 #include "error_messages.hpp"
6 
7 #include <array>
8 #include <string_view>
9 
10 namespace redfish
11 {
12 
13 namespace chassis_utils
14 {
15 /**
16  * @brief Retrieves valid chassis path
17  * @param asyncResp   Pointer to object holding response data
18  * @param callback  Callback for next step to get valid chassis path
19  */
20 template <typename Callback>
21 void getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22                          const std::string& chassisId, Callback&& callback)
23 {
24     BMCWEB_LOG_DEBUG("checkChassisId enter");
25     constexpr std::array<std::string_view, 2> interfaces = {
26         "xyz.openbmc_project.Inventory.Item.Board",
27         "xyz.openbmc_project.Inventory.Item.Chassis"};
28 
29     // Get the Chassis Collection
30     dbus::utility::getSubTreePaths(
31         "/xyz/openbmc_project/inventory", 0, interfaces,
32         [callback{std::forward<Callback>(callback)}, asyncResp,
33          chassisId](const boost::system::error_code& ec,
34                     const dbus::utility::MapperGetSubTreePathsResponse&
35                         chassisPaths) mutable {
36         BMCWEB_LOG_DEBUG("getValidChassisPath respHandler enter");
37         if (ec)
38         {
39             BMCWEB_LOG_ERROR("getValidChassisPath respHandler DBUS error: {}",
40                              ec);
41             messages::internalError(asyncResp->res);
42             return;
43         }
44 
45         std::optional<std::string> chassisPath;
46         for (const std::string& chassis : chassisPaths)
47         {
48             sdbusplus::message::object_path path(chassis);
49             std::string chassisName = path.filename();
50             if (chassisName.empty())
51             {
52                 BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
53                 continue;
54             }
55             if (chassisName == chassisId)
56             {
57                 chassisPath = chassis;
58                 break;
59             }
60         }
61         callback(chassisPath);
62         });
63     BMCWEB_LOG_DEBUG("checkChassisId exit");
64 }
65 
66 } // namespace chassis_utils
67 } // namespace redfish
68