1 #pragma once
2 
3 #include "app.hpp"
4 #include "generated/enums/resource.hpp"
5 #include "logging.hpp"
6 #include "query.hpp"
7 #include "registries/privilege_registry.hpp"
8 #include "utils/chassis_utils.hpp"
9 
10 #include <boost/url/format.hpp>
11 
12 #include <memory>
13 #include <optional>
14 #include <string>
15 
16 namespace redfish
17 {
18 
doPowerSubsystemCollection(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId,const std::optional<std::string> & validChassisPath)19 inline void doPowerSubsystemCollection(
20     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21     const std::string& chassisId,
22     const std::optional<std::string>& validChassisPath)
23 {
24     if (!validChassisPath)
25     {
26         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
27         return;
28     }
29 
30     asyncResp->res.addHeader(
31         boost::beast::http::field::link,
32         "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby");
33     asyncResp->res.jsonValue["@odata.type"] =
34         "#PowerSubsystem.v1_1_0.PowerSubsystem";
35     asyncResp->res.jsonValue["Name"] = "Power Subsystem";
36     asyncResp->res.jsonValue["Id"] = "PowerSubsystem";
37     asyncResp->res.jsonValue["@odata.id"] =
38         boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem", chassisId);
39     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
40     asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
41     asyncResp->res.jsonValue["PowerSupplies"]["@odata.id"] =
42         boost::urls::format(
43             "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies", chassisId);
44 }
45 
handlePowerSubsystemCollectionHead(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId)46 inline void handlePowerSubsystemCollectionHead(
47     App& app, const crow::Request& req,
48     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
49     const std::string& chassisId)
50 {
51     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
52     {
53         return;
54     }
55 
56     auto respHandler = [asyncResp, chassisId](
57                            const std::optional<std::string>& validChassisPath) {
58         if (!validChassisPath)
59         {
60             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
61             return;
62         }
63         asyncResp->res.addHeader(
64             boost::beast::http::field::link,
65             "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby");
66     };
67     redfish::chassis_utils::getValidChassisPath(asyncResp, chassisId,
68                                                 std::move(respHandler));
69 }
70 
handlePowerSubsystemCollectionGet(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId)71 inline void handlePowerSubsystemCollectionGet(
72     App& app, const crow::Request& req,
73     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
74     const std::string& chassisId)
75 {
76     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
77     {
78         return;
79     }
80 
81     redfish::chassis_utils::getValidChassisPath(
82         asyncResp, chassisId,
83         std::bind_front(doPowerSubsystemCollection, asyncResp, chassisId));
84 }
85 
requestRoutesPowerSubsystem(App & app)86 inline void requestRoutesPowerSubsystem(App& app)
87 {
88     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
89         .privileges(redfish::privileges::headPowerSubsystem)
90         .methods(boost::beast::http::verb::head)(
91             std::bind_front(handlePowerSubsystemCollectionHead, std::ref(app)));
92 
93     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
94         .privileges(redfish::privileges::getPowerSubsystem)
95         .methods(boost::beast::http::verb::get)(
96             std::bind_front(handlePowerSubsystemCollectionGet, std::ref(app)));
97 }
98 
99 } // namespace redfish
100