Fixing EtSI BB ClassCastException
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / MonitorVnfmNodeTask.java
index 65b05e2..a7a4ead 100644 (file)
  */
 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
 
-import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREATE_VNF_NODE_STATUS;
-import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.DELETE_VNF_NODE_STATUS;
-import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.VNF_ASSIGNED;
-import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.VNF_CREATED;
 import static org.onap.so.bpmn.servicedecomposition.entities.ResourceKey.GENERIC_VNF_ID;
+import java.util.Optional;
 import org.onap.aai.domain.yang.GenericVnf;
 import org.onap.so.bpmn.common.BuildingBlockExecution;
+import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.exception.GenericVnfNotFoundException;
 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
 import org.onap.so.client.exception.ExceptionBuilder;
+import org.onap.so.client.orchestration.AAIVnfResources;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
 
 
 /**
  * 
  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
+ * @author Waqas Ikram (waqas.ikram@est.tech)
  *
  */
-@Component
-public class MonitorVnfmNodeTask {
+public abstract class MonitorVnfmNodeTask {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(MonitorVnfmNodeTask.class);
 
     private final ExtractPojosForBB extractPojosForBB;
     private final ExceptionBuilder exceptionUtil;
+    private final AAIVnfResources aaiVnfResources;
 
     @Autowired
-    public MonitorVnfmNodeTask(final ExtractPojosForBB extractPojosForBB, final ExceptionBuilder exceptionUtil) {
+    public MonitorVnfmNodeTask(final ExtractPojosForBB extractPojosForBB, final ExceptionBuilder exceptionUtil,
+            final AAIVnfResources aaiVnfResources) {
         this.exceptionUtil = exceptionUtil;
         this.extractPojosForBB = extractPojosForBB;
+        this.aaiVnfResources = aaiVnfResources;
     }
 
     /**
@@ -61,17 +62,43 @@ public class MonitorVnfmNodeTask {
     public void getNodeStatus(final BuildingBlockExecution execution) {
         try {
             LOGGER.debug("Executing getNodeStatus  ...");
-            final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
-            String orchestrationStatus = vnf.getOrchestrationStatus();
-            LOGGER.debug("Orchestration Status in AAI {}", orchestrationStatus);
-            execution.setVariable(CREATE_VNF_NODE_STATUS, VNF_CREATED.equalsIgnoreCase(orchestrationStatus));
-            execution.setVariable(DELETE_VNF_NODE_STATUS, VNF_ASSIGNED.equalsIgnoreCase(orchestrationStatus));
+
+            final org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf vnf =
+                    extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
+
+            final String vnfId = vnf.getVnfId();
+            LOGGER.debug("Query A&AI for generic VNF using vnfID: {}", vnfId);
+            final Optional<GenericVnf> aaiGenericVnfOptional = aaiVnfResources.getGenericVnf(vnfId);
+
+            if (!aaiGenericVnfOptional.isPresent()) {
+                throw new GenericVnfNotFoundException("Unable to find generic vnf in A&AI using vnfID: " + vnfId);
+            }
+            final GenericVnf genericVnf = aaiGenericVnfOptional.get();
+            final String orchestrationStatus = genericVnf.getOrchestrationStatus();
+            LOGGER.debug("Found generic vnf with orchestration status : {}", orchestrationStatus);
+
+            execution.setVariable(getNodeStatusVariableName(), isOrchestrationStatusValid(orchestrationStatus));
+
         } catch (final Exception exception) {
             LOGGER.error("Unable to get vnf from AAI", exception);
             exceptionUtil.buildAndThrowWorkflowException(execution, 1220, exception);
         }
     }
 
+    /**
+     * Get variable to store in execution context
+     * 
+     * @return the variable name
+     */
+    public abstract String getNodeStatusVariableName();
+
+    /**
+     * @param orchestrationStatus the orchestration status from A&AI
+     * @return true if valid
+     */
+    public abstract boolean isOrchestrationStatusValid(final String orchestrationStatus);
+
+
     /**
      * Log and throw exception on timeout for job status
      *