c26fa19bfae06a28378227cbaf5e3baa362058d3
[platform/upstream/iotivity.git] / service / simulator / src / common / simulator_resource_model.cpp
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 #include "simulator_resource_model.h"
22 #include "simulator_exceptions.h"
23 #include "logger.h"
24 #include "OCPlatform.h"
25 #include <sstream>
26 #include <boost/lexical_cast.hpp>
27
28 template <typename T>
29 struct TypeConverter
30 {
31     constexpr static SimulatorResourceModel::Attribute::ValueType type =
32             SimulatorResourceModel::Attribute::ValueType::UNKNOWN;
33 };
34
35 template <>
36 struct TypeConverter<int>
37 {
38     constexpr static SimulatorResourceModel::Attribute::ValueType type =
39             SimulatorResourceModel::Attribute::ValueType::INTEGER;
40 };
41
42 template <>
43 struct TypeConverter<double>
44 {
45     constexpr static SimulatorResourceModel::Attribute::ValueType type =
46             SimulatorResourceModel::Attribute::ValueType::DOUBLE;
47 };
48
49 template <>
50 struct TypeConverter<bool>
51 {
52     constexpr static SimulatorResourceModel::Attribute::ValueType type =
53             SimulatorResourceModel::Attribute::ValueType::BOOLEAN;
54 };
55
56 template <>
57 struct TypeConverter<std::string>
58 {
59     constexpr static SimulatorResourceModel::Attribute::ValueType type =
60             SimulatorResourceModel::Attribute::ValueType::STRING;
61 };
62
63 class attribute_type_visitor : public boost::static_visitor<
64         SimulatorResourceModel::Attribute::ValueType>
65 {
66     public:
67         template <typename T>
68         result_type operator ()(const T &)
69         {
70             return TypeConverter<T>::type;
71         }
72 };
73
74 class to_string_visitor : public boost::static_visitor<std::string>
75 {
76     public:
77         template <typename T>
78         result_type operator ()(const T &value)
79         {
80             try
81             {
82                 return boost::lexical_cast<std::string>(value);
83             }
84             catch (const boost::bad_lexical_cast &e)
85             {
86                 return "";
87             }
88         }
89 };
90
91 class add_to_representation : public boost::static_visitor<>
92 {
93     public:
94         add_to_representation(OC::OCRepresentation &rep, const std::string &key)
95             : m_rep(rep), m_key(key) {}
96
97         template <typename T>
98         void operator ()(const T &value)
99         {
100             m_rep.setValue(m_key, value);
101         }
102
103         OC::OCRepresentation &&getRep()
104         {
105             return std::move(m_rep);
106         }
107
108     private:
109         OC::OCRepresentation m_rep;
110         std::string m_key;
111 };
112
113 class range_validation : public boost::static_visitor<bool>
114 {
115     public:
116         range_validation (SimulatorResourceModel::Attribute &attrItem)
117             : m_attrItem(attrItem) {}
118
119         bool operator ()(int &value)
120         {
121             int min, max;
122             m_attrItem.getRange(min, max);
123             if (value >= min && value <= max)
124                 return true;
125             return false;
126         }
127
128         bool operator ()(double &value)
129         {
130             std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
131                 = m_attrItem.getAllowedValues();
132             for (SimulatorResourceModel::Attribute::ValueVariant & val : values)
133             {
134                 SimulatorResourceModel::Attribute::ValueVariant vVal = value;
135                 if (val == vVal)
136                     return true;
137             }
138             return false;
139         }
140
141         bool operator ()(bool &value)
142         {
143             return true;
144         }
145
146         bool operator ()(std::string &value)
147         {
148             std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
149                 = m_attrItem.getAllowedValues();
150             for (SimulatorResourceModel::Attribute::ValueVariant & vVal : values)
151             {
152                 std::string val = boost::get<std::string>(vVal);
153                 if (val == value)
154                     return true;
155             }
156
157             return false;
158         }
159
160     private:
161         SimulatorResourceModel::Attribute &m_attrItem;
162 };
163
164 SimulatorResourceModel::Attribute::ValueVariant
165 &SimulatorResourceModel::Attribute::AllowedValues::at(unsigned int index)
166 {
167     return m_values.at(index);
168 }
169
170 int SimulatorResourceModel::Attribute::AllowedValues::size() const
171 {
172     return m_values.size();
173 }
174
175 std::vector<std::string> SimulatorResourceModel::Attribute::AllowedValues::toString() const
176 {
177     std::vector<std::string> values;
178
179     for (auto & value : m_values)
180     {
181         to_string_visitor visitor;
182         values.push_back(boost::apply_visitor(visitor, value));
183     }
184     return values;
185 }
186
187 std::vector<SimulatorResourceModel::Attribute::ValueVariant>
188 SimulatorResourceModel::Attribute::AllowedValues::getValues()
189 {
190     return m_values;
191 }
192
193 std::string SimulatorResourceModel::Attribute::getName(void) const
194 {
195     return m_name;
196 }
197
198 void SimulatorResourceModel::Attribute::setName(const std::string &name)
199 {
200     m_name = name;
201 }
202
203 void SimulatorResourceModel::Attribute::getRange(int &min, int &max) const
204 {
205     min = m_min;
206     max = m_max;
207 }
208
209 void SimulatorResourceModel::Attribute::setRange(const int &min, const int &max)
210 {
211     m_min = min;
212     m_max = max;
213 }
214
215 int SimulatorResourceModel::Attribute::getAllowedValuesSize() const
216 {
217     return m_allowedValues.size();
218 }
219
220 void SimulatorResourceModel::Attribute::setFromAllowedValue(unsigned int index)
221 {
222     m_value = m_allowedValues.at(index);
223 }
224
225 SimulatorResourceModel::Attribute::ValueType SimulatorResourceModel::Attribute::getValueType() const
226 {
227     attribute_type_visitor typeVisitor;
228     return boost::apply_visitor(typeVisitor, m_value);
229 }
230
231 std::string SimulatorResourceModel::Attribute::valueToString() const
232 {
233     to_string_visitor visitor;
234     return boost::apply_visitor(visitor, m_value);
235 }
236
237 std::vector<std::string> SimulatorResourceModel::Attribute::allowedValuesToString() const
238 {
239     return m_allowedValues.toString();
240 }
241
242 void SimulatorResourceModel::Attribute::addValuetoRepresentation(OC::OCRepresentation &rep,
243         const std::string &key) const
244 {
245     add_to_representation visitor(rep, key);
246     boost::apply_visitor(visitor, m_value);
247     rep = visitor.getRep();
248 }
249
250 bool SimulatorResourceModel::Attribute::compare(SimulatorResourceModel::Attribute &attribute)
251 {
252     // Check the value types
253     if (m_value.which() != attribute.getValue().which())
254     {
255         return false;
256     }
257
258     // Check the value in allowed range
259     range_validation visitor(*this);
260     return boost::apply_visitor(visitor, attribute.getValue());
261 }
262
263 std::vector<SimulatorResourceModel::Attribute::ValueVariant>
264 SimulatorResourceModel::Attribute::getAllowedValues()
265 {
266     return m_allowedValues.getValues();
267 }
268
269 bool SimulatorResourceModel::getAttribute(const std::string &attrName, Attribute &value)
270 {
271     if (m_attributes.end() != m_attributes.find(attrName))
272     {
273         value = m_attributes[attrName];
274         return true;
275     }
276
277     return false;
278 }
279
280 std::map<std::string, SimulatorResourceModel::Attribute> SimulatorResourceModel::getAttributes()
281 const
282 {
283     return m_attributes;
284 }
285
286 void SimulatorResourceModel::addAttribute(const SimulatorResourceModel::Attribute &attribute)
287 {
288     if (!attribute.getName().empty() &&
289         m_attributes.end() == m_attributes.find(attribute.getName()))
290     {
291         m_attributes[attribute.getName()] = attribute;
292     }
293 }
294
295 void SimulatorResourceModel::setRange(const std::string &attrName, const int min, const int max)
296 {
297     if (m_attributes.end() != m_attributes.find(attrName))
298         m_attributes[attrName].setRange(min, max);
299 }
300
301 void SimulatorResourceModel::setUpdateInterval(const std::string &attrName, int interval)
302 {
303     if (m_attributes.end() != m_attributes.find(attrName))
304         m_attributes[attrName].setUpdateFrequencyTime(interval);
305 }
306
307 void SimulatorResourceModel::updateAttributeFromAllowedValues(const std::string &attrName,
308         unsigned int index)
309 {
310     if (m_attributes.end() != m_attributes.find(attrName))
311         m_attributes[attrName].setFromAllowedValue(index);
312 }
313
314 void SimulatorResourceModel::removeAttribute(const std::string &attrName)
315 {
316    if (attrName.empty() || m_attributes.end() == m_attributes.find(attrName))
317    {
318        OC_LOG(ERROR, TAG, "Attribute name is empty or not found in model!");
319        throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Attribute not found in model!");
320    }
321
322     m_attributes.erase(attrName);
323     return;
324 }
325
326 OC::OCRepresentation SimulatorResourceModel::getOCRepresentation() const
327 {
328     OC::OCRepresentation rep;
329     for (auto & attribute : m_attributes)
330     {
331         (attribute.second).addValuetoRepresentation(rep, attribute.first);
332     }
333
334     return rep;
335 }
336
337 bool SimulatorResourceModel::update(OC::OCRepresentation &ocRep)
338 {
339     if (0 == ocRep.size())
340         return true;
341
342     // Convert OCRepresentation to SimulatorResourceModel
343     SimulatorResourceModelSP resModel = create(ocRep);
344
345     return update(resModel);
346 }
347
348 bool SimulatorResourceModel::update(SimulatorResourceModelSP &repModel)
349 {
350     std::map<std::string, SimulatorResourceModel::Attribute> attributes = repModel->getAttributes();
351     for (auto & attributeItem : attributes)
352     {
353         // Check the attribute presence
354         SimulatorResourceModel::Attribute attribute;
355         if (false == getAttribute((attributeItem.second).getName(), attribute))
356         {
357             return false;
358         }
359
360         // Check the validity of the value to be set
361         if (false == attribute.compare(attributeItem.second))
362         {
363             return false;
364         }
365         m_attributes[(attributeItem.second).getName()].setValue((attributeItem.second).getValue());
366     }
367
368     return true;
369 }
370
371 SimulatorResourceModelSP SimulatorResourceModel::create(const OC::OCRepresentation &ocRep)
372 {
373     SimulatorResourceModelSP resModel(new SimulatorResourceModel);
374     for (auto & attributeItem : ocRep)
375     {
376         SimulatorResourceModel::Attribute attribute;
377         if (attributeItem.type() == OC::AttributeType::Integer)
378             attribute.setValue(attributeItem.getValue<int>());
379         if (attributeItem.type() == OC::AttributeType::Double)
380             attribute.setValue(attributeItem.getValue<double>());
381         if (attributeItem.type() == OC::AttributeType::String)
382             attribute.setValue(attributeItem.getValue<std::string>());
383
384         attribute.setName(attributeItem.attrname());
385         resModel->m_attributes[attributeItem.attrname()] = attribute;
386     }
387     return resModel;
388 }
389