xref: /openbmc/bmcweb/features/redfish/lib/telemetry_service.hpp (revision 539d8c6bc399e516d39bcaa18262cf206f4c1035)
1081ebf06SWludzik, Jozef #pragma once
2081ebf06SWludzik, Jozef 
33ccb3adbSEd Tanous #include "app.hpp"
43ccb3adbSEd Tanous #include "dbus_utility.hpp"
5*539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
63ccb3adbSEd Tanous #include "query.hpp"
73ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
83ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
9081ebf06SWludzik, Jozef #include "utils/telemetry_utils.hpp"
10d093c996SEd Tanous #include "utils/time_utils.hpp"
11081ebf06SWludzik, Jozef 
1289474494SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
1389474494SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
147e860f15SJohn Edward Broadbent 
15081ebf06SWludzik, Jozef namespace redfish
16081ebf06SWludzik, Jozef {
17081ebf06SWludzik, Jozef 
1826bd9b57SJohn Edward Broadbent inline void handleTelemetryServiceGet(
1945ca1b86SEd Tanous     crow::App& app, const crow::Request& req,
20104f09c9SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21081ebf06SWludzik, Jozef {
223ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2345ca1b86SEd Tanous     {
2445ca1b86SEd Tanous         return;
2545ca1b86SEd Tanous     }
268d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
27081ebf06SWludzik, Jozef         "#TelemetryService.v1_2_1.TelemetryService";
2826bd9b57SJohn Edward Broadbent     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TelemetryService";
298d1b46d7Szhanghch05     asyncResp->res.jsonValue["Id"] = "TelemetryService";
308d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Telemetry Service";
31081ebf06SWludzik, Jozef 
328d1b46d7Szhanghch05     asyncResp->res.jsonValue["MetricReportDefinitions"]["@odata.id"] =
33081ebf06SWludzik, Jozef         "/redfish/v1/TelemetryService/MetricReportDefinitions";
348d1b46d7Szhanghch05     asyncResp->res.jsonValue["MetricReports"]["@odata.id"] =
35081ebf06SWludzik, Jozef         "/redfish/v1/TelemetryService/MetricReports";
3607148cf2SLukasz Kazmierczak     asyncResp->res.jsonValue["Triggers"]["@odata.id"] =
3707148cf2SLukasz Kazmierczak         "/redfish/v1/TelemetryService/Triggers";
38081ebf06SWludzik, Jozef 
3989474494SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
4089474494SKrzysztof Grobelny         *crow::connections::systemBus, telemetry::service,
4189474494SKrzysztof Grobelny         "/xyz/openbmc_project/Telemetry/Reports",
4289474494SKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.ReportManager",
435e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
44b9d36b47SEd Tanous                     const dbus::utility::DBusPropertiesMap& ret) {
45081ebf06SWludzik, Jozef         if (ec == boost::system::errc::host_unreachable)
46081ebf06SWludzik, Jozef         {
47*539d8c6bSEd Tanous             asyncResp->res.jsonValue["Status"]["State"] =
48*539d8c6bSEd Tanous                 resource::State::Absent;
49081ebf06SWludzik, Jozef             return;
50081ebf06SWludzik, Jozef         }
51081ebf06SWludzik, Jozef         if (ec)
52081ebf06SWludzik, Jozef         {
5362598e31SEd Tanous             BMCWEB_LOG_ERROR("respHandler DBus error {}", ec);
54081ebf06SWludzik, Jozef             messages::internalError(asyncResp->res);
55081ebf06SWludzik, Jozef             return;
56081ebf06SWludzik, Jozef         }
57081ebf06SWludzik, Jozef 
58*539d8c6bSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
59081ebf06SWludzik, Jozef 
60081ebf06SWludzik, Jozef         const size_t* maxReports = nullptr;
61081ebf06SWludzik, Jozef         const uint64_t* minInterval = nullptr;
6289474494SKrzysztof Grobelny 
6389474494SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
6489474494SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), ret, "MaxReports", maxReports,
6589474494SKrzysztof Grobelny             "MinInterval", minInterval);
6689474494SKrzysztof Grobelny 
6789474494SKrzysztof Grobelny         if (!success)
68081ebf06SWludzik, Jozef         {
69081ebf06SWludzik, Jozef             messages::internalError(asyncResp->res);
70081ebf06SWludzik, Jozef             return;
71081ebf06SWludzik, Jozef         }
72081ebf06SWludzik, Jozef 
7389474494SKrzysztof Grobelny         if (maxReports != nullptr)
7489474494SKrzysztof Grobelny         {
75081ebf06SWludzik, Jozef             asyncResp->res.jsonValue["MaxReports"] = *maxReports;
7689474494SKrzysztof Grobelny         }
7789474494SKrzysztof Grobelny 
7889474494SKrzysztof Grobelny         if (minInterval != nullptr)
7989474494SKrzysztof Grobelny         {
80081ebf06SWludzik, Jozef             asyncResp->res.jsonValue["MinCollectionInterval"] =
8189474494SKrzysztof Grobelny                 time_utils::toDurationString(std::chrono::milliseconds(
8289474494SKrzysztof Grobelny                     static_cast<time_t>(*minInterval)));
8389474494SKrzysztof Grobelny         }
84479e899dSKrzysztof Grobelny         nlohmann::json::array_t supportedCollectionFunctions;
85479e899dSKrzysztof Grobelny         supportedCollectionFunctions.emplace_back("Maximum");
86479e899dSKrzysztof Grobelny         supportedCollectionFunctions.emplace_back("Minimum");
87479e899dSKrzysztof Grobelny         supportedCollectionFunctions.emplace_back("Average");
88479e899dSKrzysztof Grobelny         supportedCollectionFunctions.emplace_back("Summation");
89479e899dSKrzysztof Grobelny 
90479e899dSKrzysztof Grobelny         asyncResp->res.jsonValue["SupportedCollectionFunctions"] =
91479e899dSKrzysztof Grobelny             std::move(supportedCollectionFunctions);
9289474494SKrzysztof Grobelny     });
93081ebf06SWludzik, Jozef }
9426bd9b57SJohn Edward Broadbent 
9526bd9b57SJohn Edward Broadbent inline void requestRoutesTelemetryService(App& app)
9626bd9b57SJohn Edward Broadbent {
9726bd9b57SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/")
9826bd9b57SJohn Edward Broadbent         .privileges(redfish::privileges::getTelemetryService)
9945ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)(
10045ca1b86SEd Tanous             std::bind_front(handleTelemetryServiceGet, std::ref(app)));
10126bd9b57SJohn Edward Broadbent }
10226bd9b57SJohn Edward Broadbent 
103081ebf06SWludzik, Jozef } // namespace redfish
104