Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / simulator / inc / simulator_resource_model.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 /**
22  * @file   simulator_resource_model.h
23  *
24  * @brief   This file contains a class which represents the resource model for simulator
25  *             resources and provides a set of functions for updating the model.
26  */
27
28 #ifndef SIMULATOR_RESOURCE_MODEL_H_
29 #define SIMULATOR_RESOURCE_MODEL_H_
30
31 #include <string>
32 #include <vector>
33 #include "OCPlatform.h"
34 #include <climits>
35
36 /**
37  * @class   SimulatorResourceModel
38  * @brief   This class provides a set of functions for accessing and manipulating the resource model.
39  */
40 class SimulatorResourceModel
41 {
42     public:
43         SimulatorResourceModel() = default;
44         SimulatorResourceModel(const SimulatorResourceModel &) = default;
45         SimulatorResourceModel &operator=(const SimulatorResourceModel &) = default;
46         SimulatorResourceModel(SimulatorResourceModel &&) = default;
47         SimulatorResourceModel &operator=(SimulatorResourceModel && ) = default;
48
49         /**
50           * @class   Attribute
51           * @brief   This class represents a resource attribute whose values can be generic.
52           */
53         class Attribute
54         {
55             public:
56                 typedef boost::variant <
57                 int,
58                 double,
59                 bool,
60                 std::string
61                 > ValueVariant;
62
63                 enum class ValueType
64                 {
65                     UNKNOWN,
66                     INTEGER,
67                     DOUBLE,
68                     BOOLEAN,
69                     STRING
70                 };
71
72                 Attribute()
73                 {
74                     m_min = INT_MIN;
75                     m_max = INT_MAX;
76                     m_updateInterval = -1;
77                 }
78
79                 Attribute(const std::string &attrName)
80                 {
81                     m_name = attrName;
82                     m_min = INT_MIN;
83                     m_max = INT_MAX;
84                     m_updateInterval = -1;
85                 }
86
87                 /**
88                  * API to get attribute's name.
89                  *
90                  * @return Attribute name.
91                  */
92                 std::string getName(void) const;
93
94                 /**
95                  * API to set the name of attribute.
96                  *
97                  * @param name - Attribute name.
98                  */
99                 void setName(const std::string &name);
100
101                 /**
102                  * API to get attribute's value.
103                  *
104                  * @return value of attribute.
105                  */
106                 template <typename T>
107                 T getValue() const
108                 {
109                     T val = T();
110                     return boost::get<T>(m_value);
111                 }
112
113                 /**
114                  * API to get attribute's value.
115                  *
116                  * @return value of attribute as ValueVariant.
117                  */
118                 ValueVariant &getValue()
119                 {
120                     return m_value;
121                 }
122
123                 /**
124                  * API to get attribute's value type.
125                  *
126                  * @return ValueType enum.
127                  */
128                 ValueType getValueType() const;
129
130                 /**
131                  * API to set the attribute's value.
132                  *
133                  * @param value - value to be set.
134                  */
135                 template <typename T>
136                 void setValue(const T &value)
137                 {
138                     m_value = value;
139                 }
140
141                 /**
142                  * API to set the attribute's value from allowed values container.
143                  *
144                  * @param allowedValueIndex - Index of value to be set from allowed vaules container.
145                  */
146                 void setFromAllowedValue(unsigned int index);
147
148                 /**
149                  * API to get range of attribute's value.
150                  */
151                 void getRange(int &min, int &max) const;
152
153                 /**
154                  * API to set range of attribute's value.
155                  *
156                  * @param min - minimum value could be set as attribute value.
157                  * @param max - maximum value could be set as attribute value.
158                  */
159                 void setRange(const int &min, const int &max);
160
161                 /**
162                  * API to set the values to allowed values set.
163                  *
164                  * @param values - vector of values which will be set as allowed values.
165                  */
166                 template <typename T>
167                 bool setAllowedValues(const std::vector<T> &values)
168                 {
169                     ValueVariant temp = values.at(0);
170                     if (temp.which() != m_value.which())
171                     {
172                         return false;
173                     }
174
175                     m_allowedValues.addValues(values);
176                     return true;
177                 }
178
179                 /**
180                  * API to get the number of values present in allowed values set.
181                  *
182                  * @return Size of the allowed values.
183                  */
184                 int getAllowedValuesSize() const;
185
186                 /**
187                  * API to get the string representation of the value.
188                  *
189                  * @return Attribute's value as a string.
190                  */
191                 std::string valueToString() const;
192
193                 /**
194                  * API to get the string representation of all the allowed values.
195                  *
196                  * @return All allowed values as a string.
197                  */
198                 std::vector<std::string> allowedValuesToString() const;
199
200                 void addValuetoRepresentation(OC::OCRepresentation &rep,
201                                               const std::string &key) const;
202
203                 bool compare(Attribute &attribute);
204
205                 std::vector<ValueVariant> getAllowedValues() const;
206
207                 int getUpdateFrequencyTime() {return m_updateInterval;}
208                 void setUpdateFrequencyTime(int interval) {m_updateInterval = interval;}
209
210             private:
211                 class AllowedValues
212                 {
213                     public:
214                         template <typename T>
215                         void addValue(const T &value)
216                         {
217                             ValueVariant temp = value;
218                             m_values.push_back(temp);
219                         }
220
221                         template <typename T>
222                         void addValues(const std::vector<T> &values)
223                         {
224                             for (auto value : values)
225                             {
226                                 ValueVariant vValue = value;
227                                 m_values.push_back(vValue);
228                             }
229                         }
230
231                         ValueVariant &at(unsigned int index);
232                         int size() const;
233                         std::vector<std::string> toString() const;
234                         std::vector<ValueVariant> getValues() const;
235                     private:
236                         std::vector<ValueVariant> m_values;
237                 };
238
239                 std::string m_name;
240                 ValueVariant m_value;
241                 int m_max;
242                 int m_min;
243                 AllowedValues m_allowedValues;
244                 int m_updateInterval;
245         };
246
247         /**
248          * API to get the number of attributes in the resource model.
249          *
250          * @return Number of attributes.
251          */
252         int size() const { return m_attributes.size(); }
253
254         /**
255          * API to get the value of an attribute.
256          *
257          * @param attrName - Attribute name
258          * @param value - Attribute value
259          *
260          * @return true if attribute exists, otherwise false.
261          */
262         bool getAttribute(const std::string &attrName, Attribute &value);
263
264         /**
265          * API to get the entire list of attributes in the form of key-value pair.
266          * Attribute name is the key and an instance of Attribute is the value.
267          *
268          * @return A map of all the attributes
269          */
270         std::map<std::string, Attribute> getAttributes() const;
271
272         /**
273          * API to add new attribute to resource model.
274          *
275          * @param attrName - Attribute name
276          * @param attrValue - Attribute value
277          */
278         template <typename T>
279         void addAttribute(const std::string &attrName, const T &attrValue)
280         {
281             if (m_attributes.end() == m_attributes.find(attrName))
282             {
283                 m_attributes[attrName] = Attribute(attrName);
284                 m_attributes[attrName].setValue(attrValue);
285             }
286         }
287
288         /**
289           * API to add new attribute to resource model.
290           *
291           * @param attr  - Attribute pointer
292           *
293           */
294         void addAttribute(const Attribute &attribute, bool overwrite = false);
295
296         /**
297          * API to set range of attribute value.
298          *
299          * @param attrName - Attribute name.
300          * @param min - Minimum value could be set as attribute value.
301          * @param max - Maximum value could be set as attribute value.
302          */
303         void setRange(const std::string &attrName, const int min, const int max);
304
305         OC::OCRepresentation getOCRepresentation() const;
306         static std::shared_ptr<SimulatorResourceModel> create(const OC::OCRepresentation &ocRep);
307
308         template <typename T>
309         void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
310         {
311             if (m_attributes.end() != m_attributes.find(attrName))
312                 m_attributes[attrName].setAllowedValues(values);
313         }
314
315         bool update(OC::OCRepresentation &ocRep);
316
317         bool update(std::shared_ptr<SimulatorResourceModel> &repModel);
318
319         template <typename T>
320         void updateAttribute(const std::string &attrName, const T &value)
321         {
322             if (m_attributes.end() != m_attributes.find(attrName))
323                 m_attributes[attrName].setValue(value);
324         }
325
326         void updateAttributeFromAllowedValues(const std::string &attrName, unsigned int index);
327
328         void removeAttribute(const std::string &attrName);
329
330         void setUpdateInterval(const std::string &attrName, int interval);
331
332     private:
333         std::map<std::string, Attribute> m_attributes;
334 };
335
336 typedef std::shared_ptr<SimulatorResourceModel> SimulatorResourceModelSP;
337 typedef std::shared_ptr<SimulatorResourceModel::Attribute> AttributeSP;
338
339 #endif