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.db.request.beans.InfraActiveRequests;
54 import org.onap.so.exceptions.ValidationException;
55 import org.onap.so.logger.ErrorCode;
56 import org.onap.so.logger.MessageEnum;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.stereotype.Component;
61 import com.fasterxml.jackson.core.JsonProcessingException;
62 import com.fasterxml.jackson.databind.ObjectMapper;
63 import com.fasterxml.jackson.databind.SerializationFeature;
64 import io.swagger.annotations.ApiOperation;
69 public class ManualTasks {
70 private static Logger logger = LoggerFactory.getLogger(ManualTasks.class);
73 @org.springframework.beans.factory.annotation.Value("${mso.camunda.rest.task.uri}")
74 private String taskUri;
77 private RequestClientFactory reqClientFactory;
80 private MsoRequest msoRequest;
83 private ResponseBuilder builder;
86 @Path("/{version:[vV]1}/{taskId}/complete")
87 @Consumes(MediaType.APPLICATION_JSON)
88 @Produces(MediaType.APPLICATION_JSON)
89 @ApiOperation(value = "Complete specified task", response = Response.class)
91 public Response completeTask(String request, @PathParam("version") String version,
92 @PathParam("taskId") String taskId, @Context ContainerRequestContext requestContext) throws ApiException {
94 String requestId = requestContext.getProperty("requestId").toString();
95 logger.info(LoggingAnchor.TWO, MessageEnum.APIH_GENERATED_REQUEST_ID.toString(), requestId);
96 long startTime = System.currentTimeMillis();
97 logger.debug("requestId is: {}", requestId);
98 TasksRequest taskRequest = null;
99 String apiVersion = version.substring(1);
102 ObjectMapper mapper = new ObjectMapper();
103 taskRequest = mapper.readValue(request, TasksRequest.class);
105 if (taskRequest.getRequestDetails() == null) {
106 throw new ValidationException("requestDetails");
108 if (taskRequest.getRequestDetails().getRequestInfo() == null) {
109 throw new ValidationException("requestInfo");
111 if (empty(taskRequest.getRequestDetails().getRequestInfo().getSource())) {
112 throw new ValidationException("source");
114 if (empty(taskRequest.getRequestDetails().getRequestInfo().getRequestorId())) {
115 throw new ValidationException("requestorId");
118 } catch (IOException e) {
119 ErrorLoggerInfo errorLoggerInfo =
120 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
124 ValidateException validateException =
125 new ValidateException.Builder("Mapping of request to JSON object failed: " + e.getMessage(),
126 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
127 .errorInfo(errorLoggerInfo).build();
129 throw validateException;
130 } catch (ValidationException e) {
131 ErrorLoggerInfo errorLoggerInfo =
132 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
133 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
136 ValidateException validateException =
137 new ValidateException.Builder("Mapping of request to JSON Object failed. " + e.getMessage(),
138 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
140 throw validateException;
143 // Create Request Record
144 InfraActiveRequests currentActiveReq =
145 msoRequest.createRequestObject(taskRequest, Action.completeTask, requestId, Status.PENDING, request);
147 // Transform the request to Camunda-style Complete request
148 Variables variablesForComplete = new Variables();
149 Value sourceValue = new Value();
150 sourceValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getSource());
151 Value responseValue = new Value();
152 responseValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getResponseValue().name());
153 Value requestorIdValue = new Value();
154 requestorIdValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getRequestorId());
155 variablesForComplete.setSource(sourceValue);
156 variablesForComplete.setResponseValue(responseValue);
157 variablesForComplete.setRequestorId(requestorIdValue);
159 String camundaJsonReq = null;
161 ObjectMapper mapper = new ObjectMapper();
162 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
163 camundaJsonReq = mapper.writeValueAsString(variablesForComplete);
164 } catch (JsonProcessingException e) {
166 ErrorLoggerInfo errorLoggerInfo =
167 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.UnknownError)
168 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
171 ValidateException validateException =
172 new ValidateException.Builder("Mapping of JSON object to Camunda request failed",
173 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR)
174 .errorInfo(errorLoggerInfo).build();
175 throw validateException;
178 RequestClient requestClient = null;
179 HttpResponse response = null;
180 long subStartTime = System.currentTimeMillis();
181 String requestUrl = taskUri + "/" + taskId + "/complete";
183 requestClient = reqClientFactory.getRequestClient(requestUrl);
184 // Capture audit event
186 response = requestClient.post(camundaJsonReq);
188 } catch (Exception e) {
190 ErrorLoggerInfo errorLoggerInfo =
191 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, ErrorCode.AvailabilityError)
192 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
196 BPMNFailureException bpmnFailureException =
197 new BPMNFailureException.Builder(String.valueOf(HttpStatus.SC_BAD_GATEWAY),
198 HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_NO_SERVER_RESOURCES).errorInfo(errorLoggerInfo)
201 throw bpmnFailureException;
204 if (response == null) {
205 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR,
206 ErrorCode.BusinessProcesssError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
209 BPMNFailureException bpmnFailureException =
210 new BPMNFailureException.Builder(String.valueOf(HttpStatus.SC_BAD_GATEWAY),
211 HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_NO_SERVER_RESOURCES).errorInfo(errorLoggerInfo)
214 throw bpmnFailureException;
218 ResponseHandler respHandler = new ResponseHandler(response, requestClient.getType());
219 int bpelStatus = respHandler.getStatus();
221 // BPEL accepted the request, the request is in progress
222 if (bpelStatus == HttpStatus.SC_NO_CONTENT || bpelStatus == HttpStatus.SC_ACCEPTED) {
223 logger.debug("Received good response from Camunda");
224 TaskRequestReference trr = new TaskRequestReference();
225 trr.setTaskId(taskId);
226 String completeResp = null;
228 ObjectMapper mapper = new ObjectMapper();
229 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
230 completeResp = mapper.writeValueAsString(trr);
231 } catch (JsonProcessingException e) {
233 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR,
234 ErrorCode.BusinessProcesssError).build();
237 ValidateException validateException =
238 new ValidateException.Builder("Request Failed due to bad response format", bpelStatus,
239 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
241 throw validateException;
243 logger.debug("Response to the caller: {}", completeResp);
244 logger.debug("End of the transaction, the final response is: {}", completeResp);
245 return builder.buildResponse(HttpStatus.SC_ACCEPTED, requestId, completeResp, apiVersion);
247 ErrorLoggerInfo errorLoggerInfo =
248 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.BusinessProcesssError)
252 BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(bpelStatus),
253 bpelStatus, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
255 throw bpmnFailureException;
261 private static boolean empty(String s) {
262 return (s == null || s.trim().isEmpty());