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 for (SimulatorResourceModel::Attribute::ValueVariant & val : values)
134 SimulatorResourceModel::Attribute::ValueVariant vVal = value;
141 bool operator ()(bool &value)
146 bool operator ()(std::string &value)
148 std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
149 = m_attrItem.getAllowedValues();
150 for (SimulatorResourceModel::Attribute::ValueVariant & vVal : values)
152 std::string val = boost::get<std::string>(vVal);
161 SimulatorResourceModel::Attribute &m_attrItem;
164 SimulatorResourceModel::Attribute::ValueVariant
165 &SimulatorResourceModel::Attribute::AllowedValues::at(unsigned int index)
167 return m_values.at(index);
170 int SimulatorResourceModel::Attribute::AllowedValues::size() const
172 return m_values.size();
175 std::vector<std::string> SimulatorResourceModel::Attribute::AllowedValues::toString() const
177 std::vector<std::string> values;
179 for (auto & value : m_values)
181 to_string_visitor visitor;
182 values.push_back(boost::apply_visitor(visitor, value));
187 std::vector<SimulatorResourceModel::Attribute::ValueVariant>
188 SimulatorResourceModel::Attribute::AllowedValues::getValues()
193 std::string SimulatorResourceModel::Attribute::getName(void) const
198 void SimulatorResourceModel::Attribute::setName(const std::string &name)
203 void SimulatorResourceModel::Attribute::getRange(int &min, int &max) const
209 void SimulatorResourceModel::Attribute::setRange(const int &min, const int &max)
215 int SimulatorResourceModel::Attribute::getAllowedValuesSize() const
217 return m_allowedValues.size();
220 void SimulatorResourceModel::Attribute::setFromAllowedValue(unsigned int index)
222 m_value = m_allowedValues.at(index);
225 SimulatorResourceModel::Attribute::ValueType SimulatorResourceModel::Attribute::getValueType() const
227 attribute_type_visitor typeVisitor;
228 return boost::apply_visitor(typeVisitor, m_value);
231 std::string SimulatorResourceModel::Attribute::valueToString() const
233 to_string_visitor visitor;
234 return boost::apply_visitor(visitor, m_value);
237 std::vector<std::string> SimulatorResourceModel::Attribute::allowedValuesToString() const
239 return m_allowedValues.toString();
242 void SimulatorResourceModel::Attribute::addValuetoRepresentation(OC::OCRepresentation &rep,
243 const std::string &key) const
245 add_to_representation visitor(rep, key);
246 boost::apply_visitor(visitor, m_value);
247 rep = visitor.getRep();
250 bool SimulatorResourceModel::Attribute::compare(SimulatorResourceModel::Attribute &attribute)
252 // Check the value types
253 if (m_value.which() != attribute.getValue().which())
258 // Check the value in allowed range
259 range_validation visitor(*this);
260 return boost::apply_visitor(visitor, attribute.getValue());
263 std::vector<SimulatorResourceModel::Attribute::ValueVariant>
264 SimulatorResourceModel::Attribute::getAllowedValues()
266 return m_allowedValues.getValues();
269 bool SimulatorResourceModel::getAttribute(const std::string &attrName, Attribute &value)
271 if (m_attributes.end() != m_attributes.find(attrName))
273 value = m_attributes[attrName];
280 std::map<std::string, SimulatorResourceModel::Attribute> SimulatorResourceModel::getAttributes()
286 void SimulatorResourceModel::addAttribute(const SimulatorResourceModel::Attribute &attribute)
288 if (!attribute.getName().empty() &&
289 m_attributes.end() == m_attributes.find(attribute.getName()))
291 m_attributes[attribute.getName()] = attribute;
295 void SimulatorResourceModel::setRange(const std::string &attrName, const int min, const int max)
297 if (m_attributes.end() != m_attributes.find(attrName))
298 m_attributes[attrName].setRange(min, max);
301 void SimulatorResourceModel::setUpdateInterval(const std::string &attrName, int interval)
303 if (m_attributes.end() != m_attributes.find(attrName))
304 m_attributes[attrName].setUpdateFrequencyTime(interval);
307 void SimulatorResourceModel::updateAttributeFromAllowedValues(const std::string &attrName,
310 if (m_attributes.end() != m_attributes.find(attrName))
311 m_attributes[attrName].setFromAllowedValue(index);
314 void SimulatorResourceModel::removeAttribute(const std::string &attrName)
316 if (attrName.empty() || m_attributes.end() == m_attributes.find(attrName))
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!");
322 m_attributes.erase(attrName);
326 OC::OCRepresentation SimulatorResourceModel::getOCRepresentation() const
328 OC::OCRepresentation rep;
329 for (auto & attribute : m_attributes)
331 (attribute.second).addValuetoRepresentation(rep, attribute.first);
337 bool SimulatorResourceModel::update(OC::OCRepresentation &ocRep)
339 if (0 == ocRep.size())
342 // Convert OCRepresentation to SimulatorResourceModel
343 SimulatorResourceModelSP resModel = create(ocRep);
345 return update(resModel);
348 bool SimulatorResourceModel::update(SimulatorResourceModelSP &repModel)
350 std::map<std::string, SimulatorResourceModel::Attribute> attributes = repModel->getAttributes();
351 for (auto & attributeItem : attributes)
353 // Check the attribute presence
354 SimulatorResourceModel::Attribute attribute;
355 if (false == getAttribute((attributeItem.second).getName(), attribute))
360 // Check the validity of the value to be set
361 if (false == attribute.compare(attributeItem.second))
365 m_attributes[(attributeItem.second).getName()].setValue((attributeItem.second).getValue());
371 SimulatorResourceModelSP SimulatorResourceModel::create(const OC::OCRepresentation &ocRep)
373 SimulatorResourceModelSP resModel(new SimulatorResourceModel);
374 for (auto & attributeItem : ocRep)
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>());
384 attribute.setName(attributeItem.attrname());
385 resModel->m_attributes[attributeItem.attrname()] = attribute;