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