[SO] SO changes to support Delete AS
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / cnfm / tasks / MonitorCnfmJobTask.java
 
 package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;
 
-import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREATE_CNF_STATUS_RESPONSE_PARAM_NAME;
+import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CNF_STATUS_RESPONSE_PARAM_NAME;
 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.OPERATION_STATUS_PARAM_NAME;
 import java.net.URI;
 import java.util.Optional;
-import org.onap.logging.filter.base.ONAPComponents;
+import java.util.Set;
+import org.camunda.bpm.engine.delegate.BpmnError;
 import org.onap.so.bpmn.common.BuildingBlockExecution;
 import org.onap.so.client.exception.ExceptionBuilder;
+import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
+import org.onap.so.cnfm.lcm.model.AsLcmOpOcc.OperationStateEnum;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
-import com.google.common.collect.ImmutableSet;
-import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
-import org.onap.so.cnfm.lcm.model.AsLcmOpOcc.OperationStateEnum;
-
 
 /**
  * @author sagar.shetty@est.tech
+ * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
  */
 @Component
-public class MonitorCnfmCreateJobTask {
+public class MonitorCnfmJobTask {
 
-    public static final ImmutableSet<OperationStateEnum> OPERATION_FINISHED_STATES =
-            ImmutableSet.of(OperationStateEnum.COMPLETED, OperationStateEnum.FAILED, OperationStateEnum.ROLLED_BACK);
     private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
-    private static final Logger LOGGER = LoggerFactory.getLogger(MonitorCnfmCreateJobTask.class);
+    private static final String MONITOR_JOB_NAME = "MonitorJobName";
+    public static final Set<OperationStateEnum> OPERATION_FINISHED_STATES =
+            Set.of(OperationStateEnum.COMPLETED, OperationStateEnum.FAILED, OperationStateEnum.ROLLED_BACK);
+    private static final Logger LOGGER = LoggerFactory.getLogger(MonitorCnfmJobTask.class);
     protected final ExceptionBuilder exceptionUtil;
     private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
 
     @Autowired
-    public MonitorCnfmCreateJobTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
+    public MonitorCnfmJobTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
             final ExceptionBuilder exceptionUtil) {
         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
         this.exceptionUtil = exceptionUtil;
     }
 
     /**
-     * Get the current operation status of instantiation job
-     *
+     * Get the current operation status of cnfm job
+     * 
      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
      */
     public void getCurrentOperationStatus(final BuildingBlockExecution execution) {
         try {
             LOGGER.debug("Executing getCurrentOperationStatus  ...");
-            final URI operationStatusURL = execution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL);
+            final Optional<URI> operationStatusURL = Optional.of(execution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL));
             LOGGER.debug("Executing getCurrentOperationStatus for CNF... :{}", operationStatusURL);
-            final Optional<AsLcmOpOcc> instantiateOperationJobStatus =
-                    cnfmHttpServiceProvider.getInstantiateOperationJobStatus(operationStatusURL.toString());
-            if (instantiateOperationJobStatus.isPresent()) {
-                final AsLcmOpOcc asLcmOpOccResponse = instantiateOperationJobStatus.get();
-                if (asLcmOpOccResponse.getOperationState() != null) {
-                    final OperationStateEnum operationStatus = asLcmOpOccResponse.getOperationState();
-                    LOGGER.debug("Operation {} with {} and operation retrieval status : {}", asLcmOpOccResponse.getId(),
-                            operationStatus, asLcmOpOccResponse.getOperationState());
-                    execution.setVariable(OPERATION_STATUS_PARAM_NAME, asLcmOpOccResponse.getOperationState());
-                }
+            final Optional<AsLcmOpOcc> operationalJobStatus =
+                    cnfmHttpServiceProvider.getOperationJobStatus(String.valueOf(operationStatusURL
+                            .orElseThrow(() -> new BpmnError("Operational Status check url Not found"))));
 
-                LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}",
-                        asLcmOpOccResponse.getId(), asLcmOpOccResponse.getOperationState());
+            final AsLcmOpOcc asLcmOpOccResponse = operationalJobStatus
+                    .orElseThrow(() -> new BpmnError("Unable to get operational Job status from the CNFM"));
+
+            if (asLcmOpOccResponse.getOperationState() != null) {
+                final OperationStateEnum operationStatus = asLcmOpOccResponse.getOperationState();
+                LOGGER.debug("Operation {} with {} and operation retrieval status : {}", asLcmOpOccResponse.getId(),
+                        operationStatus, operationStatus);
+                execution.setVariable(OPERATION_STATUS_PARAM_NAME, operationStatus);
+            } else {
+                LOGGER.debug("Operation {} without operationStatus", asLcmOpOccResponse.getId());
+                exceptionUtil.buildAndThrowWorkflowException(execution, 1206,
+                        new Exception("Operation Status is empty"));
             }
-            execution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME, instantiateOperationJobStatus.orElseThrow());
+            execution.setVariable(CNF_STATUS_RESPONSE_PARAM_NAME, asLcmOpOccResponse);
             LOGGER.debug("Finished executing getCurrentOperationStatus for CNF...");
         } catch (final Exception exception) {
-            final String message = "Unable to invoke get current Operation status";
-            LOGGER.error(message);
-            exceptionUtil.buildAndThrowWorkflowException(execution, 1209, message, ONAPComponents.SO);
-
+            LOGGER.error("Unable to invoke get current Operation status");
+            exceptionUtil.buildAndThrowWorkflowException(execution, 1209, exception);
         }
     }
 
@@ -93,36 +95,30 @@ public class MonitorCnfmCreateJobTask {
      *
      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
      */
-    public void timeOutLogFailue(final BuildingBlockExecution execution) {
-        final String message = "CNF Instantiation operation time out";
+    public void timeOutLogFailure(final BuildingBlockExecution execution) {
+        String message = "CNF" + execution.getVariable(MONITOR_JOB_NAME) + "operation time out";
         LOGGER.error(message);
-        exceptionUtil.buildAndThrowWorkflowException(execution, 1205, message, ONAPComponents.SO);
+        exceptionUtil.buildAndThrowWorkflowException(execution, 1205, new Exception(message));
     }
 
     /**
-     * Check the final status of instantiation throw exception if not completed successfully
-     *
+     * Check the final status of cnfm job throw exception if not completed successfully
+     * 
      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
      */
     public void checkIfOperationWasSuccessful(final BuildingBlockExecution execution) {
         LOGGER.debug("Executing CNF checkIfOperationWasSuccessful  ...");
         final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
-        final AsLcmOpOcc cnfInstantiateStautusResponse = execution.getVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME);
-        if (operationStatusOption == null) {
-            final String message = "Unable to instantiate CNF jobId: "
-                    + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null")
-                    + "Unable to retrieve OperationStatus";
+        final AsLcmOpOcc cnfStautusResponse = execution.getVariable(CNF_STATUS_RESPONSE_PARAM_NAME);
+        if ((operationStatusOption == OperationStateEnum.FAILED)
+                || (operationStatusOption == OperationStateEnum.FAILED_TEMP)) {
+            final String message = "Unable to" + execution.getVariable(MONITOR_JOB_NAME) + "CNF jobId: "
+                    + (cnfStautusResponse != null ? cnfStautusResponse.getId() : "null");
             LOGGER.error(message);
-            exceptionUtil.buildAndThrowWorkflowException(execution, 1206, message, ONAPComponents.SO);
-        } else {
-            if (operationStatusOption != OperationStateEnum.COMPLETED) {
-                final String message = "Unable to instantiate jobId: "
-                        + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null")
-                        + " OperationStatus: " + operationStatusOption;
-                LOGGER.error(message);
-                exceptionUtil.buildAndThrowWorkflowException(execution, 1207, message, ONAPComponents.SO);
-            }
-            LOGGER.debug("Successfully completed CNF instatiation of job status {}", cnfInstantiateStautusResponse);
+            exceptionUtil.buildAndThrowWorkflowException(execution, 1206, new Exception());
+        } else if ((operationStatusOption == OperationStateEnum.COMPLETED)) {
+            String monitorJobName = execution.getVariable(MONITOR_JOB_NAME);
+            LOGGER.debug("Successfully completed CNF {} job", monitorJobName);
         }
     }