xref: /openbmc/bmcweb/redfish-core/lib/environment_metrics.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "app.hpp"
6 #include "async_resp.hpp"
7 #include "error_messages.hpp"
8 #include "http_request.hpp"
9 #include "query.hpp"
10 #include "registries/privilege_registry.hpp"
11 #include "utils/chassis_utils.hpp"
12 
13 #include <boost/beast/http/field.hpp>
14 #include <boost/beast/http/verb.hpp>
15 #include <boost/url/format.hpp>
16 
17 #include <functional>
18 #include <memory>
19 #include <optional>
20 #include <string>
21 #include <utility>
22 
23 namespace redfish
24 {
25 
handleEnvironmentMetricsHead(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId)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 
handleEnvironmentMetricsGet(App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId)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";
78         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
79             "/redfish/v1/Chassis/{}/EnvironmentMetrics", chassisId);
80     };
81 
82     redfish::chassis_utils::getValidChassisPath(asyncResp, chassisId,
83                                                 std::move(respHandler));
84 }
85 
requestRoutesEnvironmentMetrics(App & app)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
100