appc-dg-common sonar fixes part 1
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / onap / appc / dg / common / impl / VnfExecutionFlowImpl.java
index 8129e2e..fe7545d 100644 (file)
@@ -27,11 +27,11 @@ package org.onap.appc.dg.common.impl;
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 import com.att.eelf.i18n.EELFResourceManager;
-import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
-import org.onap.appc.exceptions.APPCException;
-
-import java.util.*;
-
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import org.onap.appc.dg.common.VnfExecutionFlow;
 import org.onap.appc.dg.dependencymanager.DependencyManager;
 import org.onap.appc.dg.dependencymanager.exception.DependencyModelNotFound;
@@ -39,23 +39,30 @@ import org.onap.appc.dg.dependencymanager.impl.DependencyModelFactory;
 import org.onap.appc.dg.flowbuilder.FlowBuilder;
 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
 import org.onap.appc.dg.flowbuilder.impl.FlowBuilderFactory;
-import org.onap.appc.dg.objects.*;
+import org.onap.appc.dg.objects.DependencyTypes;
+import org.onap.appc.dg.objects.FlowStrategies;
+import org.onap.appc.dg.objects.InventoryModel;
+import org.onap.appc.dg.objects.Node;
+import org.onap.appc.dg.objects.VnfcDependencyModel;
+import org.onap.appc.dg.objects.VnfcFlowModel;
 import org.onap.appc.domainmodel.Vnf;
 import org.onap.appc.domainmodel.Vnfc;
 import org.onap.appc.domainmodel.Vserver;
+import org.onap.appc.exceptions.APPCException;
 import org.onap.appc.i18n.Msg;
 import org.onap.appc.metadata.objects.DependencyModelIdentifier;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
 public class VnfExecutionFlowImpl implements VnfExecutionFlow {
 
     private final EELFLogger logger = EELFManager.getInstance().getLogger(VnfExecutionFlowImpl.class);
-    private static final String VNFC_FLOW="vnfcFlow[";
-    private static final String VNF_VNFC="vnf.vnfc[";
+    private static final String VNFC_FLOW = "vnfcFlow[";
+    private static final String VNF_VNFC = "vnf.vnfc[";
+
     /**
-     * Constructor
-     * <p>Used through blueprint
+     * Constructor <p>Used through blueprint
      */
-    public VnfExecutionFlowImpl(){
+    public VnfExecutionFlowImpl() {
         // do nothing
     }
 
@@ -70,133 +77,135 @@ public class VnfExecutionFlowImpl implements VnfExecutionFlow {
 
             if (logger.isTraceEnabled()) {
                 logger.trace("Input received from DG Node : dependencyType = " + dependencyType +
-                        " , flowStrategy = " + flowStrategy +
-                        ", DependencyModelIdentifier = " + modelIdentifier.toString());
+                    " , flowStrategy = " + flowStrategy +
+                    ", DependencyModelIdentifier = " + modelIdentifier.toString());
             }
 
             DependencyManager dependencyManager = DependencyModelFactory.createDependencyManager();
             dependencyModel = dependencyManager.getVnfcDependencyModel(
-                    modelIdentifier, DependencyTypes.findByString(dependencyType));
+                modelIdentifier, DependencyTypes.findByString(dependencyType));
         } catch (DependencyModelNotFound e) {
-            String msg = EELFResourceManager.format(Msg.DEPENDENCY_MODEL_NOT_FOUND,params.get(Constants.VNF_TYPE), e.getMessage());
+            String msg = EELFResourceManager
+                .format(Msg.DEPENDENCY_MODEL_NOT_FOUND, params.get(Constants.VNF_TYPE), e.getMessage());
             logger.error(msg);
-            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
-            context.setAttribute("dependencyModelFound","false");
+            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
+            context.setAttribute("dependencyModelFound", "false");
             return;
-        } catch (InvalidDependencyModelException e){
-            String msg = EELFResourceManager.format(Msg.INVALID_DEPENDENCY_MODEL,params.get(Constants.VNF_TYPE), e.getMessage());
+        } catch (InvalidDependencyModelException e) {
+            String msg = EELFResourceManager
+                .format(Msg.INVALID_DEPENDENCY_MODEL, params.get(Constants.VNF_TYPE), e.getMessage());
             logger.error(msg);
-            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
-            throw new RuntimeException(e.getMessage(),e);
-        } catch (APPCException e){
+            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
+            throw new RuntimeException(e.getMessage(), e);
+        } catch (APPCException e) {
             logger.error(e.getMessage());
-            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
-            throw new RuntimeException(e.getMessage(),e);
-        }
-        catch (RuntimeException e){
+            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
+            throw new RuntimeException(e.getMessage(), e);
+        } catch (RuntimeException e) {
             logger.error(e.getMessage());
-            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
+            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
             throw e;
         }
 
-
-        context.setAttribute("dependencyModelFound","true");
-        if(logger.isDebugEnabled()){
-            logger.debug("Dependency Model = " +dependencyModel);
+        context.setAttribute("dependencyModelFound", "true");
+        if (logger.isDebugEnabled()) {
+            logger.debug("Dependency Model = " + dependencyModel);
         }
         logger.info("Building Inventory Model from DG context");
         InventoryModel inventoryModel = null;
         try {
-             inventoryModel = readInventoryModel(context);
+            inventoryModel = readInventoryModel(context);
         } catch (APPCException e) {
             logger.error(e.getMessage());
-            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
-            throw new RuntimeException(e.getMessage(),e);
+            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
+            throw new RuntimeException(e.getMessage(), e);
         }
-        if(logger.isDebugEnabled()){
-            logger.debug("Inventory Model = " +inventoryModel);
+        if (logger.isDebugEnabled()) {
+            logger.debug("Inventory Model = " + inventoryModel);
         }
 
-        if(logger.isDebugEnabled()){
+        if (logger.isDebugEnabled()) {
             logger.debug("Validating inventory model with dependency model");
         }
         try {
             validateInventoryModelWithDependencyModel(dependencyModel, inventoryModel);
-        }
-        catch (APPCException e){
+        } catch (APPCException e) {
             logger.error(e.getMessage());
-            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
-            throw new RuntimeException(e.getMessage(),e);
+            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
+            throw new RuntimeException(e.getMessage(), e);
         }
         logger.info("Creating flow builder");
         FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(
-                FlowStrategies.findByString(flowStrategy));
+            FlowStrategies.findByString(flowStrategy));
 
         logger.info("Building Vnf flow model");
         VnfcFlowModel flowModel;
-        try{
-            flowModel = flowBuilder.buildFlowModel(dependencyModel,inventoryModel);
-        }
-        catch (InvalidDependencyModelException e){
-            String msg = EELFResourceManager.format(Msg.INVALID_DEPENDENCY_MODEL,params.get(Constants.VNF_TYPE), e.getMessage());
+        try {
+            flowModel = flowBuilder.buildFlowModel(dependencyModel, inventoryModel);
+        } catch (InvalidDependencyModelException e) {
+            String msg = EELFResourceManager
+                .format(Msg.INVALID_DEPENDENCY_MODEL, params.get(Constants.VNF_TYPE), e.getMessage());
             logger.error(msg);
-            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
-            throw new RuntimeException(e.getMessage(),e);
+            context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
+            throw new RuntimeException(e.getMessage(), e);
         }
 
         // remove VNFCs from the flow model where vserver list is empty
         reconcileFlowModel(flowModel);
-        populateContext(flowModel,context);
-        if(logger.isDebugEnabled()){
+        populateContext(flowModel, context);
+        if (logger.isDebugEnabled()) {
             logContext(context);
         }
-        String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVnfExecutionFlowData","VNF ID " + params.get(Constants.VNF_TYPE));
+        String msg = EELFResourceManager
+            .format(Msg.SUCCESS_EVENT_MESSAGE, "GetVnfExecutionFlowData", "VNF ID " + params.get(Constants.VNF_TYPE));
         context.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
     }
 
-    private void validateInput(String dependencyType, String flowStrategy, Map<String, String> params) throws APPCException {
+    private void validateInput(String dependencyType, String flowStrategy, Map<String, String> params)
+        throws APPCException {
         DependencyTypes dependencyTypes = DependencyTypes.findByString(dependencyType);
-        if(dependencyTypes == null){
-            throw new APPCException("Dependency type from the input : " + dependencyType +" is invalid.");
+        if (dependencyTypes == null) {
+            throw new APPCException("Dependency type from the input : " + dependencyType + " is invalid.");
         }
         FlowStrategies flowStrategies = FlowStrategies.findByString(flowStrategy);
-        if(flowStrategies == null){
-            throw new APPCException("Flow Strategy from the input : " + flowStrategy +" is invalid.");
+        if (flowStrategies == null) {
+            throw new APPCException("Flow Strategy from the input : " + flowStrategy + " is invalid.");
         }
         String vnfType = params.get(Constants.VNF_TYPE);
-        if(vnfType ==null || vnfType.length() ==0){
+        if (vnfType == null || vnfType.length() == 0) {
             throw new APPCException("Vnf Type is not passed in the input");
         }
         String vnfVersion = params.get(Constants.VNF_VERION);
-        if(vnfVersion == null || vnfVersion.length() ==0){
+        if (vnfVersion == null || vnfVersion.length() == 0) {
             throw new APPCException("Vnf Version not found");
         }
     }
 
     private void logContext(SvcLogicContext context) {
-        for(String key:context.getAttributeKeySet()){
-            logger.debug(key + " = " + context.getAttribute(key) + "\n" );
+        for (String key : context.getAttributeKeySet()) {
+            logger.debug(key + " = " + context.getAttribute(key) + "\n");
         }
     }
 
     private void populateContext(VnfcFlowModel flowModel, SvcLogicContext context) {
-        int flowIndex=0;
+        int flowIndex = 0;
         Iterator<List<Vnfc>> iterator = flowModel.getModelIterator();
-        while (iterator.hasNext()){
-            for(Vnfc vnfc:iterator.next()){
-                context.setAttribute(VNFC_FLOW+flowIndex+"].vnfcName",vnfc.getVnfcName());
-                context.setAttribute(VNFC_FLOW+flowIndex+"].vnfcType",vnfc.getVnfcType());
-                context.setAttribute(VNFC_FLOW+flowIndex+"].resilienceType",vnfc.getResilienceType());
-                context.setAttribute(VNFC_FLOW+flowIndex+"].vmCount",Integer.toString(vnfc.getVserverList().size()));
-                int vmIndex =0;
-                for(Vserver vm :vnfc.getVserverList()){
-                    context.setAttribute(VNFC_FLOW+flowIndex+"].vm["+vmIndex+"].url",vm.getUrl());
+        while (iterator.hasNext()) {
+            for (Vnfc vnfc : iterator.next()) {
+                context.setAttribute(VNFC_FLOW + flowIndex + "].vnfcName", vnfc.getVnfcName());
+                context.setAttribute(VNFC_FLOW + flowIndex + "].vnfcType", vnfc.getVnfcType());
+                context.setAttribute(VNFC_FLOW + flowIndex + "].resilienceType", vnfc.getResilienceType());
+                context
+                    .setAttribute(VNFC_FLOW + flowIndex + "].vmCount", Integer.toString(vnfc.getVserverList().size()));
+                int vmIndex = 0;
+                for (Vserver vm : vnfc.getVserverList()) {
+                    context.setAttribute(VNFC_FLOW + flowIndex + "].vm[" + vmIndex + "].url", vm.getUrl());
                     vmIndex++;
                 }
                 flowIndex++;
             }
         }
-        context.setAttribute("vnfcFlowCount",Integer.toString(flowIndex));
+        context.setAttribute("vnfcFlowCount", Integer.toString(flowIndex));
     }
 
     private InventoryModel readInventoryModel(SvcLogicContext context) throws APPCException {
@@ -206,17 +215,17 @@ public class VnfExecutionFlowImpl implements VnfExecutionFlow {
         String vnfcCountStr = context.getAttribute("vnf.vnfcCount");
         Integer vnfcCount = Integer.parseInt(vnfcCountStr);
         Vnf vnf = createVnf(vnfId, vnfType, vnfVersion);
-        for(Integer i=0;i<vnfcCount;i++){
-            String vnfcName = context.getAttribute(VNF_VNFC+ i+"].name");
-            String vnfcType = context.getAttribute(VNF_VNFC+ i+"].type");
-            String vmCountStr = context.getAttribute(VNF_VNFC+ i+"].vm_count");
-            if(vnfcType ==null || vnfcType.length() ==0){
-                throw new APPCException("Could not retrieve VNFC Type from DG Context for vnf.vnfc["+ i+"].type");
+        for (Integer i = 0; i < vnfcCount; i++) {
+            String vnfcName = context.getAttribute(VNF_VNFC + i + "].name");
+            String vnfcType = context.getAttribute(VNF_VNFC + i + "].type");
+            String vmCountStr = context.getAttribute(VNF_VNFC + i + "].vm_count");
+            if (vnfcType == null || vnfcType.length() == 0) {
+                throw new APPCException("Could not retrieve VNFC Type from DG Context for vnf.vnfc[" + i + "].type");
             }
             Integer vmCount = Integer.parseInt(vmCountStr);
             Vnfc vnfc = createVnfc(vnfcName, vnfcType);
-            for(Integer j=0;j<vmCount;j++){
-                String vmURL = context.getAttribute(VNF_VNFC+i+"].vm["+j+"].url");
+            for (Integer j = 0; j < vmCount; j++) {
+                String vmURL = context.getAttribute(VNF_VNFC + i + "].vm[" + j + "].url");
                 Vserver vm = createVserver(vmURL);
                 vm.setVnfc(vnfc);
                 vnfc.addVserver(vm);
@@ -233,14 +242,14 @@ public class VnfExecutionFlowImpl implements VnfExecutionFlow {
     }
 
     private Vnfc createVnfc(String vnfcName, String vnfcType) {
-        Vnfc vnfc =new Vnfc();
+        Vnfc vnfc = new Vnfc();
         vnfc.setVnfcName(vnfcName);
         vnfc.setVnfcType(vnfcType);
         return vnfc;
     }
 
     private Vnf createVnf(String vnfId, String vnfType, String vnfVersion) {
-        Vnf vnf =new Vnf();
+        Vnf vnf = new Vnf();
         vnf.setVnfType(vnfType);
         vnf.setVnfId(vnfId);
         vnf.setVnfVersion(vnfVersion);
@@ -250,10 +259,11 @@ public class VnfExecutionFlowImpl implements VnfExecutionFlow {
     private DependencyModelIdentifier readDependencyModelIdentifier(Map<String, String> params) {
         String vnfType = params.get(Constants.VNF_TYPE);
         String catalogVersion = params.get(Constants.VNF_VERION);
-        return new DependencyModelIdentifier(vnfType,catalogVersion);
+        return new DependencyModelIdentifier(vnfType, catalogVersion);
     }
 
-    private void validateInventoryModelWithDependencyModel(VnfcDependencyModel dependencyModel, InventoryModel inventoryModel) throws APPCException {
+    private void validateInventoryModelWithDependencyModel(VnfcDependencyModel dependencyModel,
+        InventoryModel inventoryModel) throws APPCException {
         Set<String> dependencyModelVnfcSet = new HashSet<>();
         Set<String> dependencyModelMandatoryVnfcSet = new HashSet<>();
         Set<String> inventoryModelVnfcsSet = new HashSet<>();
@@ -293,7 +303,8 @@ public class VnfExecutionFlowImpl implements VnfExecutionFlow {
                 Vnfc vnfc = vnfcIterator.next();
                 if (vnfc.getVserverList().size() == 0) {
                     if (logger.isDebugEnabled()) {
-                        logger.debug("No vservers present for Vnfc type: " + vnfc.getVnfcType() + ". Hence, removing it from the flow model.");
+                        logger.debug("No vservers present for Vnfc type: " + vnfc.getVnfcType()
+                            + ". Hence, removing it from the flow model.");
                     }
                     vnfcIterator.remove();
                 }