blob: 20dee5166a441d2ee8168eabf02f49e0456b124d [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",
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, Jozef76833cb2020-12-21 14:42:41 +010037
Patrick Williams3a1c2972023-05-10 07:51:04 -050038 std::vector<LabeledSensorInfo> labeledSensorsInfo =
39 triggerFactory->getLabeledSensorsInfo(yield, sensors);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010040
Patrick Williams3a1c2972023-05-10 07:51:04 -050041 auto reportIds = utils::transform<std::vector>(
42 reports,
43 [](const auto& item) { return utils::reportPathToId(item); });
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020044
Patrick Williams3a1c2972023-05-10 07:51:04 -050045 return addTrigger(id, name, triggerActions, labeledSensorsInfo,
46 reportIds, labeledTriggerThresholdParams)
47 .getPath();
Patrick Williamsc7935fa2023-10-20 11:19:30 -050048 });
Patrick Williams3a1c2972023-05-10 07:51:04 -050049 });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010050}
51
52void 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 Zwolak4416fce2021-03-17 03:21:06 +010059
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010060void 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 Dompke32305f12022-07-05 15:37:21 +020072void 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 Grobelny62c08e92022-09-16 10:28:53 +020087 throw errors::InvalidArgument("ThresholdParams.Id",
88 "UserId too long.");
Szymon Dompke32305f12022-07-05 15:37:21 +020089 }
90 }
91 }
92}
93
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010094void TriggerManager::verifyAddTrigger(
Szymon Dompke32305f12022-07-05 15:37:21 +020095 const std::vector<std::string>& reportIds,
96 const LabeledTriggerThresholdParams& thresholdParams) const
Cezary Zwolak4416fce2021-03-17 03:21:06 +010097{
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 Dompke32305f12022-07-05 15:37:21 +0200105 verifyReportIds(reportIds);
106 verifyThresholdParams(thresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100107}
108
109interfaces::Trigger& TriggerManager::addTrigger(
Szymon Dompkee28aa532021-10-27 12:33:12 +0200110 const std::string& triggerIdIn, const std::string& triggerNameIn,
Szymon Dompke20013012021-07-23 09:54:20 +0200111 const std::vector<std::string>& triggerActions,
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100112 const std::vector<LabeledSensorInfo>& labeledSensorsInfo,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100113 const std::vector<std::string>& reportIds,
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100114 const LabeledTriggerThresholdParams& labeledThresholdParams)
115{
Krzysztof Grobelnya950e422021-12-31 13:49:00 +0100116 const auto existingTriggerIds = utils::transform(
117 triggers, [](const auto& trigger) { return trigger->getId(); });
Szymon Dompkee28aa532021-10-27 12:33:12 +0200118
Szymon Dompke32305f12022-07-05 15:37:21 +0200119 auto [id, name] = utils::makeIdName(triggerIdIn, triggerNameIn,
120 triggerNameDefault, existingTriggerIds);
Szymon Dompkee28aa532021-10-27 12:33:12 +0200121
Szymon Dompke32305f12022-07-05 15:37:21 +0200122 verifyAddTrigger(reportIds, labeledThresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100123
124 triggers.emplace_back(triggerFactory->make(
Krzysztof Grobelnya950e422021-12-31 13:49:00 +0100125 id, name, triggerActions, reportIds, *this, *triggerStorage,
126 labeledThresholdParams, labeledSensorsInfo));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100127
128 return *triggers.back();
129}
130
131void 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 Dompkee28aa532021-10-27 12:33:12 +0200150 const std::string& id = data->at("Id").get_ref<std::string&>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100151 const std::string& name = data->at("Name").get_ref<std::string&>();
152 int thresholdParamsDiscriminator =
153 data->at("ThresholdParamsDiscriminator").get<int>();
Szymon Dompke20013012021-07-23 09:54:20 +0200154 const std::vector<std::string> triggerActions =
155 data->at("TriggerActions").get<std::vector<std::string>>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100156
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 Grobelnyb8cc78d2021-11-29 15:54:53 +0100171 auto reportIds =
172 data->at("ReportIds").get<std::vector<std::string>>();
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100173
174 auto labeledSensorsInfo =
175 data->at("Sensors").get<std::vector<LabeledSensorInfo>>();
176
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100177 addTrigger(id, name, triggerActions, labeledSensorsInfo, reportIds,
178 labeledThresholdParams);
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100179 }
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}