X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fsimulator%2Fsrc%2Fcommon%2Frequest_model_builder.cpp;h=ff62586bca84c656f013f397d791425d66602a7e;hb=390866079e285d2c74918432c0d597d5da52f8a0;hp=9a2002196d5a44baa7408e587b17449c4b3aa62a;hpb=3e9402ad71cb3e93266a77796f44d17bab9853fd;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/simulator/src/common/request_model_builder.cpp b/service/simulator/src/common/request_model_builder.cpp old mode 100644 new mode 100755 index 9a20021..ff62586 --- a/service/simulator/src/common/request_model_builder.cpp +++ b/service/simulator/src/common/request_model_builder.cpp @@ -19,43 +19,88 @@ ******************************************************************/ #include "request_model_builder.h" +#include "resource_model_schema_builder.h" #include "logger.h" +#include "Raml.h" #define TAG "REQ_MODEL_BUILDER" -RequestModelBuilder::RequestModelBuilder(std::shared_ptr &raml) - : m_raml (raml) {} +static std::string getRequestType(RAML::ActionType actionType) +{ + switch (actionType) + { + case RAML::ActionType::GET: + return "GET"; + case RAML::ActionType::PUT: + return "PUT"; + case RAML::ActionType::POST: + return "POST"; + case RAML::ActionType::DELETE: + return "DELETE"; + } + + return ""; // This code should never reach +} -std::map RequestModelBuilder::build(const std::string &uri) +std::unordered_map RequestModelBuilder::build( + const std::shared_ptr &raml, const std::string &uri) { - std::map modelList; - if (!m_raml) + std::unordered_map requestModels; + + if (!raml) { - return modelList; + OIC_LOG(ERROR, TAG, "Raml pointer is null!"); + return requestModels; } - for (auto & resource : m_raml->getResources()) + for (auto &resource : raml->getResources()) { // Pick the resource based on the resource uri. if (std::string::npos == uri.find((resource.second)->getResourceUri())) continue; - // Construcut Request and Response Model from RAML::Action - for (auto & action : (resource.second)->getActions()) + // Construct Request and Response Model from RAML::Action + for (auto &action : (resource.second)->getActions()) { RequestModelSP requestModel = createRequestModel(action.second); if (requestModel) - modelList[requestModel->type()] = requestModel; + { + requestModels[requestModel->getType()] = requestModel; + } } + + break; } - return modelList; + return requestModels; } -RequestModelSP RequestModelBuilder::createRequestModel(const RAML::ActionPtr &action) +std::unordered_map RequestModelBuilder::build( + const std::shared_ptr &resource) { - OC_LOG(DEBUG, TAG, "Creating request model"); + std::unordered_map requestModels; + + if (!resource) + { + OIC_LOG(ERROR, TAG, "Resource pointer is null!"); + return requestModels; + } + + // Construct Request and Response Model from RAML::Action + for (auto &action : resource->getActions()) + { + RequestModelSP requestModel = createRequestModel(action.second); + if (requestModel) + { + requestModels[requestModel->getType()] = requestModel; + } + } + return requestModels; +} + +RequestModelSP RequestModelBuilder::createRequestModel(const RAML::ActionPtr &action) +{ // Validate the action type. Only GET, PUT, POST and DELETE are supported. RAML::ActionType actionType = action->getType(); if (actionType != RAML::ActionType::GET @@ -63,35 +108,34 @@ RequestModelSP RequestModelBuilder::createRequestModel(const RAML::ActionPtr &ac && actionType != RAML::ActionType::POST && actionType != RAML::ActionType::DELETE) { - OC_LOG(ERROR, TAG, "Failed to create request model as it is of unknown type!"); + OIC_LOG(ERROR, TAG, "Request model is of unknown type!"); return nullptr; } - // Construct RequestModel RequestModelSP requestModel(new RequestModel(getRequestType(actionType))); // Get the allowed query parameters of the request - for (auto & qpEntry : action->getQueryParameters()) + for (auto &qpEntry : action->getQueryParameters()) { - for (auto & value : (qpEntry.second)->getEnumeration()) + for (auto &value : (qpEntry.second)->getEnumeration()) { requestModel->addQueryParam(qpEntry.first, value); } } + // Set the request body schema RAML::RequestResponseBodyPtr requestBody = action->getRequestBody("application/json"); - SimulatorResourceModelSP repSchema = createRepSchema(requestBody); - requestModel->setRepSchema(repSchema); + requestModel->setRequestBodyModel(createRepSchema(requestBody)); - // Corresponsing responses - for (auto & responseEntry : action->getResponses()) + // Corresponsing responses for this request + for (auto &responseEntry : action->getResponses()) { std::string codeStr = responseEntry.first; int code = boost::lexical_cast(codeStr); ResponseModelSP responseModel = createResponseModel(code, responseEntry.second); if (nullptr != responseModel) { - requestModel->addResponseModel(code, responseModel); + requestModel->setResponseModel(code, responseModel); } } @@ -103,120 +147,19 @@ ResponseModelSP RequestModelBuilder::createResponseModel(int code, { ResponseModelSP responseModel(new ResponseModel(code)); RAML::RequestResponseBodyPtr responseBody = response->getResponseBody("application/json"); - SimulatorResourceModelSP repSchema = createRepSchema(responseBody); - responseModel->setRepSchema(repSchema); + responseModel->setResponseBodyModel(createRepSchema(responseBody)); return responseModel; } -SimulatorResourceModelSP RequestModelBuilder::createRepSchema(const RAML::RequestResponseBodyPtr - &rep) +std::shared_ptr RequestModelBuilder::createRepSchema( + const RAML::RequestResponseBodyPtr &responseBody) { - if (!rep) - { - return nullptr; - } - RAML::SchemaPtr schema = rep->getSchema(); - if (!schema) + if (!responseBody) { + OIC_LOG(ERROR, TAG, "Response body is null!"); return nullptr; } - RAML::JsonSchemaPtr properties = schema->getProperties(); - if (!properties || 0 == properties->getProperties().size()) - return nullptr; - - SimulatorResourceModelSP repSchema = std::make_shared(); - for (auto & propertyEntry : properties->getProperties()) - { - std::string propName = propertyEntry.second->getName(); - if ("rt" == propName || "resourceType" == propName || "if" == propName - || "p" == propName || "n" == propName || "id" == propName) - continue; - - int valueType = propertyEntry.second->getValueType(); - switch (valueType) - { - case 0: // Integer - { - // Add the attribute with value - repSchema->addAttribute(propertyEntry.second->getName(), propertyEntry.second->getValue()); - - // Set the range - int min, max, multipleof; - propertyEntry.second->getRange(min, max, multipleof); - repSchema->setRange(propertyEntry.second->getName(), min, max); - } - break; - - case 1: // Double - { - // Add the attribute with value - repSchema->addAttribute(propertyEntry.second->getName(), propertyEntry.second->getValue()); - - std::vector propValues = - propertyEntry.second->getAllowedValues(); - - // TODO: Use RAML function once available - if (0 < propertyEntry.second->getAllowedValuesSize()) - { - std::vector allowedValues; - for (auto & propValue : propValues) - { - double value = boost::lexical_cast (propValue); - allowedValues.push_back(value); - } - repSchema->setAllowedValues(propertyEntry.second->getName(), allowedValues); - } - } - break; - - case 2: // Boolean - { - // Add the attribute with value - repSchema->addAttribute(propertyEntry.second->getName(), propertyEntry.second->getValue()); - } - break; - - case 3: // String - { - // Add the attribute with value - repSchema->addAttribute(propertyEntry.second->getName(), - propertyEntry.second->getValue()); - - std::vector propValues = - propertyEntry.second->getAllowedValues(); - - // TODO: Use RAML function once available - if (0 < propertyEntry.second->getAllowedValuesSize()) - { - std::vector allowedValues; - for (auto & propValue : propValues) - { - std::string value = boost::lexical_cast (propValue); - allowedValues.push_back(value); - } - repSchema->setAllowedValues(propertyEntry.second->getName(), allowedValues); - } - } - break; - } - } - - return repSchema; -} - -RequestType RequestModelBuilder::getRequestType(RAML::ActionType actionType) -{ - switch (actionType) - { - case RAML::ActionType::GET: - return RequestType::RQ_TYPE_GET; - case RAML::ActionType::PUT: - return RequestType::RQ_TYPE_PUT; - case RAML::ActionType::POST: - return RequestType::RQ_TYPE_POST; - case RAML::ActionType::DELETE: - return RequestType::RQ_TYPE_DELETE; - } + return ResourceModelSchemaBuilder(responseBody).build(); }