Add junit coverage to RequestInfoBuilder class
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / node / FlowControlNode.java
index b2f4039..fd88360 100644 (file)
@@ -59,15 +59,13 @@ import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VSERVE
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
-import java.io.FileInputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -89,10 +87,7 @@ import org.onap.appc.flow.controller.interfaceData.DependencyInfo;
 import org.onap.appc.flow.controller.interfaceData.Input;
 import org.onap.appc.flow.controller.interfaceData.InventoryInfo;
 import org.onap.appc.flow.controller.interfaceData.RequestInfo;
-import org.onap.appc.flow.controller.interfaceData.Vm;
-import org.onap.appc.flow.controller.interfaceData.VnfInfo;
 import org.onap.appc.flow.controller.interfaceData.Vnfcs;
-import org.onap.appc.flow.controller.interfaceData.Vnfcslist;
 import org.onap.appc.flow.controller.interfaces.FlowExecutorInterface;
 import org.onap.appc.flow.controller.utils.EncryptionTool;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
@@ -102,7 +97,19 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
 public class FlowControlNode implements SvcLogicJavaPlugin {
 
   private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlNode.class);
-  private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
+
+  private final FlowControlDBService dbService;
+  private final FlowSequenceGenerator flowSequenceGenerator;
+
+  public FlowControlNode() {
+    this.dbService = FlowControlDBService.initialise();
+    this.flowSequenceGenerator = new FlowSequenceGenerator();
+  }
+
+  FlowControlNode(FlowControlDBService dbService, FlowSequenceGenerator flowSequenceGenerator) {
+    this.dbService = dbService;
+    this.flowSequenceGenerator = flowSequenceGenerator;
+  }
 
   public void processFlow(Map<String, String> inParams, SvcLogicContext ctx)
       throws SvcLogicException {
@@ -119,7 +126,6 @@ public class FlowControlNode implements SvcLogicJavaPlugin {
       localContext.setAttribute(RESPONSE_PREFIX, responsePrefix);
       ctx.setAttribute(RESPONSE_PREFIX, responsePrefix);
 
-      FlowControlDBService dbService = FlowControlDBService.initialise();
       dbService.getFlowReferenceData(ctx, inParams, localContext);
 
       for (String key : localContext.getAttributeKeySet()) {
@@ -142,81 +148,12 @@ public class FlowControlNode implements SvcLogicJavaPlugin {
 
     String fn = "FlowExecutorNode.processflowSequence";
     log.debug(fn + "Received model for flow : " + localContext.toString());
-    FlowControlDBService dbService = FlowControlDBService.initialise();
-    String flowSequence = null;
-    for (String key : localContext.getAttributeKeySet()) {
-      log.debug(key + "=" + ctx.getAttribute(key));
-    }
-    if (localContext.getAttribute(SEQUENCE_TYPE) != null) {
-      if (localContext.getAttribute(GENERATION_NODE) != null) {
-        GraphExecutor transactionExecutor = new GraphExecutor();
-        Boolean generatorExists = transactionExecutor.hasGraph(
-            "APPC_COMMOM",
-            localContext.getAttribute(GENERATION_NODE),
-            null,
-            "sync"
-        );
-
-        if (generatorExists) {
-          flowSequence = transactionExecutor.executeGraph(
-              "APPC_COMMOM",
-              localContext.getAttribute(GENERATION_NODE),
-              null, "sync", null)
-              .getProperty(FLOW_SEQUENCE);
-        } else {
-          throw new Exception("Can not find Custom defined Flow Generator for "
-              + localContext.getAttribute(GENERATION_NODE));
-        }
-
-      } else if ((localContext.getAttribute(SEQUENCE_TYPE)).equalsIgnoreCase(DESINGTIME)) {
-
-        localContext.setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));
-        flowSequence = dbService.getDesignTimeFlowModel(localContext);
-
-        if (flowSequence == null) {
-          throw new Exception("Flow Sequence is not found User Designed VNF " + ctx.getAttribute(VNF_TYPE));
-        }
-
-      } else if ((localContext.getAttribute(SEQUENCE_TYPE)).equalsIgnoreCase(RUNTIME)) {
-
-        Transaction transaction = new Transaction();
-        String input = collectInputParams(ctx, transaction);
-        log.info("CollectInputParamsData-Input: " + input);
-
-        RestExecutor restExe = new RestExecutor();
-        Map<String, String> flowSeq = restExe.execute(transaction, localContext);
-
-        JSONObject sequence = new JSONObject(flowSeq.get("restResponse"));
-        if (sequence.has("output")) {
-          flowSequence = sequence.getJSONObject("output").toString();
-        }
-        log.info("MultistepSequenceGenerator-Output: " + flowSequence);
-
-        if (flowSequence == null) {
-          throw new Exception("Failed to get the Flow Sequece runtime for VNF type"
-              + ctx.getAttribute(VNF_TYPE));
-        }
-
-      } else if ((localContext.getAttribute(SEQUENCE_TYPE)).equalsIgnoreCase(EXTERNAL)) {
-        //String input = collectInputParams(localContext);
-        //    flowSequnce = ""; //get it from the External interface calling the Rest End point - TBD
-        //if(flowSequnce == null)
 
-        throw new Exception("Flow Sequence not found for " + ctx.getAttribute(VNF_TYPE));
+    localContext
+        .getAttributeKeySet()
+        .forEach(key -> log.debug(key + "=" + ctx.getAttribute(key)));
 
-      } else {
-        //No other type of model supported...
-        //in Future can get flowModel from other generators which will be included here
-        throw new Exception("No information found for sequence Owner Design-Time Vs Run-Time");
-      }
-
-    } else {
-      FlowGenerator flowGenerator = new FlowGenerator();
-      Transactions trans = flowGenerator.createSingleStepModel(inParams, ctx);
-      ObjectMapper mapper = new ObjectMapper();
-      flowSequence = mapper.writeValueAsString(trans);
-      log.debug("Single step Flow Sequence : " + flowSequence);
-    }
+    String flowSequence = flowSequenceGenerator.getFlowSequence(inParams, ctx, localContext);
 
     log.debug("Received Flow Sequence : " + flowSequence);
     HashMap<Integer, Transaction> transactionMap = createTransactionMap(flowSequence, localContext);
@@ -352,7 +289,6 @@ public class FlowControlNode implements SvcLogicJavaPlugin {
   private void compileFlowDependencies(Transaction transaction, SvcLogicContext localContext)
       throws Exception {
 
-    FlowControlDBService dbService = FlowControlDBService.initialise();
     dbService.populateModuleAndRPC(transaction, localContext.getAttribute(VNF_TYPE));
     ObjectMapper mapper = new ObjectMapper();
     log.debug("Individual Transaction Details :" + transaction.toString());
@@ -368,210 +304,4 @@ public class FlowControlNode implements SvcLogicJavaPlugin {
     //get a field in transction class as transactionhandle interface and register the Handler here for each trnactions
   }
 
-  private String collectInputParams(SvcLogicContext ctx, Transaction transaction) throws Exception {
-
-    String fn = "FlowExecuteNode.collectInputParams";
-    Properties prop = loadProperties();
-    log.info("Loaded Properties " + prop.toString());
-
-    String vnfId = ctx.getAttribute(VNF_ID);
-    String inputData = null;
-    log.debug(fn + "vnfId :" + vnfId);
-
-    if (StringUtils.isBlank(vnfId)) {
-      throw new Exception("VnfId is missing");
-    }
-
-    try {
-      ActionIdentifier actionIdentifier = new ActionIdentifier();
-      log.debug("Enter ActionIdentifier");
-      if (StringUtils.isNotBlank(vnfId)) {
-        actionIdentifier.setVnfId(vnfId);
-      }
-      if (StringUtils.isNotBlank(ctx.getAttribute(VSERVER_ID))) {
-        actionIdentifier.setVserverId(ctx.getAttribute(VSERVER_ID));
-      }
-      if (StringUtils.isNotBlank(ctx.getAttribute(VNFC_NAME))) {
-        actionIdentifier.setVnfcName(ctx.getAttribute(VNFC_NAME));
-      }
-      log.info("ActionIdentifierData" + actionIdentifier.toString());
-
-      RequestInfo requestInfo = new RequestInfo();
-      log.info("Enter RequestInfo");
-      requestInfo.setAction(ctx.getAttribute(REQUEST_ACTION));
-      requestInfo.setActionLevel(ctx.getAttribute(ACTION_LEVEL));
-      requestInfo.setPayload(ctx.getAttribute(PAYLOAD));
-      requestInfo.setActionIdentifier(actionIdentifier);
-      log.debug("RequestInfo: " + requestInfo.toString());
-
-      InventoryInfo inventoryInfo = getInventoryInfo(ctx, vnfId);
-      DependencyInfo dependencyInfo = getDependencyInfo(ctx);
-      Capabilities capabilities = getCapabilitiesData(ctx);
-
-      Input input = new Input();
-      log.info("Enter InputData");
-      input.setRequestInfo(requestInfo);
-      input.setInventoryInfo(inventoryInfo);
-      input.setDependencyInfo(dependencyInfo);
-      input.setCapabilities(capabilities);
-      log.info(fn + "Input parameters:" + input.toString());
-
-      ObjectMapper mapper = new ObjectMapper();
-      mapper.setSerializationInclusion(Include.NON_NULL);
-      mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
-      inputData = mapper.writeValueAsString(input);
-      log.info("InputDataJson:" + inputData);
-
-    } catch (Exception e) {
-      log.error("Error occurred in " + fn, e);
-    }
-
-    String resourceUri = prop.getProperty(SEQ_GENERATOR_URL);
-    log.info(fn + "resourceUri= " + resourceUri);
-
-    EncryptionTool et = EncryptionTool.getInstance();
-    String pass = et.decrypt(prop.getProperty(SEQ_GENERATOR_PWD));
-
-    transaction.setPayload(inputData);
-    transaction.setExecutionRPC("POST");
-    transaction.setuId(prop.getProperty(SEQ_GENERATOR_UID));
-    transaction.setPswd(pass);
-    transaction.setExecutionEndPoint(resourceUri);
-
-    return inputData;
-  }
-
-  private DependencyInfo getDependencyInfo(SvcLogicContext ctx) throws Exception {
-
-    String fn = "FlowExecutorNode.getDependencyInfo";
-    DependencyInfo dependencyInfo = new DependencyInfo();
-    FlowControlDBService dbService = FlowControlDBService.initialise();
-    String dependencyData = dbService.getDependencyInfo(ctx);
-    log.info(fn + "dependencyDataInput:" + dependencyData);
-
-    if (dependencyData != null) {
-      ObjectMapper mapper = new ObjectMapper();
-      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-      mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
-      //JsonNode dependencyInfoData = mapper.readTree(dependencyData).get("dependencyInfo");
-      JsonNode vnfcData = mapper.readTree(dependencyData).get("vnfcs");
-      List<Vnfcs> vnfclist = Arrays.asList(mapper.readValue(vnfcData.toString(), Vnfcs[].class));
-      dependencyInfo.getVnfcs().addAll(vnfclist);
-
-      log.info("Dependency Output:" + dependencyInfo.toString());
-    }
-    return dependencyInfo;
-  }
-
-  private Capabilities getCapabilitiesData(SvcLogicContext ctx) throws Exception {
-
-    String fn = "FlowExecutorNode.getCapabilitiesData";
-    Capabilities capabilities = new Capabilities();
-    FlowControlDBService dbService = FlowControlDBService.initialise();
-    String capabilitiesData = dbService.getCapabilitiesData(ctx);
-    log.info(fn + "capabilitiesDataInput:" + capabilitiesData);
-
-    if (capabilitiesData != null) {
-      ObjectMapper mapper = new ObjectMapper();
-      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-      mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
-      JsonNode capabilitiesNode = mapper.readValue(capabilitiesData, JsonNode.class);
-      log.info("capabilitiesNode:" + capabilitiesNode.toString());
-
-      JsonNode vnfs = capabilitiesNode.findValue(VNF);
-      List<String> vnfsList = new ArrayList<>();
-      if (vnfs != null) {
-        for (int i = 0; i < vnfs.size(); i++) {
-          String vnf = vnfs.get(i).asText();
-          vnfsList.add(vnf);
-        }
-      }
-
-      JsonNode vfModules = capabilitiesNode.findValue(VF_MODULE);
-      List<String> vfModulesList = new ArrayList<>();
-      if (vfModules != null) {
-        for (int i = 0; i < vfModules.size(); i++) {
-          String vfModule = vfModules.get(i).asText();
-          vfModulesList.add(vfModule);
-        }
-      }
-
-      JsonNode vnfcs = capabilitiesNode.findValue(VNFC);
-      List<String> vnfcsList = new ArrayList<>();
-      if (vnfcs != null) {
-        for (int i = 0; i < vnfcs.size(); i++) {
-          String vnfc1 = vnfcs.get(i).asText();
-          vnfcsList.add(vnfc1);
-        }
-      }
-
-      JsonNode vms = capabilitiesNode.findValue(VM);
-      List<String> vmList = new ArrayList<>();
-      if (vms != null) {
-        for (int i = 0; i < vms.size(); i++) {
-          String vm1 = vms.get(i).asText();
-          vmList.add(vm1);
-        }
-      }
-
-      capabilities.getVnfc().addAll(vnfcsList);
-      capabilities.getVnf().addAll(vnfsList);
-      capabilities.getVfModule().addAll(vfModulesList);
-      capabilities.getVm().addAll(vmList);
-
-      log.info("Capabilities Output:" + capabilities.toString());
-    }
-    return capabilities;
-  }
-
-  private InventoryInfo getInventoryInfo(SvcLogicContext ctx, String vnfId) throws Exception {
-    String fn = "FlowExecutorNode.getInventoryInfo";
-
-    VnfInfo vnfInfo = new VnfInfo();
-    vnfInfo.setVnfId(vnfId);
-    vnfInfo.setVnfName(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-name"));
-    vnfInfo.setVnfType(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-type"));
-
-    String vmcount = ctx.getAttribute("tmp.vnfInfo.vm-count");
-    if (StringUtils.isNotBlank(vmcount)) {
-      int vmCount = Integer.parseInt(vmcount);
-      log.info(fn + "vmcount:" + vmCount);
-
-      Vm vm = new Vm();
-      Vnfcslist vnfc = new Vnfcslist();
-      for (int i = 0; i < vmCount; i++) {
-        vm.setVserverId(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vserver-id"));
-        String vnfccount = ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-count");
-        int vnfcCount = Integer.parseInt(vnfccount);
-        if (vnfcCount > 0) {
-          vnfc.setVnfcName(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-name"));
-          vnfc.setVnfcType(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-type"));
-          vm.setVnfc(vnfc);
-        }
-        vnfInfo.getVm().add(vm);
-      }
-    }
-    InventoryInfo inventoryInfo = new InventoryInfo();
-    inventoryInfo.setVnfInfo(vnfInfo);
-    log.info(fn + "Inventory Output:" + inventoryInfo.toString());
-
-    return inventoryInfo;
-  }
-
-  private static Properties loadProperties() throws Exception {
-    Properties props = new Properties();
-    String propDir = System.getenv(SDNC_CONFIG_DIR_VAR);
-    if (propDir == null) {
-      throw new Exception("Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
-    }
-    String propFile = propDir + APPC_FLOW_CONTROLLER;
-    try (InputStream propStream = new FileInputStream(propFile)) {
-
-      props.load(propStream);
-
-    } catch (Exception e) {
-      throw new Exception("Could not load properties file " + propFile, e);
-    }
-    return props;
-  }
 }