blob: 5812311ac20b18beddcfb5487938bc677b946262 [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 Grobelnydcc4e192021-03-08 09:09:34 +000018ReadingParameters
19 convertToReadingParameters(ReadingParametersPastVersion params)
20{
21 return utils::transform(params, [](const auto& param) {
22 using namespace std::chrono_literals;
23
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010024 const auto& [sensorPath, operationType, id, metadata] = param;
25
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000026 return ReadingParameters::value_type(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010027 std::vector<
28 std::tuple<sdbusplus::message::object_path, std::string>>{
29 {sensorPath, metadata}},
30 operationType, id, utils::enumToString(CollectionTimeScope::point),
31 0u);
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000032 });
33}
34
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020035ReportManager::ReportManager(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020036 std::unique_ptr<interfaces::ReportFactory> reportFactoryIn,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010037 std::unique_ptr<interfaces::JsonStorage> reportStorageIn,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010038 const std::shared_ptr<sdbusplus::asio::object_server>& objServerIn) :
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020039 reportFactory(std::move(reportFactoryIn)),
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010040 reportStorage(std::move(reportStorageIn)), objServer(objServerIn)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020041{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020042 reports.reserve(maxReports);
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010043
Wludzik, Jozefe2362792020-10-27 17:23:55 +010044 loadFromPersistent();
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020045
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020046 reportManagerIface = objServer->add_unique_interface(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020047 reportManagerPath, reportManagerIfaceName, [this](auto& dbusIface) {
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020048 dbusIface.register_property_r(
Wludzik, Jozef503c1582020-12-11 14:48:01 +010049 "MaxReports", size_t{}, sdbusplus::vtable::property_::const_,
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020050 [](const auto&) { return maxReports; });
51 dbusIface.register_property_r(
52 "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_,
53 [](const auto&) -> uint64_t { return minInterval.count(); });
Krzysztof Grobelny60fee072022-01-13 16:25:04 +010054 dbusIface.register_property_r(
55 "SupportedOperationTypes", std::vector<std::string>{},
56 sdbusplus::vtable::property_::const_,
57 [](const auto&) -> std::vector<std::string> {
Szymon Dompkefdb06a12022-02-11 11:04:44 +010058 return utils::transform<std::vector>(
Krzysztof Grobelny60fee072022-01-13 16:25:04 +010059 utils::convDataOperationType, [](const auto& item) {
60 return std::string(item.first);
61 });
62 });
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020063
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020064 dbusIface.register_method(
Wludzik, Jozefe2362792020-10-27 17:23:55 +010065 "AddReport", [this](boost::asio::yield_context& yield,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010066 const std::string& reportId,
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020067 const std::string& reportingType,
68 const bool emitsReadingsUpdate,
69 const bool logToMetricReportsCollection,
70 const uint64_t interval,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000071 ReadingParametersPastVersion metricParams) {
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020072 constexpr auto enabledDefault = true;
Szymon Dompke3eb56862021-09-20 15:32:04 +020073 constexpr uint64_t appendLimitDefault = 0;
74 constexpr ReportUpdates reportUpdatesDefault =
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010075 ReportUpdates::overwrite;
76
77 std::vector<ReportAction> reportActions;
78
79 if (emitsReadingsUpdate)
80 {
81 reportActions.emplace_back(
82 ReportAction::emitsReadingsUpdate);
83 }
84 if (logToMetricReportsCollection)
85 {
86 reportActions.emplace_back(
87 ReportAction::logToMetricReportsCollection);
88 }
89
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010090 return addReport(yield, reportId, reportId,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010091 utils::toReportingType(reportingType),
92 reportActions, Milliseconds(interval),
93 appendLimitDefault, reportUpdatesDefault,
94 convertToReadingParameters(
95 std::move(metricParams)),
96 enabledDefault)
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000097 .getPath();
98 });
99
100 dbusIface.register_method(
101 "AddReportFutureVersion",
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100102 [this](
103 boost::asio::yield_context& yield,
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +0200104 const std::vector<
105 std::pair<std::string, AddReportFutureVersionVariant>>&
106 properties) {
107 std::optional<std::string> reportId;
108 std::optional<std::string> reportName;
109 std::optional<std::string> reportingType;
110 std::optional<std::vector<std::string>> reportActions;
111 std::optional<uint64_t> interval;
112 std::optional<uint64_t> appendLimit;
113 std::optional<std::string> reportUpdates;
114 std::optional<ReadingParameters> metricParams;
115 std::optional<bool> enabled;
116
117 sdbusplus::unpackProperties(
118 properties, "Id", reportId, "Name", reportName,
119 "ReportingType", reportingType, "ReportActions",
120 reportActions, "Interval", interval, "AppendLimit",
121 appendLimit, "ReportUpdates", reportUpdates,
122 "MetricParams", metricParams, "Enabled", enabled);
123
124 return addReport(
125 yield, reportId.value_or(""),
126 reportName.value_or(""),
127 utils::toReportingType(
128 reportingType.value_or(utils::enumToString(
129 ReportingType::onRequest))),
130 utils::transform(
131 reportActions.value_or(
132 std::vector<std::string>{}),
133 [](const auto& reportAction) {
134 return utils::toReportAction(
135 reportAction);
136 }),
137 Milliseconds(interval.value_or(0)),
138 appendLimit.value_or(0),
139 utils::toReportUpdates(
140 reportUpdates.value_or(utils::enumToString(
141 ReportUpdates::overwrite))),
142 metricParams.value_or(ReadingParameters{}),
143 enabled.value_or(true))
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000144 .getPath();
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200145 });
146 });
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +0200147}
148
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200149void ReportManager::removeReport(const interfaces::Report* report)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +0200150{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200151 reports.erase(
152 std::remove_if(reports.begin(), reports.end(),
153 [report](const auto& x) { return report == x.get(); }),
154 reports.end());
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +0200155}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100156
Szymon Dompke32305f12022-07-05 15:37:21 +0200157void ReportManager::verifyMetricParameters(
158 const std::vector<LabeledMetricParameters>& readingParams)
159{
160 namespace ts = utils::tstring;
161
162 for (auto readingParam : readingParams)
163 {
164 if (readingParam.at_label<ts::Id>().length() >
165 utils::constants::maxIdNameLength)
166 {
167 throw sdbusplus::exception::SdBusError(
168 static_cast<int>(std::errc::invalid_argument),
169 "MetricId too long");
170 }
171 }
172}
173
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000174void ReportManager::verifyAddReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100175 const std::string& reportId, const std::string& reportName,
176 const ReportingType reportingType, Milliseconds interval,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000177 const ReportUpdates reportUpdates, const uint64_t appendLimit,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000178 const std::vector<LabeledMetricParameters>& readingParams)
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100179{
Szymon Dompke32305f12022-07-05 15:37:21 +0200180 namespace ts = utils::tstring;
181
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100182 if (reports.size() >= maxReports)
183 {
184 throw sdbusplus::exception::SdBusError(
185 static_cast<int>(std::errc::too_many_files_open),
186 "Reached maximal report count");
187 }
188
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100189 if (appendLimit > maxAppendLimit &&
190 appendLimit != std::numeric_limits<uint64_t>::max())
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000191 {
192 throw sdbusplus::exception::SdBusError(
193 static_cast<int>(std::errc::invalid_argument),
194 "Append limit out of range");
195 }
196
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200197 if ((reportingType == ReportingType::periodic && interval < minInterval) ||
198 (reportingType != ReportingType::periodic &&
199 interval != Milliseconds{0}))
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100200 {
201 throw sdbusplus::exception::SdBusError(
202 static_cast<int>(std::errc::invalid_argument), "Invalid interval");
203 }
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100204
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000205 size_t metricCount = 0;
206 for (auto metricParam : readingParams)
207 {
208 auto metricParamsVec =
209 metricParam.at_label<utils::tstring::SensorPath>();
210 metricCount += metricParamsVec.size();
211 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000212
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000213 if (readingParams.size() > maxNumberMetrics ||
214 metricCount > maxNumberMetrics)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100215 {
216 throw sdbusplus::exception::SdBusError(
217 static_cast<int>(std::errc::argument_list_too_long),
218 "Too many reading parameters");
219 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000220
Szymon Dompke32305f12022-07-05 15:37:21 +0200221 verifyMetricParameters(readingParams);
222
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000223 try
224 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000225 for (const LabeledMetricParameters& item : readingParams)
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000226 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000227 utils::toOperationType(
228 utils::toUnderlying(item.at_label<ts::OperationType>()));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000229 }
230 }
231 catch (const std::exception& e)
232 {
233 throw sdbusplus::exception::SdBusError(
234 static_cast<int>(std::errc::invalid_argument), e.what());
235 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000236}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100237
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000238interfaces::Report& ReportManager::addReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100239 boost::asio::yield_context& yield, const std::string& reportId,
240 const std::string& reportName, const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100241 const std::vector<ReportAction>& reportActions, Milliseconds interval,
242 const uint64_t appendLimit, const ReportUpdates reportUpdates,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200243 ReadingParameters metricParams, const bool enabled)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000244{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000245 auto labeledMetricParams =
246 reportFactory->convertMetricParams(yield, metricParams);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000247
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100248 return addReport(reportId, reportName, reportingType, reportActions,
249 interval, appendLimit, reportUpdates,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100250 std::move(labeledMetricParams), enabled, Readings{});
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000251}
252
253interfaces::Report& ReportManager::addReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100254 const std::string& reportId, const std::string& reportName,
255 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100256 const std::vector<ReportAction>& reportActions, Milliseconds interval,
257 const uint64_t appendLimit, const ReportUpdates reportUpdates,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200258 std::vector<LabeledMetricParameters> labeledMetricParams,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100259 const bool enabled, Readings readings)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000260{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100261 const auto existingReportIds = utils::transform(
262 reports, [](const auto& report) { return report->getId(); });
263
Szymon Dompke32305f12022-07-05 15:37:21 +0200264 auto [id, name] = utils::makeIdName(reportId, reportName, reportNameDefault,
265 existingReportIds);
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100266
267 verifyAddReport(id, name, reportingType, interval, reportUpdates,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000268 appendLimit, labeledMetricParams);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000269
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100270 reports.emplace_back(
271 reportFactory->make(id, name, reportingType, reportActions, interval,
272 appendLimit, reportUpdates, *this, *reportStorage,
273 labeledMetricParams, enabled, std::move(readings)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000274 return *reports.back();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100275}
276
277void ReportManager::loadFromPersistent()
278{
279 std::vector<interfaces::JsonStorage::FilePath> paths =
280 reportStorage->list();
281
282 for (const auto& path : paths)
283 {
284 std::optional<nlohmann::json> data = reportStorage->load(path);
285 try
286 {
287 size_t version = data->at("Version").get<size_t>();
288 if (version != Report::reportVersion)
289 {
290 throw std::logic_error("Invalid version");
291 }
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100292 bool enabled = data->at("Enabled").get<bool>();
293 std::string& id = data->at("Id").get_ref<std::string&>();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100294 std::string& name = data->at("Name").get_ref<std::string&>();
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100295
296 uint32_t reportingType = data->at("ReportingType").get<uint32_t>();
297 std::vector<ReportAction> reportActions = utils::transform(
298 data->at("ReportActions").get<std::vector<uint32_t>>(),
299 [](const auto reportAction) {
300 return utils::toReportAction(reportAction);
301 });
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100302 uint64_t interval = data->at("Interval").get<uint64_t>();
Szymon Dompke3eb56862021-09-20 15:32:04 +0200303 uint64_t appendLimit = data->at("AppendLimit").get<uint64_t>();
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100304 uint32_t reportUpdates = data->at("ReportUpdates").get<uint32_t>();
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000305 auto readingParameters =
306 data->at("ReadingParameters")
307 .get<std::vector<LabeledMetricParameters>>();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100308
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100309 Readings readings = {};
310
311 if (auto it = data->find("MetricValues"); it != data->end())
312 {
313 const auto labeledReadings = it->get<LabeledReadings>();
314 readings = utils::toReadings(labeledReadings);
315 }
316
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100317 addReport(id, name, utils::toReportingType(reportingType),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100318 reportActions, Milliseconds(interval), appendLimit,
319 utils::toReportUpdates(reportUpdates),
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100320 std::move(readingParameters), enabled,
321 std::move(readings));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100322 }
323 catch (const std::exception& e)
324 {
325 phosphor::logging::log<phosphor::logging::level::ERR>(
326 "Failed to load report from storage",
327 phosphor::logging::entry(
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100328 "FILENAME=%s",
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100329 static_cast<std::filesystem::path>(path).c_str()),
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100330 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100331 reportStorage->remove(path);
332 }
333 }
334}