blob: f65b120bf1b6fd26c0fa7f21bf1b5adf28e98ce3 [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 Williamsc7935fa2023-10-20 11:19:30 -050024 managerIface = objServer->add_unique_interface(
25 triggerManagerPath, triggerManagerIfaceName, [this](auto& iface) {
Patrick Williams583ba442025-02-03 14:28:19 -050026 iface.register_method(
27 "AddTrigger",
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 std::vector<numeric::ThresholdParam>&
35 numericThresholds,
36 const std::vector<discrete::ThresholdParam>&
37 discreteThresholds) {
38 LabeledTriggerThresholdParams labeledTriggerThresholdParams;
39 if (!numericThresholds.empty())
40 {
41 labeledTriggerThresholdParams =
42 utils::ToLabeledThresholdParamConversion()(
43 numericThresholds);
44 }
45 if (!discreteThresholds.empty())
46 {
47 labeledTriggerThresholdParams =
48 utils::ToLabeledThresholdParamConversion()(
49 discreteThresholds);
50 }
51 std::vector<LabeledSensorInfo> labeledSensorsInfo =
52 triggerFactory->getLabeledSensorsInfo(yield, sensors);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010053
Patrick Williams583ba442025-02-03 14:28:19 -050054 auto reportIds = utils::transform<std::vector>(
55 reports, [](const auto& item) {
56 return utils::reportPathToId(item);
57 });
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020058
Patrick Williams583ba442025-02-03 14:28:19 -050059 return addTrigger(id, name, triggerActions,
60 labeledSensorsInfo, reportIds,
61 labeledTriggerThresholdParams)
62 .getPath();
63 });
Patrick Williamsc7935fa2023-10-20 11:19:30 -050064 });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010065}
66
67void TriggerManager::removeTrigger(const interfaces::Trigger* trigger)
68{
69 triggers.erase(
70 std::remove_if(triggers.begin(), triggers.end(),
71 [trigger](const auto& x) { return trigger == x.get(); }),
72 triggers.end());
73}
Cezary Zwolak4416fce2021-03-17 03:21:06 +010074
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010075void TriggerManager::verifyReportIds(
76 const std::vector<std::string>& newReportIds)
77{
78 if (std::unordered_set(newReportIds.begin(), newReportIds.end()).size() !=
79 newReportIds.size())
80 {
81 throw sdbusplus::exception::SdBusError(
82 static_cast<int>(std::errc::invalid_argument),
83 "Duplicate element in ReportIds");
84 }
85}
86
Szymon Dompke32305f12022-07-05 15:37:21 +020087void TriggerManager::verifyThresholdParams(
88 const LabeledTriggerThresholdParams& thresholdParams)
89{
90 namespace ts = utils::tstring;
91
92 if (auto discreteParams =
93 std::get_if<std::vector<discrete::LabeledThresholdParam>>(
94 &thresholdParams);
95 discreteParams != nullptr)
96 {
97 for (auto discreteParam : *discreteParams)
98 {
99 if (discreteParam.at_label<ts::UserId>().length() >
100 utils::constants::maxIdNameLength)
101 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200102 throw errors::InvalidArgument("ThresholdParams.Id",
103 "UserId too long.");
Szymon Dompke32305f12022-07-05 15:37:21 +0200104 }
105 }
106 }
107}
108
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100109void TriggerManager::verifyAddTrigger(
Szymon Dompke32305f12022-07-05 15:37:21 +0200110 const std::vector<std::string>& reportIds,
111 const LabeledTriggerThresholdParams& thresholdParams) const
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100112{
113 if (triggers.size() >= maxTriggers)
114 {
115 throw sdbusplus::exception::SdBusError(
116 static_cast<int>(std::errc::too_many_files_open),
117 "Reached maximal trigger count");
118 }
119
Szymon Dompke32305f12022-07-05 15:37:21 +0200120 verifyReportIds(reportIds);
121 verifyThresholdParams(thresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100122}
123
124interfaces::Trigger& TriggerManager::addTrigger(
Szymon Dompkee28aa532021-10-27 12:33:12 +0200125 const std::string& triggerIdIn, const std::string& triggerNameIn,
Szymon Dompke20013012021-07-23 09:54:20 +0200126 const std::vector<std::string>& triggerActions,
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100127 const std::vector<LabeledSensorInfo>& labeledSensorsInfo,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100128 const std::vector<std::string>& reportIds,
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100129 const LabeledTriggerThresholdParams& labeledThresholdParams)
130{
Krzysztof Grobelnya950e422021-12-31 13:49:00 +0100131 const auto existingTriggerIds = utils::transform(
132 triggers, [](const auto& trigger) { return trigger->getId(); });
Szymon Dompkee28aa532021-10-27 12:33:12 +0200133
Szymon Dompke32305f12022-07-05 15:37:21 +0200134 auto [id, name] = utils::makeIdName(triggerIdIn, triggerNameIn,
135 triggerNameDefault, existingTriggerIds);
Szymon Dompkee28aa532021-10-27 12:33:12 +0200136
Szymon Dompke32305f12022-07-05 15:37:21 +0200137 verifyAddTrigger(reportIds, labeledThresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100138
139 triggers.emplace_back(triggerFactory->make(
Krzysztof Grobelnya950e422021-12-31 13:49:00 +0100140 id, name, triggerActions, reportIds, *this, *triggerStorage,
141 labeledThresholdParams, labeledSensorsInfo));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100142
143 return *triggers.back();
144}
145
146void TriggerManager::loadFromPersistent()
147{
148 std::vector<interfaces::JsonStorage::FilePath> paths =
149 triggerStorage->list();
150
151 for (const auto& path : paths)
152 {
153 std::optional<nlohmann::json> data = triggerStorage->load(path);
154 try
155 {
156 if (!data.has_value())
157 {
158 throw std::runtime_error("Empty storage");
159 }
160 size_t version = data->at("Version").get<size_t>();
161 if (version != Trigger::triggerVersion)
162 {
163 throw std::runtime_error("Invalid version");
164 }
Szymon Dompkee28aa532021-10-27 12:33:12 +0200165 const std::string& id = data->at("Id").get_ref<std::string&>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100166 const std::string& name = data->at("Name").get_ref<std::string&>();
167 int thresholdParamsDiscriminator =
168 data->at("ThresholdParamsDiscriminator").get<int>();
Szymon Dompke20013012021-07-23 09:54:20 +0200169 const std::vector<std::string> triggerActions =
170 data->at("TriggerActions").get<std::vector<std::string>>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100171
172 LabeledTriggerThresholdParams labeledThresholdParams;
173 if (0 == thresholdParamsDiscriminator)
174 {
175 labeledThresholdParams =
176 data->at("ThresholdParams")
177 .get<std::vector<numeric::LabeledThresholdParam>>();
178 }
179 else
180 {
181 labeledThresholdParams =
182 data->at("ThresholdParams")
183 .get<std::vector<discrete::LabeledThresholdParam>>();
184 }
185
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100186 auto reportIds =
187 data->at("ReportIds").get<std::vector<std::string>>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100188
189 auto labeledSensorsInfo =
190 data->at("Sensors").get<std::vector<LabeledSensorInfo>>();
191
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100192 addTrigger(id, name, triggerActions, labeledSensorsInfo, reportIds,
193 labeledThresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100194 }
195 catch (const std::exception& e)
196 {
197 phosphor::logging::log<phosphor::logging::level::ERR>(
198 "Failed to load trigger from storage",
199 phosphor::logging::entry(
200 "FILENAME=%s",
201 static_cast<std::filesystem::path>(path).c_str()),
202 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
203 triggerStorage->remove(path);
204 }
205 }
206}