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 |
Albert Zhang | 4ca3ec3 | 2021-06-13 14:39:38 +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" |
| 8 | #include "http_request.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 9 | #include "query.hpp" |
| 10 | #include "registries/privilege_registry.hpp" |
Albert Zhang | 4ca3ec3 | 2021-06-13 14:39:38 +0800 | [diff] [blame] | 11 | #include "utils/chassis_utils.hpp" |
| 12 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 13 | #include <boost/beast/http/field.hpp> |
| 14 | #include <boost/beast/http/verb.hpp> |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 15 | #include <boost/url/format.hpp> |
| 16 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 17 | #include <functional> |
Albert Zhang | 4ca3ec3 | 2021-06-13 14:39:38 +0800 | [diff] [blame] | 18 | #include <memory> |
| 19 | #include <optional> |
| 20 | #include <string> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 21 | #include <utility> |
Albert Zhang | 4ca3ec3 | 2021-06-13 14:39:38 +0800 | [diff] [blame] | 22 | |
| 23 | namespace redfish |
| 24 | { |
| 25 | |
| 26 | inline void handleEnvironmentMetricsHead( |
| 27 | App& app, const crow::Request& req, |
| 28 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 29 | const std::string& chassisId) |
| 30 | { |
| 31 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 32 | { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | auto respHandler = [asyncResp, chassisId]( |
| 37 | const std::optional<std::string>& validChassisPath) { |
| 38 | if (!validChassisPath) |
| 39 | { |
| 40 | messages::resourceNotFound(asyncResp->res, "Chassis", chassisId); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | asyncResp->res.addHeader( |
| 45 | boost::beast::http::field::link, |
| 46 | "</redfish/v1/JsonSchemas/EnvironmentMetrics/EnvironmentMetrics.json>; rel=describedby"); |
| 47 | }; |
| 48 | |
| 49 | redfish::chassis_utils::getValidChassisPath(asyncResp, chassisId, |
| 50 | std::move(respHandler)); |
| 51 | } |
| 52 | |
| 53 | inline void handleEnvironmentMetricsGet( |
| 54 | App& app, const crow::Request& req, |
| 55 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 56 | const std::string& chassisId) |
| 57 | { |
| 58 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 59 | { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | auto respHandler = [asyncResp, chassisId]( |
| 64 | const std::optional<std::string>& validChassisPath) { |
| 65 | if (!validChassisPath) |
| 66 | { |
| 67 | messages::resourceNotFound(asyncResp->res, "Chassis", chassisId); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | asyncResp->res.addHeader( |
| 72 | boost::beast::http::field::link, |
| 73 | "</redfish/v1/JsonSchemas/EnvironmentMetrics/EnvironmentMetrics.json>; rel=describedby"); |
| 74 | asyncResp->res.jsonValue["@odata.type"] = |
| 75 | "#EnvironmentMetrics.v1_3_0.EnvironmentMetrics"; |
| 76 | asyncResp->res.jsonValue["Name"] = "Chassis Environment Metrics"; |
| 77 | asyncResp->res.jsonValue["Id"] = "EnvironmentMetrics"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 78 | asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 79 | "/redfish/v1/Chassis/{}/EnvironmentMetrics", chassisId); |
Albert Zhang | 4ca3ec3 | 2021-06-13 14:39:38 +0800 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | redfish::chassis_utils::getValidChassisPath(asyncResp, chassisId, |
| 83 | std::move(respHandler)); |
| 84 | } |
| 85 | |
| 86 | inline void requestRoutesEnvironmentMetrics(App& app) |
| 87 | { |
| 88 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/EnvironmentMetrics/") |
| 89 | .privileges(redfish::privileges::headEnvironmentMetrics) |
| 90 | .methods(boost::beast::http::verb::head)( |
| 91 | std::bind_front(handleEnvironmentMetricsHead, std::ref(app))); |
| 92 | |
| 93 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/EnvironmentMetrics/") |
| 94 | .privileges(redfish::privileges::getEnvironmentMetrics) |
| 95 | .methods(boost::beast::http::verb::get)( |
| 96 | std::bind_front(handleEnvironmentMetricsGet, std::ref(app))); |
| 97 | } |
| 98 | |
| 99 | } // namespace redfish |