Implementation of APIs for managing resource list and callback
[platform/upstream/iotivity.git] / service / simulator / src / 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_ATTRIBUTE_H_
29 #define SIMULATOR_RESOURCE_ATTRIBUTE_H_
30
31 #include <string>
32 #include <vector>
33 #include "OCPlatform.h"
34 #include "OCApi.h"
35
36 class SimulatorResourceServer;
37 /**
38  * @class   SimulatorResourceModel
39  * @brief   This class provides a set of functions for accessing and manipulating the resource model.
40  */
41 class SimulatorResourceModel
42 {
43         friend class SimulatorResourceServer;
44
45     public:
46
47         SimulatorResourceModel() = default;
48         SimulatorResourceModel(SimulatorResourceModel &&) = default;
49         SimulatorResourceModel(const SimulatorResourceModel &) = default;
50         SimulatorResourceModel &operator=(const SimulatorResourceModel &) = default;
51         SimulatorResourceModel &operator=(SimulatorResourceModel &&) = default;
52
53         virtual ~SimulatorResourceModel() {}
54
55         /**
56           * @class   Attribute
57           * @brief   This class represents a resource attribute whose values can be generic.
58           */
59         class Attribute
60         {
61             public:
62                 typedef boost::variant <
63                 int,
64                 double,
65                 std::string
66                 > ValueVariant;
67
68                 Attribute() = default;
69                 Attribute(const std::string &attrName) : m_name(attrName) {}
70
71                 std::string getName(void) const;
72                 void setName(const std::string &name);
73
74                 template <typename T>
75                 T getValue() const
76                 {
77                     T val = T();
78                     return boost::get<T>(m_value);
79                 }
80
81                 ValueVariant &getValue()
82                 {
83                     return m_value;
84                 }
85
86                 int getValueType() const
87                 {
88                     return m_value.which();
89                 }
90
91                 template <typename T>
92                 void setValue(const T &value)
93                 {
94                     m_value = value;
95                 }
96
97                 void setFromAllowedValue(const int allowedValueIndex);
98
99                 void getRange(int &min, int &max) const;
100
101                 void setRange(const int &min, const int &max);
102
103                 template <typename T>
104                 bool setAllowedValues(const std::vector<T> &values)
105                 {
106                     ValueVariant temp = values.at(0);
107                     if (temp.which() != m_value.which())
108                     {
109                         return false;
110                     }
111
112                     m_allowedValues.addValues(values);
113                     return true;
114                 }
115
116                 /**
117                   * This method is used to get the size of the allowed values.
118                   *
119                   * @return Size of the allowed values
120                   */
121                 int getAllowedValuesSize() const;
122
123                 /**
124                   * This method is used to get the string representation of the value.
125                   *
126                   * @return Attribute's value as a string
127                   */
128                 std::string valueToString() const;
129
130                 /**
131                   * This method is used to get the string representation of all the allowed values.
132                   *
133                   * @return All allowed values as a string
134                   */
135                 std::string allowedValuesToString() const;
136                 std::vector<std::string> allowedValuesToVectorString() const;
137
138                 void addValuetoRepresentation(OC::OCRepresentation &rep,
139                                               const std::string &key) const;
140
141                 bool compare(Attribute &attribute);
142
143                 std::vector<ValueVariant> getAllowedValues();
144
145                 int getUpdateFrequencyTime() {return m_updateInterval;}
146                 void setUpdateFrequencyTime(int interval) {m_updateInterval = interval;}
147
148             private:
149                 class AllowedValues
150                 {
151                     public:
152                         template <typename T>
153                         void addValue(const T &value)
154                         {
155                             ValueVariant temp = value;
156                             m_values.push_back(temp);
157                         }
158
159                         template <typename T>
160                         void addValues(const std::vector<T> &values)
161                         {
162                             for (auto value : values)
163                             {
164                                 ValueVariant vValue = value;
165                                 m_values.push_back(vValue);
166                             }
167                         }
168
169                         ValueVariant &at(int index);
170                         int size() const;
171                         std::string toString() const;
172                         std::vector<std::string> toVectorString() const;
173                         std::vector<ValueVariant> getValues();
174                     private:
175                         std::vector<ValueVariant> m_values;
176                 };
177
178                 std::string m_name;
179                 ValueVariant m_value;
180                 int m_max;
181                 int m_min;
182                 AllowedValues m_allowedValues;
183                 int m_updateInterval;
184         };
185
186         /**
187          * This method is used to get the number of attributes in the resource.
188          *
189          * @return Count of attributes
190          */
191         int size() const { return m_attributes.size(); }
192
193         /**
194           * This method is used to get the value of an attribute.
195           *
196           * @param attrName - Attribute name
197           * @param value - Attribute value
198           *
199           * @return Boolean, true if attribute exists, otherwise false.
200           */
201         bool getAttribute(const std::string &attrName, Attribute &value);
202
203         /**
204           * This method is used to get the entire list of attributes in the form of key-value pair.
205           * Attribute name is the key and an instance of Attribute is the value.
206           *
207           * @return A map of all the attributes
208           */
209         std::map<std::string, Attribute> getAttributes() const;
210
211         static SimulatorResourceModel create(const OC::OCRepresentation &ocRep);
212
213         template <typename T>
214         void addAttribute(const std::string &attrName, const T &attrValue)
215         {
216             if (m_attributes.end() == m_attributes.find(attrName))
217             {
218                 m_attributes[attrName] = Attribute(attrName);
219                 m_attributes[attrName].setValue(attrValue);
220             }
221         }
222
223     private:
224         void setRange(const std::string &attrName, const int min, const int max);
225
226         template <typename T>
227         void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
228         {
229             if (m_attributes.end() != m_attributes.find(attrName))
230                 m_attributes[attrName].setAllowedValues(values);
231         }
232
233         void setUpdateInterval(const std::string &attrName, int interval);
234
235         template <typename T>
236         void updateAttribute(const std::string &attrName, const T &value)
237         {
238             if (m_attributes.end() != m_attributes.find(attrName))
239                 m_attributes[attrName].setValue(value);
240         }
241
242         void updateAttributeFromAllowedValues(const std::string &attrName, const int allowedValueIndex);
243
244         void removeAttribute(const std::string &attrName);
245
246         OC::OCRepresentation getOCRepresentation() const;
247
248         bool update(OC::OCRepresentation &ocRep);
249
250         bool update(SimulatorResourceModel &repModel);
251
252         std::map<std::string, Attribute> m_attributes;
253 };
254
255 #endif