xref: /openbmc/bmcweb/features/redfish/lib/power_supply.hpp (revision ef4c65b741724d724452a3a0efe8dff0d450514a)
1 #pragma once
2 
3 #include "app.hpp"
4 #include "dbus_utility.hpp"
5 #include "query.hpp"
6 #include "registries/privilege_registry.hpp"
7 #include "utils/chassis_utils.hpp"
8 
9 #include <boost/url/format.hpp>
10 
11 #include <memory>
12 #include <optional>
13 #include <string>
14 
15 namespace redfish
16 {
17 
18 inline void updatePowerSupplyList(
19     const std::shared_ptr<bmcweb::AsyncResp>& /* asyncResp */,
20     const std::string& /* chassisId */,
21     const std::string& /* powerSupplyPath */)
22 {
23     // TODO In order for the validator to pass, the Members property will be
24     // implemented on the next commit
25 }
26 
27 inline void
28     doPowerSupplyCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29                             const std::string& chassisId,
30                             const std::optional<std::string>& validChassisPath)
31 {
32     if (!validChassisPath)
33     {
34         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
35         return;
36     }
37 
38     asyncResp->res.addHeader(
39         boost::beast::http::field::link,
40         "</redfish/v1/JsonSchemas/PowerSupplyCollection/PowerSupplyCollection.json>; rel=describedby");
41     asyncResp->res.jsonValue["@odata.type"] =
42         "#PowerSupplyCollection.PowerSupplyCollection";
43     asyncResp->res.jsonValue["Name"] = "Power Supply Collection";
44     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
45         "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies", chassisId);
46     asyncResp->res.jsonValue["Description"] =
47         "The collection of PowerSupply resource instances.";
48     asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
49     asyncResp->res.jsonValue["Members@odata.count"] = 0;
50 
51     std::string powerPath = *validChassisPath + "/powered_by";
52     dbus::utility::getAssociationEndPoints(
53         powerPath, [asyncResp, chassisId](
54                        const boost::system::error_code& ec,
55                        const dbus::utility::MapperEndPoints& endpoints) {
56             if (ec)
57             {
58                 if (ec.value() != EBADR)
59                 {
60                     BMCWEB_LOG_ERROR << "DBUS response error" << ec.value();
61                     messages::internalError(asyncResp->res);
62                 }
63                 return;
64             }
65 
66             for (const auto& endpoint : endpoints)
67             {
68                 updatePowerSupplyList(asyncResp, chassisId, endpoint);
69             }
70         });
71 }
72 
73 inline void handlePowerSupplyCollectionHead(
74     App& app, const crow::Request& req,
75     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
76     const std::string& chassisId)
77 {
78     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
79     {
80         return;
81     }
82 
83     redfish::chassis_utils::getValidChassisPath(
84         asyncResp, chassisId,
85         [asyncResp,
86          chassisId](const std::optional<std::string>& validChassisPath) {
87         if (!validChassisPath)
88         {
89             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
90             return;
91         }
92         asyncResp->res.addHeader(
93             boost::beast::http::field::link,
94             "</redfish/v1/JsonSchemas/PowerSupplyCollection/PowerSupplyCollection.json>; rel=describedby");
95         });
96 }
97 
98 inline void handlePowerSupplyCollectionGet(
99     App& app, const crow::Request& req,
100     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
101     const std::string& chassisId)
102 {
103     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
104     {
105         return;
106     }
107 
108     redfish::chassis_utils::getValidChassisPath(
109         asyncResp, chassisId,
110         std::bind_front(doPowerSupplyCollection, asyncResp, chassisId));
111 }
112 
113 inline void requestRoutesPowerSupplyCollection(App& app)
114 {
115     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/")
116         .privileges(redfish::privileges::headPowerSupplyCollection)
117         .methods(boost::beast::http::verb::head)(
118             std::bind_front(handlePowerSupplyCollectionHead, std::ref(app)));
119 
120     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/")
121         .privileges(redfish::privileges::getPowerSupplyCollection)
122         .methods(boost::beast::http::verb::get)(
123             std::bind_front(handlePowerSupplyCollectionGet, std::ref(app)));
124 }
125 
126 } // namespace redfish
127