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