blob: 984b434287565ff6c4a664fd2f7f17ee30b36c90 [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "trigger_manager.hpp"
2
Cezary Zwolak4416fce2021-03-17 03:21:06 +01003#include "trigger.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00004#include "types/trigger_types.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +01005#include "utils/conversion_trigger.hpp"
Szymon Dompke1cdd7e42022-06-08 14:43:13 +02006#include "utils/dbus_path_utils.hpp"
Szymon Dompke32305f12022-07-05 15:37:21 +02007#include "utils/make_id_name.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +01008#include "utils/transform.hpp"
Szymon Dompke32305f12022-07-05 15:37:21 +02009#include "utils/tstring.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +010010
11#include <phosphor-logging/log.hpp>
12
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010013#include <unordered_set>
14
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010015TriggerManager::TriggerManager(
16 std::unique_ptr<interfaces::TriggerFactory> triggerFactoryIn,
Cezary Zwolaka4e67612021-02-18 13:16:16 +010017 std::unique_ptr<interfaces::JsonStorage> triggerStorageIn,
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010018 const std::shared_ptr<sdbusplus::asio::object_server>& objServer) :
Cezary Zwolaka4e67612021-02-18 13:16:16 +010019 triggerFactory(std::move(triggerFactoryIn)),
20 triggerStorage(std::move(triggerStorageIn))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010021{
Cezary Zwolak4416fce2021-03-17 03:21:06 +010022 loadFromPersistent();
23
Patrick Williams3a1c2972023-05-10 07:51:04 -050024 managerIface = objServer->add_unique_interface(triggerManagerPath,
25 triggerManagerIfaceName,
26 [this](auto& iface) {
27 iface.register_method(
28 "AddTrigger",
29 [this](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) {
35 LabeledTriggerThresholdParams labeledTriggerThresholdParams =
36 std::visit(utils::ToLabeledThresholdParamConversion(),
37 thresholds);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010038
Patrick Williams3a1c2972023-05-10 07:51:04 -050039 std::vector<LabeledSensorInfo> labeledSensorsInfo =
40 triggerFactory->getLabeledSensorsInfo(yield, sensors);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010041
Patrick Williams3a1c2972023-05-10 07:51:04 -050042 auto reportIds = utils::transform<std::vector>(
43 reports,
44 [](const auto& item) { return utils::reportPathToId(item); });
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020045
Patrick Williams3a1c2972023-05-10 07:51:04 -050046 return addTrigger(id, name, triggerActions, labeledSensorsInfo,
47 reportIds, labeledTriggerThresholdParams)
48 .getPath();
49 });
50 });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010051}
52
53void TriggerManager::removeTrigger(const interfaces::Trigger* trigger)
54{
55 triggers.erase(
56 std::remove_if(triggers.begin(), triggers.end(),
57 [trigger](const auto& x) { return trigger == x.get(); }),
58 triggers.end());
59}
Cezary Zwolak4416fce2021-03-17 03:21:06 +010060
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010061void TriggerManager::verifyReportIds(
62 const std::vector<std::string>& newReportIds)
63{
64 if (std::unordered_set(newReportIds.begin(), newReportIds.end()).size() !=
65 newReportIds.size())
66 {
67 throw sdbusplus::exception::SdBusError(
68 static_cast<int>(std::errc::invalid_argument),
69 "Duplicate element in ReportIds");
70 }
71}
72
Szymon Dompke32305f12022-07-05 15:37:21 +020073void TriggerManager::verifyThresholdParams(
74 const LabeledTriggerThresholdParams& thresholdParams)
75{
76 namespace ts = utils::tstring;
77
78 if (auto discreteParams =
79 std::get_if<std::vector<discrete::LabeledThresholdParam>>(
80 &thresholdParams);
81 discreteParams != nullptr)
82 {
83 for (auto discreteParam : *discreteParams)
84 {
85 if (discreteParam.at_label<ts::UserId>().length() >
86 utils::constants::maxIdNameLength)
87 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +020088 throw errors::InvalidArgument("ThresholdParams.Id",
89 "UserId too long.");
Szymon Dompke32305f12022-07-05 15:37:21 +020090 }
91 }
92 }
93}
94
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010095void TriggerManager::verifyAddTrigger(
Szymon Dompke32305f12022-07-05 15:37:21 +020096 const std::vector<std::string>& reportIds,
97 const LabeledTriggerThresholdParams& thresholdParams) const
Cezary Zwolak4416fce2021-03-17 03:21:06 +010098{
99 if (triggers.size() >= maxTriggers)
100 {
101 throw sdbusplus::exception::SdBusError(
102 static_cast<int>(std::errc::too_many_files_open),
103 "Reached maximal trigger count");
104 }
105
Szymon Dompke32305f12022-07-05 15:37:21 +0200106 verifyReportIds(reportIds);
107 verifyThresholdParams(thresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100108}
109
110interfaces::Trigger& TriggerManager::addTrigger(
Szymon Dompkee28aa532021-10-27 12:33:12 +0200111 const std::string& triggerIdIn, const std::string& triggerNameIn,
Szymon Dompke20013012021-07-23 09:54:20 +0200112 const std::vector<std::string>& triggerActions,
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100113 const std::vector<LabeledSensorInfo>& labeledSensorsInfo,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100114 const std::vector<std::string>& reportIds,
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100115 const LabeledTriggerThresholdParams& labeledThresholdParams)
116{
Krzysztof Grobelnya950e422021-12-31 13:49:00 +0100117 const auto existingTriggerIds = utils::transform(
118 triggers, [](const auto& trigger) { return trigger->getId(); });
Szymon Dompkee28aa532021-10-27 12:33:12 +0200119
Szymon Dompke32305f12022-07-05 15:37:21 +0200120 auto [id, name] = utils::makeIdName(triggerIdIn, triggerNameIn,
121 triggerNameDefault, existingTriggerIds);
Szymon Dompkee28aa532021-10-27 12:33:12 +0200122
Szymon Dompke32305f12022-07-05 15:37:21 +0200123 verifyAddTrigger(reportIds, labeledThresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100124
125 triggers.emplace_back(triggerFactory->make(
Krzysztof Grobelnya950e422021-12-31 13:49:00 +0100126 id, name, triggerActions, reportIds, *this, *triggerStorage,
127 labeledThresholdParams, labeledSensorsInfo));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100128
129 return *triggers.back();
130}
131
132void TriggerManager::loadFromPersistent()
133{
134 std::vector<interfaces::JsonStorage::FilePath> paths =
135 triggerStorage->list();
136
137 for (const auto& path : paths)
138 {
139 std::optional<nlohmann::json> data = triggerStorage->load(path);
140 try
141 {
142 if (!data.has_value())
143 {
144 throw std::runtime_error("Empty storage");
145 }
146 size_t version = data->at("Version").get<size_t>();
147 if (version != Trigger::triggerVersion)
148 {
149 throw std::runtime_error("Invalid version");
150 }
Szymon Dompkee28aa532021-10-27 12:33:12 +0200151 const std::string& id = data->at("Id").get_ref<std::string&>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100152 const std::string& name = data->at("Name").get_ref<std::string&>();
153 int thresholdParamsDiscriminator =
154 data->at("ThresholdParamsDiscriminator").get<int>();
Szymon Dompke20013012021-07-23 09:54:20 +0200155 const std::vector<std::string> triggerActions =
156 data->at("TriggerActions").get<std::vector<std::string>>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100157
158 LabeledTriggerThresholdParams labeledThresholdParams;
159 if (0 == thresholdParamsDiscriminator)
160 {
161 labeledThresholdParams =
162 data->at("ThresholdParams")
163 .get<std::vector<numeric::LabeledThresholdParam>>();
164 }
165 else
166 {
167 labeledThresholdParams =
168 data->at("ThresholdParams")
169 .get<std::vector<discrete::LabeledThresholdParam>>();
170 }
171
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100172 auto reportIds =
173 data->at("ReportIds").get<std::vector<std::string>>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100174
175 auto labeledSensorsInfo =
176 data->at("Sensors").get<std::vector<LabeledSensorInfo>>();
177
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100178 addTrigger(id, name, triggerActions, labeledSensorsInfo, reportIds,
179 labeledThresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100180 }
181 catch (const std::exception& e)
182 {
183 phosphor::logging::log<phosphor::logging::level::ERR>(
184 "Failed to load trigger from storage",
185 phosphor::logging::entry(
186 "FILENAME=%s",
187 static_cast<std::filesystem::path>(path).c_str()),
188 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
189 triggerStorage->remove(path);
190 }
191 }
192}