xref: /openbmc/bmcweb/features/redfish/lib/power_subsystem.hpp (revision a72100201c2ebf3e8da33e9f325ae871bc4598bf)
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 
272ea468a0SGeorge Liu     asyncResp->res.addHeader(
282ea468a0SGeorge Liu         boost::beast::http::field::link,
292ea468a0SGeorge 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*a7210020SGeorge Liu     asyncResp->res.jsonValue["PowerSupplies"]["@odata.id"] =
39*a7210020SGeorge Liu         crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId,
40*a7210020SGeorge Liu                                      "PowerSubsystem", "PowerSupplies");
412ea468a0SGeorge Liu }
4277b36437SChicago Duan 
432ea468a0SGeorge Liu inline void handlePowerSubsystemCollectionHead(
442ea468a0SGeorge Liu     App& app, const crow::Request& req,
452ea468a0SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
462ea468a0SGeorge Liu     const std::string& chassisId)
472ea468a0SGeorge Liu {
482ea468a0SGeorge Liu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
492ea468a0SGeorge Liu     {
502ea468a0SGeorge Liu         return;
512ea468a0SGeorge Liu     }
522ea468a0SGeorge Liu 
532ea468a0SGeorge Liu     auto respHandler = [asyncResp, chassisId](
542ea468a0SGeorge Liu                            const std::optional<std::string>& validChassisPath) {
552ea468a0SGeorge Liu         if (!validChassisPath)
562ea468a0SGeorge Liu         {
572ea468a0SGeorge Liu             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
582ea468a0SGeorge Liu             return;
592ea468a0SGeorge Liu         }
6077b36437SChicago Duan         asyncResp->res.addHeader(
6177b36437SChicago Duan             boost::beast::http::field::link,
6277b36437SChicago Duan             "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby");
632ea468a0SGeorge Liu     };
642ea468a0SGeorge Liu     redfish::chassis_utils::getValidChassisPath(asyncResp, chassisId,
652ea468a0SGeorge Liu                                                 std::move(respHandler));
6677b36437SChicago Duan }
6777b36437SChicago Duan 
6877b36437SChicago Duan inline void handlePowerSubsystemCollectionGet(
6977b36437SChicago Duan     App& app, const crow::Request& req,
7077b36437SChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7177b36437SChicago Duan     const std::string& chassisId)
7277b36437SChicago Duan {
7377b36437SChicago Duan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
7477b36437SChicago Duan     {
7577b36437SChicago Duan         return;
7677b36437SChicago Duan     }
7777b36437SChicago Duan 
7877b36437SChicago Duan     redfish::chassis_utils::getValidChassisPath(
7977b36437SChicago Duan         asyncResp, chassisId,
8077b36437SChicago Duan         std::bind_front(doPowerSubsystemCollection, asyncResp, chassisId));
8177b36437SChicago Duan }
8277b36437SChicago Duan 
8377b36437SChicago Duan inline void requestRoutesPowerSubsystem(App& app)
8477b36437SChicago Duan {
8577b36437SChicago Duan     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
862ea468a0SGeorge Liu         .privileges(redfish::privileges::headPowerSubsystem)
872ea468a0SGeorge Liu         .methods(boost::beast::http::verb::head)(
882ea468a0SGeorge Liu             std::bind_front(handlePowerSubsystemCollectionHead, std::ref(app)));
892ea468a0SGeorge Liu 
902ea468a0SGeorge Liu     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
9177b36437SChicago Duan         .privileges(redfish::privileges::getPowerSubsystem)
9277b36437SChicago Duan         .methods(boost::beast::http::verb::get)(
9377b36437SChicago Duan             std::bind_front(handlePowerSubsystemCollectionGet, std::ref(app)));
9477b36437SChicago Duan }
9577b36437SChicago Duan 
9677b36437SChicago Duan } // namespace redfish
97