blob: 0e7f1e548b12b3ce82699ae5f556f8062e975512 [file] [log] [blame]
Matthew Barth3174e722020-09-15 15:13:40 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "event.hpp"
17
Matthew Barth776ca562021-03-31 09:50:58 -050018#include "action.hpp"
Matthew Barthe5578602021-03-30 12:53:24 -050019#include "config_base.hpp"
Matthew Barth391ade02021-01-15 14:33:21 -060020#include "group.hpp"
Matthew Barthe5578602021-03-30 12:53:24 -050021#include "manager.hpp"
Matthew Barth9403a212021-05-17 09:31:50 -050022#include "sdbusplus.hpp"
Matthew Barth54b5a242021-05-21 11:02:52 -050023#include "trigger.hpp"
Matthew Barthe5578602021-03-30 12:53:24 -050024
Matthew Barth3174e722020-09-15 15:13:40 -050025#include <nlohmann/json.hpp>
26#include <phosphor-logging/log.hpp>
27#include <sdbusplus/bus.hpp>
28
Matthew Barthe5578602021-03-30 12:53:24 -050029#include <algorithm>
Patrick Williamsfbf47032023-07-17 12:27:34 -050030#include <format>
Matthew Barth12bae962021-01-15 16:18:11 -060031#include <optional>
Matthew Barth12bae962021-01-15 16:18:11 -060032
Matthew Barth3174e722020-09-15 15:13:40 -050033namespace phosphor::fan::control::json
34{
35
36using json = nlohmann::json;
37using namespace phosphor::logging;
38
Matthew Barth3695ac32021-10-06 14:55:30 -050039std::map<configKey, std::unique_ptr<Group>> Event::allGroups;
40
Matthew Barth9403a212021-05-17 09:31:50 -050041Event::Event(const json& jsonObj, Manager* mgr,
Matthew Barth9f1632e2021-03-31 15:51:50 -050042 std::map<configKey, std::unique_ptr<Zone>>& zones) :
Patrick Williamsdfddd642024-08-16 15:21:51 -040043 ConfigBase(jsonObj), _bus(util::SDBusPlus::getBus()), _manager(mgr),
44 _zones(zones)
Matthew Barth3174e722020-09-15 15:13:40 -050045{
Matthew Barth619dc0f2021-05-20 09:27:02 -050046 // Event groups are optional
47 if (jsonObj.contains("groups"))
Matthew Barth3174e722020-09-15 15:13:40 -050048 {
Matthew Barthe386fbb2021-06-30 14:33:55 -050049 setGroups(jsonObj, _profiles, _groups);
Matthew Barth3174e722020-09-15 15:13:40 -050050 }
Matthew Barth619dc0f2021-05-20 09:27:02 -050051 // Event actions are optional
52 if (jsonObj.contains("actions"))
Matthew Barth3174e722020-09-15 15:13:40 -050053 {
Matthew Barth619dc0f2021-05-20 09:27:02 -050054 setActions(jsonObj);
Matthew Barth3174e722020-09-15 15:13:40 -050055 }
Matthew Barth619dc0f2021-05-20 09:27:02 -050056 setTriggers(jsonObj);
Matthew Barth3174e722020-09-15 15:13:40 -050057}
58
Matthew Barth54b5a242021-05-21 11:02:52 -050059void Event::enable()
60{
Matt Spinlerd1f97f42021-10-29 16:19:24 -050061 for (const auto& [type, trigger] : _triggers)
Matthew Barth54b5a242021-05-21 11:02:52 -050062 {
Matt Spinlerd1f97f42021-10-29 16:19:24 -050063 // Don't call the powerOn or powerOff triggers
64 if (type.find("power") == std::string::npos)
65 {
66 trigger(getName(), _manager, _groups, _actions);
67 }
68 }
69}
70
71void Event::powerOn()
72{
73 for (const auto& [type, trigger] : _triggers)
74 {
75 if (type == "poweron")
76 {
77 trigger(getName(), _manager, _groups, _actions);
78 }
79 }
80}
81
82void Event::powerOff()
83{
84 for (const auto& [type, trigger] : _triggers)
85 {
86 if (type == "poweroff")
87 {
88 trigger(getName(), _manager, _groups, _actions);
89 }
Matthew Barth54b5a242021-05-21 11:02:52 -050090 }
91}
92
Matthew Barth3695ac32021-10-06 14:55:30 -050093std::map<configKey, std::unique_ptr<Group>>&
94 Event::getAllGroups(bool loadGroups)
Matthew Barthc8bde4a2021-05-19 15:34:49 -050095{
Matthew Barth3695ac32021-10-06 14:55:30 -050096 if (allGroups.empty() && loadGroups)
97 {
98 allGroups = Manager::getConfig<Group>(true);
99 }
100
101 return allGroups;
Matthew Barthc8bde4a2021-05-19 15:34:49 -0500102}
103
Matthew Barth46b34482021-04-06 11:27:23 -0500104void Event::configGroup(Group& group, const json& jsonObj)
105{
106 if (!jsonObj.contains("interface") || !jsonObj.contains("property") ||
107 !jsonObj["property"].contains("name"))
108 {
109 log<level::ERR>("Missing required group attribute",
110 entry("JSON=%s", jsonObj.dump().c_str()));
111 throw std::runtime_error("Missing required group attribute");
112 }
113
114 // Get the group members' interface
115 auto intf = jsonObj["interface"].get<std::string>();
116 group.setInterface(intf);
117
118 // Get the group members' property name
119 auto prop = jsonObj["property"]["name"].get<std::string>();
120 group.setProperty(prop);
121
122 // Get the group members' data type
123 if (jsonObj["property"].contains("type"))
124 {
125 std::optional<std::string> type =
126 jsonObj["property"]["type"].get<std::string>();
127 group.setType(type);
128 }
129
130 // Get the group members' expected value
131 if (jsonObj["property"].contains("value"))
132 {
133 std::optional<PropertyVariantType> value =
134 getJsonValue(jsonObj["property"]["value"]);
135 group.setValue(value);
136 }
137}
138
Matthew Barthe386fbb2021-06-30 14:33:55 -0500139void Event::setGroups(const json& jsonObj,
140 const std::vector<std::string>& profiles,
141 std::vector<Group>& groups)
Matthew Barth3174e722020-09-15 15:13:40 -0500142{
Matthew Barthe386fbb2021-06-30 14:33:55 -0500143 if (jsonObj.contains("groups"))
Matthew Barth3174e722020-09-15 15:13:40 -0500144 {
Matthew Barth3695ac32021-10-06 14:55:30 -0500145 auto& availGroups = getAllGroups();
Matthew Barthe386fbb2021-06-30 14:33:55 -0500146 for (const auto& jsonGrp : jsonObj["groups"])
Matthew Barth3174e722020-09-15 15:13:40 -0500147 {
Matthew Barthe386fbb2021-06-30 14:33:55 -0500148 if (!jsonGrp.contains("name"))
149 {
Patrick Williamsfbf47032023-07-17 12:27:34 -0500150 auto msg = std::format("Missing required group name attribute");
Matthew Barthe386fbb2021-06-30 14:33:55 -0500151 log<level::ERR>(msg.c_str(),
152 entry("JSON=%s", jsonGrp.dump().c_str()));
153 throw std::runtime_error(msg.c_str());
154 }
Matthew Barth12bae962021-01-15 16:18:11 -0600155
Matthew Barthe386fbb2021-06-30 14:33:55 -0500156 configKey eventProfile =
157 std::make_pair(jsonGrp["name"].get<std::string>(), profiles);
Patrick Williamsdfddd642024-08-16 15:21:51 -0400158 auto grpEntry = std::find_if(
159 availGroups.begin(), availGroups.end(),
160 [&eventProfile](const auto& grp) {
161 return Manager::inConfig(grp.first, eventProfile);
162 });
Matthew Barthe386fbb2021-06-30 14:33:55 -0500163 if (grpEntry != availGroups.end())
164 {
165 auto group = Group(*grpEntry->second);
166 configGroup(group, jsonGrp);
167 groups.emplace_back(group);
168 }
Matthew Barth12bae962021-01-15 16:18:11 -0600169 }
Matthew Barthe5578602021-03-30 12:53:24 -0500170 }
Matthew Barth3174e722020-09-15 15:13:40 -0500171}
172
Matthew Barthc8bde4a2021-05-19 15:34:49 -0500173void Event::setActions(const json& jsonObj)
Matthew Barth3174e722020-09-15 15:13:40 -0500174{
Matthew Barth46b34482021-04-06 11:27:23 -0500175 for (const auto& jsonAct : jsonObj["actions"])
Matthew Barth3174e722020-09-15 15:13:40 -0500176 {
Matthew Barth46b34482021-04-06 11:27:23 -0500177 if (!jsonAct.contains("name"))
Matthew Barth3174e722020-09-15 15:13:40 -0500178 {
179 log<level::ERR>("Missing required event action name",
Matthew Barth46b34482021-04-06 11:27:23 -0500180 entry("JSON=%s", jsonAct.dump().c_str()));
Matthew Barth3174e722020-09-15 15:13:40 -0500181 throw std::runtime_error("Missing required event action name");
182 }
Matthew Barth46b34482021-04-06 11:27:23 -0500183
Matthew Barth46b34482021-04-06 11:27:23 -0500184 // Determine list of zones action should be run against
185 std::vector<std::reference_wrapper<Zone>> actionZones;
Matthew Bartha8ea0912021-05-03 14:33:52 -0500186 if (!jsonAct.contains("zones"))
Matthew Barth46b34482021-04-06 11:27:23 -0500187 {
188 // No zones configured on the action results in the action running
189 // against all zones matching the event's active profiles
190 for (const auto& zone : _zones)
191 {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400192 configKey eventProfile =
193 std::make_pair(zone.second->getName(), _profiles);
194 auto zoneEntry = std::find_if(
195 _zones.begin(), _zones.end(),
196 [&eventProfile](const auto& z) {
197 return Manager::inConfig(z.first, eventProfile);
198 });
Matthew Barth46b34482021-04-06 11:27:23 -0500199 if (zoneEntry != _zones.end())
200 {
201 actionZones.emplace_back(*zoneEntry->second);
202 }
203 }
204 }
205 else
206 {
207 // Zones configured on the action result in the action only running
208 // against those zones if they match the event's active profiles
Matthew Bartha8ea0912021-05-03 14:33:52 -0500209 for (const auto& jsonZone : jsonAct["zones"])
Matthew Barth46b34482021-04-06 11:27:23 -0500210 {
211 configKey eventProfile =
212 std::make_pair(jsonZone.get<std::string>(), _profiles);
Patrick Williamsdfddd642024-08-16 15:21:51 -0400213 auto zoneEntry = std::find_if(
214 _zones.begin(), _zones.end(),
215 [&eventProfile](const auto& z) {
216 return Manager::inConfig(z.first, eventProfile);
217 });
Matthew Barth46b34482021-04-06 11:27:23 -0500218 if (zoneEntry != _zones.end())
219 {
220 actionZones.emplace_back(*zoneEntry->second);
221 }
222 }
223 }
224 if (actionZones.empty())
225 {
226 log<level::DEBUG>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500227 std::format("No zones configured for event {}'s action {} "
Matthew Barth46b34482021-04-06 11:27:23 -0500228 "based on the active profile(s)",
229 getName(), jsonAct["name"].get<std::string>())
230 .c_str());
231 }
232
Matthew Barth5fb52682021-09-29 13:57:02 -0500233 // Action specific groups, if any given, will override the use of event
234 // groups in the action(s)
235 std::vector<Group> actionGroups;
236 setGroups(jsonAct, _profiles, actionGroups);
237 if (!actionGroups.empty())
Matthew Barth776ca562021-03-31 09:50:58 -0500238 {
Matthew Barth5fb52682021-09-29 13:57:02 -0500239 // Create the action for the event using the action's groups
240 auto actObj = ActionFactory::getAction(
241 jsonAct["name"].get<std::string>(), jsonAct,
242 std::move(actionGroups), std::move(actionZones));
243 if (actObj)
244 {
Matt Spinlere6fc2102022-04-07 14:31:29 -0500245 actObj->setEventName(_name);
Matthew Barth5fb52682021-09-29 13:57:02 -0500246 _actions.emplace_back(std::move(actObj));
247 }
248 }
249 else
250 {
251 // Create the action for the event using the event's groups
252 auto actObj = ActionFactory::getAction(
253 jsonAct["name"].get<std::string>(), jsonAct, _groups,
254 std::move(actionZones));
255 if (actObj)
256 {
Matt Spinlere6fc2102022-04-07 14:31:29 -0500257 actObj->setEventName(_name);
Matthew Barth5fb52682021-09-29 13:57:02 -0500258 _actions.emplace_back(std::move(actObj));
259 }
260 }
261
262 if (actionGroups.empty() && _groups.empty())
263 {
264 log<level::DEBUG>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500265 std::format("No groups configured for event {}'s action {} "
Matthew Barth5fb52682021-09-29 13:57:02 -0500266 "based on the active profile(s)",
267 getName(), jsonAct["name"].get<std::string>())
268 .c_str());
Matthew Barth776ca562021-03-31 09:50:58 -0500269 }
Matthew Barth3174e722020-09-15 15:13:40 -0500270 }
271}
272
Matthew Barth9f1632e2021-03-31 15:51:50 -0500273void Event::setTriggers(const json& jsonObj)
274{
275 if (!jsonObj.contains("triggers"))
276 {
277 log<level::ERR>("Missing required event triggers list",
278 entry("JSON=%s", jsonObj.dump().c_str()));
279 throw std::runtime_error("Missing required event triggers list");
280 }
Matthew Barth0620be72021-04-14 13:31:12 -0500281 for (const auto& jsonTrig : jsonObj["triggers"])
282 {
283 if (!jsonTrig.contains("class"))
284 {
285 log<level::ERR>("Missing required event trigger class",
286 entry("JSON=%s", jsonTrig.dump().c_str()));
287 throw std::runtime_error("Missing required event trigger class");
288 }
289 // The class of trigger used to run the event actions
290 auto tClass = jsonTrig["class"].get<std::string>();
291 std::transform(tClass.begin(), tClass.end(), tClass.begin(), tolower);
292 auto trigFunc = trigger::triggers.find(tClass);
293 if (trigFunc != trigger::triggers.end())
294 {
Matthew Barth54b5a242021-05-21 11:02:52 -0500295 _triggers.emplace_back(
Matt Spinlerd1f97f42021-10-29 16:19:24 -0500296 trigFunc->first,
Matthew Barthb6ebac82021-05-21 11:15:33 -0500297 trigFunc->second(jsonTrig, getName(), _actions));
Matthew Barth0620be72021-04-14 13:31:12 -0500298 }
299 else
300 {
301 // Construct list of available triggers
302 auto availTrigs = std::accumulate(
303 std::next(trigger::triggers.begin()), trigger::triggers.end(),
304 trigger::triggers.begin()->first, [](auto list, auto trig) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400305 return std::move(list) + ", " + trig.first;
306 });
Matthew Barth0620be72021-04-14 13:31:12 -0500307 log<level::ERR>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500308 std::format("Trigger '{}' is not recognized", tClass).c_str(),
Matthew Barth0620be72021-04-14 13:31:12 -0500309 entry("AVAILABLE_TRIGGERS=%s", availTrigs.c_str()));
310 throw std::runtime_error("Unsupported trigger class name given");
311 }
312 }
Matthew Barth9f1632e2021-03-31 15:51:50 -0500313}
314
Matt Spinlerc3eb7b32022-04-25 15:44:16 -0500315json Event::dump() const
316{
317 json actionData;
318 std::for_each(_actions.begin(), _actions.end(),
319 [&actionData](const auto& action) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400320 actionData[action->getUniqueName()] = action->dump();
321 });
Matt Spinlerc3eb7b32022-04-25 15:44:16 -0500322
323 std::vector<std::string> groupData;
324 std::for_each(_groups.begin(), _groups.end(),
325 [&groupData](const auto& group) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400326 groupData.push_back(group.getName());
327 });
Matt Spinlerc3eb7b32022-04-25 15:44:16 -0500328
329 json eventData;
330 eventData["groups"] = groupData;
331 eventData["actions"] = actionData;
332
333 return eventData;
334}
335
Matthew Barth3174e722020-09-15 15:13:40 -0500336} // namespace phosphor::fan::control::json