xref: /openbmc/bmcweb/features/redfish/lib/power_subsystem.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
377b36437SChicago Duan #pragma once
477b36437SChicago Duan 
577b36437SChicago Duan #include "app.hpp"
6*d7857201SEd Tanous #include "async_resp.hpp"
7*d7857201SEd Tanous #include "error_messages.hpp"
8539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
9*d7857201SEd Tanous #include "http_request.hpp"
1077b36437SChicago Duan #include "query.hpp"
1177b36437SChicago Duan #include "registries/privilege_registry.hpp"
1277b36437SChicago Duan #include "utils/chassis_utils.hpp"
1377b36437SChicago Duan 
14*d7857201SEd Tanous #include <boost/beast/http/field.hpp>
15*d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
16ef4c65b7SEd Tanous #include <boost/url/format.hpp>
17ef4c65b7SEd Tanous 
18*d7857201SEd Tanous #include <functional>
1977b36437SChicago Duan #include <memory>
2077b36437SChicago Duan #include <optional>
2177b36437SChicago Duan #include <string>
22*d7857201SEd Tanous #include <utility>
2377b36437SChicago Duan 
2477b36437SChicago Duan namespace redfish
2577b36437SChicago Duan {
2677b36437SChicago Duan 
2777b36437SChicago Duan inline void doPowerSubsystemCollection(
2877b36437SChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2977b36437SChicago Duan     const std::string& chassisId,
3077b36437SChicago Duan     const std::optional<std::string>& validChassisPath)
3177b36437SChicago Duan {
3277b36437SChicago Duan     if (!validChassisPath)
3377b36437SChicago Duan     {
3477b36437SChicago Duan         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
3577b36437SChicago Duan         return;
3677b36437SChicago Duan     }
3777b36437SChicago Duan 
382ea468a0SGeorge Liu     asyncResp->res.addHeader(
392ea468a0SGeorge Liu         boost::beast::http::field::link,
402ea468a0SGeorge Liu         "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby");
4177b36437SChicago Duan     asyncResp->res.jsonValue["@odata.type"] =
4277b36437SChicago Duan         "#PowerSubsystem.v1_1_0.PowerSubsystem";
4377b36437SChicago Duan     asyncResp->res.jsonValue["Name"] = "Power Subsystem";
4477b36437SChicago Duan     asyncResp->res.jsonValue["Id"] = "PowerSubsystem";
45ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
46ef4c65b7SEd Tanous         boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem", chassisId);
47539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
48539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
49a7210020SGeorge Liu     asyncResp->res.jsonValue["PowerSupplies"]["@odata.id"] =
50ef4c65b7SEd Tanous         boost::urls::format(
51ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies", chassisId);
522ea468a0SGeorge Liu }
5377b36437SChicago Duan 
542ea468a0SGeorge Liu inline void handlePowerSubsystemCollectionHead(
552ea468a0SGeorge Liu     App& app, const crow::Request& req,
562ea468a0SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
572ea468a0SGeorge Liu     const std::string& chassisId)
582ea468a0SGeorge Liu {
592ea468a0SGeorge Liu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
602ea468a0SGeorge Liu     {
612ea468a0SGeorge Liu         return;
622ea468a0SGeorge Liu     }
632ea468a0SGeorge Liu 
642ea468a0SGeorge Liu     auto respHandler = [asyncResp, chassisId](
652ea468a0SGeorge Liu                            const std::optional<std::string>& validChassisPath) {
662ea468a0SGeorge Liu         if (!validChassisPath)
672ea468a0SGeorge Liu         {
682ea468a0SGeorge Liu             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
692ea468a0SGeorge Liu             return;
702ea468a0SGeorge Liu         }
7177b36437SChicago Duan         asyncResp->res.addHeader(
7277b36437SChicago Duan             boost::beast::http::field::link,
7377b36437SChicago Duan             "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby");
742ea468a0SGeorge Liu     };
752ea468a0SGeorge Liu     redfish::chassis_utils::getValidChassisPath(asyncResp, chassisId,
762ea468a0SGeorge Liu                                                 std::move(respHandler));
7777b36437SChicago Duan }
7877b36437SChicago Duan 
7977b36437SChicago Duan inline void handlePowerSubsystemCollectionGet(
8077b36437SChicago Duan     App& app, const crow::Request& req,
8177b36437SChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8277b36437SChicago Duan     const std::string& chassisId)
8377b36437SChicago Duan {
8477b36437SChicago Duan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
8577b36437SChicago Duan     {
8677b36437SChicago Duan         return;
8777b36437SChicago Duan     }
8877b36437SChicago Duan 
8977b36437SChicago Duan     redfish::chassis_utils::getValidChassisPath(
9077b36437SChicago Duan         asyncResp, chassisId,
9177b36437SChicago Duan         std::bind_front(doPowerSubsystemCollection, asyncResp, chassisId));
9277b36437SChicago Duan }
9377b36437SChicago Duan 
9477b36437SChicago Duan inline void requestRoutesPowerSubsystem(App& app)
9577b36437SChicago Duan {
9677b36437SChicago Duan     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
972ea468a0SGeorge Liu         .privileges(redfish::privileges::headPowerSubsystem)
982ea468a0SGeorge Liu         .methods(boost::beast::http::verb::head)(
992ea468a0SGeorge Liu             std::bind_front(handlePowerSubsystemCollectionHead, std::ref(app)));
1002ea468a0SGeorge Liu 
1012ea468a0SGeorge Liu     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/")
10277b36437SChicago Duan         .privileges(redfish::privileges::getPowerSubsystem)
10377b36437SChicago Duan         .methods(boost::beast::http::verb::get)(
10477b36437SChicago Duan             std::bind_front(handlePowerSubsystemCollectionGet, std::ref(app)));
10577b36437SChicago Duan }
10677b36437SChicago Duan 
10777b36437SChicago Duan } // namespace redfish
108