xref: /openbmc/bmcweb/redfish-core/lib/telemetry_service.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 "dbus_utility.hpp"
8 #include "error_messages.hpp"
9 #include "generated/enums/resource.hpp"
10 #include "http_request.hpp"
11 #include "logging.hpp"
12 #include "query.hpp"
13 #include "registries/privilege_registry.hpp"
14 #include "utils/dbus_utils.hpp"
15 #include "utils/telemetry_utils.hpp"
16 #include "utils/time_utils.hpp"
17 
18 #include <boost/beast/http/verb.hpp>
19 #include <sdbusplus/unpack_properties.hpp>
20 
21 #include <chrono>
22 #include <cstddef>
23 #include <cstdint>
24 #include <ctime>
25 #include <functional>
26 #include <memory>
27 #include <utility>
28 
29 namespace redfish
30 {
31 
handleTelemetryServiceGet(crow::App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp)32 inline void handleTelemetryServiceGet(
33     crow::App& app, const crow::Request& req,
34     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
35 {
36     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
37     {
38         return;
39     }
40     asyncResp->res.jsonValue["@odata.type"] =
41         "#TelemetryService.v1_2_1.TelemetryService";
42     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TelemetryService";
43     asyncResp->res.jsonValue["Id"] = "TelemetryService";
44     asyncResp->res.jsonValue["Name"] = "Telemetry Service";
45 
46     asyncResp->res.jsonValue["MetricReportDefinitions"]["@odata.id"] =
47         "/redfish/v1/TelemetryService/MetricReportDefinitions";
48     asyncResp->res.jsonValue["MetricReports"]["@odata.id"] =
49         "/redfish/v1/TelemetryService/MetricReports";
50     asyncResp->res.jsonValue["Triggers"]["@odata.id"] =
51         "/redfish/v1/TelemetryService/Triggers";
52 
53     dbus::utility::getAllProperties(
54         telemetry::service, "/xyz/openbmc_project/Telemetry/Reports",
55         "xyz.openbmc_project.Telemetry.ReportManager",
56         [asyncResp](const boost::system::error_code& ec,
57                     const dbus::utility::DBusPropertiesMap& ret) {
58             if (ec == boost::system::errc::host_unreachable)
59             {
60                 asyncResp->res.jsonValue["Status"]["State"] =
61                     resource::State::Absent;
62                 return;
63             }
64             if (ec)
65             {
66                 BMCWEB_LOG_ERROR("respHandler DBus error {}", ec);
67                 messages::internalError(asyncResp->res);
68                 return;
69             }
70 
71             asyncResp->res.jsonValue["Status"]["State"] =
72                 resource::State::Enabled;
73 
74             const size_t* maxReports = nullptr;
75             const uint64_t* minInterval = nullptr;
76 
77             const bool success = sdbusplus::unpackPropertiesNoThrow(
78                 dbus_utils::UnpackErrorPrinter(), ret, "MaxReports", maxReports,
79                 "MinInterval", minInterval);
80 
81             if (!success)
82             {
83                 messages::internalError(asyncResp->res);
84                 return;
85             }
86 
87             if (maxReports != nullptr)
88             {
89                 asyncResp->res.jsonValue["MaxReports"] = *maxReports;
90             }
91 
92             if (minInterval != nullptr)
93             {
94                 asyncResp->res.jsonValue["MinCollectionInterval"] =
95                     time_utils::toDurationString(std::chrono::milliseconds(
96                         static_cast<time_t>(*minInterval)));
97             }
98             nlohmann::json::array_t supportedCollectionFunctions;
99             supportedCollectionFunctions.emplace_back("Maximum");
100             supportedCollectionFunctions.emplace_back("Minimum");
101             supportedCollectionFunctions.emplace_back("Average");
102             supportedCollectionFunctions.emplace_back("Summation");
103 
104             asyncResp->res.jsonValue["SupportedCollectionFunctions"] =
105                 std::move(supportedCollectionFunctions);
106         });
107 }
108 
requestRoutesTelemetryService(App & app)109 inline void requestRoutesTelemetryService(App& app)
110 {
111     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/")
112         .privileges(redfish::privileges::getTelemetryService)
113         .methods(boost::beast::http::verb::get)(
114             std::bind_front(handleTelemetryServiceGet, std::ref(app)));
115 }
116 
117 } // namespace redfish
118