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