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