blob: 430a29a63218d9e864be39fbd65abed0bd7348b7 [file] [log] [blame]
George Liua7210022022-10-05 15:44:11 +08001#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
Ed Tanousef4c65b2023-04-24 15:28:50 -07009#include <boost/url/format.hpp>
10
George Liua7210022022-10-05 15:44:11 +080011#include <memory>
12#include <optional>
13#include <string>
14
15namespace redfish
16{
17
18inline 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
27inline 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";
Ed Tanousef4c65b2023-04-24 15:28:50 -070044 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
45 "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies", chassisId);
George Liua7210022022-10-05 15:44:11 +080046 asyncResp->res.jsonValue["Description"] =
47 "The collection of PowerSupply resource instances.";
George Liu7a2bb2c2023-04-11 10:44:33 +080048 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
49 asyncResp->res.jsonValue["Members@odata.count"] = 0;
George Liua7210022022-10-05 15:44:11 +080050
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
73inline 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
98inline 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
113inline 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