xref: /openbmc/bmcweb/features/redfish/lib/power_subsystem.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
377b36437SChicago Duan #pragma once
477b36437SChicago Duan 
577b36437SChicago Duan #include "app.hpp"
6539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
7ca9e6be6SNan Zhou #include "logging.hpp"
877b36437SChicago Duan #include "query.hpp"
977b36437SChicago Duan #include "registries/privilege_registry.hpp"
1077b36437SChicago Duan #include "utils/chassis_utils.hpp"
1177b36437SChicago Duan 
12ef4c65b7SEd Tanous #include <boost/url/format.hpp>
13ef4c65b7SEd Tanous 
1477b36437SChicago Duan #include <memory>
1577b36437SChicago Duan #include <optional>
1677b36437SChicago Duan #include <string>
1777b36437SChicago Duan 
1877b36437SChicago Duan namespace redfish
1977b36437SChicago Duan {
2077b36437SChicago Duan 
2177b36437SChicago Duan inline void doPowerSubsystemCollection(
2277b36437SChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2377b36437SChicago Duan     const std::string& chassisId,
2477b36437SChicago Duan     const std::optional<std::string>& validChassisPath)
2577b36437SChicago Duan {
2677b36437SChicago Duan     if (!validChassisPath)
2777b36437SChicago Duan     {
2877b36437SChicago Duan         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
2977b36437SChicago Duan         return;
3077b36437SChicago Duan     }
3177b36437SChicago Duan 
322ea468a0SGeorge Liu     asyncResp->res.addHeader(
332ea468a0SGeorge Liu         boost::beast::http::field::link,
342ea468a0SGeorge Liu         "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby");
3577b36437SChicago Duan     asyncResp->res.jsonValue["@odata.type"] =
3677b36437SChicago Duan         "#PowerSubsystem.v1_1_0.PowerSubsystem";
3777b36437SChicago Duan     asyncResp->res.jsonValue["Name"] = "Power Subsystem";
3877b36437SChicago Duan     asyncResp->res.jsonValue["Id"] = "PowerSubsystem";
39ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
40ef4c65b7SEd Tanous         boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem", chassisId);
41539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
42539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
43a7210020SGeorge Liu     asyncResp->res.jsonValue["PowerSupplies"]["@odata.id"] =
44ef4c65b7SEd Tanous         boost::urls::format(
45ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies", chassisId);
462ea468a0SGeorge Liu }
4777b36437SChicago Duan 
482ea468a0SGeorge Liu inline void handlePowerSubsystemCollectionHead(
492ea468a0SGeorge Liu     App& app, const crow::Request& req,
502ea468a0SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
512ea468a0SGeorge Liu     const std::string& chassisId)
522ea468a0SGeorge Liu {
532ea468a0SGeorge Liu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
542ea468a0SGeorge Liu     {
552ea468a0SGeorge Liu         return;
562ea468a0SGeorge Liu     }
572ea468a0SGeorge Liu 
582ea468a0SGeorge Liu     auto respHandler = [asyncResp, chassisId](
592ea468a0SGeorge Liu                            const std::optional<std::string>& validChassisPath) {
602ea468a0SGeorge Liu         if (!validChassisPath)
612ea468a0SGeorge Liu         {
622ea468a0SGeorge Liu             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
632ea468a0SGeorge Liu             return;
642ea468a0SGeorge Liu         }
6577b36437SChicago Duan         asyncResp->res.addHeader(
6677b36437SChicago Duan             boost::beast::http::field::link,
6777b36437SChicago Duan             "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby");
682ea468a0SGeorge Liu     };
692ea468a0SGeorge Liu     redfish::chassis_utils::getValidChassisPath(asyncResp, chassisId,
702ea468a0SGeorge Liu                                                 std::move(respHandler));
7177b36437SChicago Duan }
7277b36437SChicago Duan 
7377b36437SChicago Duan inline void handlePowerSubsystemCollectionGet(
7477b36437SChicago Duan     App& app, const crow::Request& req,
7577b36437SChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7677b36437SChicago Duan     const std::string& chassisId)
7777b36437SChicago Duan {
7877b36437SChicago Duan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
7977b36437SChicago Duan     {
8077b36437SChicago Duan         return;
8177b36437SChicago Duan     }
8277b36437SChicago Duan 
8377b36437SChicago Duan     redfish::chassis_utils::getValidChassisPath(
8477b36437SChicago Duan         asyncResp, chassisId,
8577b36437SChicago Duan         std::bind_front(doPowerSubsystemCollection, asyncResp, chassisId));
8677b36437SChicago Duan }
8777b36437SChicago Duan 
8877b36437SChicago Duan inline void requestRoutesPowerSubsystem(App& app)
8977b36437SChicago Duan {
9077b36437SChicago Duan     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
912ea468a0SGeorge Liu         .privileges(redfish::privileges::headPowerSubsystem)
922ea468a0SGeorge Liu         .methods(boost::beast::http::verb::head)(
932ea468a0SGeorge Liu             std::bind_front(handlePowerSubsystemCollectionHead, std::ref(app)));
942ea468a0SGeorge Liu 
952ea468a0SGeorge Liu     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
9677b36437SChicago Duan         .privileges(redfish::privileges::getPowerSubsystem)
9777b36437SChicago Duan         .methods(boost::beast::http::verb::get)(
9877b36437SChicago Duan             std::bind_front(handlePowerSubsystemCollectionGet, std::ref(app)));
9977b36437SChicago Duan }
10077b36437SChicago Duan 
10177b36437SChicago Duan } // namespace redfish
102