******************************************************************/
#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::Raml> &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<RequestType, RequestModelSP> RequestModelBuilder::build(const std::string &uri)
+std::unordered_map<std::string, RequestModelSP> RequestModelBuilder::build(
+ const std::shared_ptr<RAML::Raml> &raml, const std::string &uri)
{
- std::map<RequestType, RequestModelSP> modelList;
- if (!m_raml)
+ std::unordered_map<std::string, RequestModelSP> 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<std::string, RequestModelSP> RequestModelBuilder::build(
+ const std::shared_ptr<RAML::RamlResource> &resource)
{
- OC_LOG(DEBUG, TAG, "Creating request model");
+ std::unordered_map<std::string, RequestModelSP> 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
&& 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<int>(codeStr);
ResponseModelSP responseModel = createResponseModel(code, responseEntry.second);
if (nullptr != responseModel)
{
- requestModel->addResponseModel(code, responseModel);
+ requestModel->setResponseModel(code, responseModel);
}
}
{
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<SimulatorResourceModelSchema> 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<SimulatorResourceModel>();
- 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<int>());
-
- // 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<double>());
-
- std::vector<SimulatorResourceModel::Attribute::ValueVariant> propValues =
- propertyEntry.second->getAllowedValues();
-
- // TODO: Use RAML function once available
- if (0 < propertyEntry.second->getAllowedValuesSize())
- {
- std::vector<double> allowedValues;
- for (auto & propValue : propValues)
- {
- double value = boost::lexical_cast<double> (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<bool>());
- }
- break;
-
- case 3: // String
- {
- // Add the attribute with value
- repSchema->addAttribute(propertyEntry.second->getName(),
- propertyEntry.second->getValue<std::string>());
-
- std::vector<SimulatorResourceModel::Attribute::ValueVariant> propValues =
- propertyEntry.second->getAllowedValues();
-
- // TODO: Use RAML function once available
- if (0 < propertyEntry.second->getAllowedValuesSize())
- {
- std::vector<std::string> allowedValues;
- for (auto & propValue : propValues)
- {
- std::string value = boost::lexical_cast<std::string> (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();
}