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