2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.so.apihandlerinfra;
26 import java.io.IOException;
27 import javax.transaction.Transactional;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.container.ContainerRequestContext;
34 import javax.ws.rs.core.Context;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import org.onap.so.logger.LoggingAnchor;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.HttpStatus;
40 import org.onap.so.apihandler.common.ErrorNumbers;
41 import org.onap.so.apihandler.common.RequestClient;
42 import org.onap.so.apihandler.common.RequestClientFactory;
43 import org.onap.so.apihandler.common.ResponseBuilder;
44 import org.onap.so.apihandler.common.ResponseHandler;
45 import org.onap.so.apihandlerinfra.exceptions.ApiException;
46 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
47 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
48 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
49 import org.onap.so.apihandlerinfra.tasksbeans.TaskRequestReference;
50 import org.onap.so.apihandlerinfra.tasksbeans.TasksRequest;
51 import org.onap.so.apihandlerinfra.tasksbeans.Value;
52 import org.onap.so.apihandlerinfra.tasksbeans.Variables;
53 import org.onap.so.exceptions.ValidationException;
54 import org.onap.so.logger.ErrorCode;
55 import org.onap.so.logger.MessageEnum;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.stereotype.Component;
60 import com.fasterxml.jackson.core.JsonProcessingException;
61 import com.fasterxml.jackson.databind.ObjectMapper;
62 import com.fasterxml.jackson.databind.SerializationFeature;
63 import io.swagger.annotations.ApiOperation;
68 public class ManualTasks {
69 private static Logger logger = LoggerFactory.getLogger(ManualTasks.class);
72 @org.springframework.beans.factory.annotation.Value("${mso.camunda.rest.task.uri}")
73 private String taskUri;
76 private RequestClientFactory reqClientFactory;
79 private MsoRequest msoRequest;
82 private ResponseBuilder builder;
85 @Path("/{version:[vV]1}/{taskId}/complete")
86 @Consumes(MediaType.APPLICATION_JSON)
87 @Produces(MediaType.APPLICATION_JSON)
88 @ApiOperation(value = "Complete specified task", response = Response.class)
90 public Response completeTask(String request, @PathParam("version") String version,
91 @PathParam("taskId") String taskId, @Context ContainerRequestContext requestContext) throws ApiException {
93 String requestId = requestContext.getProperty("requestId").toString();
94 logger.info(LoggingAnchor.TWO, MessageEnum.APIH_GENERATED_REQUEST_ID.toString(), requestId);
95 logger.debug("requestId is: {}", requestId);
96 TasksRequest taskRequest = null;
97 String apiVersion = version.substring(1);
100 ObjectMapper mapper = new ObjectMapper();
101 taskRequest = mapper.readValue(request, TasksRequest.class);
103 if (taskRequest.getRequestDetails() == null) {
104 throw new ValidationException("requestDetails");
106 if (taskRequest.getRequestDetails().getRequestInfo() == null) {
107 throw new ValidationException("requestInfo");
109 if (empty(taskRequest.getRequestDetails().getRequestInfo().getSource())) {
110 throw new ValidationException("source");
112 if (empty(taskRequest.getRequestDetails().getRequestInfo().getRequestorId())) {
113 throw new ValidationException("requestorId");
116 } catch (IOException e) {
117 ErrorLoggerInfo errorLoggerInfo =
118 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
122 ValidateException validateException =
123 new ValidateException.Builder("Mapping of request to JSON object failed: " + e.getMessage(),
124 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
125 .errorInfo(errorLoggerInfo).build();
127 throw validateException;
128 } catch (ValidationException e) {
129 ErrorLoggerInfo errorLoggerInfo =
130 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
131 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
134 ValidateException validateException =
135 new ValidateException.Builder("Mapping of request to JSON Object failed. " + e.getMessage(),
136 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
138 throw validateException;
142 // Transform the request to Camunda-style Complete request
143 Variables variablesForComplete = new Variables();
144 Value sourceValue = new Value();
145 sourceValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getSource());
146 Value responseValue = new Value();
147 responseValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getResponseValue().name());
148 Value requestorIdValue = new Value();
149 requestorIdValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getRequestorId());
150 variablesForComplete.setSource(sourceValue);
151 variablesForComplete.setResponseValue(responseValue);
152 variablesForComplete.setRequestorId(requestorIdValue);
154 String camundaJsonReq;
156 ObjectMapper mapper = new ObjectMapper();
157 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
158 camundaJsonReq = mapper.writeValueAsString(variablesForComplete);
159 } catch (JsonProcessingException e) {
161 ErrorLoggerInfo errorLoggerInfo =
162 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.UnknownError)
163 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
166 ValidateException validateException =
167 new ValidateException.Builder("Mapping of JSON object to Camunda request failed",
168 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR)
169 .errorInfo(errorLoggerInfo).build();
170 throw validateException;
173 RequestClient requestClient;
174 HttpResponse response;
175 String requestUrl = taskUri + "/" + taskId + "/complete";
177 requestClient = reqClientFactory.getRequestClient(requestUrl);
178 // Capture audit event
180 response = requestClient.post(camundaJsonReq);
182 } catch (Exception e) {
184 ErrorLoggerInfo errorLoggerInfo =
185 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, ErrorCode.AvailabilityError)
186 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
190 BPMNFailureException bpmnFailureException =
191 new BPMNFailureException.Builder(String.valueOf(HttpStatus.SC_BAD_GATEWAY),
192 HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_NO_SERVER_RESOURCES).errorInfo(errorLoggerInfo)
195 throw bpmnFailureException;
198 if (response == null) {
199 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR,
200 ErrorCode.BusinessProcesssError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
203 BPMNFailureException bpmnFailureException =
204 new BPMNFailureException.Builder(String.valueOf(HttpStatus.SC_BAD_GATEWAY),
205 HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_NO_SERVER_RESOURCES).errorInfo(errorLoggerInfo)
208 throw bpmnFailureException;
212 ResponseHandler respHandler = new ResponseHandler(response, requestClient.getType());
213 int bpelStatus = respHandler.getStatus();
215 // BPEL accepted the request, the request is in progress
216 if (bpelStatus == HttpStatus.SC_NO_CONTENT || bpelStatus == HttpStatus.SC_ACCEPTED) {
217 logger.debug("Received good response from Camunda");
218 TaskRequestReference trr = new TaskRequestReference();
219 trr.setTaskId(taskId);
220 String completeResp = null;
222 ObjectMapper mapper = new ObjectMapper();
223 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
224 completeResp = mapper.writeValueAsString(trr);
225 } catch (JsonProcessingException e) {
227 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR,
228 ErrorCode.BusinessProcesssError).build();
231 ValidateException validateException =
232 new ValidateException.Builder("Request Failed due to bad response format", bpelStatus,
233 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
235 throw validateException;
237 logger.debug("Response to the caller: {}", completeResp);
238 logger.debug("End of the transaction, the final response is: {}", completeResp);
239 return builder.buildResponse(HttpStatus.SC_ACCEPTED, requestId, completeResp, apiVersion);
241 ErrorLoggerInfo errorLoggerInfo =
242 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.BusinessProcesssError)
246 BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(bpelStatus),
247 bpelStatus, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
249 throw bpmnFailureException;
255 private static boolean empty(String s) {
256 return (s == null || s.trim().isEmpty());