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