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 
20 #include <nlohmann/json.hpp>
21 
22 #include <set>
23 
24 namespace phosphor::fan::control::json
25 {
26 
27 using json = nlohmann::json;
28 
29 /**
30  * @class Group - Represents a group of dbus objects for configured events
31  *
32  * A group contains a list of dbus objects that are logically grouped together
33  * to be used within one-or-more configured fan control events. An event object
34  * is configured to apply a set of actions against a list of groups that could
35  * result in a fan control target change. A group may also be configured against
36  * a list of profiles(OPTIONAL) and or denote a specific service(OPTIONAL) that
37  * serves the list of dbus objects in the group.
38  *
39  * (When no profile for a group is given, the group defaults to always be used
40  * within the events its included in)
41  *
42  */
43 class Group : public ConfigBase
44 {
45   public:
46     /* JSON file name for groups */
47     static constexpr auto confFileName = "groups.json";
48 
49     Group() = delete;
50     Group(Group&&) = delete;
51     Group& operator=(const Group&) = delete;
52     Group& operator=(Group&&) = delete;
53     ~Group() = default;
54 
55     /**
56      * Constructor
57      * Parses and populates a configuration group from JSON object data
58      *
59      * @param[in] jsonObj - JSON object
60      */
61     explicit Group(const json& jsonObj);
62 
63     /**
64      * Copy Constructor
65      * Creates a group from another group's originally parsed JSON object data
66      *
67      * @param[in] origObj - Original Group object to be created from
68      */
69     Group(const Group& origObj);
70 
71     /**
72      * @brief Get the members
73      *
74      * @return List of dbus paths representing the members of the group
75      */
getMembers() const76     inline const auto& getMembers() const
77     {
78         return _members;
79     }
80 
81     /**
82      * @brief Get the service
83      *
84      * @return Service name serving the members of the group
85      */
getService() const86     inline const auto& getService() const
87     {
88         return _service;
89     }
90 
91     /**
92      * @brief Set the dbus interface name for the group
93      */
setInterface(const std::string & intf)94     inline void setInterface(const std::string& intf)
95     {
96         _interface = intf;
97     }
98 
99     /**
100      * @brief Get the group's dbus interface name
101      */
getInterface() const102     inline const auto& getInterface() const
103     {
104         return _interface;
105     }
106 
107     /**
108      * @brief Set the dbus property name for the group
109      */
setProperty(const std::string & prop)110     inline void setProperty(const std::string& prop)
111     {
112         _property = prop;
113     }
114 
115     /**
116      * @brief Get the group's dbus property name
117      */
getProperty() const118     inline const auto& getProperty() const
119     {
120         return _property;
121     }
122 
123     /**
124      * @brief Set the dbus property's data type for the group
125      */
setType(const std::optional<std::string> & type)126     inline void setType(const std::optional<std::string>& type)
127     {
128         _type = type;
129     }
130 
131     /**
132      * @brief Get the group's dbus property's data type
133      */
getType() const134     inline const auto& getType() const
135     {
136         return _type;
137     }
138 
139     /**
140      * @brief Set the dbus property's expected value for the group
141      */
setValue(const std::optional<PropertyVariantType> & value)142     inline void setValue(const std::optional<PropertyVariantType>& value)
143     {
144         _value = value;
145     }
146 
147     /**
148      * @brief Get the group's dbus property's expected value
149      */
getValue() const150     inline const auto& getValue() const
151     {
152         return _value;
153     }
154 
155     /**
156      * @brief Get the set of all configured group members
157      */
getAllMembers()158     static const std::set<std::string>& getAllMembers()
159     {
160         return _allMembers;
161     }
162 
163   private:
164     /* Members of the group */
165     std::vector<std::string> _members;
166 
167     /* Service name serving all the members */
168     std::string _service;
169 
170     /* Dbus interface name for all the members */
171     std::string _interface;
172 
173     /* Dbus property name for all the members */
174     std::string _property;
175 
176     /* Optional property's data type for all members */
177     std::optional<std::string> _type;
178 
179     /* Optional property value for all the members */
180     std::optional<PropertyVariantType> _value;
181 
182     /* Single set of all group members across all groups */
183     static std::set<std::string> _allMembers;
184 
185     /**
186      * @brief Parse and set the members list
187      *
188      * @param[in] jsonObj - JSON object for the group
189      *
190      * Sets the list of dbus paths making up the members of the group
191      */
192     void setMembers(const json& jsonObj);
193 
194     /**
195      * @brief Parse and set the service name(OPTIONAL)
196      *
197      * @param[in] jsonObj - JSON object for the group
198      *
199      * Sets the service name serving the members. It is recommended this service
200      * name be provided for a group containing members served by the fan control
201      * application itself, otherwise they may not be mapped correctly into any
202      * configured events.
203      */
204     void setService(const json& jsonObj);
205 };
206 
207 } // namespace phosphor::fan::control::json
208