blob: 260801488451cecd4e012da207fe6aad86c40e14 [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#pragma once
17
Matthew Barth44ab7692021-03-26 11:40:10 -050018#include "action.hpp"
Matthew Barth3174e722020-09-15 15:13:40 -050019#include "config_base.hpp"
Matthew Barth391ade02021-01-15 14:33:21 -060020#include "group.hpp"
Matthew Barth3174e722020-09-15 15:13:40 -050021
22#include <nlohmann/json.hpp>
23#include <sdbusplus/bus.hpp>
24
Matthew Barth44ab7692021-03-26 11:40:10 -050025#include <memory>
Matthew Barth12bae962021-01-15 16:18:11 -060026#include <optional>
Matthew Barth44ab7692021-03-26 11:40:10 -050027#include <tuple>
Matthew Barth12bae962021-01-15 16:18:11 -060028
Matthew Barth3174e722020-09-15 15:13:40 -050029namespace phosphor::fan::control::json
30{
31
32using json = nlohmann::json;
33
34/**
35 * @class Event - Represents a configured fan control event
36 *
37 * Fan control events are optional, therefore the "events.json" file is
38 * also optional. An event object can be used to enable a specific change to
39 * how fan control should function. These events contain the configured
40 * attributes that result in how fans are controlled within a system. Events
41 * are made up of groups of sensors, triggers from those sensors, and actions
42 * to be run when a trigger occurs. Events may also have a precondition that
43 * must exist before the event is loaded into fan control. The triggers,
44 * actions, and preconditions configured must be available within the fan
45 * control application source.
46 *
47 * When no events exist, the configured fans are set to their corresponding
Matthew Barth44ab7692021-03-26 11:40:10 -050048 * zone's `full_speed` value.
Matthew Barth3174e722020-09-15 15:13:40 -050049 */
50class Event : public ConfigBase
51{
52 static constexpr auto precondName = 0;
53 static constexpr auto precondGroups = 1;
54 static constexpr auto precondEvents = 2;
55 using Precondition =
Matthew Barth44ab7692021-03-26 11:40:10 -050056 std::tuple<std::string,
57 std::vector<std::tuple<std::string, std::string, std::string,
58 PropertyVariantType>>,
59 std::vector<Event>>;
Matthew Barth3174e722020-09-15 15:13:40 -050060
61 public:
62 /* JSON file name for events */
63 static constexpr auto confFileName = "events.json";
64
65 Event() = delete;
66 Event(const Event&) = delete;
67 Event(Event&&) = delete;
68 Event& operator=(const Event&) = delete;
69 Event& operator=(Event&&) = delete;
70 ~Event() = default;
71
72 /**
73 * Constructor
74 * Parses and populates a configuration event from JSON object data
75 *
Matthew Barth3174e722020-09-15 15:13:40 -050076 * @param[in] jsonObj - JSON object
Matthew Barthd4bd0ae2021-04-14 12:42:03 -050077 * @param[in] mgr - Manager of this event
Matthew Barth44ab7692021-03-26 11:40:10 -050078 * @param[in] groups - Available groups that can be used
Matthew Barth9f1632e2021-03-31 15:51:50 -050079 * @param[in] zones - Reference to the configured zones
Matthew Barth3174e722020-09-15 15:13:40 -050080 */
Matthew Barth9403a212021-05-17 09:31:50 -050081 Event(const json& jsonObj, Manager* mgr,
Matthew Barth9f1632e2021-03-31 15:51:50 -050082 std::map<configKey, std::unique_ptr<Group>>& groups,
83 std::map<configKey, std::unique_ptr<Zone>>& zones);
Matthew Barth3174e722020-09-15 15:13:40 -050084
85 /**
86 * @brief Get the precondition
87 *
88 * @return The precondition details of the event
89 */
90 inline const auto& getPrecond() const
91 {
92 return _precond;
93 }
94
95 /**
96 * @brief Get the groups
97 *
98 * @return List of groups associated with the event
99 */
100 inline const auto& getGroups() const
101 {
102 return _groups;
103 }
104
105 /**
Matthew Barth3174e722020-09-15 15:13:40 -0500106 * @brief Get the actions
107 *
108 * @return List of actions to perform for the event
109 */
110 inline const auto& getActions() const
111 {
112 return _actions;
113 }
114
115 private:
Matthew Barth391ade02021-01-15 14:33:21 -0600116 /* The sdbusplus bus object */
117 sdbusplus::bus::bus& _bus;
118
Matthew Barthd4bd0ae2021-04-14 12:42:03 -0500119 /* The event's manager */
120 Manager* _manager;
121
Matthew Barth3174e722020-09-15 15:13:40 -0500122 /* A precondition the event has in order to be enabled */
123 Precondition _precond;
124
125 /* List of groups associated with the event */
Matthew Barthe5578602021-03-30 12:53:24 -0500126 std::vector<Group> _groups;
Matthew Barth3174e722020-09-15 15:13:40 -0500127
Matthew Barth9f1632e2021-03-31 15:51:50 -0500128 /* Reference to the configured zones */
129 std::map<configKey, std::unique_ptr<Zone>>& _zones;
130
Matthew Barth3174e722020-09-15 15:13:40 -0500131 /* List of actions for this event */
Matthew Barth44ab7692021-03-26 11:40:10 -0500132 std::vector<std::unique_ptr<ActionBase>> _actions;
Matthew Barth3174e722020-09-15 15:13:40 -0500133
134 /**
Matthew Barth46b34482021-04-06 11:27:23 -0500135 * @brief Parse group parameters and configure a group object
136 *
137 * @param[in] group - Group object to get configured
138 * @param[in] jsonObj - JSON object for the group
139 *
140 * Configures a given group from a set of JSON configuration attributes
141 */
142 void configGroup(Group& group, const json& jsonObj);
143
144 /**
Matthew Barth3174e722020-09-15 15:13:40 -0500145 * @brief Parse and set the event's precondition(OPTIONAL)
146 *
147 * @param[in] jsonObj - JSON object for the event
Matthew Barth44ab7692021-03-26 11:40:10 -0500148 * @param[in] groups - Available groups that can be used
Matthew Barth3174e722020-09-15 15:13:40 -0500149 *
150 * Sets the precondition of the event in order to be enabled
151 */
Matthew Barth44ab7692021-03-26 11:40:10 -0500152 void setPrecond(const json& jsonObj,
153 std::map<configKey, std::unique_ptr<Group>>& groups);
Matthew Barth3174e722020-09-15 15:13:40 -0500154
155 /**
156 * @brief Parse and set the event's groups(OPTIONAL)
157 *
158 * @param[in] jsonObj - JSON object for the event
Matthew Barth44ab7692021-03-26 11:40:10 -0500159 * @param[in] groups - Available groups that can be used
Matthew Barth3174e722020-09-15 15:13:40 -0500160 *
161 * Sets the list of groups associated with the event
162 */
Matthew Barth44ab7692021-03-26 11:40:10 -0500163 void setGroups(const json& jsonObj,
164 std::map<configKey, std::unique_ptr<Group>>& groups);
Matthew Barth3174e722020-09-15 15:13:40 -0500165
166 /**
Matthew Barth9f1632e2021-03-31 15:51:50 -0500167 * @brief Parse and set the event's actions(OPTIONAL)
168 *
169 * @param[in] jsonObj - JSON object for the event
170 * @param[in] groups - Available groups that can be used
171 *
172 * Sets the list of actions to perform for the event
173 */
174 void setActions(const json& jsonObj,
175 std::map<configKey, std::unique_ptr<Group>>& groups);
176
177 /**
Matthew Barth3174e722020-09-15 15:13:40 -0500178 * @brief Parse and set the event's triggers
179 *
180 * @param[in] jsonObj - JSON object for the event
181 *
182 * Sets the list of triggers for the event
183 */
184 void setTriggers(const json& jsonObj);
Matthew Barth3174e722020-09-15 15:13:40 -0500185};
186
187} // namespace phosphor::fan::control::json