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 | |
| 18 | #include <nlohmann/json.hpp> |
| 19 | #include <phosphor-logging/log.hpp> |
| 20 | #include <sdbusplus/bus.hpp> |
| 21 | |
| 22 | namespace phosphor::fan::control::json |
| 23 | { |
| 24 | |
| 25 | using json = nlohmann::json; |
| 26 | using namespace phosphor::logging; |
| 27 | |
| 28 | Event::Event(sdbusplus::bus::bus& bus, const json& jsonObj) : |
| 29 | ConfigBase(jsonObj) |
| 30 | { |
| 31 | if (jsonObj.contains("profiles")) |
| 32 | { |
| 33 | for (const auto& profile : jsonObj["profiles"]) |
| 34 | { |
| 35 | _profiles.emplace_back(profile.get<std::string>()); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Event could have a precondition |
| 40 | if (!jsonObj.contains("precondition")) |
| 41 | { |
| 42 | // Event groups are optional |
| 43 | if (jsonObj.contains("groups")) |
| 44 | { |
| 45 | setGroups(jsonObj); |
| 46 | } |
| 47 | setTriggers(jsonObj); |
| 48 | // Event actions are optional |
| 49 | if (jsonObj.contains("actions")) |
| 50 | { |
| 51 | setActions(jsonObj); |
| 52 | } |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | setPrecond(jsonObj); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void Event::setPrecond(const json& jsonObj) |
| 61 | { |
| 62 | const auto& precond = jsonObj["precondition"]; |
| 63 | if (!precond.contains("name") || !precond.contains("groups") || |
| 64 | !precond.contains("triggers") || !precond.contains("events")) |
| 65 | { |
| 66 | log<level::ERR>("Missing required event precondition attributes", |
| 67 | entry("JSON=%s", precond.dump().c_str())); |
| 68 | throw std::runtime_error( |
| 69 | "Missing required event precondition attributes"); |
| 70 | } |
| 71 | setGroups(precond); |
| 72 | setTriggers(precond); |
| 73 | } |
| 74 | |
| 75 | void Event::setGroups(const json& jsonObj) |
| 76 | { |
| 77 | for (const auto& group : jsonObj["groups"]) |
| 78 | { |
| 79 | if (!group.contains("name") || !group.contains("interface") || |
| 80 | !group.contains("property")) |
| 81 | { |
| 82 | log<level::ERR>("Missing required event group attributes", |
| 83 | entry("JSON=%s", group.dump().c_str())); |
| 84 | throw std::runtime_error("Missing required event group attributes"); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void Event::setTriggers(const json& jsonObj) |
| 90 | { |
| 91 | if (!jsonObj.contains("triggers")) |
| 92 | { |
| 93 | log<level::ERR>("Missing required event triggers list", |
| 94 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 95 | throw std::runtime_error("Missing required event triggers list"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void Event::setActions(const json& jsonObj) |
| 100 | { |
| 101 | for (const auto& action : jsonObj["actions"]) |
| 102 | { |
| 103 | if (!action.contains("name")) |
| 104 | { |
| 105 | log<level::ERR>("Missing required event action name", |
| 106 | entry("JSON=%s", action.dump().c_str())); |
| 107 | throw std::runtime_error("Missing required event action name"); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } // namespace phosphor::fan::control::json |