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