1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "simulator_resource_model.h"
22 #include "OCPlatform.h"
24 #include <boost/lexical_cast.hpp>
29 constexpr static SimulatorResourceModel::Attribute::ValueType type =
30 SimulatorResourceModel::Attribute::ValueType::UNKNOWN;
34 struct TypeConverter<int>
36 constexpr static SimulatorResourceModel::Attribute::ValueType type =
37 SimulatorResourceModel::Attribute::ValueType::INTEGER;
41 struct TypeConverter<double>
43 constexpr static SimulatorResourceModel::Attribute::ValueType type =
44 SimulatorResourceModel::Attribute::ValueType::DOUBLE;
48 struct TypeConverter<bool>
50 constexpr static SimulatorResourceModel::Attribute::ValueType type =
51 SimulatorResourceModel::Attribute::ValueType::BOOLEAN;
55 struct TypeConverter<std::string>
57 constexpr static SimulatorResourceModel::Attribute::ValueType type =
58 SimulatorResourceModel::Attribute::ValueType::STRING;
61 class attribute_type_visitor : public boost::static_visitor<
62 SimulatorResourceModel::Attribute::ValueType>
66 result_type operator ()(const T &)
68 return TypeConverter<T>::type;
72 class to_string_visitor : public boost::static_visitor<std::string>
76 result_type operator ()(const T &value)
80 return boost::lexical_cast<std::string>(value);
82 catch (const boost::bad_lexical_cast &e)
89 class add_to_representation : public boost::static_visitor<>
92 add_to_representation(OC::OCRepresentation &rep, const std::string &key)
93 : m_rep(rep), m_key(key) {}
96 void operator ()(const T &value)
98 m_rep.setValue(m_key, value);
101 OC::OCRepresentation &&getRep()
103 return std::move(m_rep);
107 OC::OCRepresentation m_rep;
111 class range_validation : public boost::static_visitor<bool>
114 range_validation (SimulatorResourceModel::Attribute &attrItem)
115 : m_attrItem(attrItem) {}
117 bool operator ()(int &value)
120 m_attrItem.getRange(min, max);
121 if (value >= min && value <= max)
126 bool operator ()(double &value)
128 std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
129 = m_attrItem.getAllowedValues();
130 for (SimulatorResourceModel::Attribute::ValueVariant & val : values)
132 SimulatorResourceModel::Attribute::ValueVariant vVal = value;
139 bool operator ()(bool &value)
144 bool operator ()(std::string &value)
146 std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
147 = m_attrItem.getAllowedValues();
148 for (SimulatorResourceModel::Attribute::ValueVariant & vVal : values)
150 std::string val = boost::get<std::string>(vVal);
159 SimulatorResourceModel::Attribute &m_attrItem;
162 SimulatorResourceModel::Attribute::ValueVariant
163 &SimulatorResourceModel::Attribute::AllowedValues::at(unsigned int index)
165 return m_values.at(index);
168 int SimulatorResourceModel::Attribute::AllowedValues::size() const
170 return m_values.size();
173 std::vector<std::string> SimulatorResourceModel::Attribute::AllowedValues::toString() const
175 std::vector<std::string> values;
177 for (auto & value : m_values)
179 to_string_visitor visitor;
180 values.push_back(boost::apply_visitor(visitor, value));
185 std::vector<SimulatorResourceModel::Attribute::ValueVariant>
186 SimulatorResourceModel::Attribute::AllowedValues::getValues()
191 std::string SimulatorResourceModel::Attribute::getName(void) const
196 void SimulatorResourceModel::Attribute::setName(const std::string &name)
201 void SimulatorResourceModel::Attribute::getRange(int &min, int &max) const
207 void SimulatorResourceModel::Attribute::setRange(const int &min, const int &max)
213 int SimulatorResourceModel::Attribute::getAllowedValuesSize() const
215 return m_allowedValues.size();
218 void SimulatorResourceModel::Attribute::setFromAllowedValue(unsigned int index)
220 m_value = m_allowedValues.at(index);
223 SimulatorResourceModel::Attribute::ValueType SimulatorResourceModel::Attribute::getValueType() const
225 attribute_type_visitor typeVisitor;
226 return boost::apply_visitor(typeVisitor, m_value);
229 std::string SimulatorResourceModel::Attribute::valueToString() const
231 to_string_visitor visitor;
232 return boost::apply_visitor(visitor, m_value);
235 std::vector<std::string> SimulatorResourceModel::Attribute::allowedValuesToString() const
237 return m_allowedValues.toString();
240 void SimulatorResourceModel::Attribute::addValuetoRepresentation(OC::OCRepresentation &rep,
241 const std::string &key) const
243 add_to_representation visitor(rep, key);
244 boost::apply_visitor(visitor, m_value);
245 rep = visitor.getRep();
248 bool SimulatorResourceModel::Attribute::compare(SimulatorResourceModel::Attribute &attribute)
250 // Check the value types
251 if (m_value.which() != attribute.getValue().which())
256 // Check the value in allowed range
257 range_validation visitor(*this);
258 return boost::apply_visitor(visitor, attribute.getValue());
261 std::vector<SimulatorResourceModel::Attribute::ValueVariant>
262 SimulatorResourceModel::Attribute::getAllowedValues()
264 return m_allowedValues.getValues();
267 bool SimulatorResourceModel::getAttribute(const std::string &attrName, Attribute &value)
269 if (m_attributes.end() != m_attributes.find(attrName))
271 value = m_attributes[attrName];
278 std::map<std::string, SimulatorResourceModel::Attribute> SimulatorResourceModel::getAttributes()
284 void SimulatorResourceModel::addAttribute(const SimulatorResourceModel::Attribute &attribute)
286 if (!attribute.getName().empty() &&
287 m_attributes.end() == m_attributes.find(attribute.getName()))
289 m_attributes[attribute.getName()] = attribute;
293 void SimulatorResourceModel::setRange(const std::string &attrName, const int min, const int max)
295 if (m_attributes.end() != m_attributes.find(attrName))
296 m_attributes[attrName].setRange(min, max);
299 void SimulatorResourceModel::setUpdateInterval(const std::string &attrName, int interval)
301 if (m_attributes.end() != m_attributes.find(attrName))
302 m_attributes[attrName].setUpdateFrequencyTime(interval);
305 void SimulatorResourceModel::updateAttributeFromAllowedValues(const std::string &attrName,
308 if (m_attributes.end() != m_attributes.find(attrName))
309 m_attributes[attrName].setFromAllowedValue(index);
312 void SimulatorResourceModel::removeAttribute(const std::string &attrName)
314 if (m_attributes.end() == m_attributes.find(attrName))
319 m_attributes.erase(attrName);
323 OC::OCRepresentation SimulatorResourceModel::getOCRepresentation() const
325 OC::OCRepresentation rep;
326 for (auto & attribute : m_attributes)
328 (attribute.second).addValuetoRepresentation(rep, attribute.first);
334 bool SimulatorResourceModel::update(OC::OCRepresentation &ocRep)
336 if (0 == ocRep.size())
339 // Convert OCRepresentation to SimulatorResourceModel
340 SimulatorResourceModelSP resModel = create(ocRep);
342 return update(resModel);
345 bool SimulatorResourceModel::update(SimulatorResourceModelSP &repModel)
347 std::map<std::string, SimulatorResourceModel::Attribute> attributes = repModel->getAttributes();
348 for (auto & attributeItem : attributes)
350 // Check the attribute presence
351 SimulatorResourceModel::Attribute attribute;
352 if (false == getAttribute((attributeItem.second).getName(), attribute))
357 // Check the validity of the value to be set
358 if (false == attribute.compare(attributeItem.second))
362 m_attributes[(attributeItem.second).getName()].setValue((attributeItem.second).getValue());
368 SimulatorResourceModelSP SimulatorResourceModel::create(const OC::OCRepresentation &ocRep)
370 SimulatorResourceModelSP resModel(new SimulatorResourceModel);
371 for (auto & attributeItem : ocRep)
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>());
381 attribute.setName(attributeItem.attrname());
382 resModel->m_attributes[attributeItem.attrname()] = attribute;