xref: /openbmc/bmcweb/features/redfish/lib/power_supply.hpp (revision a72100201c2ebf3e8da33e9f325ae871bc4598bf)
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 
48     std::string powerPath = *validChassisPath + "/powered_by";
49     dbus::utility::getAssociationEndPoints(
50         powerPath, [asyncResp, chassisId](
51                        const boost::system::error_code& ec,
52                        const dbus::utility::MapperEndPoints& endpoints) {
53             if (ec)
54             {
55                 if (ec.value() != EBADR)
56                 {
57                     BMCWEB_LOG_ERROR << "DBUS response error" << ec.value();
58                     messages::internalError(asyncResp->res);
59                 }
60                 return;
61             }
62 
63             for (const auto& endpoint : endpoints)
64             {
65                 updatePowerSupplyList(asyncResp, chassisId, endpoint);
66             }
67         });
68 }
69 
70 inline void handlePowerSupplyCollectionHead(
71     App& app, const crow::Request& req,
72     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
73     const std::string& chassisId)
74 {
75     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
76     {
77         return;
78     }
79 
80     redfish::chassis_utils::getValidChassisPath(
81         asyncResp, chassisId,
82         [asyncResp,
83          chassisId](const std::optional<std::string>& validChassisPath) {
84         if (!validChassisPath)
85         {
86             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
87             return;
88         }
89         asyncResp->res.addHeader(
90             boost::beast::http::field::link,
91             "</redfish/v1/JsonSchemas/PowerSupplyCollection/PowerSupplyCollection.json>; rel=describedby");
92         });
93 }
94 
95 inline void handlePowerSupplyCollectionGet(
96     App& app, const crow::Request& req,
97     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
98     const std::string& chassisId)
99 {
100     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
101     {
102         return;
103     }
104 
105     redfish::chassis_utils::getValidChassisPath(
106         asyncResp, chassisId,
107         std::bind_front(doPowerSupplyCollection, asyncResp, chassisId));
108 }
109 
110 inline void requestRoutesPowerSupplyCollection(App& app)
111 {
112     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/")
113         .privileges(redfish::privileges::headPowerSupplyCollection)
114         .methods(boost::beast::http::verb::head)(
115             std::bind_front(handlePowerSupplyCollectionHead, std::ref(app)));
116 
117     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/")
118         .privileges(redfish::privileges::getPowerSupplyCollection)
119         .methods(boost::beast::http::verb::get)(
120             std::bind_front(handlePowerSupplyCollectionGet, std::ref(app)));
121 }
122 
123 } // namespace redfish
124