Merge "fixed aai response code in updateAAIVfModule"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionBBTasks.java
index 9e4b01e..d9125e4 100644 (file)
@@ -22,6 +22,7 @@ package org.onap.so.bpmn.infrastructure.workflow.tasks;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
 
 import org.camunda.bpm.engine.delegate.BpmnError;
 import org.camunda.bpm.engine.delegate.DelegateExecution;
@@ -50,12 +51,15 @@ 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 String RETRY_COUNT = "retryCount";
        private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBTasks.class);
 
        @Autowired
        private RequestsDbClient requestDbclient;
        @Autowired
        private WorkflowAction workflowAction;
+       @Autowired
+       private WorkflowActionBBFailure workflowActionBBFailure;
        
        public void selectBB(DelegateExecution execution) {
                List<ExecuteBuildingBlock> flowsToExecute = (List<ExecuteBuildingBlock>) execution
@@ -108,7 +112,7 @@ public class WorkflowActionBBTasks {
                String statusMessage = this.getStatusMessage(completedBB.getBuildingBlock().getBpmnFlowName(), 
                                nextBB.getBuildingBlock().getBpmnFlowName(), completedBBs, remainingBBs);
                Long percentProgress = this.getPercentProgress(completedBBs, totalBBs);
-               request.setStatusMessage(statusMessage);
+               request.setFlowStatus(statusMessage);
                request.setProgress(percentProgress);
                request.setLastModifiedBy("CamundaBPMN");
                return request;
@@ -200,34 +204,29 @@ public class WorkflowActionBBTasks {
                execution.setVariable("mso-service-instance-id", resourceId);
        }
 
-       public void setupFalloutHandler(DelegateExecution execution) {
-               final String requestId = (String) execution.getVariable(G_REQUEST_ID);
-               final String action = (String) execution.getVariable(G_ACTION);
-               final String resourceId = (String) execution.getVariable("resourceId");
-               String exceptionMsg = "";
-               if (execution.getVariable("WorkflowActionErrorMessage") != null) {
-                       exceptionMsg = (String) execution.getVariable("WorkflowActionErrorMessage");
-               } else {
-                       exceptionMsg = "Error in WorkflowAction";
-               }
-               execution.setVariable("mso-service-instance-id", resourceId);
-               execution.setVariable("mso-request-id", requestId);
-               String falloutRequest = "<aetgt:FalloutHandlerRequest xmlns:aetgt=\"http://org.onap/so/workflow/schema/v1\"xmlns:ns=\"http://org.onap/so/request/types/v1\"xmlns:wfsch=\"http://org.onap/so/workflow/schema/v1\"><request-info xmlns=\"http://org.onap/so/infra/vnf-request/v1\"><request-id>"
-                               + requestId + "</request-id><action>" + action
-                               + "</action><source>VID</source></request-info><aetgt:WorkflowException xmlns:aetgt=\"http://org.onap/so/workflow/schema/v1\"><aetgt:ErrorMessage>"
-                               + exceptionMsg
-                               + "</aetgt:ErrorMessage><aetgt:ErrorCode>7000</aetgt:ErrorCode></aetgt:WorkflowException></aetgt:FalloutHandlerRequest>";
-               execution.setVariable("falloutRequest", falloutRequest);
-       }
-
        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");
+               String requestId = (String) execution.getVariable(G_REQUEST_ID);
+               String retryDuration = (String) execution.getVariable("RetryDuration");
+               int retryCount = (int) execution.getVariable(RETRY_COUNT);
+               if (handlingCode.equals("Retry")){
+                       workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
+                       try{
+                               InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
+                               request.setRetryStatusMessage("Retry " + retryCount+1 + "/5 will be started in " + retryDuration);
+                               requestDbclient.updateInfraActiveRequests(request); 
+                       } catch(Exception ex){
+                               logger.warn("Failed to update Request Db Infra Active Requests with Retry Status",ex);
+                       }
+                       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);
                }
        }
 
@@ -236,69 +235,54 @@ 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");
-       }
-
-       public void abortCallErrorHandling(DelegateExecution execution) {
-               String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
-               logger.error(msg);
-               throw new BpmnError(msg);
-       }
-
-       public void updateRequestStatusToFailed(DelegateExecution execution) {
-               try {
-                       String requestId = (String) execution.getVariable(G_REQUEST_ID);
-                       InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
-                       String errorMsg = null;
-                       try {
-                               WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
-                               request.setStatusMessage(exception.getErrorMessage());
-                       } catch (Exception ex) {
-                               //log error and attempt to extact WorkflowExceptionMessage
-                               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) {
-                                       logger.error("Failed to extract workflow exception message from WorkflowException",ex);
-                                       request.setStatusMessage("Unexpected Error in BPMN");
+                       
+                       int flowSize = rollbackFlows.size();
+                       String handlingCode = (String) execution.getVariable("handlingCode");
+                       if(handlingCode.equals("RollbackToAssigned")){
+                               for(int i = 0; i<flowSize -1; i++){
+                                       if(rollbackFlows.get(i).getBuildingBlock().getBpmnFlowName().contains("Unassign")){
+                                               rollbackFlows.remove(i);
+                                       }
                                }
                        }
-                       request.setRequestStatus("FAILED");
-                       request.setLastModifiedBy("CamundaBPMN");
-                       requestDbclient.updateInfraActiveRequests(request);
-               } catch (Exception e) {
-                       workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
+                       
+                       workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
+                       
+                       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.");
                }
        }
 }