blob: 0dcb6d53ba82f054b219a75d04a0515e4ccea7dd [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 Williams3a1c2972023-05-10 07:51:04 -050030 reportManagerPath, reportManagerIfaceName,
31 [this](auto& dbusIface) {
32 dbusIface.register_property_r("MaxReports", size_t{},
33 sdbusplus::vtable::property_::const_,
34 [](const auto&) { return maxReports; });
35 dbusIface.register_property_r(
36 "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_,
37 [](const auto&) -> uint64_t { return minInterval.count(); });
38 dbusIface.register_property_r(
39 "SupportedOperationTypes", std::vector<std::string>{},
40 sdbusplus::vtable::property_::const_,
41 [](const auto&) -> std::vector<std::string> {
42 return utils::transform<std::vector>(
43 utils::convDataOperationType,
44 [](const auto& item) { return std::string(item.first); });
45 });
Patrick Williams3a1c2972023-05-10 07:51:04 -050046 dbusIface.register_method(
47 "AddReport",
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000048 [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, uint64_t interval,
52 ReadingParameters readingParameters, bool enabled) {
53 if (reportingType.empty())
Patrick Williams3a1c2972023-05-10 07:51:04 -050054 {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000055 reportingType = utils::enumToString(ReportingType::onRequest);
Patrick Williams3a1c2972023-05-10 07:51:04 -050056 }
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010057
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000058 if (reportUpdates.empty())
Patrick Williams3a1c2972023-05-10 07:51:04 -050059 {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000060 reportUpdates = utils::enumToString(ReportUpdates::overwrite);
Patrick Williams3a1c2972023-05-10 07:51:04 -050061 }
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +020062
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000063 if (appendLimit == std::numeric_limits<uint64_t>::max())
Patrick Williams3a1c2972023-05-10 07:51:04 -050064 {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000065 appendLimit = maxAppendLimit;
Patrick Williams3a1c2972023-05-10 07:51:04 -050066 }
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +020067
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000068 if (interval == std::numeric_limits<uint64_t>::max())
69 {
70 interval = 0;
71 }
72
73 return addReport(yield, reportId, reportName,
74 utils::toReportingType(reportingType),
75 utils::transform(
76 reportActions,
77 [](const auto& reportAction) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050078 return utils::toReportAction(reportAction);
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000079 }),
80 Milliseconds(interval), appendLimit,
81 utils::toReportUpdates(reportUpdates),
82 readingParameters, enabled)
Patrick Williams3a1c2972023-05-10 07:51:04 -050083 .getPath();
84 });
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020085 });
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020086}
87
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020088void ReportManager::removeReport(const interfaces::Report* report)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020089{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020090 reports.erase(
91 std::remove_if(reports.begin(), reports.end(),
92 [report](const auto& x) { return report == x.get(); }),
93 reports.end());
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020094}
Wludzik, Jozefe2362792020-10-27 17:23:55 +010095
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000096void ReportManager::verifyAddReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010097 const std::string& reportId, const std::string& reportName,
98 const ReportingType reportingType, Milliseconds interval,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +000099 const ReportUpdates reportUpdates, const uint64_t appendLimit,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000100 const std::vector<LabeledMetricParameters>& readingParams)
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100101{
Szymon Dompke32305f12022-07-05 15:37:21 +0200102 namespace ts = utils::tstring;
103
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100104 if (reports.size() >= maxReports)
105 {
106 throw sdbusplus::exception::SdBusError(
107 static_cast<int>(std::errc::too_many_files_open),
108 "Reached maximal report count");
109 }
110
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100111 if (appendLimit > maxAppendLimit &&
112 appendLimit != std::numeric_limits<uint64_t>::max())
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000113 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200114 throw errors::InvalidArgument("AppendLimit", "Out of range.");
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000115 }
116
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200117 if ((reportingType == ReportingType::periodic && interval < minInterval) ||
118 (reportingType != ReportingType::periodic &&
119 interval != Milliseconds{0}))
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100120 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200121 throw errors::InvalidArgument("Interval");
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100122 }
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100123
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000124 size_t metricCount = 0;
125 for (auto metricParam : readingParams)
126 {
127 auto metricParamsVec =
128 metricParam.at_label<utils::tstring::SensorPath>();
129 metricCount += metricParamsVec.size();
130 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000131
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000132 if (readingParams.size() > maxNumberMetrics ||
133 metricCount > maxNumberMetrics)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100134 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200135 throw errors::InvalidArgument("MetricParams", "Too many.");
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100136 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000137
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200138 for (const LabeledMetricParameters& item : readingParams)
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000139 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200140 utils::toOperationType(
141 utils::toUnderlying(item.at_label<ts::OperationType>()));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000142 }
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000143}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100144
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000145interfaces::Report& ReportManager::addReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100146 boost::asio::yield_context& yield, const std::string& reportId,
147 const std::string& reportName, const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100148 const std::vector<ReportAction>& reportActions, Milliseconds interval,
149 const uint64_t appendLimit, const ReportUpdates reportUpdates,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200150 ReadingParameters metricParams, const bool enabled)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000151{
Patrick Williams3a1c2972023-05-10 07:51:04 -0500152 auto labeledMetricParams = reportFactory->convertMetricParams(yield,
153 metricParams);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000154
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100155 return addReport(reportId, reportName, reportingType, reportActions,
156 interval, appendLimit, reportUpdates,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100157 std::move(labeledMetricParams), enabled, Readings{});
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000158}
159
160interfaces::Report& ReportManager::addReport(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100161 const std::string& reportId, const std::string& reportName,
162 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100163 const std::vector<ReportAction>& reportActions, Milliseconds interval,
164 const uint64_t appendLimit, const ReportUpdates reportUpdates,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200165 std::vector<LabeledMetricParameters> labeledMetricParams,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100166 const bool enabled, Readings readings)
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000167{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100168 const auto existingReportIds = utils::transform(
169 reports, [](const auto& report) { return report->getId(); });
170
Szymon Dompke32305f12022-07-05 15:37:21 +0200171 auto [id, name] = utils::makeIdName(reportId, reportName, reportNameDefault,
172 existingReportIds);
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100173
174 verifyAddReport(id, name, reportingType, interval, reportUpdates,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000175 appendLimit, labeledMetricParams);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000176
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100177 reports.emplace_back(
178 reportFactory->make(id, name, reportingType, reportActions, interval,
179 appendLimit, reportUpdates, *this, *reportStorage,
180 labeledMetricParams, enabled, std::move(readings)));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000181 return *reports.back();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100182}
183
184void ReportManager::loadFromPersistent()
185{
186 std::vector<interfaces::JsonStorage::FilePath> paths =
187 reportStorage->list();
188
189 for (const auto& path : paths)
190 {
191 std::optional<nlohmann::json> data = reportStorage->load(path);
192 try
193 {
194 size_t version = data->at("Version").get<size_t>();
195 if (version != Report::reportVersion)
196 {
197 throw std::logic_error("Invalid version");
198 }
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100199 bool enabled = data->at("Enabled").get<bool>();
200 std::string& id = data->at("Id").get_ref<std::string&>();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100201 std::string& name = data->at("Name").get_ref<std::string&>();
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100202
203 uint32_t reportingType = data->at("ReportingType").get<uint32_t>();
204 std::vector<ReportAction> reportActions = utils::transform(
205 data->at("ReportActions").get<std::vector<uint32_t>>(),
206 [](const auto reportAction) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500207 return utils::toReportAction(reportAction);
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100208 });
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100209 uint64_t interval = data->at("Interval").get<uint64_t>();
Szymon Dompke3eb56862021-09-20 15:32:04 +0200210 uint64_t appendLimit = data->at("AppendLimit").get<uint64_t>();
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100211 uint32_t reportUpdates = data->at("ReportUpdates").get<uint32_t>();
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000212 auto readingParameters =
213 data->at("ReadingParameters")
214 .get<std::vector<LabeledMetricParameters>>();
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100215
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100216 Readings readings = {};
217
218 if (auto it = data->find("MetricValues"); it != data->end())
219 {
220 const auto labeledReadings = it->get<LabeledReadings>();
221 readings = utils::toReadings(labeledReadings);
222 }
223
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100224 addReport(id, name, utils::toReportingType(reportingType),
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100225 reportActions, Milliseconds(interval), appendLimit,
226 utils::toReportUpdates(reportUpdates),
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100227 std::move(readingParameters), enabled,
228 std::move(readings));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100229 }
230 catch (const std::exception& e)
231 {
232 phosphor::logging::log<phosphor::logging::level::ERR>(
233 "Failed to load report from storage",
234 phosphor::logging::entry(
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100235 "FILENAME=%s",
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100236 static_cast<std::filesystem::path>(path).c_str()),
Wludzik, Jozef982c5b52021-01-02 12:05:21 +0100237 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100238 reportStorage->remove(path);
239 }
240 }
241}