blob: 5cc824edec381fd125bb9955feb37f71e7b8476c [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 Williams3a1c2972023-05-10 07:51:04 -050026 iface.register_method(
27 "AddTrigger",
Ed Tanous2efa95d2024-10-19 11:36:53 -070028 [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>& numericThresholds,
35 const std::vector<discrete::ThresholdParam>&
36 discreteThresholds) {
37 LabeledTriggerThresholdParams labeledTriggerThresholdParams;
38 if (!numericThresholds.empty())
39 {
40 labeledTriggerThresholdParams =
41 utils::ToLabeledThresholdParamConversion()(
42 numericThresholds);
43 }
44 if (!discreteThresholds.empty())
45 {
46 labeledTriggerThresholdParams =
47 utils::ToLabeledThresholdParamConversion()(
48 discreteThresholds);
49 }
Patrick Williams3a1c2972023-05-10 07:51:04 -050050 std::vector<LabeledSensorInfo> labeledSensorsInfo =
51 triggerFactory->getLabeledSensorsInfo(yield, sensors);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010052
Patrick Williams3a1c2972023-05-10 07:51:04 -050053 auto reportIds = utils::transform<std::vector>(
54 reports,
55 [](const auto& item) { return utils::reportPathToId(item); });
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020056
Patrick Williams3a1c2972023-05-10 07:51:04 -050057 return addTrigger(id, name, triggerActions, labeledSensorsInfo,
58 reportIds, labeledTriggerThresholdParams)
59 .getPath();
Patrick Williamsc7935fa2023-10-20 11:19:30 -050060 });
Patrick Williams3a1c2972023-05-10 07:51:04 -050061 });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010062}
63
64void TriggerManager::removeTrigger(const interfaces::Trigger* trigger)
65{
66 triggers.erase(
67 std::remove_if(triggers.begin(), triggers.end(),
68 [trigger](const auto& x) { return trigger == x.get(); }),
69 triggers.end());
70}
Cezary Zwolak4416fce2021-03-17 03:21:06 +010071
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010072void TriggerManager::verifyReportIds(
73 const std::vector<std::string>& newReportIds)
74{
75 if (std::unordered_set(newReportIds.begin(), newReportIds.end()).size() !=
76 newReportIds.size())
77 {
78 throw sdbusplus::exception::SdBusError(
79 static_cast<int>(std::errc::invalid_argument),
80 "Duplicate element in ReportIds");
81 }
82}
83
Szymon Dompke32305f12022-07-05 15:37:21 +020084void TriggerManager::verifyThresholdParams(
85 const LabeledTriggerThresholdParams& thresholdParams)
86{
87 namespace ts = utils::tstring;
88
89 if (auto discreteParams =
90 std::get_if<std::vector<discrete::LabeledThresholdParam>>(
91 &thresholdParams);
92 discreteParams != nullptr)
93 {
94 for (auto discreteParam : *discreteParams)
95 {
96 if (discreteParam.at_label<ts::UserId>().length() >
97 utils::constants::maxIdNameLength)
98 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +020099 throw errors::InvalidArgument("ThresholdParams.Id",
100 "UserId too long.");
Szymon Dompke32305f12022-07-05 15:37:21 +0200101 }
102 }
103 }
104}
105
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100106void TriggerManager::verifyAddTrigger(
Szymon Dompke32305f12022-07-05 15:37:21 +0200107 const std::vector<std::string>& reportIds,
108 const LabeledTriggerThresholdParams& thresholdParams) const
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100109{
110 if (triggers.size() >= maxTriggers)
111 {
112 throw sdbusplus::exception::SdBusError(
113 static_cast<int>(std::errc::too_many_files_open),
114 "Reached maximal trigger count");
115 }
116
Szymon Dompke32305f12022-07-05 15:37:21 +0200117 verifyReportIds(reportIds);
118 verifyThresholdParams(thresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100119}
120
121interfaces::Trigger& TriggerManager::addTrigger(
Szymon Dompkee28aa532021-10-27 12:33:12 +0200122 const std::string& triggerIdIn, const std::string& triggerNameIn,
Szymon Dompke20013012021-07-23 09:54:20 +0200123 const std::vector<std::string>& triggerActions,
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100124 const std::vector<LabeledSensorInfo>& labeledSensorsInfo,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100125 const std::vector<std::string>& reportIds,
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100126 const LabeledTriggerThresholdParams& labeledThresholdParams)
127{
Krzysztof Grobelnya950e422021-12-31 13:49:00 +0100128 const auto existingTriggerIds = utils::transform(
129 triggers, [](const auto& trigger) { return trigger->getId(); });
Szymon Dompkee28aa532021-10-27 12:33:12 +0200130
Szymon Dompke32305f12022-07-05 15:37:21 +0200131 auto [id, name] = utils::makeIdName(triggerIdIn, triggerNameIn,
132 triggerNameDefault, existingTriggerIds);
Szymon Dompkee28aa532021-10-27 12:33:12 +0200133
Szymon Dompke32305f12022-07-05 15:37:21 +0200134 verifyAddTrigger(reportIds, labeledThresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100135
136 triggers.emplace_back(triggerFactory->make(
Krzysztof Grobelnya950e422021-12-31 13:49:00 +0100137 id, name, triggerActions, reportIds, *this, *triggerStorage,
138 labeledThresholdParams, labeledSensorsInfo));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100139
140 return *triggers.back();
141}
142
143void TriggerManager::loadFromPersistent()
144{
145 std::vector<interfaces::JsonStorage::FilePath> paths =
146 triggerStorage->list();
147
148 for (const auto& path : paths)
149 {
150 std::optional<nlohmann::json> data = triggerStorage->load(path);
151 try
152 {
153 if (!data.has_value())
154 {
155 throw std::runtime_error("Empty storage");
156 }
157 size_t version = data->at("Version").get<size_t>();
158 if (version != Trigger::triggerVersion)
159 {
160 throw std::runtime_error("Invalid version");
161 }
Szymon Dompkee28aa532021-10-27 12:33:12 +0200162 const std::string& id = data->at("Id").get_ref<std::string&>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100163 const std::string& name = data->at("Name").get_ref<std::string&>();
164 int thresholdParamsDiscriminator =
165 data->at("ThresholdParamsDiscriminator").get<int>();
Szymon Dompke20013012021-07-23 09:54:20 +0200166 const std::vector<std::string> triggerActions =
167 data->at("TriggerActions").get<std::vector<std::string>>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100168
169 LabeledTriggerThresholdParams labeledThresholdParams;
170 if (0 == thresholdParamsDiscriminator)
171 {
172 labeledThresholdParams =
173 data->at("ThresholdParams")
174 .get<std::vector<numeric::LabeledThresholdParam>>();
175 }
176 else
177 {
178 labeledThresholdParams =
179 data->at("ThresholdParams")
180 .get<std::vector<discrete::LabeledThresholdParam>>();
181 }
182
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100183 auto reportIds =
184 data->at("ReportIds").get<std::vector<std::string>>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100185
186 auto labeledSensorsInfo =
187 data->at("Sensors").get<std::vector<LabeledSensorInfo>>();
188
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100189 addTrigger(id, name, triggerActions, labeledSensorsInfo, reportIds,
190 labeledThresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100191 }
192 catch (const std::exception& e)
193 {
194 phosphor::logging::log<phosphor::logging::level::ERR>(
195 "Failed to load trigger from storage",
196 phosphor::logging::entry(
197 "FILENAME=%s",
198 static_cast<std::filesystem::path>(path).c_str()),
199 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
200 triggerStorage->remove(path);
201 }
202 }
203}