blob: 94f860cc9208ae09a9ee804d82b780b24bd70a66 [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 Barth391ade02021-01-15 14:33:21 -060018#include "group.hpp"
19#include "json_parser.hpp"
20
Matthew Barth3174e722020-09-15 15:13:40 -050021#include <nlohmann/json.hpp>
22#include <phosphor-logging/log.hpp>
23#include <sdbusplus/bus.hpp>
24
25namespace phosphor::fan::control::json
26{
27
28using json = nlohmann::json;
29using namespace phosphor::logging;
30
Matthew Barth391ade02021-01-15 14:33:21 -060031const std::map<configKey, std::unique_ptr<Group>> Event::_availGrps =
32 getConfig<Group>(true);
33
Matthew Barth3174e722020-09-15 15:13:40 -050034Event::Event(sdbusplus::bus::bus& bus, const json& jsonObj) :
Matthew Barth391ade02021-01-15 14:33:21 -060035 ConfigBase(jsonObj), _bus(bus)
Matthew Barth3174e722020-09-15 15:13:40 -050036{
37 if (jsonObj.contains("profiles"))
38 {
39 for (const auto& profile : jsonObj["profiles"])
40 {
41 _profiles.emplace_back(profile.get<std::string>());
42 }
43 }
44
45 // Event could have a precondition
46 if (!jsonObj.contains("precondition"))
47 {
48 // Event groups are optional
49 if (jsonObj.contains("groups"))
50 {
51 setGroups(jsonObj);
52 }
53 setTriggers(jsonObj);
54 // Event actions are optional
55 if (jsonObj.contains("actions"))
56 {
57 setActions(jsonObj);
58 }
59 }
60 else
61 {
62 setPrecond(jsonObj);
63 }
64}
65
66void Event::setPrecond(const json& jsonObj)
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 }
77 setGroups(precond);
78 setTriggers(precond);
79}
80
81void Event::setGroups(const json& jsonObj)
82{
83 for (const auto& group : jsonObj["groups"])
84 {
85 if (!group.contains("name") || !group.contains("interface") ||
86 !group.contains("property"))
87 {
88 log<level::ERR>("Missing required event group attributes",
89 entry("JSON=%s", group.dump().c_str()));
90 throw std::runtime_error("Missing required event group attributes");
91 }
92 }
93}
94
95void Event::setTriggers(const json& jsonObj)
96{
97 if (!jsonObj.contains("triggers"))
98 {
99 log<level::ERR>("Missing required event triggers list",
100 entry("JSON=%s", jsonObj.dump().c_str()));
101 throw std::runtime_error("Missing required event triggers list");
102 }
103}
104
105void Event::setActions(const json& jsonObj)
106{
107 for (const auto& action : jsonObj["actions"])
108 {
109 if (!action.contains("name"))
110 {
111 log<level::ERR>("Missing required event action name",
112 entry("JSON=%s", action.dump().c_str()));
113 throw std::runtime_error("Missing required event action name");
114 }
115 }
116}
117
118} // namespace phosphor::fan::control::json