Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 1 | /** |
| 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 Barth | 776ca56 | 2021-03-31 09:50:58 -0500 | [diff] [blame] | 18 | #include "action.hpp" |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 19 | #include "config_base.hpp" |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 20 | #include "group.hpp" |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 21 | #include "manager.hpp" |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 22 | #include "sdbusplus.hpp" |
Matthew Barth | 0620be7 | 2021-04-14 13:31:12 -0500 | [diff] [blame] | 23 | #include "triggers/trigger.hpp" |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 24 | |
| 25 | #include <fmt/format.h> |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 26 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 27 | #include <nlohmann/json.hpp> |
| 28 | #include <phosphor-logging/log.hpp> |
| 29 | #include <sdbusplus/bus.hpp> |
| 30 | |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 31 | #include <algorithm> |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 32 | #include <optional> |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 33 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 34 | namespace phosphor::fan::control::json |
| 35 | { |
| 36 | |
| 37 | using json = nlohmann::json; |
| 38 | using namespace phosphor::logging; |
| 39 | |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 40 | Event::Event(const json& jsonObj, Manager* mgr, |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame] | 41 | std::map<configKey, std::unique_ptr<Zone>>& zones) : |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 42 | ConfigBase(jsonObj), |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 43 | _bus(util::SDBusPlus::getBus()), _manager(mgr), _zones(zones) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 44 | { |
Matthew Barth | 619dc0f | 2021-05-20 09:27:02 -0500 | [diff] [blame] | 45 | // Event groups are optional |
| 46 | if (jsonObj.contains("groups")) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 47 | { |
Matthew Barth | 619dc0f | 2021-05-20 09:27:02 -0500 | [diff] [blame] | 48 | setGroups(jsonObj, _groups); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 49 | } |
Matthew Barth | 619dc0f | 2021-05-20 09:27:02 -0500 | [diff] [blame] | 50 | // Event actions are optional |
| 51 | if (jsonObj.contains("actions")) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 52 | { |
Matthew Barth | 619dc0f | 2021-05-20 09:27:02 -0500 | [diff] [blame] | 53 | setActions(jsonObj); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 54 | } |
Matthew Barth | 619dc0f | 2021-05-20 09:27:02 -0500 | [diff] [blame] | 55 | setTriggers(jsonObj); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 58 | auto& Event::getAvailGroups() |
| 59 | { |
| 60 | static auto groups = Manager::getConfig<Group>(true); |
| 61 | return groups; |
| 62 | } |
| 63 | |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 64 | void Event::configGroup(Group& group, const json& jsonObj) |
| 65 | { |
| 66 | if (!jsonObj.contains("interface") || !jsonObj.contains("property") || |
| 67 | !jsonObj["property"].contains("name")) |
| 68 | { |
| 69 | log<level::ERR>("Missing required group attribute", |
| 70 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 71 | throw std::runtime_error("Missing required group attribute"); |
| 72 | } |
| 73 | |
| 74 | // Get the group members' interface |
| 75 | auto intf = jsonObj["interface"].get<std::string>(); |
| 76 | group.setInterface(intf); |
| 77 | |
| 78 | // Get the group members' property name |
| 79 | auto prop = jsonObj["property"]["name"].get<std::string>(); |
| 80 | group.setProperty(prop); |
| 81 | |
| 82 | // Get the group members' data type |
| 83 | if (jsonObj["property"].contains("type")) |
| 84 | { |
| 85 | std::optional<std::string> type = |
| 86 | jsonObj["property"]["type"].get<std::string>(); |
| 87 | group.setType(type); |
| 88 | } |
| 89 | |
| 90 | // Get the group members' expected value |
| 91 | if (jsonObj["property"].contains("value")) |
| 92 | { |
| 93 | std::optional<PropertyVariantType> value = |
| 94 | getJsonValue(jsonObj["property"]["value"]); |
| 95 | group.setValue(value); |
| 96 | } |
| 97 | } |
| 98 | |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 99 | void Event::setGroups(const json& jsonObj, std::vector<Group>& groups) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 100 | { |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 101 | auto& availGroups = getAvailGroups(); |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 102 | for (const auto& jsonGrp : jsonObj["groups"]) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 103 | { |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 104 | if (!jsonGrp.contains("name")) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 105 | { |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 106 | auto msg = fmt::format( |
| 107 | "Missing required group name attribute in event {}", getName()); |
| 108 | log<level::ERR>(msg.c_str(), |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 109 | entry("JSON=%s", jsonGrp.dump().c_str())); |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 110 | throw std::runtime_error(msg.c_str()); |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 111 | } |
| 112 | |
Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 113 | configKey eventProfile = |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 114 | std::make_pair(jsonGrp["name"].get<std::string>(), _profiles); |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 115 | auto grpEntry = |
| 116 | std::find_if(availGroups.begin(), availGroups.end(), |
| 117 | [&eventProfile](const auto& grp) { |
| 118 | return Manager::inConfig(grp.first, eventProfile); |
| 119 | }); |
| 120 | if (grpEntry != availGroups.end()) |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 121 | { |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 122 | auto group = Group(*grpEntry->second); |
| 123 | configGroup(group, jsonGrp); |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 124 | groups.emplace_back(group); |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 125 | } |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 126 | } |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 129 | void Event::setActions(const json& jsonObj) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 130 | { |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 131 | for (const auto& jsonAct : jsonObj["actions"]) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 132 | { |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 133 | if (!jsonAct.contains("name")) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 134 | { |
| 135 | log<level::ERR>("Missing required event action name", |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 136 | entry("JSON=%s", jsonAct.dump().c_str())); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 137 | throw std::runtime_error("Missing required event action name"); |
| 138 | } |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 139 | |
| 140 | // Append action specific groups to the list of event groups for each |
| 141 | // action in the event |
| 142 | auto actionGroups = _groups; |
Matthew Barth | a8ea091 | 2021-05-03 14:33:52 -0500 | [diff] [blame] | 143 | if (jsonAct.contains("groups")) |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 144 | { |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 145 | setGroups(jsonAct, actionGroups); |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 146 | } |
| 147 | if (actionGroups.empty()) |
| 148 | { |
| 149 | log<level::DEBUG>( |
| 150 | fmt::format("No groups configured for event {}'s action {} " |
| 151 | "based on the active profile(s)", |
| 152 | getName(), jsonAct["name"].get<std::string>()) |
| 153 | .c_str()); |
| 154 | } |
| 155 | |
| 156 | // Determine list of zones action should be run against |
| 157 | std::vector<std::reference_wrapper<Zone>> actionZones; |
Matthew Barth | a8ea091 | 2021-05-03 14:33:52 -0500 | [diff] [blame] | 158 | if (!jsonAct.contains("zones")) |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 159 | { |
| 160 | // No zones configured on the action results in the action running |
| 161 | // against all zones matching the event's active profiles |
| 162 | for (const auto& zone : _zones) |
| 163 | { |
| 164 | configKey eventProfile = |
| 165 | std::make_pair(zone.second->getName(), _profiles); |
| 166 | auto zoneEntry = std::find_if(_zones.begin(), _zones.end(), |
| 167 | [&eventProfile](const auto& z) { |
| 168 | return Manager::inConfig( |
| 169 | z.first, eventProfile); |
| 170 | }); |
| 171 | if (zoneEntry != _zones.end()) |
| 172 | { |
| 173 | actionZones.emplace_back(*zoneEntry->second); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | // Zones configured on the action result in the action only running |
| 180 | // against those zones if they match the event's active profiles |
Matthew Barth | a8ea091 | 2021-05-03 14:33:52 -0500 | [diff] [blame] | 181 | for (const auto& jsonZone : jsonAct["zones"]) |
Matthew Barth | 46b3448 | 2021-04-06 11:27:23 -0500 | [diff] [blame] | 182 | { |
| 183 | configKey eventProfile = |
| 184 | std::make_pair(jsonZone.get<std::string>(), _profiles); |
| 185 | auto zoneEntry = std::find_if(_zones.begin(), _zones.end(), |
| 186 | [&eventProfile](const auto& z) { |
| 187 | return Manager::inConfig( |
| 188 | z.first, eventProfile); |
| 189 | }); |
| 190 | if (zoneEntry != _zones.end()) |
| 191 | { |
| 192 | actionZones.emplace_back(*zoneEntry->second); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | if (actionZones.empty()) |
| 197 | { |
| 198 | log<level::DEBUG>( |
| 199 | fmt::format("No zones configured for event {}'s action {} " |
| 200 | "based on the active profile(s)", |
| 201 | getName(), jsonAct["name"].get<std::string>()) |
| 202 | .c_str()); |
| 203 | } |
| 204 | |
| 205 | // Create the action for the event |
| 206 | auto actObj = ActionFactory::getAction( |
| 207 | jsonAct["name"].get<std::string>(), jsonAct, |
| 208 | std::move(actionGroups), std::move(actionZones)); |
Matthew Barth | 776ca56 | 2021-03-31 09:50:58 -0500 | [diff] [blame] | 209 | if (actObj) |
| 210 | { |
| 211 | _actions.emplace_back(std::move(actObj)); |
| 212 | } |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame] | 216 | void Event::setTriggers(const json& jsonObj) |
| 217 | { |
| 218 | if (!jsonObj.contains("triggers")) |
| 219 | { |
| 220 | log<level::ERR>("Missing required event triggers list", |
| 221 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 222 | throw std::runtime_error("Missing required event triggers list"); |
| 223 | } |
Matthew Barth | 0620be7 | 2021-04-14 13:31:12 -0500 | [diff] [blame] | 224 | for (const auto& jsonTrig : jsonObj["triggers"]) |
| 225 | { |
| 226 | if (!jsonTrig.contains("class")) |
| 227 | { |
| 228 | log<level::ERR>("Missing required event trigger class", |
| 229 | entry("JSON=%s", jsonTrig.dump().c_str())); |
| 230 | throw std::runtime_error("Missing required event trigger class"); |
| 231 | } |
| 232 | // The class of trigger used to run the event actions |
| 233 | auto tClass = jsonTrig["class"].get<std::string>(); |
| 234 | std::transform(tClass.begin(), tClass.end(), tClass.begin(), tolower); |
| 235 | auto trigFunc = trigger::triggers.find(tClass); |
| 236 | if (trigFunc != trigger::triggers.end()) |
| 237 | { |
| 238 | trigFunc->second(jsonTrig, getName(), _manager, _actions); |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | // Construct list of available triggers |
| 243 | auto availTrigs = std::accumulate( |
| 244 | std::next(trigger::triggers.begin()), trigger::triggers.end(), |
| 245 | trigger::triggers.begin()->first, [](auto list, auto trig) { |
| 246 | return std::move(list) + ", " + trig.first; |
| 247 | }); |
| 248 | log<level::ERR>( |
| 249 | fmt::format("Trigger '{}' is not recognized", tClass).c_str(), |
| 250 | entry("AVAILABLE_TRIGGERS=%s", availTrigs.c_str())); |
| 251 | throw std::runtime_error("Unsupported trigger class name given"); |
| 252 | } |
| 253 | } |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame] | 254 | } |
| 255 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 256 | } // namespace phosphor::fan::control::json |