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"
22 #include "resource_model_schema_builder.h"
26 #define TAG "REQ_MODEL_BUILDER"
28 static std::string getRequestType(RAML::ActionType actionType)
32 case RAML::ActionType::GET:
34 case RAML::ActionType::PUT:
36 case RAML::ActionType::POST:
38 case RAML::ActionType::DELETE:
42 return ""; // This code should never reach
45 std::unordered_map<std::string, RequestModelSP> RequestModelBuilder::build(
46 const std::shared_ptr<RAML::Raml> &raml, const std::string &uri)
48 std::unordered_map<std::string, RequestModelSP> requestModels;
52 OIC_LOG(ERROR, TAG, "Raml pointer is null!");
56 for (auto &resource : raml->getResources())
58 // Pick the resource based on the resource uri.
59 if (std::string::npos == uri.find((resource.second)->getResourceUri()))
62 // Construct Request and Response Model from RAML::Action
63 for (auto &action : (resource.second)->getActions())
65 RequestModelSP requestModel = createRequestModel(action.second);
68 requestModels[requestModel->getType()] = requestModel;
78 std::unordered_map<std::string, RequestModelSP> RequestModelBuilder::build(
79 const std::shared_ptr<RAML::RamlResource> &resource)
81 std::unordered_map<std::string, RequestModelSP> requestModels;
85 OIC_LOG(ERROR, TAG, "Resource pointer is null!");
89 // Construct Request and Response Model from RAML::Action
90 for (auto &action : resource->getActions())
92 RequestModelSP requestModel = createRequestModel(action.second);
95 requestModels[requestModel->getType()] = requestModel;
102 RequestModelSP RequestModelBuilder::createRequestModel(const RAML::ActionPtr &action)
104 // Validate the action type. Only GET, PUT, POST and DELETE are supported.
105 RAML::ActionType actionType = action->getType();
106 if (actionType != RAML::ActionType::GET
107 && actionType != RAML::ActionType::PUT
108 && actionType != RAML::ActionType::POST
109 && actionType != RAML::ActionType::DELETE)
111 OIC_LOG(ERROR, TAG, "Request model is of unknown type!");
115 RequestModelSP requestModel(new RequestModel(getRequestType(actionType)));
117 // Get the allowed query parameters of the request
118 for (auto &qpEntry : action->getQueryParameters())
120 for (auto &value : (qpEntry.second)->getEnumeration())
122 requestModel->addQueryParam(qpEntry.first, value);
126 // Set the request body schema
127 RAML::RequestResponseBodyPtr requestBody = action->getRequestBody("application/json");
128 requestModel->setRequestBodyModel(createRepSchema(requestBody));
130 // Corresponsing responses for this request
131 for (auto &responseEntry : action->getResponses())
133 std::string codeStr = responseEntry.first;
134 int code = boost::lexical_cast<int>(codeStr);
135 ResponseModelSP responseModel = createResponseModel(code, responseEntry.second);
136 if (nullptr != responseModel)
138 requestModel->setResponseModel(code, responseModel);
145 ResponseModelSP RequestModelBuilder::createResponseModel(int code,
146 const RAML::ResponsePtr &response)
148 ResponseModelSP responseModel(new ResponseModel(code));
149 RAML::RequestResponseBodyPtr responseBody = response->getResponseBody("application/json");
150 responseModel->setResponseBodyModel(createRepSchema(responseBody));
151 return responseModel;
154 std::shared_ptr<SimulatorResourceModelSchema> RequestModelBuilder::createRepSchema(
155 const RAML::RequestResponseBodyPtr &responseBody)
159 OIC_LOG(ERROR, TAG, "Response body is null!");
163 return ResourceModelSchemaBuilder(responseBody).build();