Fix for prevent tool reported defects.
[platform/upstream/iotivity.git] / service / simulator / src / client-controller / request_sender.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_sender.h"
22 #include "simulator_logger.h"
23 #include "simulator_utils.h"
24 #include "logger.h"
25
26 #define TAG "GET_REQUEST_SNDR"
27
28 RequestSender::RequestSender(RequestType type, std::shared_ptr<OC::OCResource> &ocResource)
29     :   m_type(type), m_ocResource(ocResource) {}
30
31 void RequestSender::sendRequest(const std::map<std::string, std::string> &queryParam,
32                                 ResponseCallback responseCb, bool verifyResponse)
33 {
34     sendRequest(std::string(), queryParam, nullptr, responseCb, verifyResponse);
35 }
36
37 void RequestSender::sendRequest(const std::string &interfaceType,
38                                 const std::map<std::string, std::string> &queryParam,
39                                 ResponseCallback responseCb, bool verifyResponse)
40 {
41     sendRequest(interfaceType, queryParam, nullptr, responseCb, verifyResponse);
42 }
43
44 void RequestSender::sendRequest(const std::map<std::string, std::string> &queryParam,
45                                 SimulatorResourceModelSP repModel,
46                                 ResponseCallback responseCb, bool verifyResponse)
47 {
48     sendRequest(std::string(), queryParam, repModel, responseCb, verifyResponse);
49 }
50
51 void RequestSender::sendRequest(const std::string &interfaceType,
52                                 const std::map<std::string, std::string> &queryParam,
53                                 SimulatorResourceModelSP repModel,
54                                 ResponseCallback responseCb, bool verifyResponse)
55 {
56     // Add query paramter "if" if interfaceType is not empty
57     OC::QueryParamsMap queryParamCpy(queryParam);
58     if (!interfaceType.empty())
59         queryParamCpy["if"] = interfaceType;
60
61     // Add the request into request list
62     RequestDetailSP requestDetail(new RequestDetail);
63     requestDetail->type = m_type;
64     requestDetail->queryParam = queryParamCpy;
65     requestDetail->body = repModel;
66     requestDetail->verifyResponse = verifyResponse;
67     requestDetail->responseCb = responseCb;
68
69     int requestId = m_requestList.add(requestDetail);
70
71     OCStackResult ocResult = send(queryParamCpy, repModel, std::bind(
72                                       &RequestSender::onResponseReceived, this,
73                                       std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, requestId));
74     if (OC_STACK_OK != ocResult)
75     {
76         OC_LOG_V(ERROR, TAG, "Sending request failed [errorcode: %d]", ocResult);
77         m_requestList.remove(requestId);
78         throw SimulatorException(static_cast<SimulatorResult>(ocResult), "Failed to send request!");
79     }
80 }
81
82 void RequestSender::setRequestModel(const RequestModelSP &requestModel)
83 {
84     m_requestModel = requestModel;
85 }
86
87 void RequestSender::onResponseReceived(const OC::HeaderOptions &headerOptions,
88                                        const OC::OCRepresentation &rep, const int errorCode, int requestId)
89 {
90     SIM_LOG(ILogger::INFO, "Response recieved..." << "\n" << getPayloadString(rep));
91
92     // Ignore the response recieved for invalid requests
93     RequestDetailSP request = m_requestList.remove(requestId);
94     if (!request)
95     {
96         return;
97     }
98
99     // Validate the response as per the schema given by RAML
100     ValidationStatus validationStatus {false, SIMULATOR_ERROR};
101     if (request->verifyResponse && m_requestModel
102         && !errorCode) // TODO: Validate responses other than "200"
103     {
104         validationStatus.errorCode = m_requestModel->validateResponse(200, rep);
105         validationStatus.isVerified = true;
106     }
107
108     SimulatorResourceModelSP repModel = SimulatorResourceModel::create(rep);
109     request->responseCb(static_cast<SimulatorResult>(errorCode), repModel);
110 }
111
112 GETRequestSender::GETRequestSender(std::shared_ptr<OC::OCResource> &ocResource)
113     :   RequestSender(RequestType::RQ_TYPE_GET, ocResource) {}
114
115 OCStackResult GETRequestSender::send(OC::QueryParamsMap &queryParams,
116                                      SimulatorResourceModelSP &repModel, OC::GetCallback callback)
117 {
118     SIM_LOG(ILogger::INFO, "Sending GET request..." << "\n" << getRequestString(queryParams));
119
120     return m_ocResource->get(queryParams, callback);
121 }
122
123 PUTRequestSender::PUTRequestSender(std::shared_ptr<OC::OCResource> &ocResource)
124     :   RequestSender(RequestType::RQ_TYPE_PUT, ocResource) {}
125
126 OCStackResult PUTRequestSender::send(OC::QueryParamsMap &queryParams,
127                                      SimulatorResourceModelSP &repModel, OC::GetCallback callback)
128 {
129     OC::OCRepresentation ocRep;
130     if (repModel)
131     {
132         ocRep = repModel->getOCRepresentation();
133     }
134
135     SIM_LOG(ILogger::INFO, "Sending PUT request..." << "\n" << getRequestString(queryParams, ocRep));
136     return m_ocResource->put(ocRep, queryParams, callback);
137 }
138
139 POSTRequestSender::POSTRequestSender(std::shared_ptr<OC::OCResource> &ocResource)
140     :   RequestSender(RequestType::RQ_TYPE_POST, ocResource) {}
141
142 OCStackResult POSTRequestSender::send(OC::QueryParamsMap &queryParams,
143                                       SimulatorResourceModelSP &repModel, OC::GetCallback callback)
144 {
145     OC::OCRepresentation ocRep;
146     if (repModel)
147         ocRep = repModel->getOCRepresentation();
148
149     SIM_LOG(ILogger::INFO, "Sending POST request..." << "\n" << getRequestString(queryParams, ocRep));
150     return m_ocResource->post(ocRep, queryParams, callback);
151 }