Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 1 | #include "trigger_manager.hpp" |
| 2 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 3 | #include "trigger.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 4 | #include "types/trigger_types.hpp" |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 5 | #include "utils/conversion_trigger.hpp" |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 6 | #include "utils/generate_id.hpp" |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 7 | #include "utils/transform.hpp" |
| 8 | |
| 9 | #include <phosphor-logging/log.hpp> |
| 10 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 11 | #include <unordered_set> |
| 12 | |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 13 | TriggerManager::TriggerManager( |
| 14 | std::unique_ptr<interfaces::TriggerFactory> triggerFactoryIn, |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 15 | std::unique_ptr<interfaces::JsonStorage> triggerStorageIn, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 16 | const std::shared_ptr<sdbusplus::asio::object_server>& objServer) : |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 17 | triggerFactory(std::move(triggerFactoryIn)), |
| 18 | triggerStorage(std::move(triggerStorageIn)) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 19 | { |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 20 | loadFromPersistent(); |
| 21 | |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 22 | managerIface = objServer->add_unique_interface( |
| 23 | triggerManagerPath, triggerManagerIfaceName, [this](auto& iface) { |
| 24 | iface.register_method( |
| 25 | "AddTrigger", |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 26 | [this](boost::asio::yield_context& yield, const std::string& id, |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 27 | const std::string& name, |
| 28 | const std::vector<std::string>& triggerActions, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 29 | const SensorsInfo& sensors, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 30 | const std::vector<std::string>& reportIds, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 31 | const TriggerThresholdParamsExt& thresholds) { |
| 32 | LabeledTriggerThresholdParams |
| 33 | labeledTriggerThresholdParams = std::visit( |
| 34 | utils::ToLabeledThresholdParamConversion(), |
| 35 | thresholds); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 36 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 37 | std::vector<LabeledSensorInfo> labeledSensorsInfo = |
| 38 | triggerFactory->getLabeledSensorsInfo(yield, sensors); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 39 | |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 40 | return addTrigger(id, name, triggerActions, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 41 | labeledSensorsInfo, reportIds, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 42 | labeledTriggerThresholdParams) |
| 43 | .getPath(); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 44 | }); |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | void TriggerManager::removeTrigger(const interfaces::Trigger* trigger) |
| 49 | { |
| 50 | triggers.erase( |
| 51 | std::remove_if(triggers.begin(), triggers.end(), |
| 52 | [trigger](const auto& x) { return trigger == x.get(); }), |
| 53 | triggers.end()); |
| 54 | } |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 55 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 56 | void TriggerManager::verifyReportIds( |
| 57 | const std::vector<std::string>& newReportIds) |
| 58 | { |
| 59 | if (std::unordered_set(newReportIds.begin(), newReportIds.end()).size() != |
| 60 | newReportIds.size()) |
| 61 | { |
| 62 | throw sdbusplus::exception::SdBusError( |
| 63 | static_cast<int>(std::errc::invalid_argument), |
| 64 | "Duplicate element in ReportIds"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void TriggerManager::verifyAddTrigger( |
| 69 | const std::string& triggerId, const std::string& triggerName, |
| 70 | const std::vector<std::string>& newReportIds) const |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 71 | { |
| 72 | if (triggers.size() >= maxTriggers) |
| 73 | { |
| 74 | throw sdbusplus::exception::SdBusError( |
| 75 | static_cast<int>(std::errc::too_many_files_open), |
| 76 | "Reached maximal trigger count"); |
| 77 | } |
| 78 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 79 | utils::verifyIdCharacters(triggerId); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 80 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 81 | for (const auto& trigger : triggers) |
| 82 | { |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 83 | if (trigger->getId() == triggerId) |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 84 | { |
| 85 | throw sdbusplus::exception::SdBusError( |
| 86 | static_cast<int>(std::errc::file_exists), "Duplicate trigger"); |
| 87 | } |
| 88 | } |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 89 | |
| 90 | verifyReportIds(newReportIds); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | interfaces::Trigger& TriggerManager::addTrigger( |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 94 | const std::string& triggerIdIn, const std::string& triggerNameIn, |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 95 | const std::vector<std::string>& triggerActions, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 96 | const std::vector<LabeledSensorInfo>& labeledSensorsInfo, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 97 | const std::vector<std::string>& reportIds, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 98 | const LabeledTriggerThresholdParams& labeledThresholdParams) |
| 99 | { |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 100 | const auto existingTriggerIds = utils::transform( |
| 101 | triggers, [](const auto& trigger) { return trigger->getId(); }); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 102 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 103 | auto [id, name] = |
| 104 | utils::generateId(triggerIdIn, triggerNameIn, triggerNameDefault, |
| 105 | existingTriggerIds, maxTriggerIdLength); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 106 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 107 | verifyAddTrigger(id, name, reportIds); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 108 | |
| 109 | triggers.emplace_back(triggerFactory->make( |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 110 | id, name, triggerActions, reportIds, *this, *triggerStorage, |
| 111 | labeledThresholdParams, labeledSensorsInfo)); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 112 | |
| 113 | return *triggers.back(); |
| 114 | } |
| 115 | |
| 116 | void TriggerManager::loadFromPersistent() |
| 117 | { |
| 118 | std::vector<interfaces::JsonStorage::FilePath> paths = |
| 119 | triggerStorage->list(); |
| 120 | |
| 121 | for (const auto& path : paths) |
| 122 | { |
| 123 | std::optional<nlohmann::json> data = triggerStorage->load(path); |
| 124 | try |
| 125 | { |
| 126 | if (!data.has_value()) |
| 127 | { |
| 128 | throw std::runtime_error("Empty storage"); |
| 129 | } |
| 130 | size_t version = data->at("Version").get<size_t>(); |
| 131 | if (version != Trigger::triggerVersion) |
| 132 | { |
| 133 | throw std::runtime_error("Invalid version"); |
| 134 | } |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 135 | const std::string& id = data->at("Id").get_ref<std::string&>(); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 136 | const std::string& name = data->at("Name").get_ref<std::string&>(); |
| 137 | int thresholdParamsDiscriminator = |
| 138 | data->at("ThresholdParamsDiscriminator").get<int>(); |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 139 | const std::vector<std::string> triggerActions = |
| 140 | data->at("TriggerActions").get<std::vector<std::string>>(); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 141 | |
| 142 | LabeledTriggerThresholdParams labeledThresholdParams; |
| 143 | if (0 == thresholdParamsDiscriminator) |
| 144 | { |
| 145 | labeledThresholdParams = |
| 146 | data->at("ThresholdParams") |
| 147 | .get<std::vector<numeric::LabeledThresholdParam>>(); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | labeledThresholdParams = |
| 152 | data->at("ThresholdParams") |
| 153 | .get<std::vector<discrete::LabeledThresholdParam>>(); |
| 154 | } |
| 155 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 156 | auto reportIds = |
| 157 | data->at("ReportIds").get<std::vector<std::string>>(); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 158 | |
| 159 | auto labeledSensorsInfo = |
| 160 | data->at("Sensors").get<std::vector<LabeledSensorInfo>>(); |
| 161 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 162 | addTrigger(id, name, triggerActions, labeledSensorsInfo, reportIds, |
| 163 | labeledThresholdParams); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 164 | } |
| 165 | catch (const std::exception& e) |
| 166 | { |
| 167 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 168 | "Failed to load trigger from storage", |
| 169 | phosphor::logging::entry( |
| 170 | "FILENAME=%s", |
| 171 | static_cast<std::filesystem::path>(path).c_str()), |
| 172 | phosphor::logging::entry("EXCEPTION_MSG=%s", e.what())); |
| 173 | triggerStorage->remove(path); |
| 174 | } |
| 175 | } |
| 176 | } |