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