trigger_manager.cpp (a950e42ba579a3498face1654e58c94d9ff1cf9a) trigger_manager.cpp (b4ef22e4d365bde35a7fce7950033c9271c68ce7)
1#include "trigger_manager.hpp"
2
3#include "trigger.hpp"
4#include "types/trigger_types.hpp"
5#include "utils/conversion_trigger.hpp"
6#include "utils/generate_id.hpp"
7#include "utils/transform.hpp"
8
9#include <phosphor-logging/log.hpp>
10
1#include "trigger_manager.hpp"
2
3#include "trigger.hpp"
4#include "types/trigger_types.hpp"
5#include "utils/conversion_trigger.hpp"
6#include "utils/generate_id.hpp"
7#include "utils/transform.hpp"
8
9#include <phosphor-logging/log.hpp>
10
11#include <unordered_set>
12
11TriggerManager::TriggerManager(
12 std::unique_ptr<interfaces::TriggerFactory> triggerFactoryIn,
13 std::unique_ptr<interfaces::JsonStorage> triggerStorageIn,
14 const std::shared_ptr<sdbusplus::asio::object_server>& objServer) :
15 triggerFactory(std::move(triggerFactoryIn)),
16 triggerStorage(std::move(triggerStorageIn))
17{
18 loadFromPersistent();

--- 27 unchanged lines hidden (view full) ---

46void TriggerManager::removeTrigger(const interfaces::Trigger* trigger)
47{
48 triggers.erase(
49 std::remove_if(triggers.begin(), triggers.end(),
50 [trigger](const auto& x) { return trigger == x.get(); }),
51 triggers.end());
52}
53
13TriggerManager::TriggerManager(
14 std::unique_ptr<interfaces::TriggerFactory> triggerFactoryIn,
15 std::unique_ptr<interfaces::JsonStorage> triggerStorageIn,
16 const std::shared_ptr<sdbusplus::asio::object_server>& objServer) :
17 triggerFactory(std::move(triggerFactoryIn)),
18 triggerStorage(std::move(triggerStorageIn))
19{
20 loadFromPersistent();

--- 27 unchanged lines hidden (view full) ---

48void TriggerManager::removeTrigger(const interfaces::Trigger* trigger)
49{
50 triggers.erase(
51 std::remove_if(triggers.begin(), triggers.end(),
52 [trigger](const auto& x) { return trigger == x.get(); }),
53 triggers.end());
54}
55
54void TriggerManager::verifyAddTrigger(const std::string& triggerId,
55 const std::string& triggerName) const
56void TriggerManager::verifyReportIds(
57 const std::vector<std::string>& newReportIds)
56{
58{
59 if (std::unordered_set(newReportIds.begin(), newReportIds.end()).size() !=
60 newReportIds.size())
61 {
62 throw sdbusplus::exception::SdBusError(
63 static_cast<int>(std::errc::invalid_argument),
64 "Duplicate element in ReportIds");
65 }
66}
67
68void TriggerManager::verifyAddTrigger(
69 const std::string& triggerId, const std::string& triggerName,
70 const std::vector<std::string>& newReportIds) const
71{
57 if (triggers.size() >= maxTriggers)
58 {
59 throw sdbusplus::exception::SdBusError(
60 static_cast<int>(std::errc::too_many_files_open),
61 "Reached maximal trigger count");
62 }
63
64 utils::verifyIdCharacters(triggerId);
65
66 for (const auto& trigger : triggers)
67 {
68 if (trigger->getId() == triggerId)
69 {
70 throw sdbusplus::exception::SdBusError(
71 static_cast<int>(std::errc::file_exists), "Duplicate trigger");
72 }
73 }
72 if (triggers.size() >= maxTriggers)
73 {
74 throw sdbusplus::exception::SdBusError(
75 static_cast<int>(std::errc::too_many_files_open),
76 "Reached maximal trigger count");
77 }
78
79 utils::verifyIdCharacters(triggerId);
80
81 for (const auto& trigger : triggers)
82 {
83 if (trigger->getId() == triggerId)
84 {
85 throw sdbusplus::exception::SdBusError(
86 static_cast<int>(std::errc::file_exists), "Duplicate trigger");
87 }
88 }
89
90 verifyReportIds(newReportIds);
74}
75
76interfaces::Trigger& TriggerManager::addTrigger(
77 const std::string& triggerIdIn, const std::string& triggerNameIn,
78 const std::vector<std::string>& triggerActions,
79 const std::vector<LabeledSensorInfo>& labeledSensorsInfo,
80 const std::vector<std::string>& reportIds,
81 const LabeledTriggerThresholdParams& labeledThresholdParams)
82{
83 const auto existingTriggerIds = utils::transform(
84 triggers, [](const auto& trigger) { return trigger->getId(); });
85
86 auto [id, name] =
87 utils::generateId(triggerIdIn, triggerNameIn, triggerNameDefault,
88 existingTriggerIds, maxTriggerIdLength);
89
91}
92
93interfaces::Trigger& TriggerManager::addTrigger(
94 const std::string& triggerIdIn, const std::string& triggerNameIn,
95 const std::vector<std::string>& triggerActions,
96 const std::vector<LabeledSensorInfo>& labeledSensorsInfo,
97 const std::vector<std::string>& reportIds,
98 const LabeledTriggerThresholdParams& labeledThresholdParams)
99{
100 const auto existingTriggerIds = utils::transform(
101 triggers, [](const auto& trigger) { return trigger->getId(); });
102
103 auto [id, name] =
104 utils::generateId(triggerIdIn, triggerNameIn, triggerNameDefault,
105 existingTriggerIds, maxTriggerIdLength);
106
90 verifyAddTrigger(id, name);
107 verifyAddTrigger(id, name, reportIds);
91
92 triggers.emplace_back(triggerFactory->make(
93 id, name, triggerActions, reportIds, *this, *triggerStorage,
94 labeledThresholdParams, labeledSensorsInfo));
95
96 return *triggers.back();
97}
98

--- 53 unchanged lines hidden (view full) ---

152 phosphor::logging::entry(
153 "FILENAME=%s",
154 static_cast<std::filesystem::path>(path).c_str()),
155 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
156 triggerStorage->remove(path);
157 }
158 }
159}
108
109 triggers.emplace_back(triggerFactory->make(
110 id, name, triggerActions, reportIds, *this, *triggerStorage,
111 labeledThresholdParams, labeledSensorsInfo));
112
113 return *triggers.back();
114}
115

--- 53 unchanged lines hidden (view full) ---

169 phosphor::logging::entry(
170 "FILENAME=%s",
171 static_cast<std::filesystem::path>(path).c_str()),
172 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
173 triggerStorage->remove(path);
174 }
175 }
176}
177
178std::vector<std::string>
179 TriggerManager::getTriggerIdsForReport(const std::string& reportId) const
180{
181 std::vector<std::string> result;
182 for (const auto& trigger : triggers)
183 {
184 const auto& reportIds = trigger->getReportIds();
185 if (std::find(reportIds.begin(), reportIds.end(), reportId) !=
186 reportIds.end())
187 {
188 result.emplace_back(trigger->getId());
189 }
190 }
191 return result;
192}