blob: e9ccbb8940679c46c12f5fac26cad99854f324f9 [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 Williams3a1c2972023-05-10 07:51:04 -050031 dbusIface.register_property_r("MaxReports", size_t{},
32 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> {
Patrick Williamsc7935fa2023-10-20 11:19:30 -050041 return utils::transform<std::vector>(
42 utils::convDataOperationType,
43 [](const auto& item) { return std::string(item.first); });
44 });
Patrick Williams3a1c2972023-05-10 07:51:04 -050045 dbusIface.register_method(
46 "AddReport",
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000047 [this](boost::asio::yield_context& yield, std::string reportId,
48 std::string reportName, std::string reportingType,
49 std::string reportUpdates, uint64_t appendLimit,
50 std::vector<std::string> reportActions, uint64_t interval,
51 ReadingParameters readingParameters, bool enabled) {
52 if (reportingType.empty())
Patrick Williams3a1c2972023-05-10 07:51:04 -050053 {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000054 reportingType = utils::enumToString(ReportingType::onRequest);
Patrick Williams3a1c2972023-05-10 07:51:04 -050055 }
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010056
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000057 if (reportUpdates.empty())
Patrick Williams3a1c2972023-05-10 07:51:04 -050058 {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000059 reportUpdates = utils::enumToString(ReportUpdates::overwrite);
Patrick Williams3a1c2972023-05-10 07:51:04 -050060 }
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +020061
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000062 if (appendLimit == std::numeric_limits<uint64_t>::max())
Patrick Williams3a1c2972023-05-10 07:51:04 -050063 {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000064 appendLimit = maxAppendLimit;
Patrick Williams3a1c2972023-05-10 07:51:04 -050065 }
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +020066
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000067 if (interval == std::numeric_limits<uint64_t>::max())
68 {
69 interval = 0;
70 }
71
72 return addReport(yield, reportId, reportName,
73 utils::toReportingType(reportingType),
Patrick Williamsc7935fa2023-10-20 11:19:30 -050074 utils::transform(reportActions,
75 [](const auto& reportAction) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050076 return utils::toReportAction(reportAction);
Patrick Williamsc7935fa2023-10-20 11:19:30 -050077 }),
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000078 Milliseconds(interval), appendLimit,
79 utils::toReportUpdates(reportUpdates),
80 readingParameters, enabled)
Patrick Williams3a1c2972023-05-10 07:51:04 -050081 .getPath();
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020082 });
Patrick Williamsc7935fa2023-10-20 11:19:30 -050083 });
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020084}
85
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020086void ReportManager::removeReport(const interfaces::Report* report)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020087{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020088 reports.erase(
89 std::remove_if(reports.begin(), reports.end(),
90 [report](const auto& x) { return report == x.get(); }),
91 reports.end());
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020092}
Wludzik, Jozefe2362792020-10-27 17:23:55 +010093
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000094void ReportManager::verifyAddReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010095 const std::string& reportId, const std::string& reportName,
96 const ReportingType reportingType, Milliseconds interval,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +000097 const ReportUpdates reportUpdates, const uint64_t appendLimit,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000098 const std::vector<LabeledMetricParameters>& readingParams)
Wludzik, Jozefe2362792020-10-27 17:23:55 +010099{
Szymon Dompke32305f12022-07-05 15:37:21 +0200100 namespace ts = utils::tstring;
101
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100102 if (reports.size() >= maxReports)
103 {
104 throw sdbusplus::exception::SdBusError(
105 static_cast<int>(std::errc::too_many_files_open),
106 "Reached maximal report count");
107 }
108
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100109 if (appendLimit > maxAppendLimit &&
110 appendLimit != std::numeric_limits<uint64_t>::max())
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000111 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200112 throw errors::InvalidArgument("AppendLimit", "Out of range.");
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000113 }
114
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200115 if ((reportingType == ReportingType::periodic && interval < minInterval) ||
116 (reportingType != ReportingType::periodic &&
117 interval != Milliseconds{0}))
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100118 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200119 throw errors::InvalidArgument("Interval");
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100120 }
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100121
Michal Orzel5e7cbf42024-08-01 15:44:42 +0200122 verifyMetricParams(readingParams);
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000123
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200124 for (const LabeledMetricParameters& item : readingParams)
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000125 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200126 utils::toOperationType(
127 utils::toUnderlying(item.at_label<ts::OperationType>()));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000128 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000129}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100130
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000131interfaces::Report& ReportManager::addReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100132 boost::asio::yield_context& yield, const std::string& reportId,
133 const std::string& reportName, const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100134 const std::vector<ReportAction>& reportActions, Milliseconds interval,
135 const uint64_t appendLimit, const ReportUpdates reportUpdates,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200136 ReadingParameters metricParams, const bool enabled)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000137{
Patrick Williams3a1c2972023-05-10 07:51:04 -0500138 auto labeledMetricParams = reportFactory->convertMetricParams(yield,
139 metricParams);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000140
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100141 return addReport(reportId, reportName, reportingType, reportActions,
142 interval, appendLimit, reportUpdates,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100143 std::move(labeledMetricParams), enabled, Readings{});
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000144}
145
146interfaces::Report& ReportManager::addReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100147 const std::string& reportId, const std::string& reportName,
148 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100149 const std::vector<ReportAction>& reportActions, Milliseconds interval,
150 const uint64_t appendLimit, const ReportUpdates reportUpdates,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200151 std::vector<LabeledMetricParameters> labeledMetricParams,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100152 const bool enabled, Readings readings)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000153{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100154 const auto existingReportIds = utils::transform(
155 reports, [](const auto& report) { return report->getId(); });
156
Szymon Dompke32305f12022-07-05 15:37:21 +0200157 auto [id, name] = utils::makeIdName(reportId, reportName, reportNameDefault,
158 existingReportIds);
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100159
160 verifyAddReport(id, name, reportingType, interval, reportUpdates,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000161 appendLimit, labeledMetricParams);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000162
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100163 reports.emplace_back(
164 reportFactory->make(id, name, reportingType, reportActions, interval,
165 appendLimit, reportUpdates, *this, *reportStorage,
166 labeledMetricParams, enabled, std::move(readings)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000167 return *reports.back();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100168}
169
170void ReportManager::loadFromPersistent()
171{
172 std::vector<interfaces::JsonStorage::FilePath> paths =
173 reportStorage->list();
174
175 for (const auto& path : paths)
176 {
177 std::optional<nlohmann::json> data = reportStorage->load(path);
178 try
179 {
180 size_t version = data->at("Version").get<size_t>();
181 if (version != Report::reportVersion)
182 {
183 throw std::logic_error("Invalid version");
184 }
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100185 bool enabled = data->at("Enabled").get<bool>();
186 std::string& id = data->at("Id").get_ref<std::string&>();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100187 std::string& name = data->at("Name").get_ref<std::string&>();
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100188
189 uint32_t reportingType = data->at("ReportingType").get<uint32_t>();
190 std::vector<ReportAction> reportActions = utils::transform(
191 data->at("ReportActions").get<std::vector<uint32_t>>(),
192 [](const auto reportAction) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500193 return utils::toReportAction(reportAction);
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500194 });
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100195 uint64_t interval = data->at("Interval").get<uint64_t>();
Szymon Dompke3eb56862021-09-20 15:32:04 +0200196 uint64_t appendLimit = data->at("AppendLimit").get<uint64_t>();
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100197 uint32_t reportUpdates = data->at("ReportUpdates").get<uint32_t>();
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000198 auto readingParameters =
199 data->at("ReadingParameters")
200 .get<std::vector<LabeledMetricParameters>>();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100201
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100202 Readings readings = {};
203
204 if (auto it = data->find("MetricValues"); it != data->end())
205 {
206 const auto labeledReadings = it->get<LabeledReadings>();
207 readings = utils::toReadings(labeledReadings);
208 }
209
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100210 addReport(id, name, utils::toReportingType(reportingType),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100211 reportActions, Milliseconds(interval), appendLimit,
212 utils::toReportUpdates(reportUpdates),
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100213 std::move(readingParameters), enabled,
214 std::move(readings));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100215 }
216 catch (const std::exception& e)
217 {
218 phosphor::logging::log<phosphor::logging::level::ERR>(
219 "Failed to load report from storage",
220 phosphor::logging::entry(
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100221 "FILENAME=%s",
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100222 static_cast<std::filesystem::path>(path).c_str()),
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100223 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100224 reportStorage->remove(path);
225 }
226 }
227}
Michal Orzel5e7cbf42024-08-01 15:44:42 +0200228
229void ReportManager::verifyMetricParams(
230 const std::vector<LabeledMetricParameters>& metricParams)
231{
232 size_t metricCount = 0;
233 for (const auto& metricParam : metricParams)
234 {
235 auto metricParamsVec =
236 metricParam.at_label<utils::tstring::SensorPath>();
237 metricCount += metricParamsVec.size();
238 }
239
240 if (metricParams.size() > ReportManager::maxNumberMetrics ||
241 metricCount > ReportManager::maxNumberMetrics)
242 {
243 throw errors::InvalidArgument("ReadingParameters", "Too many");
244 }
245}