blob: 932ffe0d65657d42499aeca8c5662e94d9fdc281 [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;
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200115 std::optional<ReadingParameters> readingParameters;
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +0200116 std::optional<bool> enabled;
117
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200118 try
119 {
120 sdbusplus::unpackProperties(
121 properties, "Id", reportId, "Name", reportName,
122 "ReportingType", reportingType, "ReportActions",
123 reportActions, "Interval", interval, "AppendLimit",
124 appendLimit, "ReportUpdates", reportUpdates,
125 "MetricParams", metricParams, "Enabled", enabled,
126 "ReadingParameters", readingParameters);
127 }
128 catch (const sdbusplus::exception::UnpackPropertyError& e)
129 {
130 throw errors::InvalidArgument(e.propertyName);
131 }
132
133 if (readingParameters == std::nullopt)
134 {
135 readingParameters = metricParams;
136 }
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +0200137
138 return addReport(
139 yield, reportId.value_or(""),
140 reportName.value_or(""),
141 utils::toReportingType(
142 reportingType.value_or(utils::enumToString(
143 ReportingType::onRequest))),
144 utils::transform(
145 reportActions.value_or(
146 std::vector<std::string>{}),
147 [](const auto& reportAction) {
148 return utils::toReportAction(
149 reportAction);
150 }),
151 Milliseconds(interval.value_or(0)),
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200152 appendLimit.value_or(maxAppendLimit),
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +0200153 utils::toReportUpdates(
154 reportUpdates.value_or(utils::enumToString(
155 ReportUpdates::overwrite))),
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200156 readingParameters.value_or(ReadingParameters{}),
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +0200157 enabled.value_or(true))
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000158 .getPath();
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200159 });
160 });
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +0200161}
162
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200163void ReportManager::removeReport(const interfaces::Report* report)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +0200164{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200165 reports.erase(
166 std::remove_if(reports.begin(), reports.end(),
167 [report](const auto& x) { return report == x.get(); }),
168 reports.end());
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +0200169}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100170
Szymon Dompke32305f12022-07-05 15:37:21 +0200171void ReportManager::verifyMetricParameters(
172 const std::vector<LabeledMetricParameters>& readingParams)
173{
174 namespace ts = utils::tstring;
175
176 for (auto readingParam : readingParams)
177 {
178 if (readingParam.at_label<ts::Id>().length() >
179 utils::constants::maxIdNameLength)
180 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200181 throw errors::InvalidArgument("ReadingParameters.Id", "Too long.");
Szymon Dompke32305f12022-07-05 15:37:21 +0200182 }
183 }
184}
185
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000186void ReportManager::verifyAddReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100187 const std::string& reportId, const std::string& reportName,
188 const ReportingType reportingType, Milliseconds interval,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000189 const ReportUpdates reportUpdates, const uint64_t appendLimit,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000190 const std::vector<LabeledMetricParameters>& readingParams)
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100191{
Szymon Dompke32305f12022-07-05 15:37:21 +0200192 namespace ts = utils::tstring;
193
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100194 if (reports.size() >= maxReports)
195 {
196 throw sdbusplus::exception::SdBusError(
197 static_cast<int>(std::errc::too_many_files_open),
198 "Reached maximal report count");
199 }
200
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100201 if (appendLimit > maxAppendLimit &&
202 appendLimit != std::numeric_limits<uint64_t>::max())
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000203 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200204 throw errors::InvalidArgument("AppendLimit", "Out of range.");
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000205 }
206
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200207 if ((reportingType == ReportingType::periodic && interval < minInterval) ||
208 (reportingType != ReportingType::periodic &&
209 interval != Milliseconds{0}))
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100210 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200211 throw errors::InvalidArgument("Interval");
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100212 }
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100213
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000214 size_t metricCount = 0;
215 for (auto metricParam : readingParams)
216 {
217 auto metricParamsVec =
218 metricParam.at_label<utils::tstring::SensorPath>();
219 metricCount += metricParamsVec.size();
220 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000221
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000222 if (readingParams.size() > maxNumberMetrics ||
223 metricCount > maxNumberMetrics)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100224 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200225 throw errors::InvalidArgument("MetricParams", "Too many.");
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100226 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000227
Szymon Dompke32305f12022-07-05 15:37:21 +0200228 verifyMetricParameters(readingParams);
229
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200230 for (const LabeledMetricParameters& item : readingParams)
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000231 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200232 utils::toOperationType(
233 utils::toUnderlying(item.at_label<ts::OperationType>()));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000234 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000235}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100236
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000237interfaces::Report& ReportManager::addReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100238 boost::asio::yield_context& yield, const std::string& reportId,
239 const std::string& reportName, const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100240 const std::vector<ReportAction>& reportActions, Milliseconds interval,
241 const uint64_t appendLimit, const ReportUpdates reportUpdates,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200242 ReadingParameters metricParams, const bool enabled)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000243{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000244 auto labeledMetricParams =
245 reportFactory->convertMetricParams(yield, metricParams);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000246
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100247 return addReport(reportId, reportName, reportingType, reportActions,
248 interval, appendLimit, reportUpdates,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100249 std::move(labeledMetricParams), enabled, Readings{});
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000250}
251
252interfaces::Report& ReportManager::addReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100253 const std::string& reportId, const std::string& reportName,
254 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100255 const std::vector<ReportAction>& reportActions, Milliseconds interval,
256 const uint64_t appendLimit, const ReportUpdates reportUpdates,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200257 std::vector<LabeledMetricParameters> labeledMetricParams,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100258 const bool enabled, Readings readings)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000259{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100260 const auto existingReportIds = utils::transform(
261 reports, [](const auto& report) { return report->getId(); });
262
Szymon Dompke32305f12022-07-05 15:37:21 +0200263 auto [id, name] = utils::makeIdName(reportId, reportName, reportNameDefault,
264 existingReportIds);
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100265
266 verifyAddReport(id, name, reportingType, interval, reportUpdates,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000267 appendLimit, labeledMetricParams);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000268
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100269 reports.emplace_back(
270 reportFactory->make(id, name, reportingType, reportActions, interval,
271 appendLimit, reportUpdates, *this, *reportStorage,
272 labeledMetricParams, enabled, std::move(readings)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000273 return *reports.back();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100274}
275
276void ReportManager::loadFromPersistent()
277{
278 std::vector<interfaces::JsonStorage::FilePath> paths =
279 reportStorage->list();
280
281 for (const auto& path : paths)
282 {
283 std::optional<nlohmann::json> data = reportStorage->load(path);
284 try
285 {
286 size_t version = data->at("Version").get<size_t>();
287 if (version != Report::reportVersion)
288 {
289 throw std::logic_error("Invalid version");
290 }
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100291 bool enabled = data->at("Enabled").get<bool>();
292 std::string& id = data->at("Id").get_ref<std::string&>();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100293 std::string& name = data->at("Name").get_ref<std::string&>();
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100294
295 uint32_t reportingType = data->at("ReportingType").get<uint32_t>();
296 std::vector<ReportAction> reportActions = utils::transform(
297 data->at("ReportActions").get<std::vector<uint32_t>>(),
298 [](const auto reportAction) {
299 return utils::toReportAction(reportAction);
300 });
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100301 uint64_t interval = data->at("Interval").get<uint64_t>();
Szymon Dompke3eb56862021-09-20 15:32:04 +0200302 uint64_t appendLimit = data->at("AppendLimit").get<uint64_t>();
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100303 uint32_t reportUpdates = data->at("ReportUpdates").get<uint32_t>();
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000304 auto readingParameters =
305 data->at("ReadingParameters")
306 .get<std::vector<LabeledMetricParameters>>();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100307
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100308 Readings readings = {};
309
310 if (auto it = data->find("MetricValues"); it != data->end())
311 {
312 const auto labeledReadings = it->get<LabeledReadings>();
313 readings = utils::toReadings(labeledReadings);
314 }
315
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100316 addReport(id, name, utils::toReportingType(reportingType),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100317 reportActions, Milliseconds(interval), appendLimit,
318 utils::toReportUpdates(reportUpdates),
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100319 std::move(readingParameters), enabled,
320 std::move(readings));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100321 }
322 catch (const std::exception& e)
323 {
324 phosphor::logging::log<phosphor::logging::level::ERR>(
325 "Failed to load report from storage",
326 phosphor::logging::entry(
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100327 "FILENAME=%s",
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100328 static_cast<std::filesystem::path>(path).c_str()),
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100329 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100330 reportStorage->remove(path);
331 }
332 }
333}