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