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 "simulator_exceptions.h"
24 #include "OCPlatform.h"
26 #include <boost/lexical_cast.hpp>
31 constexpr static SimulatorResourceModel::Attribute::ValueType type =
32 SimulatorResourceModel::Attribute::ValueType::UNKNOWN;
36 struct TypeConverter<int>
38 constexpr static SimulatorResourceModel::Attribute::ValueType type =
39 SimulatorResourceModel::Attribute::ValueType::INTEGER;
43 struct TypeConverter<double>
45 constexpr static SimulatorResourceModel::Attribute::ValueType type =
46 SimulatorResourceModel::Attribute::ValueType::DOUBLE;
50 struct TypeConverter<bool>
52 constexpr static SimulatorResourceModel::Attribute::ValueType type =
53 SimulatorResourceModel::Attribute::ValueType::BOOLEAN;
57 struct TypeConverter<std::string>
59 constexpr static SimulatorResourceModel::Attribute::ValueType type =
60 SimulatorResourceModel::Attribute::ValueType::STRING;
63 class attribute_type_visitor : public boost::static_visitor<
64 SimulatorResourceModel::Attribute::ValueType>
68 result_type operator ()(const T &)
70 return TypeConverter<T>::type;
74 class to_string_visitor : public boost::static_visitor<std::string>
78 result_type operator ()(const T &value)
82 return boost::lexical_cast<std::string>(value);
84 catch (const boost::bad_lexical_cast &e)
91 class add_to_representation : public boost::static_visitor<>
94 add_to_representation(OC::OCRepresentation &rep, const std::string &key)
95 : m_rep(rep), m_key(key) {}
98 void operator ()(const T &value)
100 m_rep.setValue(m_key, value);
103 OC::OCRepresentation &&getRep()
105 return std::move(m_rep);
109 OC::OCRepresentation m_rep;
113 class range_validation : public boost::static_visitor<bool>
116 range_validation (SimulatorResourceModel::Attribute &attrItem)
117 : m_attrItem(attrItem) {}
119 bool operator ()(int &value)
122 m_attrItem.getRange(min, max);
123 if (value >= min && value <= max)
128 bool operator ()(double &value)
130 std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
131 = m_attrItem.getAllowedValues();
132 if(0 == values.size())
134 for (SimulatorResourceModel::Attribute::ValueVariant & val : values)
136 SimulatorResourceModel::Attribute::ValueVariant vVal = value;
143 bool operator ()(bool &value)
148 bool operator ()(std::string &value)
150 std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
151 = m_attrItem.getAllowedValues();
152 if(0 == values.size())
154 for (SimulatorResourceModel::Attribute::ValueVariant & vVal : values)
156 std::string val = boost::get<std::string>(vVal);
165 SimulatorResourceModel::Attribute &m_attrItem;
168 SimulatorResourceModel::Attribute::ValueVariant
169 &SimulatorResourceModel::Attribute::AllowedValues::at(unsigned int index)
171 return m_values.at(index);
174 int SimulatorResourceModel::Attribute::AllowedValues::size() const
176 return m_values.size();
179 std::vector<std::string> SimulatorResourceModel::Attribute::AllowedValues::toString() const
181 std::vector<std::string> values;
183 for (auto & value : m_values)
185 to_string_visitor visitor;
186 values.push_back(boost::apply_visitor(visitor, value));
191 std::vector<SimulatorResourceModel::Attribute::ValueVariant>
192 SimulatorResourceModel::Attribute::AllowedValues::getValues() const
197 std::string SimulatorResourceModel::Attribute::getName(void) const
202 void SimulatorResourceModel::Attribute::setName(const std::string &name)
207 void SimulatorResourceModel::Attribute::getRange(int &min, int &max) const
213 void SimulatorResourceModel::Attribute::setRange(const int &min, const int &max)
219 int SimulatorResourceModel::Attribute::getAllowedValuesSize() const
221 return m_allowedValues.size();
224 void SimulatorResourceModel::Attribute::setFromAllowedValue(unsigned int index)
226 m_value = m_allowedValues.at(index);
229 SimulatorResourceModel::Attribute::ValueType SimulatorResourceModel::Attribute::getValueType() const
231 attribute_type_visitor typeVisitor;
232 return boost::apply_visitor(typeVisitor, m_value);
235 std::string SimulatorResourceModel::Attribute::valueToString() const
237 to_string_visitor visitor;
238 return boost::apply_visitor(visitor, m_value);
241 std::vector<std::string> SimulatorResourceModel::Attribute::allowedValuesToString() const
243 return m_allowedValues.toString();
246 void SimulatorResourceModel::Attribute::addValuetoRepresentation(OC::OCRepresentation &rep,
247 const std::string &key) const
249 add_to_representation visitor(rep, key);
250 boost::apply_visitor(visitor, m_value);
251 rep = visitor.getRep();
254 bool SimulatorResourceModel::Attribute::compare(SimulatorResourceModel::Attribute &attribute)
256 // Check the value types
257 if (m_value.which() != attribute.getValue().which())
262 // Check the value in allowed range
263 range_validation visitor(*this);
264 return boost::apply_visitor(visitor, attribute.getValue());
267 std::vector<SimulatorResourceModel::Attribute::ValueVariant>
268 SimulatorResourceModel::Attribute::getAllowedValues() const
270 return m_allowedValues.getValues();
273 bool SimulatorResourceModel::getAttribute(const std::string &attrName, Attribute &value)
275 if (m_attributes.end() != m_attributes.find(attrName))
277 value = m_attributes[attrName];
284 std::map<std::string, SimulatorResourceModel::Attribute> SimulatorResourceModel::getAttributes()
290 void SimulatorResourceModel::addAttribute(const SimulatorResourceModel::Attribute &attribute, bool overwrite)
292 if (!attribute.getName().empty() &&
293 (m_attributes.end() == m_attributes.find(attribute.getName()) || overwrite))
295 m_attributes[attribute.getName()] = attribute;
299 void SimulatorResourceModel::setRange(const std::string &attrName, const int min, const int max)
301 if (m_attributes.end() != m_attributes.find(attrName))
302 m_attributes[attrName].setRange(min, max);
305 void SimulatorResourceModel::setUpdateInterval(const std::string &attrName, int interval)
307 if (m_attributes.end() != m_attributes.find(attrName))
308 m_attributes[attrName].setUpdateFrequencyTime(interval);
311 void SimulatorResourceModel::updateAttributeFromAllowedValues(const std::string &attrName,
314 if (m_attributes.end() != m_attributes.find(attrName))
315 m_attributes[attrName].setFromAllowedValue(index);
318 void SimulatorResourceModel::removeAttribute(const std::string &attrName)
320 if (attrName.empty() || m_attributes.end() == m_attributes.find(attrName))
322 OC_LOG(ERROR, TAG, "Attribute name is empty or not found in model!");
323 throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Attribute not found in model!");
326 m_attributes.erase(attrName);
330 OC::OCRepresentation SimulatorResourceModel::getOCRepresentation() const
332 OC::OCRepresentation rep;
333 for (auto & attribute : m_attributes)
335 (attribute.second).addValuetoRepresentation(rep, attribute.first);
341 bool SimulatorResourceModel::update(OC::OCRepresentation &ocRep)
343 if (0 == ocRep.size())
346 // Convert OCRepresentation to SimulatorResourceModel
347 SimulatorResourceModelSP resModel = create(ocRep);
349 return update(resModel);
352 bool SimulatorResourceModel::update(SimulatorResourceModelSP &repModel)
354 std::map<std::string, SimulatorResourceModel::Attribute> attributes = repModel->getAttributes();
355 for (auto & attributeItem : attributes)
357 // Check the attribute presence
358 SimulatorResourceModel::Attribute attribute;
359 if (false == getAttribute((attributeItem.second).getName(), attribute))
364 // Check the validity of the value to be set
365 if (false == attribute.compare(attributeItem.second))
369 m_attributes[(attributeItem.second).getName()].setValue((attributeItem.second).getValue());
375 SimulatorResourceModelSP SimulatorResourceModel::create(const OC::OCRepresentation &ocRep)
377 SimulatorResourceModelSP resModel(new SimulatorResourceModel);
378 for (auto & attributeItem : ocRep)
380 SimulatorResourceModel::Attribute attribute;
381 if (attributeItem.type() == OC::AttributeType::Integer)
382 attribute.setValue(attributeItem.getValue<int>());
383 if (attributeItem.type() == OC::AttributeType::Double)
384 attribute.setValue(attributeItem.getValue<double>());
385 if (attributeItem.type() == OC::AttributeType::String)
386 attribute.setValue(attributeItem.getValue<std::string>());
387 if (attributeItem.type() == OC::AttributeType::Boolean)
388 attribute.setValue(attributeItem.getValue<bool>());
390 attribute.setName(attributeItem.attrname());
391 resModel->m_attributes[attributeItem.attrname()] = attribute;