xref: /openbmc/bmcweb/features/redfish/lib/telemetry_service.hpp (revision b9d36b4791d77a47e1f3c5c4564fcdf7cc68c115)
1 #pragma once
2 
3 #include "utils/telemetry_utils.hpp"
4 
5 #include <app.hpp>
6 #include <dbus_utility.hpp>
7 #include <registries/privilege_registry.hpp>
8 
9 namespace redfish
10 {
11 
12 inline void handleTelemetryServiceGet(
13     const crow::Request& /*req*/,
14     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15 {
16     asyncResp->res.jsonValue["@odata.type"] =
17         "#TelemetryService.v1_2_1.TelemetryService";
18     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TelemetryService";
19     asyncResp->res.jsonValue["Id"] = "TelemetryService";
20     asyncResp->res.jsonValue["Name"] = "Telemetry Service";
21 
22     asyncResp->res.jsonValue["MetricReportDefinitions"]["@odata.id"] =
23         "/redfish/v1/TelemetryService/MetricReportDefinitions";
24     asyncResp->res.jsonValue["MetricReports"]["@odata.id"] =
25         "/redfish/v1/TelemetryService/MetricReports";
26     asyncResp->res.jsonValue["Triggers"]["@odata.id"] =
27         "/redfish/v1/TelemetryService/Triggers";
28 
29     crow::connections::systemBus->async_method_call(
30         [asyncResp](const boost::system::error_code ec,
31                     const dbus::utility::DBusPropertiesMap& ret) {
32             if (ec == boost::system::errc::host_unreachable)
33             {
34                 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
35                 return;
36             }
37             if (ec)
38             {
39                 BMCWEB_LOG_ERROR << "respHandler DBus error " << ec;
40                 messages::internalError(asyncResp->res);
41                 return;
42             }
43 
44             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
45 
46             const size_t* maxReports = nullptr;
47             const uint64_t* minInterval = nullptr;
48             for (const auto& [key, var] : ret)
49             {
50                 if (key == "MaxReports")
51                 {
52                     maxReports = std::get_if<size_t>(&var);
53                 }
54                 else if (key == "MinInterval")
55                 {
56                     minInterval = std::get_if<uint64_t>(&var);
57                 }
58             }
59             if (maxReports == nullptr || minInterval == nullptr)
60             {
61                 BMCWEB_LOG_ERROR
62                     << "Property type mismatch or property is missing";
63                 messages::internalError(asyncResp->res);
64                 return;
65             }
66 
67             asyncResp->res.jsonValue["MaxReports"] = *maxReports;
68             asyncResp->res.jsonValue["MinCollectionInterval"] =
69                 time_utils::toDurationString(std::chrono::milliseconds(
70                     static_cast<time_t>(*minInterval)));
71         },
72         telemetry::service, "/xyz/openbmc_project/Telemetry/Reports",
73         "org.freedesktop.DBus.Properties", "GetAll",
74         "xyz.openbmc_project.Telemetry.ReportManager");
75 }
76 
77 inline void requestRoutesTelemetryService(App& app)
78 {
79     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/")
80         .privileges(redfish::privileges::getTelemetryService)
81         .methods(boost::beast::http::verb::get)(handleTelemetryServiceGet);
82 }
83 
84 } // namespace redfish
85