xref: /openbmc/bmcweb/features/redfish/lib/power_subsystem.hpp (revision ca9e6be63a9ca27e61563cde688ce3e8f47c233f)
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         BMCWEB_LOG_ERROR << "Not a valid chassis ID" << chassisId;
24         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
25         return;
26     }
27 
28     asyncResp->res.jsonValue["@odata.type"] =
29         "#PowerSubsystem.v1_1_0.PowerSubsystem";
30     asyncResp->res.jsonValue["Name"] = "Power Subsystem";
31     asyncResp->res.jsonValue["Id"] = "PowerSubsystem";
32     asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
33         "redfish", "v1", "Chassis", chassisId, "PowerSubsystem");
34     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
35     asyncResp->res.jsonValue["Status"]["Health"] = "OK";
36 
37     asyncResp->res.addHeader(
38         boost::beast::http::field::link,
39         "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby");
40 }
41 
42 inline void handlePowerSubsystemCollectionGet(
43     App& app, const crow::Request& req,
44     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
45     const std::string& chassisId)
46 {
47     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
48     {
49         return;
50     }
51 
52     redfish::chassis_utils::getValidChassisPath(
53         asyncResp, chassisId,
54         std::bind_front(doPowerSubsystemCollection, asyncResp, chassisId));
55 }
56 
57 inline void requestRoutesPowerSubsystem(App& app)
58 {
59     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
60         .privileges(redfish::privileges::getPowerSubsystem)
61         .methods(boost::beast::http::verb::get)(
62             std::bind_front(handlePowerSubsystemCollectionGet, std::ref(app)));
63 }
64 
65 } // namespace redfish
66