ff62586bca84c656f013f397d791425d66602a7e
[platform/upstream/iotivity.git] / service / simulator / src / common / request_model_builder.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "request_model_builder.h"
22 #include "resource_model_schema_builder.h"
23 #include "logger.h"
24 #include "Raml.h"
25
26 #define TAG "REQ_MODEL_BUILDER"
27
28 static std::string getRequestType(RAML::ActionType actionType)
29 {
30     switch (actionType)
31     {
32         case RAML::ActionType::GET:
33             return "GET";
34         case RAML::ActionType::PUT:
35             return "PUT";
36         case RAML::ActionType::POST:
37             return "POST";
38         case RAML::ActionType::DELETE:
39             return "DELETE";
40     }
41
42     return ""; // This code should never reach
43 }
44
45 std::unordered_map<std::string, RequestModelSP> RequestModelBuilder::build(
46     const std::shared_ptr<RAML::Raml> &raml, const std::string &uri)
47 {
48     std::unordered_map<std::string, RequestModelSP> requestModels;
49
50     if (!raml)
51     {
52         OIC_LOG(ERROR, TAG, "Raml pointer is null!");
53         return requestModels;
54     }
55
56     for (auto &resource : raml->getResources())
57     {
58         // Pick the resource based on the resource uri.
59         if (std::string::npos == uri.find((resource.second)->getResourceUri()))
60             continue;
61
62         // Construct Request and Response Model from RAML::Action
63         for (auto &action :  (resource.second)->getActions())
64         {
65             RequestModelSP requestModel = createRequestModel(action.second);
66             if (requestModel)
67             {
68                 requestModels[requestModel->getType()] = requestModel;
69             }
70         }
71
72         break;
73     }
74
75     return requestModels;
76 }
77
78 std::unordered_map<std::string, RequestModelSP> RequestModelBuilder::build(
79     const std::shared_ptr<RAML::RamlResource> &resource)
80 {
81     std::unordered_map<std::string, RequestModelSP> requestModels;
82
83     if (!resource)
84     {
85         OIC_LOG(ERROR, TAG, "Resource pointer is null!");
86         return requestModels;
87     }
88
89     // Construct Request and Response Model from RAML::Action
90     for (auto &action :  resource->getActions())
91     {
92         RequestModelSP requestModel = createRequestModel(action.second);
93         if (requestModel)
94         {
95             requestModels[requestModel->getType()] = requestModel;
96         }
97     }
98
99     return requestModels;
100 }
101
102 RequestModelSP RequestModelBuilder::createRequestModel(const RAML::ActionPtr &action)
103 {
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)
110     {
111         OIC_LOG(ERROR, TAG, "Request model is of unknown type!");
112         return nullptr;
113     }
114
115     RequestModelSP requestModel(new RequestModel(getRequestType(actionType)));
116
117     // Get the allowed query parameters of the request
118     for (auto &qpEntry : action->getQueryParameters())
119     {
120         for (auto &value :  (qpEntry.second)->getEnumeration())
121         {
122             requestModel->addQueryParam(qpEntry.first, value);
123         }
124     }
125
126     // Set the request body schema
127     RAML::RequestResponseBodyPtr requestBody = action->getRequestBody("application/json");
128     requestModel->setRequestBodyModel(createRepSchema(requestBody));
129
130     // Corresponsing responses for this request
131     for (auto   &responseEntry :  action->getResponses())
132     {
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)
137         {
138             requestModel->setResponseModel(code, responseModel);
139         }
140     }
141
142     return requestModel;
143 }
144
145 ResponseModelSP RequestModelBuilder::createResponseModel(int code,
146         const RAML::ResponsePtr &response)
147 {
148     ResponseModelSP responseModel(new ResponseModel(code));
149     RAML::RequestResponseBodyPtr responseBody = response->getResponseBody("application/json");
150     responseModel->setResponseBodyModel(createRepSchema(responseBody));
151     return responseModel;
152 }
153
154 std::shared_ptr<SimulatorResourceModelSchema> RequestModelBuilder::createRepSchema(
155     const RAML::RequestResponseBodyPtr &responseBody)
156 {
157     if (!responseBody)
158     {
159         OIC_LOG(ERROR, TAG, "Response body is null!");
160         return nullptr;
161     }
162
163     return ResourceModelSchemaBuilder(responseBody).build();
164 }
165