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