/*- * #%L * MSO * %% * Copyright (C) 2016 ONAP - SO * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ package org.openecomp.mso.apihandlerinfra; import org.openecomp.mso.apihandlerinfra.tasksbeans.*; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.json.JSONArray; import org.json.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import org.openecomp.mso.apihandler.common.ErrorNumbers; import org.openecomp.mso.apihandler.common.RequestClient; import org.openecomp.mso.apihandler.common.RequestClientFactory; import org.openecomp.mso.apihandler.common.ResponseHandler; import org.openecomp.mso.logger.MessageEnum; import org.openecomp.mso.logger.MsoAlarmLogger; import org.openecomp.mso.logger.MsoLogger; import org.openecomp.mso.utils.UUIDChecker; import com.wordnik.swagger.annotations.Api; import com.wordnik.swagger.annotations.ApiOperation; @Path("/tasks") @Api(value="/tasks/{version: [vV]1}",description="Queries of Manual Tasks") public class TasksHandler { private static MsoAlarmLogger alarmLogger = new MsoAlarmLogger (); private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH); public final static String requestUrl = "mso/task/"; @Path("/{version:[vV]1}") @GET @ApiOperation(value="Finds Manual Tasks",response=Response.class) public Response queryFilters (@QueryParam("taskId") String taskId, @QueryParam("originalRequestId") String originalRequestId, @QueryParam("subscriptionServiceType") String subscriptionServiceType, @QueryParam("nfRole") String nfRole, @QueryParam("buildingBlockName") String buildingBlockName, @QueryParam("originalRequestDate") String originalRequestDate, @QueryParam("originalRequestorId") String originalRequestorId, @PathParam("version") String version) throws ParseException { Response responseBack = null; long startTime = System.currentTimeMillis (); String requestId = UUIDChecker.generateUUID(msoLogger); MsoLogger.setServiceName ("ManualTasksQuery"); // Generate a Request Id UUIDChecker.generateUUID(msoLogger); msoLogger.debug ("Incoming request received for queryFilter with taskId:" + taskId + " originalRequestId:" + originalRequestId + " subscriptionServiceType:" + subscriptionServiceType + " nfRole:" + nfRole + " buildingBlockName:" + buildingBlockName + " originalRequestDate:" + originalRequestDate + " originalRequestorId: " + originalRequestorId); // Prepare the query string to /task interface TaskVariables tv = new TaskVariables(); List tvvList = new ArrayList<>(); if (originalRequestId != null) { TaskVariableValue tvv = new TaskVariableValue(); tvv.setName("originalRequestId"); tvv.setValue(originalRequestId); tvv.setOperator("eq"); tvvList.add(tvv); } if (subscriptionServiceType != null) { TaskVariableValue tvv = new TaskVariableValue(); tvv.setName("subscriptionServiceType"); tvv.setValue(subscriptionServiceType); tvv.setOperator("eq"); tvvList.add(tvv); } if (nfRole != null) { TaskVariableValue tvv = new TaskVariableValue(); tvv.setName("nfRole"); tvv.setValue(nfRole); tvv.setOperator("eq"); tvvList.add(tvv); } if (buildingBlockName != null) { TaskVariableValue tvv = new TaskVariableValue(); tvv.setName("buildingBlockName"); tvv.setValue(buildingBlockName); tvv.setOperator("eq"); tvvList.add(tvv); } if (originalRequestDate != null) { TaskVariableValue tvv = new TaskVariableValue(); tvv.setName("originalRequestDate"); tvv.setValue(originalRequestDate); tvv.setOperator("eq"); tvvList.add(tvv); } if (originalRequestorId != null) { TaskVariableValue tvv = new TaskVariableValue(); tvv.setName("originalRequestorId"); tvv.setValue(originalRequestorId); tvv.setOperator("eq"); tvvList.add(tvv); } tv.setTaskVariables(tvvList); RequestClient requestClient = null; MsoRequest msoRequest = new MsoRequest(requestId); HttpResponse response = null; long subStartTime = System.currentTimeMillis(); try { requestClient = RequestClientFactory.getRequestClient (requestUrl, MsoPropertiesUtils.loadMsoProperties ()); // Capture audit event msoLogger.debug ("MSO API Handler Post call to Camunda engine for url: " + requestClient.getUrl ()); System.out.println("URL : " + requestClient.getUrl ()); ObjectMapper mapper = new ObjectMapper(); String camundaJsonReq = mapper.writeValueAsString(tv); msoLogger.debug("Camunda Json Request: " + camundaJsonReq); response = requestClient.post(camundaJsonReq); msoLogger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully received response from BPMN engine", "BPMN", requestUrl, null); } catch (Exception e) { msoLogger.recordMetricEvent (subStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with BPMN engine", "BPMN", requestUrl, null); msoRequest.setStatus (org.openecomp.mso.apihandlerinfra.vnfbeans.RequestStatusType.FAILED); Response resp = msoRequest.buildServiceErrorResponse (HttpStatus.SC_BAD_GATEWAY, MsoException.ServiceException, "Failed calling bpmn " + e.getMessage (), ErrorNumbers.SVC_NO_SERVER_RESOURCES, null); alarmLogger.sendAlarm ("MsoConfigurationError", MsoAlarmLogger.CRITICAL, Messages.errors.get (ErrorNumbers.NO_COMMUNICATION_TO_BPEL)); msoRequest.updateFinalStatus (Status.FAILED); msoLogger.error (MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, Constants.MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.AvailabilityError, "Exception while communicate with BPMN engine"); msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError, "Exception while communicate with BPMN engine"); msoLogger.debug ("End of the transaction, the final response is: " + (String) resp.getEntity (),e); return resp; } TasksGetResponse trr = new TasksGetResponse(); List taskList = new ArrayList<>(); ResponseHandler respHandler = new ResponseHandler (response, requestClient.getType ()); int bpelStatus = respHandler.getStatus (); if (bpelStatus == HttpStatus.SC_NO_CONTENT || bpelStatus == HttpStatus.SC_ACCEPTED) { msoLogger.debug ("Received good response from Camunda"); msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "BPMN completed the request"); String respBody = respHandler.getResponseBody(); if (respBody != null) { JSONArray data = new JSONArray(respBody); for (int i=0; i