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