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