xref: /openbmc/bmcweb/redfish-core/lib/metric_report.hpp (revision f86cdd7d287caa93d6508114d77e058937a2bf4a)
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 "dbus_singleton.hpp"
8 #include "dbus_utility.hpp"
9 #include "error_messages.hpp"
10 #include "http_request.hpp"
11 #include "logging.hpp"
12 #include "query.hpp"
13 #include "registries/privilege_registry.hpp"
14 #include "telemetry_readings.hpp"
15 #include "utils/collection.hpp"
16 #include "utils/telemetry_utils.hpp"
17 
18 #include <asm-generic/errno.h>
19 
20 #include <boost/beast/http/verb.hpp>
21 #include <boost/url/format.hpp>
22 #include <boost/url/url.hpp>
23 #include <nlohmann/json.hpp>
24 #include <sdbusplus/asio/property.hpp>
25 
26 #include <array>
27 #include <cstdint>
28 #include <memory>
29 #include <string>
30 #include <string_view>
31 #include <utility>
32 
33 namespace redfish
34 {
35 
requestRoutesMetricReportCollection(App & app)36 inline void requestRoutesMetricReportCollection(App& app)
37 {
38     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/")
39         .privileges(redfish::privileges::getMetricReportCollection)
40         .methods(boost::beast::http::verb::get)(
41             [&app](const crow::Request& req,
42                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
43                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
44                 {
45                     return;
46                 }
47 
48                 asyncResp->res.jsonValue["@odata.type"] =
49                     "#MetricReportCollection.MetricReportCollection";
50                 asyncResp->res.jsonValue["@odata.id"] =
51                     "/redfish/v1/TelemetryService/MetricReports";
52                 asyncResp->res.jsonValue["Name"] = "Metric Report Collection";
53                 constexpr std::array<std::string_view, 1> interfaces{
54                     telemetry::reportInterface};
55                 collection_util::getCollectionMembers(
56                     asyncResp,
57                     boost::urls::url(
58                         "/redfish/v1/TelemetryService/MetricReports"),
59                     interfaces,
60                     "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
61             });
62 }
63 
requestRoutesMetricReport(App & app)64 inline void requestRoutesMetricReport(App& app)
65 {
66     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReports/<str>/")
67         .privileges(redfish::privileges::getMetricReport)
68         .methods(boost::beast::http::verb::get)(
69             [&app](const crow::Request& req,
70                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
71                    const std::string& id) {
72                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
73                 {
74                     return;
75                 }
76                 const std::string reportPath = telemetry::getDbusReportPath(id);
77                 dbus::utility::async_method_call(
78                     asyncResp,
79                     [asyncResp, id,
80                      reportPath](const boost::system::error_code& ec) {
81                         if (ec.value() == EBADR ||
82                             ec == boost::system::errc::host_unreachable)
83                         {
84                             messages::resourceNotFound(asyncResp->res,
85                                                        "MetricReport", id);
86                             return;
87                         }
88                         if (ec)
89                         {
90                             BMCWEB_LOG_ERROR("respHandler DBus error {}", ec);
91                             messages::internalError(asyncResp->res);
92                             return;
93                         }
94 
95                         sdbusplus::asio::getProperty<
96                             telemetry::TimestampReadings>(
97                             *crow::connections::systemBus, telemetry::service,
98                             reportPath, telemetry::reportInterface, "Readings",
99                             [asyncResp,
100                              id](const boost::system::error_code& ec2,
101                                  const telemetry::TimestampReadings& ret) {
102                                 if (ec2)
103                                 {
104                                     BMCWEB_LOG_ERROR(
105                                         "respHandler DBus error {}", ec2);
106                                     messages::internalError(asyncResp->res);
107                                     return;
108                                 }
109 
110                                 telemetry::fillReport(asyncResp->res.jsonValue,
111                                                       id, ret);
112                             });
113                     },
114                     telemetry::service, reportPath, telemetry::reportInterface,
115                     "Update");
116             });
117 }
118 } // namespace redfish
119