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" |
| 22 | |
| 23 | #include <fmt/format.h> |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 24 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 25 | #include <nlohmann/json.hpp> |
| 26 | #include <phosphor-logging/log.hpp> |
| 27 | #include <sdbusplus/bus.hpp> |
| 28 | |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 29 | #include <algorithm> |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 30 | #include <optional> |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 31 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 32 | namespace phosphor::fan::control::json |
| 33 | { |
| 34 | |
| 35 | using json = nlohmann::json; |
| 36 | using namespace phosphor::logging; |
| 37 | |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 38 | Event::Event(const json& jsonObj, sdbusplus::bus::bus& bus, |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame^] | 39 | std::map<configKey, std::unique_ptr<Group>>& groups, |
| 40 | std::map<configKey, std::unique_ptr<Zone>>& zones) : |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 41 | ConfigBase(jsonObj), |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame^] | 42 | _bus(bus), _zones(zones) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 43 | { |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 44 | // Event could have a precondition |
| 45 | if (!jsonObj.contains("precondition")) |
| 46 | { |
| 47 | // Event groups are optional |
| 48 | if (jsonObj.contains("groups")) |
| 49 | { |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 50 | setGroups(jsonObj, groups); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 51 | } |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 52 | // Event actions are optional |
| 53 | if (jsonObj.contains("actions")) |
| 54 | { |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame^] | 55 | setActions(jsonObj, groups); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 56 | } |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame^] | 57 | setTriggers(jsonObj); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 58 | } |
| 59 | else |
| 60 | { |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 61 | setPrecond(jsonObj, groups); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 65 | void Event::setPrecond(const json& jsonObj, |
| 66 | std::map<configKey, std::unique_ptr<Group>>& groups) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 67 | { |
| 68 | const auto& precond = jsonObj["precondition"]; |
| 69 | if (!precond.contains("name") || !precond.contains("groups") || |
| 70 | !precond.contains("triggers") || !precond.contains("events")) |
| 71 | { |
| 72 | log<level::ERR>("Missing required event precondition attributes", |
| 73 | entry("JSON=%s", precond.dump().c_str())); |
| 74 | throw std::runtime_error( |
| 75 | "Missing required event precondition attributes"); |
| 76 | } |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 77 | setGroups(precond, groups); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 78 | setTriggers(precond); |
| 79 | } |
| 80 | |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 81 | void Event::setGroups(const json& jsonObj, |
| 82 | std::map<configKey, std::unique_ptr<Group>>& groups) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 83 | { |
| 84 | for (const auto& group : jsonObj["groups"]) |
| 85 | { |
| 86 | if (!group.contains("name") || !group.contains("interface") || |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 87 | !group.contains("property") || !group["property"].contains("name")) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 88 | { |
| 89 | log<level::ERR>("Missing required event group attributes", |
| 90 | entry("JSON=%s", group.dump().c_str())); |
| 91 | throw std::runtime_error("Missing required event group attributes"); |
| 92 | } |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 93 | |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 94 | // Get the group members' interface |
| 95 | auto intf = group["interface"].get<std::string>(); |
| 96 | |
| 97 | // Get the group members' property name |
| 98 | auto prop = group["property"]["name"].get<std::string>(); |
| 99 | |
| 100 | // Get the group members' data type |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 101 | std::optional<std::string> type = std::nullopt; |
| 102 | if (group["property"].contains("type")) |
| 103 | { |
| 104 | type = group["property"]["type"].get<std::string>(); |
| 105 | } |
| 106 | |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 107 | // Get the group members' expected value |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 108 | std::optional<PropertyVariantType> value = std::nullopt; |
| 109 | if (group["property"].contains("value")) |
| 110 | { |
| 111 | value = getJsonValue(group["property"]["value"]); |
| 112 | } |
| 113 | |
Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 114 | configKey eventProfile = |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 115 | std::make_pair(group["name"].get<std::string>(), _profiles); |
Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 116 | auto grpEntry = std::find_if( |
| 117 | groups.begin(), groups.end(), [&eventProfile](const auto& grp) { |
| 118 | return Manager::inConfig(grp.first, eventProfile); |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 119 | }); |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 120 | if (grpEntry != groups.end()) |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 121 | { |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 122 | auto grp = Group(*grpEntry->second); |
| 123 | grp.setInterface(intf); |
| 124 | grp.setProperty(prop); |
| 125 | grp.setType(type); |
| 126 | grp.setValue(value); |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 127 | _groups.emplace_back(grp); |
| 128 | } |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (_groups.empty()) |
| 132 | { |
| 133 | auto msg = fmt::format( |
| 134 | "No groups configured for event {} in its active profile(s)", |
| 135 | getName()); |
| 136 | log<level::ERR>(msg.c_str()); |
| 137 | throw std::runtime_error(msg); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame^] | 141 | void Event::setActions(const json& jsonObj, |
| 142 | std::map<configKey, std::unique_ptr<Group>>& groups) |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 143 | { |
| 144 | for (const auto& action : jsonObj["actions"]) |
| 145 | { |
| 146 | if (!action.contains("name")) |
| 147 | { |
| 148 | log<level::ERR>("Missing required event action name", |
| 149 | entry("JSON=%s", action.dump().c_str())); |
| 150 | throw std::runtime_error("Missing required event action name"); |
| 151 | } |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame^] | 152 | // TODO Append action specific groups to event groups list separate from |
| 153 | // each action in the event and pass reference of group to action |
| 154 | // TODO Consider supporting zones per action and pass a reference to the |
| 155 | // zone(s) the action should be run against |
Matthew Barth | 776ca56 | 2021-03-31 09:50:58 -0500 | [diff] [blame] | 156 | auto actObj = |
| 157 | ActionFactory::getAction(action["name"].get<std::string>(), action); |
| 158 | if (actObj) |
| 159 | { |
| 160 | _actions.emplace_back(std::move(actObj)); |
| 161 | } |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame^] | 165 | void Event::setTriggers(const json& jsonObj) |
| 166 | { |
| 167 | if (!jsonObj.contains("triggers")) |
| 168 | { |
| 169 | log<level::ERR>("Missing required event triggers list", |
| 170 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 171 | throw std::runtime_error("Missing required event triggers list"); |
| 172 | } |
| 173 | } |
| 174 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 175 | } // namespace phosphor::fan::control::json |