Update error handling for mapInfraActiveRequestToRequest
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / OrchestrationRequests.java
index 33c2ac8..5bf33b4 100644 (file)
@@ -28,6 +28,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 import javax.transaction.Transactional;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
@@ -45,10 +46,10 @@ import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang3.EnumUtils;
 import org.apache.http.HttpStatus;
 import org.onap.logging.ref.slf4j.ONAPLogConstants;
+import org.onap.logging.filter.base.ErrorCode;
 import org.onap.so.apihandler.common.ErrorNumbers;
 import org.onap.so.apihandler.common.ResponseBuilder;
 import org.onap.so.apihandlerinfra.exceptions.ApiException;
-import org.onap.so.apihandlerinfra.exceptions.ContactCamundaException;
 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
 import org.onap.so.constants.OrchestrationRequestFormat;
@@ -57,7 +58,6 @@ import org.onap.so.db.request.beans.InfraActiveRequests;
 import org.onap.so.db.request.beans.RequestProcessingData;
 import org.onap.so.db.request.client.RequestsDbClient;
 import org.onap.so.exceptions.ValidationException;
-import org.onap.logging.filter.base.ErrorCode;
 import org.onap.so.logger.MessageEnum;
 import org.onap.so.serviceinstancebeans.CloudRequestData;
 import org.onap.so.serviceinstancebeans.GetOrchestrationListResponse;
@@ -73,6 +73,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.MDC;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
 import org.springframework.stereotype.Component;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
@@ -105,6 +106,9 @@ public class OrchestrationRequests {
     @Autowired
     private CamundaRequestHandler camundaRequestHandler;
 
+    @Autowired
+    private Environment env;
+
     @GET
     @Path("/{version:[vV][4-8]}/{requestId}")
     @Operation(description = "Find Orchestrated Requests for a given requestId", responses = @ApiResponse(
@@ -209,7 +213,7 @@ public class OrchestrationRequests {
 
             if (isRequestProcessingDataRequired(format)) {
                 List<RequestProcessingData> requestProcessingData =
-                        requestsDbClient.getRequestProcessingDataBySoRequestId(infraActive.getRequestId());
+                        requestsDbClient.getExternalRequestProcessingDataBySoRequestId(infraActive.getRequestId());
                 if (null != requestProcessingData && !requestProcessingData.isEmpty()) {
                     request.setRequestProcessingData(mapRequestProcessingData(requestProcessingData));
                 }
@@ -306,6 +310,19 @@ public class OrchestrationRequests {
             request.setOriginalRequestId(originalRequestId);
         }
 
+        if (!version.matches("v[1-7]")) {
+            String workflowName = iar.getWorkflowName();
+            if (workflowName == null) {
+                workflowName = iar.getRequestAction();
+            }
+            request.setWorkflowName(workflowName);
+
+            String operationName = iar.getOperationName();
+            if (operationName != null) {
+                request.setOperationName(operationName);
+            }
+        }
+
         InstanceReferences ir = new InstanceReferences();
         if (iar.getNetworkId() != null)
             ir.setNetworkInstanceId(iar.getNetworkId());
@@ -352,16 +369,7 @@ public class OrchestrationRequests {
                 }
 
             } catch (IOException e) {
-                logger.error("Exception occurred", e);
-                ErrorLoggerInfo errorLoggerInfo =
-                        new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
-                                .build();
-                ValidateException validateException =
-                        new ValidateException.Builder("Mapping of request to JSON object failed : ",
-                                HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
-                                        .errorInfo(errorLoggerInfo).build();
-
-                throw validateException;
+                logger.error(String.format("Failed to parse request (id: %s) : ", request.getRequestId()), e);
             }
         }
         request.setRequestDetails(requestDetails);
@@ -436,12 +444,14 @@ public class OrchestrationRequests {
         String retryStatusMessage = iar.getRetryStatusMessage();
         String taskName = null;
 
-        if (format == null || !format.equalsIgnoreCase(OrchestrationRequestFormat.SIMPLENOTASKINFO.toString())) {
-            if (flowStatusMessage != null && !flowStatusMessage.equals("Successfully completed all Building Blocks")
-                    && !flowStatusMessage.equals("All Rollback flows have completed successfully")) {
-                taskName = camundaRequestHandler.getTaskName(iar.getRequestId());
-                if (taskName != null) {
-                    flowStatusMessage = flowStatusMessage + " TASK INFORMATION: " + taskName;
+        if (daysSinceRequest(iar) <= camundaCleanupInterval()) {
+            if (format == null || !format.equalsIgnoreCase(OrchestrationRequestFormat.SIMPLENOTASKINFO.toString())) {
+                if (flowStatusMessage != null && !flowStatusMessage.equals("Successfully completed all Building Blocks")
+                        && !flowStatusMessage.equals("All Rollback flows have completed successfully")) {
+                    taskName = camundaRequestHandler.getTaskName(iar.getRequestId());
+                    if (taskName != null) {
+                        flowStatusMessage = flowStatusMessage + " TASK INFORMATION: " + taskName;
+                    }
                 }
             }
         }
@@ -594,4 +604,20 @@ public class OrchestrationRequests {
         }
         return infraActiveRequest;
     }
+
+    protected long daysSinceRequest(InfraActiveRequests request) {
+        long startTime = request.getStartTime().getTime();
+        long now = System.currentTimeMillis();
+
+        return TimeUnit.MILLISECONDS.toDays(now - startTime);
+    }
+
+    protected int camundaCleanupInterval() {
+        String cleanupInterval = env.getProperty("mso.camundaCleanupInterval");
+        int days = 30;
+        if (cleanupInterval != null) {
+            days = Integer.parseInt(cleanupInterval);
+        }
+        return days;
+    }
 }