blob: 5944fa93ca345344da4741eb31941ce8ba28bd21 [file] [log] [blame]
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +02001#include "report_manager.hpp"
2
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +02003#include <sdbusplus/exception.hpp>
4
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +02005#include <system_error>
6
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +02007ReportManager::ReportManager(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02008 std::unique_ptr<interfaces::ReportFactory> reportFactoryIn,
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +02009 const std::shared_ptr<sdbusplus::asio::object_server>& objServerIn) :
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020010 reportFactory(std::move(reportFactoryIn)),
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020011 objServer(objServerIn)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020012{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020013 reports.reserve(maxReports);
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020014
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020015 reportManagerIface = objServer->add_unique_interface(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020016 reportManagerPath, reportManagerIfaceName, [this](auto& dbusIface) {
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020017 dbusIface.register_property_r(
18 "MaxReports", uint32_t{}, sdbusplus::vtable::property_::const_,
19 [](const auto&) { return maxReports; });
20 dbusIface.register_property_r(
21 "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_,
22 [](const auto&) -> uint64_t { return minInterval.count(); });
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020023
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020024 dbusIface.register_method(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020025 "AddReport", [this](const std::string& reportName,
26 const std::string& reportingType,
27 const bool emitsReadingsUpdate,
28 const bool logToMetricReportsCollection,
29 const uint64_t interval,
30 const ReadingParameters& metricParams) {
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020031 if (reports.size() >= maxReports)
32 {
33 throw sdbusplus::exception::SdBusError(
34 static_cast<int>(std::errc::too_many_files_open),
35 "Reached maximal report count");
36 }
37
38 for (const auto& report : reports)
39 {
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020040 if (report->getName() == reportName)
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020041 {
42 throw sdbusplus::exception::SdBusError(
43 static_cast<int>(std::errc::file_exists),
44 "Duplicate report");
45 }
46 }
47
48 std::chrono::milliseconds reportInterval{interval};
49 if (reportInterval < minInterval)
50 {
51 throw sdbusplus::exception::SdBusError(
52 static_cast<int>(std::errc::invalid_argument),
53 "Invalid interval");
54 }
55
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020056 reports.emplace_back(reportFactory->make(
57 reportName, reportingType, emitsReadingsUpdate,
58 logToMetricReportsCollection, std::move(reportInterval),
59 metricParams, *this));
60 return reports.back()->getPath();
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020061 });
62 });
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020063}
64
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020065void ReportManager::removeReport(const interfaces::Report* report)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020066{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020067 reports.erase(
68 std::remove_if(reports.begin(), reports.end(),
69 [report](const auto& x) { return report == x.get(); }),
70 reports.end());
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020071}