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