1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors 3 #pragma once 4 5 #include "app.hpp" 6 #include "generated/enums/resource.hpp" 7 #include "logging.hpp" 8 #include "query.hpp" 9 #include "registries/privilege_registry.hpp" 10 #include "utils/chassis_utils.hpp" 11 12 #include <boost/url/format.hpp> 13 14 #include <memory> 15 #include <optional> 16 #include <string> 17 18 namespace redfish 19 { 20 21 inline void doPowerSubsystemCollection( 22 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 23 const std::string& chassisId, 24 const std::optional<std::string>& validChassisPath) 25 { 26 if (!validChassisPath) 27 { 28 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId); 29 return; 30 } 31 32 asyncResp->res.addHeader( 33 boost::beast::http::field::link, 34 "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby"); 35 asyncResp->res.jsonValue["@odata.type"] = 36 "#PowerSubsystem.v1_1_0.PowerSubsystem"; 37 asyncResp->res.jsonValue["Name"] = "Power Subsystem"; 38 asyncResp->res.jsonValue["Id"] = "PowerSubsystem"; 39 asyncResp->res.jsonValue["@odata.id"] = 40 boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem", chassisId); 41 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled; 42 asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK; 43 asyncResp->res.jsonValue["PowerSupplies"]["@odata.id"] = 44 boost::urls::format( 45 "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies", chassisId); 46 } 47 48 inline void handlePowerSubsystemCollectionHead( 49 App& app, const crow::Request& req, 50 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 51 const std::string& chassisId) 52 { 53 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 54 { 55 return; 56 } 57 58 auto respHandler = [asyncResp, chassisId]( 59 const std::optional<std::string>& validChassisPath) { 60 if (!validChassisPath) 61 { 62 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId); 63 return; 64 } 65 asyncResp->res.addHeader( 66 boost::beast::http::field::link, 67 "</redfish/v1/JsonSchemas/PowerSubsystem/PowerSubsystem.json>; rel=describedby"); 68 }; 69 redfish::chassis_utils::getValidChassisPath(asyncResp, chassisId, 70 std::move(respHandler)); 71 } 72 73 inline void handlePowerSubsystemCollectionGet( 74 App& app, const crow::Request& req, 75 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 76 const std::string& chassisId) 77 { 78 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 79 { 80 return; 81 } 82 83 redfish::chassis_utils::getValidChassisPath( 84 asyncResp, chassisId, 85 std::bind_front(doPowerSubsystemCollection, asyncResp, chassisId)); 86 } 87 88 inline void requestRoutesPowerSubsystem(App& app) 89 { 90 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/") 91 .privileges(redfish::privileges::headPowerSubsystem) 92 .methods(boost::beast::http::verb::head)( 93 std::bind_front(handlePowerSubsystemCollectionHead, std::ref(app))); 94 95 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/") 96 .privileges(redfish::privileges::getPowerSubsystem) 97 .methods(boost::beast::http::verb::get)( 98 std::bind_front(handlePowerSubsystemCollectionGet, std::ref(app))); 99 } 100 101 } // namespace redfish 102