Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 1 | #include "report_manager.hpp" |
| 2 | |
Krzysztof Grobelny | 9f9ff90 | 2021-03-05 14:57:53 +0000 | [diff] [blame] | 3 | #include "interfaces/types.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 4 | #include "report.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 5 | #include "utils/conversion.hpp" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 6 | #include "utils/transform.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 7 | |
| 8 | #include <phosphor-logging/log.hpp> |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 9 | #include <sdbusplus/exception.hpp> |
| 10 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 11 | #include <stdexcept> |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 12 | #include <system_error> |
| 13 | |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 14 | ReportManager::ReportManager( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 15 | std::unique_ptr<interfaces::ReportFactory> reportFactoryIn, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 16 | std::unique_ptr<interfaces::JsonStorage> reportStorageIn, |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 17 | const std::shared_ptr<sdbusplus::asio::object_server>& objServerIn) : |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 18 | reportFactory(std::move(reportFactoryIn)), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 19 | reportStorage(std::move(reportStorageIn)), objServer(objServerIn) |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 20 | { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 21 | reports.reserve(maxReports); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 22 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 23 | loadFromPersistent(); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 24 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 25 | reportManagerIface = objServer->add_unique_interface( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 26 | reportManagerPath, reportManagerIfaceName, [this](auto& dbusIface) { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 27 | dbusIface.register_property_r( |
Wludzik, Jozef | 503c158 | 2020-12-11 14:48:01 +0100 | [diff] [blame] | 28 | "MaxReports", size_t{}, sdbusplus::vtable::property_::const_, |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 29 | [](const auto&) { return maxReports; }); |
| 30 | dbusIface.register_property_r( |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 31 | "MaxReportNameLength", size_t{}, |
| 32 | sdbusplus::vtable::property_::const_, |
| 33 | [](const auto&) { return maxReportNameLength; }); |
| 34 | dbusIface.register_property_r( |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 35 | "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_, |
| 36 | [](const auto&) -> uint64_t { return minInterval.count(); }); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 37 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 38 | dbusIface.register_method( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 39 | "AddReport", [this](boost::asio::yield_context& yield, |
| 40 | const std::string& reportName, |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 41 | const std::string& reportingType, |
| 42 | const bool emitsReadingsUpdate, |
| 43 | const bool logToMetricReportsCollection, |
| 44 | const uint64_t interval, |
Krzysztof Grobelny | 9f9ff90 | 2021-03-05 14:57:53 +0000 | [diff] [blame] | 45 | ReadingParameters metricParams) { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 46 | return addReport(yield, reportName, reportingType, |
| 47 | emitsReadingsUpdate, |
| 48 | logToMetricReportsCollection, |
| 49 | std::chrono::milliseconds(interval), |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 50 | std::move(metricParams)) |
| 51 | .getPath(); |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 52 | }); |
| 53 | }); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 56 | void ReportManager::removeReport(const interfaces::Report* report) |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 57 | { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 58 | reports.erase( |
| 59 | std::remove_if(reports.begin(), reports.end(), |
| 60 | [report](const auto& x) { return report == x.get(); }), |
| 61 | reports.end()); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 62 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 63 | |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 64 | void ReportManager::verifyReportNameLength(const std::string& reportName) |
| 65 | { |
| 66 | if (reportName.length() > maxReportNameLength) |
| 67 | { |
| 68 | throw sdbusplus::exception::SdBusError( |
| 69 | static_cast<int>(std::errc::invalid_argument), |
| 70 | "Report name exceed maximum length"); |
| 71 | } |
| 72 | } |
| 73 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 74 | void ReportManager::verifyAddReport(const std::string& reportName, |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 75 | const std::string& reportingType, |
| 76 | std::chrono::milliseconds interval, |
| 77 | const ReadingParameters& readingParams) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 78 | { |
| 79 | if (reports.size() >= maxReports) |
| 80 | { |
| 81 | throw sdbusplus::exception::SdBusError( |
| 82 | static_cast<int>(std::errc::too_many_files_open), |
| 83 | "Reached maximal report count"); |
| 84 | } |
| 85 | |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 86 | verifyReportNameLength(reportName); |
| 87 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 88 | for (const auto& report : reports) |
| 89 | { |
| 90 | if (report->getName() == reportName) |
| 91 | { |
| 92 | throw sdbusplus::exception::SdBusError( |
| 93 | static_cast<int>(std::errc::file_exists), "Duplicate report"); |
| 94 | } |
| 95 | } |
| 96 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 97 | auto found = std::find(supportedReportingType.begin(), |
| 98 | supportedReportingType.end(), reportingType); |
| 99 | if (found == supportedReportingType.end()) |
| 100 | { |
| 101 | throw sdbusplus::exception::SdBusError( |
| 102 | static_cast<int>(std::errc::invalid_argument), |
| 103 | "Invalid reportingType"); |
| 104 | } |
| 105 | |
| 106 | if (reportingType == "Periodic" && interval < minInterval) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 107 | { |
| 108 | throw sdbusplus::exception::SdBusError( |
| 109 | static_cast<int>(std::errc::invalid_argument), "Invalid interval"); |
| 110 | } |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 111 | |
Wludzik, Jozef | 503c158 | 2020-12-11 14:48:01 +0100 | [diff] [blame] | 112 | if (readingParams.size() > maxReadingParams) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 113 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 114 | { |
| 115 | throw sdbusplus::exception::SdBusError( |
| 116 | static_cast<int>(std::errc::argument_list_too_long), |
| 117 | "Too many reading parameters"); |
| 118 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 119 | |
| 120 | try |
| 121 | { |
| 122 | for (const auto& item : readingParams) |
| 123 | { |
| 124 | utils::stringToOperationType(std::get<1>(item)); |
| 125 | } |
| 126 | } |
| 127 | catch (const std::exception& e) |
| 128 | { |
| 129 | throw sdbusplus::exception::SdBusError( |
| 130 | static_cast<int>(std::errc::invalid_argument), e.what()); |
| 131 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 132 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 133 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 134 | interfaces::Report& ReportManager::addReport( |
| 135 | boost::asio::yield_context& yield, const std::string& reportName, |
| 136 | const std::string& reportingType, const bool emitsReadingsUpdate, |
| 137 | const bool logToMetricReportsCollection, std::chrono::milliseconds interval, |
| 138 | ReadingParameters metricParams) |
| 139 | { |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 140 | verifyAddReport(reportName, reportingType, interval, metricParams); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 141 | |
| 142 | reports.emplace_back(reportFactory->make( |
| 143 | yield, reportName, reportingType, emitsReadingsUpdate, |
| 144 | logToMetricReportsCollection, interval, std::move(metricParams), *this, |
| 145 | *reportStorage)); |
| 146 | return *reports.back(); |
| 147 | } |
| 148 | |
| 149 | interfaces::Report& ReportManager::addReport( |
| 150 | const std::string& reportName, const std::string& reportingType, |
| 151 | const bool emitsReadingsUpdate, const bool logToMetricReportsCollection, |
| 152 | std::chrono::milliseconds interval, |
| 153 | std::vector<LabeledMetricParameters> labeledMetricParams) |
| 154 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 155 | auto metricParams = utils::transform( |
| 156 | labeledMetricParams, [](const LabeledMetricParameters& param) { |
| 157 | using namespace utils::tstring; |
| 158 | |
| 159 | return ReadingParameters::value_type( |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 160 | sdbusplus::message::object_path( |
| 161 | param.at_index<0>().at_label<Path>()), |
| 162 | utils::enumToString(param.at_index<1>()), param.at_index<2>(), |
Krzysztof Grobelny | 9f9ff90 | 2021-03-05 14:57:53 +0000 | [diff] [blame] | 163 | param.at_index<3>()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 164 | }); |
| 165 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 166 | verifyAddReport(reportName, reportingType, interval, metricParams); |
| 167 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 168 | reports.emplace_back(reportFactory->make( |
| 169 | reportName, reportingType, emitsReadingsUpdate, |
| 170 | logToMetricReportsCollection, interval, std::move(metricParams), *this, |
| 171 | *reportStorage, labeledMetricParams)); |
| 172 | return *reports.back(); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void ReportManager::loadFromPersistent() |
| 176 | { |
| 177 | std::vector<interfaces::JsonStorage::FilePath> paths = |
| 178 | reportStorage->list(); |
| 179 | |
| 180 | for (const auto& path : paths) |
| 181 | { |
| 182 | std::optional<nlohmann::json> data = reportStorage->load(path); |
| 183 | try |
| 184 | { |
| 185 | size_t version = data->at("Version").get<size_t>(); |
| 186 | if (version != Report::reportVersion) |
| 187 | { |
| 188 | throw std::logic_error("Invalid version"); |
| 189 | } |
| 190 | std::string& name = data->at("Name").get_ref<std::string&>(); |
| 191 | std::string& reportingType = |
| 192 | data->at("ReportingType").get_ref<std::string&>(); |
| 193 | bool emitsReadingsSignal = |
| 194 | data->at("EmitsReadingsUpdate").get<bool>(); |
| 195 | bool logToMetricReportsCollection = |
| 196 | data->at("LogToMetricReportsCollection").get<bool>(); |
| 197 | uint64_t interval = data->at("Interval").get<uint64_t>(); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 198 | auto readingParameters = |
| 199 | data->at("ReadingParameters") |
| 200 | .get<std::vector<LabeledMetricParameters>>(); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 201 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 202 | addReport(name, reportingType, emitsReadingsSignal, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 203 | logToMetricReportsCollection, |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 204 | std::chrono::milliseconds(interval), |
| 205 | std::move(readingParameters)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 206 | } |
| 207 | catch (const std::exception& e) |
| 208 | { |
| 209 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 210 | "Failed to load report from storage", |
| 211 | phosphor::logging::entry( |
Wludzik, Jozef | 982c5b5 | 2021-01-02 12:05:21 +0100 | [diff] [blame] | 212 | "FILENAME=%s", |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 213 | static_cast<std::filesystem::path>(path).c_str()), |
Wludzik, Jozef | 982c5b5 | 2021-01-02 12:05:21 +0100 | [diff] [blame] | 214 | phosphor::logging::entry("EXCEPTION_MSG=%s", e.what())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 215 | reportStorage->remove(path); |
| 216 | } |
| 217 | } |
| 218 | } |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 219 | |
| 220 | void ReportManager::updateReport(const std::string& name) |
| 221 | { |
| 222 | for (auto& report : reports) |
| 223 | { |
| 224 | if (report->getName() == name) |
| 225 | { |
| 226 | report->updateReadings(); |
| 227 | return; |
| 228 | } |
| 229 | } |
| 230 | } |