xref: /openbmc/telemetry/src/report_manager.cpp (revision 7f06f613)
1 #include "report_manager.hpp"
2 
3 #include <sdbusplus/exception.hpp>
4 
5 #include <system_error>
6 
7 constexpr const char* reportManagerIfaceName =
8     "xyz.openbmc_project.Telemetry.ReportManager";
9 constexpr const char* reportManagerPath =
10     "/xyz/openbmc_project/Telemetry/Reports";
11 
12 ReportManager::ReportManager(
13     const std::shared_ptr<sdbusplus::asio::connection>& bus,
14     const std::shared_ptr<sdbusplus::asio::object_server>& objServerIn) :
15     objServer(objServerIn)
16 {
17     reports.reserve(maxReports);
18 
19     reportManagerIface = objServer->add_unique_interface(
20         reportManagerPath, reportManagerIfaceName,
21         [this, &ioc = bus->get_io_context()](auto& dbusIface) {
22             dbusIface.register_property_r(
23                 "MaxReports", uint32_t{}, sdbusplus::vtable::property_::const_,
24                 [](const auto&) { return maxReports; });
25             dbusIface.register_property_r(
26                 "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_,
27                 [](const auto&) -> uint64_t { return minInterval.count(); });
28 
29             dbusIface.register_method(
30                 "AddReport",
31                 [this, &ioc](const std::string& reportName,
32                              const std::string& reportingType,
33                              const bool emitsReadingsUpdate,
34                              const bool logToMetricReportsCollection,
35                              const uint64_t interval,
36                              const ReadingParameters& metricParams) {
37                     if (reports.size() >= maxReports)
38                     {
39                         throw sdbusplus::exception::SdBusError(
40                             static_cast<int>(std::errc::too_many_files_open),
41                             "Reached maximal report count");
42                     }
43 
44                     for (const auto& report : reports)
45                     {
46                         if (report->name == reportName)
47                         {
48                             throw sdbusplus::exception::SdBusError(
49                                 static_cast<int>(std::errc::file_exists),
50                                 "Duplicate report");
51                         }
52                     }
53 
54                     std::chrono::milliseconds reportInterval{interval};
55                     if (reportInterval < minInterval)
56                     {
57                         throw sdbusplus::exception::SdBusError(
58                             static_cast<int>(std::errc::invalid_argument),
59                             "Invalid interval");
60                     }
61 
62                     reports.emplace_back(std::make_unique<Report>(
63                         ioc, objServer, reportName, reportingType,
64                         emitsReadingsUpdate, logToMetricReportsCollection,
65                         std::move(reportInterval), metricParams, *this));
66                     return reports.back()->path;
67                 });
68         });
69 }
70 
71 void ReportManager::removeReport(const Report* report)
72 {
73     reports.erase(
74         std::remove_if(reports.begin(), reports.end(),
75                        [report](const auto& x) { return report == x.get(); }),
76         reports.end());
77 }
78