1 /******************************************************************
3 * Copyright 2016 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 "resource_model_schema_builder.h"
22 #include "simulator_resource_model_schema.h"
24 #include "Properties.h"
27 #define TAG "RES_MODEL_BUILDER"
29 ResourceModelSchemaBuilder::ResourceModelSchemaBuilder(
30 const std::shared_ptr<RAML::RequestResponseBody> &ramlSchema)
31 : m_ramlSchema (ramlSchema) {}
33 std::shared_ptr<SimulatorResourceModelSchema> ResourceModelSchemaBuilder::build()
37 OIC_LOG(ERROR, TAG, "RAML schema pointer is null!");
41 std::shared_ptr<SimulatorResourceModelSchema> modelSchema =
42 SimulatorResourceModelSchema::build();
44 auto jsonSchema = m_ramlSchema->getSchema()->getProperties();
45 for (auto &propertyElement : jsonSchema->getProperties())
47 auto property = propertyElement.second;
48 auto attributeProperty = buildAttribute(property);
50 if (!attributeProperty)
53 modelSchema->add(property->getName(), attributeProperty);
59 std::shared_ptr<AttributeProperty> ResourceModelSchemaBuilder::buildAttribute(
60 const std::shared_ptr<RAML::Properties> &property)
62 switch (property->getType().type())
64 case RAML::VariantType::INTEGER:
65 return buildIntegerAttribute(property);
67 case RAML::VariantType::DOUBLE:
68 return buildDoubleAttribute(property);
70 case RAML::VariantType::BOOLEAN:
71 return buildBooleanAttribute(property);
73 case RAML::VariantType::STRING:
74 return buildStringAttribute(property);
76 case RAML::VariantType::PROPERTY:
77 return buildArrayAttribute(property);
79 case RAML::VariantType::ARRAY:
80 return buildModelArrayAttribute(property);
82 case RAML::VariantType::OBJECT:
83 return buildModelAttribute(property);
86 OIC_LOG(ERROR, TAG, "Unknown type!");
93 std::shared_ptr<IntegerProperty> ResourceModelSchemaBuilder::buildIntegerAttribute(
94 const std::shared_ptr<RAML::Properties> &property)
96 std::shared_ptr<IntegerProperty> integerProperty = IntegerProperty::build();
99 if (property->isDefaultValue())
101 integerProperty->setDefaultValue(property->getValue<int>());
104 // Set the Range/Enum property
105 for (auto &valueProperty : property->getValueProperties())
107 if (RAML::ValueProperty::Type::RANGE == valueProperty->type())
109 integerProperty->setRange(valueProperty->min(), valueProperty->max());
112 else if (RAML::ValueProperty::Type::VALUE_SET == valueProperty->type())
114 std::vector<int> values;
115 for (auto &value : valueProperty->valueSet())
116 values.push_back(boost::get<int>(value));
117 integerProperty->setValues(values);
122 return integerProperty;
125 std::shared_ptr<DoubleProperty> ResourceModelSchemaBuilder::buildDoubleAttribute(
126 const std::shared_ptr<RAML::Properties> &property)
128 std::shared_ptr<DoubleProperty> doubleProperty = DoubleProperty::build();
131 if (property->isDefaultValue())
133 doubleProperty->setDefaultValue(property->getValue<double>());
136 // Set the Range/Enum property
137 for (auto &valueProperty : property->getValueProperties())
139 if (RAML::ValueProperty::Type::RANGE == valueProperty->type())
141 doubleProperty->setRange(valueProperty->min(), valueProperty->max());
144 else if (RAML::ValueProperty::Type::VALUE_SET == valueProperty->type())
146 std::vector<double> values;
147 for (auto &value : valueProperty->valueSet())
148 values.push_back(boost::get<double>(value));
149 doubleProperty->setValues(values);
154 return doubleProperty;
157 std::shared_ptr<BooleanProperty> ResourceModelSchemaBuilder::buildBooleanAttribute(
158 const std::shared_ptr<RAML::Properties> &property)
160 std::shared_ptr<BooleanProperty> boolProperty = BooleanProperty::build();
163 if (property->isDefaultValue())
165 boolProperty->setDefaultValue(property->getValue<bool>());
171 std::shared_ptr<StringProperty> ResourceModelSchemaBuilder::buildStringAttribute(
172 const std::shared_ptr<RAML::Properties> &property)
174 std::shared_ptr<StringProperty> stringProperty = StringProperty::build();
177 if (property->isDefaultValue())
179 stringProperty->setDefaultValue(property->getValue<std::string>());
182 // Set the Range/Enum property
183 for (auto &valueProperty : property->getValueProperties())
185 if (RAML::ValueProperty::Type::RANGE == valueProperty->type())
187 stringProperty->setRange(valueProperty->min(), valueProperty->max());
190 else if (RAML::ValueProperty::Type::VALUE_SET == valueProperty->type())
192 std::vector<std::string> values;
193 for (auto &value : valueProperty->valueSet())
194 values.push_back(boost::get<std::string>(value));
195 stringProperty->setValues(values);
200 return stringProperty;
203 std::shared_ptr<ArrayProperty> ResourceModelSchemaBuilder::buildArrayAttribute(
204 const std::shared_ptr<RAML::Properties> &property)
206 std::shared_ptr<ArrayProperty> arrayProperty = ArrayProperty::build();
208 auto arrayAttribute = boost::get<RAML::Properties>(property->getValue());
209 switch (arrayAttribute.getType().type())
211 case RAML::VariantType::INTEGER:
213 std::shared_ptr<RAML::Properties> elementAttribute =
214 std::make_shared<RAML::Properties>(arrayAttribute);
216 std::shared_ptr<IntegerProperty> elementProperty =
217 buildIntegerAttribute(elementAttribute);
219 setArrayProperties(property, arrayProperty);
220 arrayProperty->setElementProperty(elementProperty);
224 case RAML::VariantType::DOUBLE :
226 std::shared_ptr<RAML::Properties> elementAttribute =
227 std::make_shared<RAML::Properties>(arrayAttribute);
229 std::shared_ptr<DoubleProperty> elementProperty =
230 buildDoubleAttribute(elementAttribute);
232 setArrayProperties(property, arrayProperty);
233 arrayProperty->setElementProperty(elementProperty);
237 case RAML::VariantType::BOOLEAN :
239 std::shared_ptr<RAML::Properties> elementAttribute =
240 std::make_shared<RAML::Properties>(arrayAttribute);
242 std::shared_ptr<BooleanProperty> elementProperty =
243 buildBooleanAttribute(elementAttribute);
245 setArrayProperties(property, arrayProperty);
246 arrayProperty->setElementProperty(elementProperty);
250 case RAML::VariantType::STRING :
252 std::shared_ptr<RAML::Properties> elementAttribute =
253 std::make_shared<RAML::Properties>(arrayAttribute);
255 std::shared_ptr<StringProperty> elementProperty =
256 buildStringAttribute(elementAttribute);
258 setArrayProperties(property, arrayProperty);
259 arrayProperty->setElementProperty(elementProperty);
264 OIC_LOG(ERROR, TAG, "Unknown array element type!");
268 return arrayProperty;
271 std::shared_ptr<ArrayProperty> ResourceModelSchemaBuilder::buildModelArrayAttribute(
272 const std::shared_ptr<RAML::Properties> &property)
274 std::vector<RAML::Properties> attributes =
275 boost::get<std::vector<RAML::Properties> >(property->getValue());
277 std::shared_ptr<ModelProperty> modelProperty = ModelProperty::build();
279 for (auto attributeProp : attributes)
281 auto elementProperty = std::make_shared<RAML::Properties>(attributeProp);
282 modelProperty->add(elementProperty->getName(), buildAttribute(elementProperty));
285 std::shared_ptr<ArrayProperty> arrayProperty = ArrayProperty::build();
286 setArrayProperties(property, arrayProperty);
287 arrayProperty->setElementProperty(modelProperty);
289 return arrayProperty;
292 std::shared_ptr<ModelProperty> ResourceModelSchemaBuilder::buildModelAttribute(
293 const std::shared_ptr<RAML::Properties> &property)
295 std::shared_ptr<ModelProperty> modelProperty = ModelProperty::build();
297 std::vector<RAML::Properties> attributes =
298 boost::get<std::vector<RAML::Properties> >(property->getValue());
300 for (auto attributeProp : attributes)
302 auto elementProperty = std::make_shared<RAML::Properties>(attributeProp);
303 modelProperty->add(elementProperty->getName(), buildAttribute(elementProperty));
306 return modelProperty;
309 void ResourceModelSchemaBuilder::setArrayProperties(
310 const std::shared_ptr<RAML::Properties> &property,
311 const std::shared_ptr<ArrayProperty> &arrayProperty)
313 for (auto &valueProperty : property->getValueProperties())
315 if (RAML::ValueProperty::Type::ARRAY == valueProperty->type())
320 bool additionalItems = false;
322 valueProperty->valueArray(minItems, maxItems, unique, additionalItems);
323 arrayProperty->setRange(minItems, maxItems);
324 arrayProperty->setUnique(unique);
325 arrayProperty->setVariable(additionalItems);