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 #pragma once
17 
18 #include "config_base.hpp"
19 #include "types.hpp"
20 
21 #include <nlohmann/json.hpp>
22 #include <sdbusplus/bus.hpp>
23 
24 namespace phosphor::fan::control::json
25 {
26 
27 using json = nlohmann::json;
28 
29 /**
30  * @class Event - Represents a configured fan control event
31  *
32  * Fan control events are optional, therefore the "events.json" file is
33  * also optional. An event object can be used to enable a specific change to
34  * how fan control should function. These events contain the configured
35  * attributes that result in how fans are controlled within a system. Events
36  * are made up of groups of sensors, triggers from those sensors, and actions
37  * to be run when a trigger occurs. Events may also have a precondition that
38  * must exist before the event is loaded into fan control. The triggers,
39  * actions, and preconditions configured must be available within the fan
40  * control application source.
41  *
42  * When no events exist, the configured fans are set to their corresponding
43  * zone's full_speed value.
44  */
45 class Event : public ConfigBase
46 {
47     static constexpr auto precondName = 0;
48     static constexpr auto precondGroups = 1;
49     static constexpr auto precondEvents = 2;
50     using Precondition =
51         std::tuple<std::string, std::vector<PrecondGroup>, std::vector<Event>>;
52 
53   public:
54     /* JSON file name for events */
55     static constexpr auto confFileName = "events.json";
56 
57     Event() = delete;
58     Event(const Event&) = delete;
59     Event(Event&&) = delete;
60     Event& operator=(const Event&) = delete;
61     Event& operator=(Event&&) = delete;
62     ~Event() = default;
63 
64     /**
65      * Constructor
66      * Parses and populates a configuration event from JSON object data
67      *
68      * @param[in] bus - sdbusplus bus object
69      * @param[in] jsonObj - JSON object
70      */
71     Event(sdbusplus::bus::bus& bus, const json& jsonObj);
72 
73     /**
74      * @brief Get the precondition
75      *
76      * @return The precondition details of the event
77      */
78     inline const auto& getPrecond() const
79     {
80         return _precond;
81     }
82 
83     /**
84      * @brief Get the groups
85      *
86      * @return List of groups associated with the event
87      */
88     inline const auto& getGroups() const
89     {
90         return _groups;
91     }
92 
93     /**
94      * @brief Get the triggers
95      *
96      * @return List of triggers for this event
97      */
98     inline const auto& getTriggers() const
99     {
100         return _triggers;
101     }
102 
103     /**
104      * @brief Get the actions
105      *
106      * @return List of actions to perform for the event
107      */
108     inline const auto& getActions() const
109     {
110         return _actions;
111     }
112 
113   private:
114     /* A precondition the event has in order to be enabled */
115     Precondition _precond;
116 
117     /* List of groups associated with the event */
118     std::vector<Group> _groups;
119 
120     /* List of triggers for this event */
121     std::vector<Trigger> _triggers;
122 
123     /* List of actions for this event */
124     std::vector<Action> _actions;
125 
126     /**
127      * @brief Parse and set the event's precondition(OPTIONAL)
128      *
129      * @param[in] jsonObj - JSON object for the event
130      *
131      * Sets the precondition of the event in order to be enabled
132      */
133     void setPrecond(const json& jsonObj);
134 
135     /**
136      * @brief Parse and set the event's groups(OPTIONAL)
137      *
138      * @param[in] jsonObj - JSON object for the event
139      *
140      * Sets the list of groups associated with the event
141      */
142     void setGroups(const json& jsonObj);
143 
144     /**
145      * @brief Parse and set the event's triggers
146      *
147      * @param[in] jsonObj - JSON object for the event
148      *
149      * Sets the list of triggers for the event
150      */
151     void setTriggers(const json& jsonObj);
152 
153     /**
154      * @brief Parse and set the event's actions(OPTIONAL)
155      *
156      * @param[in] jsonObj - JSON object for the event
157      *
158      * Sets the list of actions to perform for the event
159      */
160     void setActions(const json& jsonObj);
161 };
162 
163 } // namespace phosphor::fan::control::json
164