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