1 /******************************************************************
3 * Copyright 2015 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 "request_model_builder.h"
24 #define TAG "REQ_MODEL_BUILDER"
26 RequestModelBuilder::RequestModelBuilder(std::shared_ptr<RAML::Raml> &raml)
29 std::map<RequestType, RequestModelSP> RequestModelBuilder::build(const std::string &uri)
31 std::map<RequestType, RequestModelSP> modelList;
37 for (auto & resource : m_raml->getResources())
39 // Pick the resource based on the resource uri.
40 if (std::string::npos == uri.find((resource.second)->getResourceUri()))
43 // Construcut Request and Response Model from RAML::Action
44 for (auto & action : (resource.second)->getActions())
46 RequestModelSP requestModel = createRequestModel(action.second);
48 modelList[requestModel->type()] = requestModel;
55 RequestModelSP RequestModelBuilder::createRequestModel(const RAML::ActionPtr &action)
57 OC_LOG(DEBUG, TAG, "Creating request model");
59 // Validate the action type. Only GET, PUT, POST and DELETE are supported.
60 RAML::ActionType actionType = action->getType();
61 if (actionType != RAML::ActionType::GET
62 && actionType != RAML::ActionType::PUT
63 && actionType != RAML::ActionType::POST
64 && actionType != RAML::ActionType::DELETE)
66 OC_LOG(ERROR, TAG, "Failed to create request model as it is of unknown type!");
70 // Construct RequestModel
71 RequestModelSP requestModel(new RequestModel(getRequestType(actionType)));
73 // Get the allowed query parameters of the request
74 for (auto & qpEntry : action->getQueryParameters())
76 for (auto & value : (qpEntry.second)->getEnumeration())
78 requestModel->addQueryParam(qpEntry.first, value);
82 RAML::RequestResponseBodyPtr requestBody = action->getRequestBody("application/json");
83 SimulatorResourceModelSP repSchema = createRepSchema(requestBody);
84 requestModel->setRepSchema(repSchema);
86 // Corresponsing responses
87 for (auto & responseEntry : action->getResponses())
89 std::string codeStr = responseEntry.first;
90 int code = boost::lexical_cast<int>(codeStr);
91 ResponseModelSP responseModel = createResponseModel(code, responseEntry.second);
92 if (nullptr != responseModel)
94 requestModel->addResponseModel(code, responseModel);
101 ResponseModelSP RequestModelBuilder::createResponseModel(int code,
102 const RAML::ResponsePtr &response)
104 ResponseModelSP responseModel(new ResponseModel(code));
105 RAML::RequestResponseBodyPtr responseBody = response->getResponseBody("application/json");
106 SimulatorResourceModelSP repSchema = createRepSchema(responseBody);
107 responseModel->setRepSchema(repSchema);
108 return responseModel;
111 SimulatorResourceModelSP RequestModelBuilder::createRepSchema(const RAML::RequestResponseBodyPtr
118 RAML::SchemaPtr schema = rep->getSchema();
124 RAML::JsonSchemaPtr properties = schema->getProperties();
125 if (!properties || 0 == properties->getProperties().size())
128 SimulatorResourceModelSP repSchema = std::make_shared<SimulatorResourceModel>();
129 for (auto & propertyEntry : properties->getProperties())
131 std::string propName = propertyEntry.second->getName();
132 if ("rt" == propName || "resourceType" == propName || "if" == propName
133 || "p" == propName || "n" == propName || "id" == propName)
136 int valueType = propertyEntry.second->getValueType();
141 // Add the attribute with value
142 repSchema->addAttribute(propertyEntry.second->getName(), propertyEntry.second->getValue<int>());
145 int min, max, multipleof;
146 propertyEntry.second->getRange(min, max, multipleof);
147 repSchema->setRange(propertyEntry.second->getName(), min, max);
153 // Add the attribute with value
154 repSchema->addAttribute(propertyEntry.second->getName(), propertyEntry.second->getValue<double>());
156 std::vector<SimulatorResourceModel::Attribute::ValueVariant> propValues =
157 propertyEntry.second->getAllowedValues();
159 // TODO: Use RAML function once available
160 if (0 < propertyEntry.second->getAllowedValuesSize())
162 std::vector<double> allowedValues;
163 for (auto & propValue : propValues)
165 double value = boost::lexical_cast<double> (propValue);
166 allowedValues.push_back(value);
168 repSchema->setAllowedValues(propertyEntry.second->getName(), allowedValues);
175 // Add the attribute with value
176 repSchema->addAttribute(propertyEntry.second->getName(), propertyEntry.second->getValue<bool>());
182 // Add the attribute with value
183 repSchema->addAttribute(propertyEntry.second->getName(),
184 propertyEntry.second->getValue<std::string>());
186 std::vector<SimulatorResourceModel::Attribute::ValueVariant> propValues =
187 propertyEntry.second->getAllowedValues();
189 // TODO: Use RAML function once available
190 if (0 < propertyEntry.second->getAllowedValuesSize())
192 std::vector<std::string> allowedValues;
193 for (auto & propValue : propValues)
195 std::string value = boost::lexical_cast<std::string> (propValue);
196 allowedValues.push_back(value);
198 repSchema->setAllowedValues(propertyEntry.second->getName(), allowedValues);
208 RequestType RequestModelBuilder::getRequestType(RAML::ActionType actionType)
212 case RAML::ActionType::GET:
213 return RequestType::RQ_TYPE_GET;
214 case RAML::ActionType::PUT:
215 return RequestType::RQ_TYPE_PUT;
216 case RAML::ActionType::POST:
217 return RequestType::RQ_TYPE_POST;
218 case RAML::ActionType::DELETE:
219 return RequestType::RQ_TYPE_DELETE;