Springboot 2.0 upgrade
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionBBTasks.java
index ab29e21..fabe8a1 100644 (file)
@@ -30,13 +30,13 @@ import org.onap.so.bpmn.common.workflow.context.WorkflowContextHolder;
 import org.onap.so.bpmn.core.WorkflowException;
 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
-
 import org.onap.so.client.exception.ExceptionBuilder;
 import org.onap.so.db.request.beans.InfraActiveRequests;
 import org.onap.so.db.request.client.RequestsDbClient;
-import org.onap.so.logger.MsoLogger;
 import org.onap.so.serviceinstancebeans.RequestReferences;
 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -50,7 +50,8 @@ public class WorkflowActionBBTasks {
        private static final String G_REQUEST_ID = "mso-request-id";
        private static final String G_ALACARTE = "aLaCarte";
        private static final String G_ACTION = "requestAction";
-       private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, WorkflowActionBBTasks.class);
+       private static final String RETRY_COUNT = "retryCount";
+       private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBTasks.class);
 
        @Autowired
        private RequestsDbClient requestDbclient;
@@ -84,10 +85,14 @@ public class WorkflowActionBBTasks {
        }
        
        public void updateFlowStatistics(DelegateExecution execution) {
-               int currentSequence = (int) execution.getVariable(G_CURRENT_SEQUENCE);
-               if(currentSequence > 1) {
-                       InfraActiveRequests request = this.getUpdatedRequest(execution, currentSequence);
-                       requestDbclient.updateInfraActiveRequests(request);
+               try{
+                       int currentSequence = (int) execution.getVariable(G_CURRENT_SEQUENCE);
+                       if(currentSequence > 1) {
+                               InfraActiveRequests request = this.getUpdatedRequest(execution, currentSequence);
+                               requestDbclient.updateInfraActiveRequests(request);
+                       }
+               }catch (Exception ex){
+                       logger.warn("Bpmn Flow Statistics was unable to update Request Db with the new completion percentage. Competion percentage may be invalid.");
                }
        }
 
@@ -146,7 +151,7 @@ public class WorkflowActionBBTasks {
                                .getProcessDefinition(execution.getProcessDefinitionId()).getKey();
                WorkflowContextHolder.getInstance().processCallback(processKey, execution.getProcessInstanceId(), requestId,
                                callbackResponse);
-               msoLogger.info("Successfully sent sync ack.");
+               logger.info("Successfully sent sync ack.");
        }
 
        public void sendErrorSyncAck(DelegateExecution execution) {
@@ -169,7 +174,7 @@ public class WorkflowActionBBTasks {
                                        callbackResponse);
                        execution.setVariable("sentSyncResponse", true);
                } catch (Exception ex) {
-                       msoLogger.debug(" Sending Sync Error Activity Failed. " + "\n" + ex.getMessage());
+                       logger.error(" Sending Sync Error Activity Failed. {}"  , ex.getMessage(), ex);
                }
        }
 
@@ -217,13 +222,18 @@ public class WorkflowActionBBTasks {
        }
 
        public void checkRetryStatus(DelegateExecution execution) {
-               if (execution.getVariable("handlingCode") == "Retry") {
-                       int currSequence = (int) execution.getVariable("gCurrentSequence");
-                       currSequence--;
-                       execution.setVariable("gCurrentSequence", currSequence);
-                       int currRetryCount = (int) execution.getVariable("retryCount");
-                       currRetryCount++;
-                       execution.setVariable("retryCount", currRetryCount);
+               String handlingCode = (String) execution.getVariable("handlingCode");
+               int retryCount = (int) execution.getVariable(RETRY_COUNT);
+               if (handlingCode.equals("Retry")){
+                       if(retryCount<5){
+                               int currSequence = (int) execution.getVariable("gCurrentSequence");
+                               execution.setVariable("gCurrentSequence", currSequence-1);
+                               execution.setVariable(RETRY_COUNT, retryCount + 1);
+                       }else{
+                               workflowAction.buildAndThrowException(execution, "Exceeded maximum retries. Ending flow with status Abort");
+                       }
+               }else{
+                       execution.setVariable(RETRY_COUNT, 0);
                }
        }
 
@@ -232,41 +242,47 @@ public class WorkflowActionBBTasks {
         * layer will rollback the flow its currently working on.
         */
        public void rollbackExecutionPath(DelegateExecution execution) {
-               List<ExecuteBuildingBlock> flowsToExecute = (List<ExecuteBuildingBlock>) execution
-                               .getVariable("flowsToExecute");
-               List<ExecuteBuildingBlock> rollbackFlows = new ArrayList();
-               int currentSequence = (int) execution.getVariable(G_CURRENT_SEQUENCE) - 1;
-               for (int i = flowsToExecute.size() - 1; i >= 0; i--) {
-                       if (i >= currentSequence) {
-                               flowsToExecute.remove(i);
-                       } else {
-                               ExecuteBuildingBlock ebb = flowsToExecute.get(i);
-                               BuildingBlock bb = flowsToExecute.get(i).getBuildingBlock();
-                               String flowName = flowsToExecute.get(i).getBuildingBlock().getBpmnFlowName();
-                               if (flowName.contains("Assign")) {
-                                       flowName = "Unassign" + flowName.substring(7, flowName.length());
-                               } else if (flowName.contains("Create")) {
-                                       flowName = "Delete" + flowName.substring(6, flowName.length());
-                               } else if (flowName.contains("Activate")) {
-                                       flowName = "Deactivate" + flowName.substring(8, flowName.length());
+               if(!(boolean)execution.getVariable("isRollback")){
+                       List<ExecuteBuildingBlock> flowsToExecute = (List<ExecuteBuildingBlock>) execution
+                                       .getVariable("flowsToExecute");
+                       List<ExecuteBuildingBlock> rollbackFlows = new ArrayList();
+                       int currentSequence = (int) execution.getVariable(G_CURRENT_SEQUENCE);
+                       int listSize = flowsToExecute.size();
+                       for (int i = listSize - 1; i >= 0; i--) {
+                               if (i > currentSequence - 1) {
+                                       flowsToExecute.remove(i);
+                               } else {
+                                       String flowName = flowsToExecute.get(i).getBuildingBlock().getBpmnFlowName();
+                                       if (flowName.contains("Assign")) {
+                                               flowName = "Unassign" + flowName.substring(6, flowName.length());
+                                       } else if (flowName.contains("Create")) {
+                                               flowName = "Delete" + flowName.substring(6, flowName.length());
+                                       } else if (flowName.contains("Activate")) {
+                                               flowName = "Deactivate" + flowName.substring(8, flowName.length());
+                                       }else{
+                                               continue;
+                                       }
+                                       flowsToExecute.get(i).getBuildingBlock().setBpmnFlowName(flowName);
+                                       rollbackFlows.add(flowsToExecute.get(i));
                                }
-                               flowsToExecute.get(i).getBuildingBlock().setBpmnFlowName(flowName);
-                               rollbackFlows.add(flowsToExecute.get(i));
                        }
+                       if (rollbackFlows.isEmpty())
+                               execution.setVariable("isRollbackNeeded", false);
+                       else
+                               execution.setVariable("isRollbackNeeded", true);
+                       execution.setVariable("flowsToExecute", rollbackFlows);
+                       execution.setVariable("handlingCode", "PreformingRollback");
+                       execution.setVariable("isRollback", true);
+                       execution.setVariable("gCurrentSequence", 0);
+                       execution.setVariable(RETRY_COUNT, 0);
+               }else{
+                       workflowAction.buildAndThrowException(execution, "Rollback has already been called. Cannot rollback a request that is currently in the rollback state.");
                }
-               if (rollbackFlows.isEmpty())
-                       execution.setVariable("isRollbackNeeded", false);
-               else
-                       execution.setVariable("isRollbackNeeded", true);
-
-               execution.setVariable("flowsToExecute", rollbackFlows);
-               execution.setVariable("handlingCode", "PreformingRollback");
        }
 
        public void abortCallErrorHandling(DelegateExecution execution) {
                String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
-               Exception exception = new Exception(msg);
-               msoLogger.error(exception);
+               logger.error(msg);
                throw new BpmnError(msg);
        }
 
@@ -275,22 +291,29 @@ public class WorkflowActionBBTasks {
                        String requestId = (String) execution.getVariable(G_REQUEST_ID);
                        InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
                        String errorMsg = null;
+                       boolean rollback = (boolean) execution.getVariable("isRollbackComplete");
                        try {
                                WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
-                               request.setStatusMessage(exception.getErrorMessage());
+                               if(exception != null && (exception.getErrorMessage()!=null || !exception.getErrorMessage().equals(""))){
+                                       errorMsg = exception.getErrorMessage();
+                               }
                        } catch (Exception ex) {
                                //log error and attempt to extact WorkflowExceptionMessage
-                               msoLogger.error(ex);
+                               logger.error("Failed to extract workflow exception from execution.",ex);
                        }
                        if (errorMsg == null){
                                try {
                                        errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
-                                       request.setStatusMessage(errorMsg);
                                } catch (Exception ex) {
-                                       msoLogger.error(ex);
-                                       request.setStatusMessage("Unexpected Error in BPMN");
+                                       logger.error("Failed to extract workflow exception message from WorkflowException",ex);
+                                       errorMsg = "Unexpected Error in BPMN.";
                                }
                        }
+                       if(rollback){
+                               errorMsg = errorMsg + " + Rollback has been completed successfully.";
+                       }
+                       request.setProgress(Long.valueOf(100));
+                       request.setStatusMessage(errorMsg);
                        request.setRequestStatus("FAILED");
                        request.setLastModifiedBy("CamundaBPMN");
                        requestDbclient.updateInfraActiveRequests(request);
@@ -298,4 +321,9 @@ public class WorkflowActionBBTasks {
                        workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
                }
        }
+       
+       public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
+               execution.setVariable("isRollbackComplete", true);
+               updateRequestStatusToFailed(execution);
+       }
 }