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