xref: /openbmc/bmcweb/features/redfish/lib/power_subsystem.hpp (revision 2ea468a096638b3ad829c538605ef3bcdbbff4ce)
177b36437SChicago Duan #pragma once
277b36437SChicago Duan 
377b36437SChicago Duan #include "app.hpp"
4ca9e6be6SNan Zhou #include "logging.hpp"
577b36437SChicago Duan #include "query.hpp"
677b36437SChicago Duan #include "registries/privilege_registry.hpp"
777b36437SChicago Duan #include "utils/chassis_utils.hpp"
877b36437SChicago Duan 
977b36437SChicago Duan #include <memory>
1077b36437SChicago Duan #include <optional>
1177b36437SChicago Duan #include <string>
1277b36437SChicago Duan 
1377b36437SChicago Duan namespace redfish
1477b36437SChicago Duan {
1577b36437SChicago Duan 
1677b36437SChicago Duan inline void doPowerSubsystemCollection(
1777b36437SChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1877b36437SChicago Duan     const std::string& chassisId,
1977b36437SChicago Duan     const std::optional<std::string>& validChassisPath)
2077b36437SChicago Duan {
2177b36437SChicago Duan     if (!validChassisPath)
2277b36437SChicago Duan     {
2377b36437SChicago Duan         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
2477b36437SChicago Duan         return;
2577b36437SChicago Duan     }
2677b36437SChicago Duan 
27*2ea468a0SGeorge Liu     asyncResp->res.addHeader(
28*2ea468a0SGeorge Liu         boost::beast::http::field::link,
29*2ea468a0SGeorge Liu         "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby");
3077b36437SChicago Duan     asyncResp->res.jsonValue["@odata.type"] =
3177b36437SChicago Duan         "#PowerSubsystem.v1_1_0.PowerSubsystem";
3277b36437SChicago Duan     asyncResp->res.jsonValue["Name"] = "Power Subsystem";
3377b36437SChicago Duan     asyncResp->res.jsonValue["Id"] = "PowerSubsystem";
3477b36437SChicago Duan     asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
3577b36437SChicago Duan         "redfish", "v1", "Chassis", chassisId, "PowerSubsystem");
3677b36437SChicago Duan     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
3777b36437SChicago Duan     asyncResp->res.jsonValue["Status"]["Health"] = "OK";
38*2ea468a0SGeorge Liu }
3977b36437SChicago Duan 
40*2ea468a0SGeorge Liu inline void handlePowerSubsystemCollectionHead(
41*2ea468a0SGeorge Liu     App& app, const crow::Request& req,
42*2ea468a0SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
43*2ea468a0SGeorge Liu     const std::string& chassisId)
44*2ea468a0SGeorge Liu {
45*2ea468a0SGeorge Liu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
46*2ea468a0SGeorge Liu     {
47*2ea468a0SGeorge Liu         return;
48*2ea468a0SGeorge Liu     }
49*2ea468a0SGeorge Liu 
50*2ea468a0SGeorge Liu     auto respHandler = [asyncResp, chassisId](
51*2ea468a0SGeorge Liu                            const std::optional<std::string>& validChassisPath) {
52*2ea468a0SGeorge Liu         if (!validChassisPath)
53*2ea468a0SGeorge Liu         {
54*2ea468a0SGeorge Liu             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
55*2ea468a0SGeorge Liu             return;
56*2ea468a0SGeorge Liu         }
5777b36437SChicago Duan         asyncResp->res.addHeader(
5877b36437SChicago Duan             boost::beast::http::field::link,
5977b36437SChicago Duan             "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby");
60*2ea468a0SGeorge Liu     };
61*2ea468a0SGeorge Liu     redfish::chassis_utils::getValidChassisPath(asyncResp, chassisId,
62*2ea468a0SGeorge Liu                                                 std::move(respHandler));
6377b36437SChicago Duan }
6477b36437SChicago Duan 
6577b36437SChicago Duan inline void handlePowerSubsystemCollectionGet(
6677b36437SChicago Duan     App& app, const crow::Request& req,
6777b36437SChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6877b36437SChicago Duan     const std::string& chassisId)
6977b36437SChicago Duan {
7077b36437SChicago Duan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
7177b36437SChicago Duan     {
7277b36437SChicago Duan         return;
7377b36437SChicago Duan     }
7477b36437SChicago Duan 
7577b36437SChicago Duan     redfish::chassis_utils::getValidChassisPath(
7677b36437SChicago Duan         asyncResp, chassisId,
7777b36437SChicago Duan         std::bind_front(doPowerSubsystemCollection, asyncResp, chassisId));
7877b36437SChicago Duan }
7977b36437SChicago Duan 
8077b36437SChicago Duan inline void requestRoutesPowerSubsystem(App& app)
8177b36437SChicago Duan {
8277b36437SChicago Duan     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
83*2ea468a0SGeorge Liu         .privileges(redfish::privileges::headPowerSubsystem)
84*2ea468a0SGeorge Liu         .methods(boost::beast::http::verb::head)(
85*2ea468a0SGeorge Liu             std::bind_front(handlePowerSubsystemCollectionHead, std::ref(app)));
86*2ea468a0SGeorge Liu 
87*2ea468a0SGeorge Liu     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
8877b36437SChicago Duan         .privileges(redfish::privileges::getPowerSubsystem)
8977b36437SChicago Duan         .methods(boost::beast::http::verb::get)(
9077b36437SChicago Duan             std::bind_front(handlePowerSubsystemCollectionGet, std::ref(app)));
9177b36437SChicago Duan }
9277b36437SChicago Duan 
9377b36437SChicago Duan } // namespace redfish
94