Design Changes to JsonSchema Parser
[platform/upstream/iotivity.git] / service / simulator / src / server / simulator_resource_factory.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 321d425..709f049
@@ -103,7 +103,41 @@ std::shared_ptr<SimulatorCollectionResource> SimulatorResourceFactory::createCol
     collectionResource->setResourceType(resourceType);\r
     return std::shared_ptr<SimulatorCollectionResource>(collectionResource);\r
 }\r
+template <typename T>\r
+void SimulatorResourceFactory::buildValueProperty(SimulatorResourceModel::Attribute &attribute,\r
+        const std::vector<RAML::ValuePropertyPtr> &valueProperties, T)\r
+{\r
+    for (auto &vp : valueProperties)\r
+    {\r
+        switch (vp->type())\r
+        {\r
+            case RAML::ValueProperty::Type::RANGE :\r
+                {\r
+                    double min = vp->min();\r
+                    double max = vp->max();\r
+                    int multipleof = vp->multipleOf();\r
+                    if (min != INT_MIN && max != INT_MAX)\r
+                    {\r
+                        SimulatorResourceModel::AttributeProperty attrProp(min, max);\r
+                        attribute.setProperty(attrProp);\r
+                    }\r
+                    break;\r
+                }\r
+            case RAML::ValueProperty::Type::VALUE_SET :\r
+                {\r
+                    std::vector<T> allowedValues;\r
+                    for (auto allow : vp->valueSet())\r
+                        allowedValues.push_back(boost::get<T>(allow));\r
+                    SimulatorResourceModel::AttributeProperty attrProp(allowedValues);\r
+                    attribute.setProperty(attrProp);\r
+                    break;\r
+                }\r
+            default:\r
+                break;\r
+        }\r
+    }\r
 \r
+}\r
 SimulatorResourceModel::Attribute SimulatorResourceFactory::buildAttribute(\r
     std::shared_ptr<RAML::Properties> propertyElement)\r
 {\r
@@ -111,103 +145,115 @@ SimulatorResourceModel::Attribute SimulatorResourceFactory::buildAttribute(
 \r
     // Build representation attribute\r
     SimulatorResourceModel::Attribute attribute(propName);\r
-    switch (propertyElement->getVariantType())\r
+    switch (propertyElement->getType().type())\r
     {\r
-        case RAML::VariantType::INT:\r
+        case RAML::VariantType::INTEGER:\r
             {\r
-                attribute.setValue(propertyElement->getValue<int>());\r
-\r
-                // Convert suppoted values\r
-                std::vector<int> allowedValues = propertyElement->getAllowedValuesInt();\r
-                if (allowedValues.size() > 0)\r
-                {\r
-                    SimulatorResourceModel::AttributeProperty attrProp(allowedValues);\r
-                    attribute.setProperty(attrProp);\r
-                }\r
+                int attributeValue = 0;\r
+                if (propertyElement->isDefaultValue())\r
+                    attributeValue = boost::get<int>(propertyElement->getValue());\r
+                attribute.setValue(attributeValue);\r
+                int type = 0;\r
+                buildValueProperty(attribute, (propertyElement->getValueProperties()), type);\r
             }\r
             break;\r
 \r
         case RAML::VariantType::DOUBLE:\r
             {\r
-                attribute.setValue(propertyElement->getValue<double>());\r
-\r
-                // Convert suppoted values\r
-                std::vector<double> allowedValues = propertyElement->getAllowedValuesDouble();\r
-                if (allowedValues.size() > 0)\r
-                {\r
-                    SimulatorResourceModel::AttributeProperty attrProp(allowedValues);\r
-                    attribute.setProperty(attrProp);\r
-                }\r
+                double attributeValue = 0;\r
+                if (propertyElement->isDefaultValue())\r
+                    attributeValue = boost::get<double>(propertyElement->getValue());\r
+                attribute.setValue(attributeValue);\r
+                double type = 0;\r
+                buildValueProperty(attribute, (propertyElement->getValueProperties()), type);\r
             }\r
             break;\r
 \r
-        case RAML::VariantType::BOOL:\r
+        case RAML::VariantType::BOOLEAN:\r
             {\r
-                attribute.setValue(propertyElement->getValue<bool>());\r
-\r
-                std::vector<bool> allowedValues = {true, false};\r
-                SimulatorResourceModel::AttributeProperty attrProp(allowedValues);\r
-                attribute.setProperty(attrProp);\r
+                bool attributeValue = false;\r
+                if (propertyElement->isDefaultValue())\r
+                    attributeValue = boost::get<bool>(propertyElement->getValue());\r
+                attribute.setValue(attributeValue);\r
+                bool type = false;\r
+                buildValueProperty(attribute, (propertyElement->getValueProperties()), type);\r
             }\r
             break;\r
 \r
         case RAML::VariantType::STRING:\r
             {\r
-                attribute.setValue(propertyElement->getValue<std::string>());\r
+                std::string attributeValue = "";\r
+                if (propertyElement->isDefaultValue())\r
+                    attributeValue = boost::get<std::string>(propertyElement->getValue());\r
+                attribute.setValue(attributeValue);\r
+                std::string type = "";\r
+                buildValueProperty(attribute, (propertyElement->getValueProperties()), type);\r
+            }\r
+            break;\r
+        case RAML::VariantType::PROPERTY:\r
+            {\r
+                RAML::Properties arrayProperty = boost::get<RAML::Properties>(propertyElement->getValue());\r
+                SimulatorResourceModel::Attribute arrayAttribute = buildAttribute(\r
+                            std::make_shared<RAML::Properties>(arrayProperty));\r
 \r
-                // Convert suppoted values\r
-                std::vector<std::string> allowedValues = propertyElement->getAllowedValuesString();\r
-                if (allowedValues.size() > 0)\r
+                switch (arrayAttribute.getType().type())\r
                 {\r
-                    SimulatorResourceModel::AttributeProperty attrProp(allowedValues);\r
-                    attribute.setProperty(attrProp);\r
+                    case SimulatorResourceModel::ValueType::INTEGER :\r
+                        {\r
+                            std::vector<int> arrValue;\r
+                            arrValue.push_back(boost::get<int>(arrayAttribute.getValue()));\r
+                            attribute.setValue(arrValue);\r
+                            int type;\r
+                            buildValueProperty(attribute, (arrayProperty.getValueProperties()), type);\r
+                            break;\r
+                        }\r
+                    case SimulatorResourceModel::ValueType::DOUBLE :\r
+                        {\r
+                            std::vector<double> arrValue;\r
+                            arrValue.push_back(boost::get<double>(arrayAttribute.getValue()));\r
+                            attribute.setValue(arrValue);\r
+                            double type;\r
+                            buildValueProperty(attribute, (arrayProperty.getValueProperties()), type);\r
+                            break;\r
+                        }\r
+                    case SimulatorResourceModel::ValueType::BOOLEAN :\r
+                        {\r
+                            std::vector<bool> arrValue;\r
+                            arrValue.push_back(boost::get<bool>(arrayAttribute.getValue()));\r
+                            attribute.setValue(arrValue);\r
+                            bool type;\r
+                            buildValueProperty(attribute, (arrayProperty.getValueProperties()), type);\r
+                            break;\r
+                        }\r
+                    case SimulatorResourceModel::ValueType::STRING :\r
+                        {\r
+                            std::vector<std::string> arrValue;\r
+                            arrValue.push_back(boost::get<std::string>(arrayAttribute.getValue()));\r
+                            attribute.setValue(arrValue);\r
+                            std::string type;\r
+                            buildValueProperty(attribute, (arrayProperty.getValueProperties()), type);\r
+                            break;\r
+                        }\r
                 }\r
             }\r
             break;\r
-    }\r
-\r
-    // Set the range property if its present\r
-    double min, max;\r
-    int multipleof;\r
-    propertyElement->getRange(min, max, multipleof);\r
-    if (min != INT_MIN && max != INT_MAX)\r
-    {\r
-        SimulatorResourceModel::AttributeProperty attrProp(min, max);\r
-        attribute.setProperty(attrProp);\r
-    }\r
-    return attribute;\r
-}\r
-\r
-SimulatorResourceModel SimulatorResourceFactory::buildResourceModel(\r
-    std::shared_ptr<RAML::Items> item)\r
-{\r
-    SimulatorResourceModel itemModel;\r
-    for ( auto &propElement : item->getProperties())\r
-    {\r
-        if (!propElement.second)\r
-            continue;\r
-\r
-        std::string propName = propElement.second->getName();\r
-        if ("p" == propName || "n" == propName || "id" == propName)\r
-        {\r
-            continue;\r
-        }\r
-\r
-        if ("array" == propElement.second->getType())\r
-        {\r
-            std::vector<SimulatorResourceModel> arrayResModel;\r
-            for ( auto &propertyItem : propElement.second->getItems())\r
+        case RAML::VariantType::ARRAY:\r
             {\r
-                arrayResModel.push_back(buildResourceModel(propertyItem));\r
+\r
+                std::vector<SimulatorResourceModel> arrayResModel;\r
+                SimulatorResourceModel arrayItem;\r
+                std::vector<RAML::Properties> arrayProperty = boost::get<std::vector<RAML::Properties> >\r
+                        (propertyElement->getValue());\r
+                for (auto val : arrayProperty)\r
+                {\r
+                    arrayItem.add(buildAttribute(std::make_shared<RAML::Properties>(val)));\r
+                }\r
+                arrayResModel.push_back(arrayItem);\r
+                attribute.setValue(arrayResModel);\r
             }\r
-            itemModel.add(propName, arrayResModel);\r
-        }\r
-        else\r
-        {\r
-            itemModel.add(buildAttribute(propElement.second));\r
-        }\r
+            break;\r
     }\r
-    return itemModel;\r
+    return attribute;\r
 }\r
 \r
 RAML::RequestResponseBodyPtr SimulatorResourceFactory::getRAMLResponseBody(\r
@@ -261,25 +307,27 @@ SimulatorResourceModel SimulatorResourceFactory::buildModelFromResponseBody(
         // Resource type\r
         if ("rt" == propName || "resourceType" == propName)\r
         {\r
-            resourceType = propertyElement.second->getValueString();\r
+            resourceType = boost::get<std::string>(propertyElement.second->getValue());\r
             continue;\r
         }\r
 \r
+        // TODO: Is "if" required to be part of resource representation?\r
         // Interface type\r
         if ("if" == propName)\r
         {\r
-            if ("string" == propertyElement.second->getType())\r
+            if (RAML::VariantType::STRING == propertyElement.second->getType().type())\r
             {\r
-                interfaceType.push_back(propertyElement.second->getValueString());\r
+                interfaceType.push_back(boost::get<std::string>(propertyElement.second->getValue()));\r
             }\r
-            else if ("array" == propertyElement.second->getType())\r
+            else if (RAML::VariantType::ARRAY == propertyElement.second->getType().type())\r
             {\r
-                for (auto &item : propertyElement.second->getItems())\r
+                RAML::Properties ifProperty = boost::get<RAML::Properties>(propertyElement.second->getValue());\r
+                for (auto vp : ifProperty.getValueProperties())\r
                 {\r
-                    if ("string" == item->getType())\r
+                    if (vp->type() == RAML::ValueProperty::Type::VALUE_SET)\r
                     {\r
-                        interfaceType = item->getAllowedValuesString();\r
-                        break;\r
+                        for (auto allow : vp->valueSet())\r
+                            interfaceType.push_back(boost::get<std::string>(allow));\r
                     }\r
                 }\r
             }\r
@@ -292,30 +340,7 @@ SimulatorResourceModel SimulatorResourceFactory::buildModelFromResponseBody(
             continue;\r
         }\r
 \r
-        // Add the attribute to resource model\r
-        if ("array" == propertyElement.second->getType())\r
-        {\r
-            std::vector<SimulatorResourceModel> arrayResModel;\r
-            for ( auto &propertyItem : propertyElement.second->getItems())\r
-            {\r
-                arrayResModel.push_back(buildResourceModel(propertyItem));\r
-            }\r
-            resModel.add(propName, arrayResModel);\r
-        }\r
-        else\r
-        {\r
-            resModel.add(buildAttribute(propertyElement.second));\r
-        }\r
-    }\r
-\r
-    if ("array" == resourceProperties->getType())\r
-    {\r
-        std::vector<SimulatorResourceModel> arrayResModel;\r
-        for ( auto &propertyItem : resourceProperties->getItems())\r
-        {\r
-            arrayResModel.push_back(buildResourceModel(propertyItem));\r
-        }\r
-        resModel.add("links", arrayResModel);\r
+        resModel.add(buildAttribute(propertyElement.second));\r
     }\r
 \r
     return resModel;\r