blob: 2959d25465d8b773ddb7a7243da97869c6b81905 [file] [log] [blame]
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +02001#include "report_manager.hpp"
2
Wludzik, Jozefe2362792020-10-27 17:23:55 +01003#include "report.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00004#include "types/report_types.hpp"
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +00005#include "utils/conversion.hpp"
Szymon Dompke32305f12022-07-05 15:37:21 +02006#include "utils/dbus_path_utils.hpp"
7#include "utils/make_id_name.hpp"
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +00008#include "utils/transform.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01009
10#include <phosphor-logging/log.hpp>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020011#include <sdbusplus/exception.hpp>
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +020012#include <sdbusplus/unpack_properties.hpp>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020013
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +020014#include <optional>
Wludzik, Jozefe2362792020-10-27 17:23:55 +010015#include <stdexcept>
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020016#include <system_error>
17
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020018ReportManager::ReportManager(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020019 std::unique_ptr<interfaces::ReportFactory> reportFactoryIn,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010020 std::unique_ptr<interfaces::JsonStorage> reportStorageIn,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010021 const std::shared_ptr<sdbusplus::asio::object_server>& objServerIn) :
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020022 reportFactory(std::move(reportFactoryIn)),
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010023 reportStorage(std::move(reportStorageIn)), objServer(objServerIn)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020024{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020025 reports.reserve(maxReports);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010026
Wludzik, Jozefe2362792020-10-27 17:23:55 +010027 loadFromPersistent();
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020028
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020029 reportManagerIface = objServer->add_unique_interface(
Patrick Williamsc7935fa2023-10-20 11:19:30 -050030 reportManagerPath, reportManagerIfaceName, [this](auto& dbusIface) {
Patrick Williams583ba442025-02-03 14:28:19 -050031 dbusIface.register_property_r(
32 "MaxReports", size_t{}, sdbusplus::vtable::property_::const_,
33 [](const auto&) { return maxReports; });
34 dbusIface.register_property_r(
35 "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_,
36 [](const auto&) -> uint64_t { return minInterval.count(); });
37 dbusIface.register_property_r(
38 "SupportedOperationTypes", std::vector<std::string>{},
39 sdbusplus::vtable::property_::const_,
40 [](const auto&) -> std::vector<std::string> {
41 return utils::transform<std::vector>(
42 utils::convDataOperationType, [](const auto& item) {
43 return std::string(item.first);
44 });
45 });
46 dbusIface.register_method(
47 "AddReport",
48 [this](boost::asio::yield_context& yield, std::string reportId,
49 std::string reportName, std::string reportingType,
50 std::string reportUpdates, uint64_t appendLimit,
51 std::vector<std::string> reportActions,
52 uint64_t interval, ReadingParameters readingParameters,
53 bool enabled) {
54 if (reportingType.empty())
55 {
56 reportingType =
57 utils::enumToString(ReportingType::onRequest);
58 }
59
60 if (reportUpdates.empty())
61 {
62 reportUpdates =
63 utils::enumToString(ReportUpdates::overwrite);
64 }
65
66 if (appendLimit == std::numeric_limits<uint64_t>::max())
67 {
68 appendLimit = maxAppendLimit;
69 }
70
71 if (interval == std::numeric_limits<uint64_t>::max())
72 {
73 interval = 0;
74 }
75
76 return addReport(yield, reportId, reportName,
77 utils::toReportingType(reportingType),
78 utils::transform(
79 reportActions,
80 [](const auto& reportAction) {
81 return utils::toReportAction(
82 reportAction);
83 }),
84 Milliseconds(interval), appendLimit,
85 utils::toReportUpdates(reportUpdates),
86 readingParameters, enabled)
87 .getPath();
88 });
Patrick Williamsc7935fa2023-10-20 11:19:30 -050089 });
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020090}
91
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020092void ReportManager::removeReport(const interfaces::Report* report)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020093{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020094 reports.erase(
95 std::remove_if(reports.begin(), reports.end(),
96 [report](const auto& x) { return report == x.get(); }),
97 reports.end());
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020098}
Wludzik, Jozefe2362792020-10-27 17:23:55 +010099
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000100void ReportManager::verifyAddReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100101 const std::string& reportId, const std::string& reportName,
102 const ReportingType reportingType, Milliseconds interval,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000103 const ReportUpdates reportUpdates, const uint64_t appendLimit,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000104 const std::vector<LabeledMetricParameters>& readingParams)
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100105{
Szymon Dompke32305f12022-07-05 15:37:21 +0200106 namespace ts = utils::tstring;
107
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100108 if (reports.size() >= maxReports)
109 {
110 throw sdbusplus::exception::SdBusError(
111 static_cast<int>(std::errc::too_many_files_open),
112 "Reached maximal report count");
113 }
114
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100115 if (appendLimit > maxAppendLimit &&
116 appendLimit != std::numeric_limits<uint64_t>::max())
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000117 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200118 throw errors::InvalidArgument("AppendLimit", "Out of range.");
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000119 }
120
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200121 if ((reportingType == ReportingType::periodic && interval < minInterval) ||
122 (reportingType != ReportingType::periodic &&
123 interval != Milliseconds{0}))
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100124 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200125 throw errors::InvalidArgument("Interval");
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100126 }
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100127
Michal Orzel5e7cbf42024-08-01 15:44:42 +0200128 verifyMetricParams(readingParams);
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000129
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200130 for (const LabeledMetricParameters& item : readingParams)
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000131 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200132 utils::toOperationType(
133 utils::toUnderlying(item.at_label<ts::OperationType>()));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000134 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000135}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100136
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000137interfaces::Report& ReportManager::addReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100138 boost::asio::yield_context& yield, const std::string& reportId,
139 const std::string& reportName, const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100140 const std::vector<ReportAction>& reportActions, Milliseconds interval,
141 const uint64_t appendLimit, const ReportUpdates reportUpdates,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200142 ReadingParameters metricParams, const bool enabled)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000143{
Patrick Williams583ba442025-02-03 14:28:19 -0500144 auto labeledMetricParams =
145 reportFactory->convertMetricParams(yield, metricParams);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000146
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100147 return addReport(reportId, reportName, reportingType, reportActions,
148 interval, appendLimit, reportUpdates,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100149 std::move(labeledMetricParams), enabled, Readings{});
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000150}
151
152interfaces::Report& ReportManager::addReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100153 const std::string& reportId, const std::string& reportName,
154 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100155 const std::vector<ReportAction>& reportActions, Milliseconds interval,
156 const uint64_t appendLimit, const ReportUpdates reportUpdates,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200157 std::vector<LabeledMetricParameters> labeledMetricParams,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100158 const bool enabled, Readings readings)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000159{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100160 const auto existingReportIds = utils::transform(
161 reports, [](const auto& report) { return report->getId(); });
162
Szymon Dompke32305f12022-07-05 15:37:21 +0200163 auto [id, name] = utils::makeIdName(reportId, reportName, reportNameDefault,
164 existingReportIds);
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100165
166 verifyAddReport(id, name, reportingType, interval, reportUpdates,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000167 appendLimit, labeledMetricParams);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000168
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100169 reports.emplace_back(
170 reportFactory->make(id, name, reportingType, reportActions, interval,
171 appendLimit, reportUpdates, *this, *reportStorage,
172 labeledMetricParams, enabled, std::move(readings)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000173 return *reports.back();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100174}
175
176void ReportManager::loadFromPersistent()
177{
178 std::vector<interfaces::JsonStorage::FilePath> paths =
179 reportStorage->list();
180
181 for (const auto& path : paths)
182 {
183 std::optional<nlohmann::json> data = reportStorage->load(path);
184 try
185 {
186 size_t version = data->at("Version").get<size_t>();
187 if (version != Report::reportVersion)
188 {
189 throw std::logic_error("Invalid version");
190 }
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100191 bool enabled = data->at("Enabled").get<bool>();
192 std::string& id = data->at("Id").get_ref<std::string&>();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100193 std::string& name = data->at("Name").get_ref<std::string&>();
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100194
195 uint32_t reportingType = data->at("ReportingType").get<uint32_t>();
196 std::vector<ReportAction> reportActions = utils::transform(
197 data->at("ReportActions").get<std::vector<uint32_t>>(),
198 [](const auto reportAction) {
Patrick Williams583ba442025-02-03 14:28:19 -0500199 return utils::toReportAction(reportAction);
200 });
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100201 uint64_t interval = data->at("Interval").get<uint64_t>();
Szymon Dompke3eb56862021-09-20 15:32:04 +0200202 uint64_t appendLimit = data->at("AppendLimit").get<uint64_t>();
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100203 uint32_t reportUpdates = data->at("ReportUpdates").get<uint32_t>();
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000204 auto readingParameters =
205 data->at("ReadingParameters")
206 .get<std::vector<LabeledMetricParameters>>();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100207
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100208 Readings readings = {};
209
210 if (auto it = data->find("MetricValues"); it != data->end())
211 {
212 const auto labeledReadings = it->get<LabeledReadings>();
213 readings = utils::toReadings(labeledReadings);
214 }
215
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100216 addReport(id, name, utils::toReportingType(reportingType),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100217 reportActions, Milliseconds(interval), appendLimit,
218 utils::toReportUpdates(reportUpdates),
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100219 std::move(readingParameters), enabled,
220 std::move(readings));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100221 }
222 catch (const std::exception& e)
223 {
224 phosphor::logging::log<phosphor::logging::level::ERR>(
225 "Failed to load report from storage",
226 phosphor::logging::entry(
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100227 "FILENAME=%s",
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100228 static_cast<std::filesystem::path>(path).c_str()),
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100229 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100230 reportStorage->remove(path);
231 }
232 }
233}
Michal Orzel5e7cbf42024-08-01 15:44:42 +0200234
235void ReportManager::verifyMetricParams(
236 const std::vector<LabeledMetricParameters>& metricParams)
237{
238 size_t metricCount = 0;
239 for (const auto& metricParam : metricParams)
240 {
241 auto metricParamsVec =
242 metricParam.at_label<utils::tstring::SensorPath>();
243 metricCount += metricParamsVec.size();
244 }
245
246 if (metricParams.size() > ReportManager::maxNumberMetrics ||
247 metricCount > ReportManager::maxNumberMetrics)
248 {
249 throw errors::InvalidArgument("ReadingParameters", "Too many");
250 }
251}