-/******************************************************************
- *
- * Copyright 2015 Samsung Electronics All Rights Reserved.
- *
- *
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************/
-
-#include "simulator_resource_factory.h"
-#include "simulator_single_resource_impl.h"
-#include "simulator_collection_resource_impl.h"
-#include "RamlParser.h"
-#include "simulator_logger.h"
-#include "logger.h"
-
-#define TAG "SIM_RESOURCE_FACTORY"
-
-SimulatorResourceFactory *SimulatorResourceFactory::getInstance()
-{
- static SimulatorResourceFactory s_instance;
- return &s_instance;
-}
-
-std::shared_ptr<SimulatorResource> SimulatorResourceFactory::createResource(
- const std::string &configPath)
-{
- // Parse the RAML file
- std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(configPath);
- RAML::RamlPtr raml = ramlParser->getRamlPtr();
-
- // Get the first resource model from RAML
- RAML::RamlResourcePtr ramlResource;
- if (0 == raml->getResources().size()
- || nullptr == (ramlResource = raml->getResources().begin()->second))
- {
- OC_LOG(ERROR, TAG, "Zero resources detected from RAML!");
- return nullptr;
- }
-
- return buildResource(ramlResource);
-}
-
-std::vector<std::shared_ptr<SimulatorResource> > SimulatorResourceFactory::createResource(
- const std::string &configPath, unsigned int count)
-{
- std::vector<std::shared_ptr<SimulatorResource>> resources;
-
- // Parse the RAML file
- std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(configPath);
- RAML::RamlPtr raml = ramlParser->getRamlPtr();
-
- // Get the first resource model from RAML
- RAML::RamlResourcePtr ramlResource;
- if (0 == raml->getResources().size()
- || nullptr == (ramlResource = raml->getResources().begin()->second))
- {
- OC_LOG(ERROR, TAG, "Zero resources detected from RAML!");
- return resources;
- }
-
- while (count--)
- {
- std::shared_ptr<SimulatorResource> resource = buildResource(ramlResource);
- if (!resource)
- {
- OC_LOG(ERROR, TAG, "Failed to create resource!");
- return resources;
- }
-
- resources.push_back(resource);
- }
-
- return resources;
-}
-
-std::shared_ptr<SimulatorSingleResource> SimulatorResourceFactory::createSingleResource(
- const std::string &name, const std::string &uri, const std::string &resourceType)
-{
- SimulatorSingleResourceImpl *simpleResource = new SimulatorSingleResourceImpl();
- simpleResource->setName(name);
- simpleResource->setURI(uri);
- simpleResource->setResourceType(resourceType);
- return std::shared_ptr<SimulatorSingleResource>(simpleResource);
-}
-
-std::shared_ptr<SimulatorCollectionResource> SimulatorResourceFactory::createCollectionResource(
- const std::string &name, const std::string &uri, const std::string &resourceType)
-{
- SimulatorCollectionResourceImpl *collectionResource = new SimulatorCollectionResourceImpl();
- collectionResource->setName(name);
- collectionResource->setURI(uri);
- collectionResource->setResourceType(resourceType);
- return std::shared_ptr<SimulatorCollectionResource>(collectionResource);
-}
-
-std::shared_ptr<SimulatorResource> SimulatorResourceFactory::buildResource(
- std::shared_ptr<RAML::RamlResource> ramlResource)
-{
- std::string name;
- std::string uri;
- std::string resourceType;
- std::string interfaceType;
-
- name = ramlResource->getDisplayName();
- uri = ramlResource->getResourceUri();
-
- // Get the resource representation schema from GET response body
- RAML::ActionPtr action = ramlResource->getAction(RAML::ActionType::GET);
- if (!action)
- {
- OC_LOG(ERROR, TAG, "Resource does not possess the GET request!");
- return nullptr;
- }
-
- RAML::ResponsePtr getResponse = action->getResponse("200");
- if (!getResponse)
- {
- OC_LOG(ERROR, TAG, "Resource does not provide valid GET response!");
- return nullptr;
- }
-
- RAML::RequestResponseBodyPtr responseBody = getResponse->getResponseBody("application/json");
- if (!responseBody)
- {
- OC_LOG(ERROR, TAG, "GET response is not of type \"application/json\" ");
- return nullptr;
- }
-
- // Iterate throgh all resource property and extract information needed for simulating resource
- RAML::JsonSchemaPtr resourceProperties = responseBody->getSchema()->getProperties();
- SimulatorResourceModel resModel;
- for ( auto &propertyElement : resourceProperties->getProperties())
- {
- if (!propertyElement.second)
- continue;
-
- std::string propName = propertyElement.second->getName();
-
- // Resource type
- if ("rt" == propName || "resourceType" == propName)
- {
- resourceType = propertyElement.second->getValueString();
- continue;
- }
-
- // Interface type
- if ("if" == propName)
- {
- interfaceType = propertyElement.second->getValueString();
- continue;
- }
-
- // Other Standard properties which should not be part of resource model
- if ("p" == propName || "n" == propName || "id" == propName)
- {
- continue;
- }
-
- // Build representation attribute
- SimulatorResourceModel::Attribute attribute(propName);
- switch (propertyElement.second->getValueType())
- {
- case 0: // Integer
- {
- attribute.setValue(propertyElement.second->getValue<int>());
-
- // Convert suppoted values
- std::vector<int> allowedValues = propertyElement.second->getAllowedValuesInt();
- if (allowedValues.size() > 0)
- {
- SimulatorResourceModel::AttributeProperty attrProp(allowedValues);
- attribute.setProperty(attrProp);
- }
- }
- break;
-
- case 1: // Double
- {
- attribute.setValue(propertyElement.second->getValue<double>());
-
- // Convert suppoted values
- std::vector<double> allowedValues = propertyElement.second->getAllowedValuesDouble();
- if (allowedValues.size() > 0)
- {
- SimulatorResourceModel::AttributeProperty attrProp(allowedValues);
- attribute.setProperty(attrProp);
- }
- }
- break;
-
- case 2: // Boolean
- {
- attribute.setValue(propertyElement.second->getValue<bool>());
-
- std::vector<bool> allowedValues = {true, false};
- SimulatorResourceModel::AttributeProperty attrProp(allowedValues);
- attribute.setProperty(attrProp);
- }
- break;
-
- case 3: // String
- {
- attribute.setValue(propertyElement.second->getValue<std::string>());
-
- // Convert suppoted values
- std::vector<std::string> allowedValues = propertyElement.second->getAllowedValuesString();
- if (allowedValues.size() > 0)
- {
- SimulatorResourceModel::AttributeProperty attrProp(allowedValues);
- attribute.setProperty(attrProp);
- }
- }
- break;
- }
-
- // Set the range property if its present
- double min, max;
- int multipleof;
- propertyElement.second->getRange(min, max, multipleof);
- if (min != INT_MIN && max != INT_MAX)
- {
- SimulatorResourceModel::AttributeProperty attrProp(min, max);
- attribute.setProperty(attrProp);
- }
-
- // Add the attribute to resource model
- resModel.add(attribute);
- }
-
- // Create simple/collection resource
- std::shared_ptr<SimulatorResource> simResource;
- if (interfaceType == "oic.if.ll"
- || resModel.containsAttribute("links") || resModel.containsAttribute("rts"))
- {
- try
- {
- std::shared_ptr<SimulatorCollectionResourceImpl> collectionRes(
- new SimulatorCollectionResourceImpl());
-
- collectionRes->setName(name);
- collectionRes->setResourceType(resourceType);
- if (ResourceURIFactory::getInstance()->isUnique(uri))
- collectionRes->setURI(uri);
- else
- collectionRes->setURI(ResourceURIFactory::getInstance()->constructURI(uri));
-
- simResource = std::dynamic_pointer_cast<SimulatorResource>(collectionRes);
- }
- catch (InvalidArgsException &e) {}
- }
- else
- {
- try
- {
- std::shared_ptr<SimulatorSingleResourceImpl> singleRes(
- new SimulatorSingleResourceImpl());
-
- singleRes->setName(name);
- singleRes->setResourceType(resourceType);
- if (ResourceURIFactory::getInstance()->isUnique(uri))
- singleRes->setURI(uri);
- else
- singleRes->setURI(ResourceURIFactory::getInstance()->constructURI(uri));
-
- singleRes->setResourceModel(resModel);
- simResource = std::dynamic_pointer_cast<SimulatorResource>(singleRes);
- }
- catch (InvalidArgsException &e) {}
- }
-
- return simResource;
-}
-
-ResourceURIFactory *ResourceURIFactory::getInstance()
-{
- static ResourceURIFactory s_instance;
- return &s_instance;
-}
-
-ResourceURIFactory::ResourceURIFactory()
- : m_id(0) {}
-
-std::string ResourceURIFactory::constructURI(const std::string &uri)
-{
- std::ostringstream os;
- os << uri;
- if (!uri.empty() && '/' != uri[uri.length() - 1])
- os << '/';
- os << m_id++;
- return os.str();
-}
-
-bool ResourceURIFactory::isUnique(const std::string &uri)
-{
- if (m_uriList.end() == m_uriList.find(uri))
- return true;
- else
- return false;
-}
-
+/******************************************************************\r
+ *\r
+ * Copyright 2015 Samsung Electronics All Rights Reserved.\r
+ *\r
+ *\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ ******************************************************************/\r
+\r
+#include "simulator_resource_factory.h"\r
+#include "simulator_single_resource_impl.h"\r
+#include "simulator_collection_resource_impl.h"\r
+#include "RamlParser.h"\r
+#include "simulator_logger.h"\r
+#include "logger.h"\r
+\r
+#define TAG "SIM_RESOURCE_FACTORY"\r
+\r
+SimulatorResourceFactory *SimulatorResourceFactory::getInstance()\r
+{\r
+ static SimulatorResourceFactory s_instance;\r
+ return &s_instance;\r
+}\r
+\r
+std::shared_ptr<SimulatorResource> SimulatorResourceFactory::createResource(\r
+ const std::string &configPath)\r
+{\r
+ // Parse the RAML file\r
+ std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(configPath);\r
+ RAML::RamlPtr raml = ramlParser->getRamlPtr();\r
+\r
+ // Get the first resource model from RAML\r
+ RAML::RamlResourcePtr ramlResource;\r
+ if (0 == raml->getResources().size()\r
+ || nullptr == (ramlResource = raml->getResources().begin()->second))\r
+ {\r
+ OC_LOG(ERROR, TAG, "Zero resources detected from RAML!");\r
+ return nullptr;\r
+ }\r
+\r
+ return buildResource(ramlResource);\r
+}\r
+\r
+std::vector<std::shared_ptr<SimulatorResource> > SimulatorResourceFactory::createResource(\r
+ const std::string &configPath, unsigned int count)\r
+{\r
+ std::vector<std::shared_ptr<SimulatorResource>> resources;\r
+\r
+ // Parse the RAML file\r
+ std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(configPath);\r
+ RAML::RamlPtr raml = ramlParser->getRamlPtr();\r
+\r
+ // Get the first resource model from RAML\r
+ RAML::RamlResourcePtr ramlResource;\r
+ if (0 == raml->getResources().size()\r
+ || nullptr == (ramlResource = raml->getResources().begin()->second))\r
+ {\r
+ OC_LOG(ERROR, TAG, "Zero resources detected from RAML!");\r
+ return resources;\r
+ }\r
+\r
+ while (count--)\r
+ {\r
+ std::shared_ptr<SimulatorResource> resource = buildResource(ramlResource);\r
+ if (!resource)\r
+ {\r
+ OC_LOG(ERROR, TAG, "Failed to create resource!");\r
+ return resources;\r
+ }\r
+\r
+ resources.push_back(resource);\r
+ }\r
+\r
+ return resources;\r
+}\r
+\r
+std::shared_ptr<SimulatorSingleResource> SimulatorResourceFactory::createSingleResource(\r
+ const std::string &name, const std::string &uri, const std::string &resourceType)\r
+{\r
+ SimulatorSingleResourceImpl *simpleResource = new SimulatorSingleResourceImpl();\r
+ simpleResource->setName(name);\r
+ simpleResource->setURI(uri);\r
+ simpleResource->setResourceType(resourceType);\r
+ return std::shared_ptr<SimulatorSingleResource>(simpleResource);\r
+}\r
+\r
+std::shared_ptr<SimulatorCollectionResource> SimulatorResourceFactory::createCollectionResource(\r
+ const std::string &name, const std::string &uri, const std::string &resourceType)\r
+{\r
+ SimulatorCollectionResourceImpl *collectionResource = new SimulatorCollectionResourceImpl();\r
+ collectionResource->setName(name);\r
+ collectionResource->setURI(uri);\r
+ collectionResource->setResourceType(resourceType);\r
+ return std::shared_ptr<SimulatorCollectionResource>(collectionResource);\r
+}\r
+\r
+SimulatorResourceModel::Attribute SimulatorResourceFactory::buildAttribute(\r
+ std::shared_ptr<RAML::Properties> propertyElement)\r
+{\r
+ std::string propName = propertyElement->getName();\r
+\r
+ // Build representation attribute\r
+ SimulatorResourceModel::Attribute attribute(propName);\r
+ switch (propertyElement->getVariantType())\r
+ {\r
+ case RAML::VariantType::INT:\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
+ }\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
+ }\r
+ break;\r
+\r
+ case RAML::VariantType::BOOL:\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
+ }\r
+ break;\r
+\r
+ case RAML::VariantType::STRING:\r
+ {\r
+ attribute.setValue(propertyElement->getValue<std::string>());\r
+\r
+ // Convert suppoted values\r
+ std::vector<std::string> allowedValues = propertyElement->getAllowedValuesString();\r
+ if (allowedValues.size() > 0)\r
+ {\r
+ SimulatorResourceModel::AttributeProperty attrProp(allowedValues);\r
+ attribute.setProperty(attrProp);\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 ("rt" == propName || "resourceType" == propName || "if" == propName ||\r
+ "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
+ {\r
+ arrayResModel.push_back(buildResourceModel(propertyItem));\r
+ }\r
+ itemModel.add(propName, arrayResModel);\r
+ }\r
+ else\r
+ {\r
+ itemModel.add(buildAttribute(propElement.second));\r
+ }\r
+ }\r
+ return itemModel;\r
+}\r
+\r
+std::shared_ptr<SimulatorResource> SimulatorResourceFactory::buildResource(\r
+ std::shared_ptr<RAML::RamlResource> ramlResource)\r
+{\r
+ std::string name;\r
+ std::string uri;\r
+ std::string resourceType;\r
+ std::vector<std::string> interfaceType;\r
+\r
+ name = ramlResource->getDisplayName();\r
+ uri = ramlResource->getResourceUri();\r
+\r
+ // Get the resource representation schema from GET response body\r
+ RAML::ActionPtr action = ramlResource->getAction(RAML::ActionType::GET);\r
+ if (!action)\r
+ {\r
+ OC_LOG(ERROR, TAG, "Resource does not possess the GET request!");\r
+ return nullptr;\r
+ }\r
+\r
+ RAML::ResponsePtr getResponse = action->getResponse("200");\r
+ if (!getResponse)\r
+ {\r
+ OC_LOG(ERROR, TAG, "Resource does not provide valid GET response!");\r
+ return nullptr;\r
+ }\r
+\r
+ RAML::RequestResponseBodyPtr responseBody = getResponse->getResponseBody("application/json");\r
+ if (!responseBody)\r
+ {\r
+ OC_LOG(ERROR, TAG, "GET response is not of type \"application/json\" ");\r
+ return nullptr;\r
+ }\r
+\r
+ // Iterate throgh all resource property and extract information needed for simulating resource\r
+ RAML::JsonSchemaPtr resourceProperties = responseBody->getSchema()->getProperties();\r
+ SimulatorResourceModel resModel;\r
+ for ( auto &propertyElement : resourceProperties->getProperties())\r
+ {\r
+ if (!propertyElement.second)\r
+ continue;\r
+\r
+ std::string propName = propertyElement.second->getName();\r
+\r
+ // Resource type\r
+ if ("rt" == propName || "resourceType" == propName)\r
+ {\r
+ resourceType = propertyElement.second->getValueString();\r
+ continue;\r
+ }\r
+\r
+ // Interface type\r
+ if ("if" == propName)\r
+ {\r
+ if ("string" == propertyElement.second->getType())\r
+ {\r
+ interfaceType.push_back(propertyElement.second->getValueString());\r
+ }\r
+ else if ("array" == propertyElement.second->getType())\r
+ {\r
+ for (auto &item : propertyElement.second->getItems())\r
+ {\r
+ if ("string" == item->getType())\r
+ {\r
+ interfaceType = item->getAllowedValuesString();\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ continue;\r
+ }\r
+\r
+ // Other Standard properties which should not be part of resource model\r
+ if ("p" == propName || "n" == propName || "id" == propName)\r
+ {\r
+ 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
+ }\r
+ // Create simple/collection resource\r
+ std::shared_ptr<SimulatorResource> simResource;\r
+ if (resModel.containsAttribute("links"))\r
+ {\r
+ try\r
+ {\r
+ std::shared_ptr<SimulatorCollectionResourceImpl> collectionRes(\r
+ new SimulatorCollectionResourceImpl());\r
+\r
+ collectionRes->setName(name);\r
+ collectionRes->setResourceType(resourceType);\r
+ collectionRes->setInterface(interfaceType);\r
+ if (ResourceURIFactory::getInstance()->isUnique(uri))\r
+ collectionRes->setURI(uri);\r
+ else\r
+ collectionRes->setURI(ResourceURIFactory::getInstance()->constructURI(uri));\r
+\r
+ collectionRes->setResourceModel(resModel);\r
+ simResource = std::dynamic_pointer_cast<SimulatorResource>(collectionRes);\r
+ }\r
+ catch (InvalidArgsException &e) {}\r
+ }\r
+ else\r
+ {\r
+ try\r
+ {\r
+ std::shared_ptr<SimulatorSingleResourceImpl> singleRes(\r
+ new SimulatorSingleResourceImpl());\r
+\r
+ singleRes->setName(name);\r
+ singleRes->setResourceType(resourceType);\r
+ singleRes->setInterface(interfaceType);\r
+ if (ResourceURIFactory::getInstance()->isUnique(uri))\r
+ singleRes->setURI(uri);\r
+ else\r
+ singleRes->setURI(ResourceURIFactory::getInstance()->constructURI(uri));\r
+\r
+ singleRes->setResourceModel(resModel);\r
+ simResource = std::dynamic_pointer_cast<SimulatorResource>(singleRes);\r
+ }\r
+ catch (InvalidArgsException &e) {}\r
+ }\r
+\r
+ return simResource;\r
+}\r
+\r
+ResourceURIFactory *ResourceURIFactory::getInstance()\r
+{\r
+ static ResourceURIFactory s_instance;\r
+ return &s_instance;\r
+}\r
+\r
+ResourceURIFactory::ResourceURIFactory()\r
+ : m_id(0) {}\r
+\r
+std::string ResourceURIFactory::constructURI(const std::string &uri)\r
+{\r
+ std::ostringstream os;\r
+ os << uri;\r
+ if (!uri.empty() && '/' != uri[uri.length() - 1])\r
+ os << '/';\r
+ os << m_id++;\r
+ return os.str();\r
+}\r
+\r
+bool ResourceURIFactory::isUnique(const std::string &uri)\r
+{\r
+ if (m_uriList.end() == m_uriList.find(uri))\r
+ return true;\r
+ else\r
+ return false;\r
+}\r
+\r