refactored code in ExecuteNodeActionImpl.java 45/61545/4
authorSandeep J <sandeejh@in.ibm.com>
Tue, 21 Aug 2018 11:18:38 +0000 (16:48 +0530)
committerTakamune Cho <tc012c@att.com>
Fri, 31 Aug 2018 12:42:40 +0000 (12:42 +0000)
created a method a called getVserverRelations(SvcLogicContext vnfCtx,
SvcLogicContext ctx) having the logic for looping through relationship-list
data, to get vserver relations. This piece of code was earlier not
covered by test cases. By putting this logic in a separate method, we
will be able to write a specific test case for this logic and will be
able to increase the code coverage by a big amount.

Issue-ID: APPC-1086
Change-Id: Iffa2a4f04a1fd2b93edc4bcdfef8625f196a305e
Signed-off-by: Sandeep J <sandeejh@in.ibm.com>
appc-dg-util/appc-dg-util-bundle/src/main/java/org/onap/appc/dg/util/impl/ExecuteNodeActionImpl.java

index d2aa68a..33b9866 100644 (file)
@@ -8,6 +8,8 @@
  * ================================================================================
  * Modifications Copyright (C) 2018 Nokia
  * ================================================================================
+ * Modifications Copyright (C) 2018 IBM.
+ * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 
 package org.onap.appc.dg.util.impl;
 
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-import com.att.eelf.i18n.EELFResourceManager;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+
 import org.onap.appc.dg.util.ExecuteNodeAction;
 import org.onap.appc.exceptions.APPCException;
 import org.onap.appc.i18n.Msg;
@@ -41,6 +41,9 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+import com.att.eelf.i18n.EELFResourceManager;
 
 public class ExecuteNodeActionImpl implements ExecuteNodeAction {
 
@@ -59,7 +62,12 @@ public class ExecuteNodeActionImpl implements ExecuteNodeAction {
     private static final String ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM = "Error Retrieving VNFC hierarchy";
     private static final String RELATED_TO_PROPERTY_LEN_PARAM = "related-to-property_length";
     public static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
+    private static Map<String, String> vnfHierarchyMap = new ConcurrentHashMap<>();
 
+    private static Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
+    private static int vmCount = 0;
+    private static Set<String> vmSet;
+    private static String vmURL;
     private AAIServiceFactory aaiServiceFactory;
 
     public ExecuteNodeActionImpl(AAIServiceFactory aaiServiceFactory) {
@@ -69,7 +77,8 @@ public class ExecuteNodeActionImpl implements ExecuteNodeAction {
     /**
      * Method called in TestDG to test timeout scenario
      *
-     * @param params waitTime time in millisecond DG is going to sleep
+     * @param params
+     *            waitTime time in millisecond DG is going to sleep
      */
     @Override
     public void waitMethod(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
@@ -80,8 +89,8 @@ public class ExecuteNodeActionImpl implements ExecuteNodeAction {
             Thread.sleep(Long.parseLong(waitTime));
             logger.info("DG waits for " + Long.parseLong(waitTime) + " milliseconds completed");
         } catch (InterruptedException e) {
-            logger.error("Error In ExecuteNodeActionImpl for waitMethod() due to InterruptedException: reason = " + e
-                .getMessage());
+            logger.error("Error In ExecuteNodeActionImpl for waitMethod() due to InterruptedException: reason = "
+                    + e.getMessage());
         }
     }
 
@@ -97,8 +106,8 @@ public class ExecuteNodeActionImpl implements ExecuteNodeAction {
         }
 
         try {
-            SvcLogicResource.QueryStatus response =
-                aaiServiceFactory.getAAIService().query(resourceType, false, null, resourceKey, ctxPrefix, null, ctx);
+            SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().query(resourceType, false, null,
+                    resourceKey, ctxPrefix, null, ctx);
             logger.info(AAI_RESPONSE_STR + response.toString());
             ctx.setAttribute(GET_RESOURCE_RESULT, response.toString());
         } catch (SvcLogicException e) {
@@ -126,8 +135,8 @@ public class ExecuteNodeActionImpl implements ExecuteNodeAction {
         data.put(attName, attValue);
 
         try {
-            SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService()
-                .update(resourceType, resourceKey, data, ctxPrefix, ctx);
+            SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().update(resourceType, resourceKey,
+                    data, ctxPrefix, ctx);
             logger.info(AAI_RESPONSE_STR + response.toString());
             ctx.setAttribute("postResource_result", response.toString());
         } catch (SvcLogicException e) {
@@ -150,8 +159,8 @@ public class ExecuteNodeActionImpl implements ExecuteNodeAction {
         }
 
         try {
-            SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService()
-                .delete(resourceType, resourceKey, ctx);
+            SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().delete(resourceType, resourceKey,
+                    ctx);
             logger.info(AAI_RESPONSE_STR + response.toString());
             ctx.setAttribute("deleteResource_result", response.toString());
         } catch (SvcLogicException e) {
@@ -162,6 +171,116 @@ public class ExecuteNodeActionImpl implements ExecuteNodeAction {
         }
     }
 
+    private void getVserverRelations(SvcLogicContext vnfCtx, SvcLogicContext ctx) throws APPCException {
+
+        logger.debug("Parsing Vserver details from VNF relations");
+        for (String ctxKeySet : vnfCtx.getAttributeKeySet()) {
+            if (ctxKeySet.startsWith("vnfRetrived.") && "vserver".equalsIgnoreCase(vnfCtx.getAttribute(ctxKeySet))) {
+                String vmKey = ctxKeySet.substring(0, ctxKeySet.length() - "related-to".length());
+                String vserverID = null;
+                String tenantID = null;
+                String cloudOwner = null;
+                String cloudRegionId = null;
+                int relationshipLength = getAttribute(vnfCtx, vmKey, RELATIONSHIP_DATA_LEN_PARAM);
+
+                for (int j = 0; j < relationshipLength; j++) { // loop inside
+                                                                // relationship
+                                                                // data, to get
+                                                                // vserver-id
+                                                                // and tenant-id
+                    String key = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
+                    String value = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
+                    vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
+                    if ("vserver.vserver-id".equals(key)) {
+                        vserverID = value;
+                    }
+                    if ("tenant.tenant-id".equals(key)) {
+                        tenantID = value;
+                    }
+                    if ("cloud-region.cloud-owner".equals(key)) {
+                        cloudOwner = value;
+                    }
+                    if ("cloud-region.cloud-region-id".equals(key)) {
+                        cloudRegionId = value;
+                    }
+                }
+                int relatedPropertyLength = getAttribute(vnfCtx, vmKey, RELATED_TO_PROPERTY_LEN_PARAM);
+                for (int j = 0; j < relatedPropertyLength; j++) { // loop inside
+                                                                    // related-to-property
+                                                                    // data, to
+                                                                    // get
+                                                                    // vserver-name
+                    String key = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-key");
+                    String value = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-value");
+                    vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
+                }
+                // Retrive VM relations to find vnfc's
+                // VM to VNFC is 1 to 1 relation
+                String vmRetrivalKey = "vserver.vserver-id = '" + vserverID + "' AND tenant.tenant_id = '" + tenantID
+                        + "'" + "' AND cloud-region.cloud-owner = '" + cloudOwner
+                        + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
+                Map<String, String> paramsVm = new HashMap<>();
+                paramsVm.put(RESOURCE_TYPE_PARAM, "vserver");
+                paramsVm.put(PREFIX_PARAM, "vmRetrived");
+                paramsVm.put(RESOURCE_KEY_PARAM, vmRetrivalKey);
+                SvcLogicContext vmCtx = new SvcLogicContext();
+
+                logger.debug("Retrieving VM details from A&AI");
+                getResource(paramsVm, vmCtx);
+                if ((SUCCESS_PARAM).equals(vmCtx.getAttribute(GET_RESOURCE_RESULT))) {
+                    if (logger.isDebugEnabled()) {
+                        logger.debug("Parsing VNFC details from VM relations");
+                    }
+                    vmURL = vmCtx.getAttribute("vmRetrived.vserver-selflink");
+                    vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].URL", vmURL);
+
+                    // loop through relationship-list data, to get vnfc
+                    // relations
+                    for (String ctxVnfcKeySet : vmCtx.getAttributeKeySet()) {
+                        if (ctxVnfcKeySet.startsWith("vmRetrived.")
+                                && "vnfc".equalsIgnoreCase(vmCtx.getAttribute(ctxVnfcKeySet))) {
+
+                            String vnfcKey = ctxVnfcKeySet.substring(0, ctxVnfcKeySet.length() - "related-to".length());
+
+                            relationshipLength = getAttribute(vmCtx, vnfcKey, RELATIONSHIP_DATA_LEN_PARAM);
+
+                            for (int j = 0; j < relationshipLength; j++) { // loop
+                                                                            // through
+                                                                            // relationship
+                                                                            // data,
+                                                                            // to
+                                                                            // get
+                                                                            // vnfc
+                                                                            // name
+                                String key = vmCtx
+                                        .getAttribute(vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
+                                String value = vmCtx
+                                        .getAttribute(vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
+                                if ("vnfc.vnfc-name".equalsIgnoreCase(key)) {
+                                    vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].VNFC", value);
+                                    vmSet = resolveVmSet(vnfcHierarchyMap, value);
+                                    vmSet.add(vmURL);
+                                    vnfcHierarchyMap.put(value, vmSet);
+                                    break; // VM to VNFC is 1 to 1 relation,
+                                            // once we got the VNFC name we can
+                                            // break the loop
+                                }
+                            }
+                        }
+                    }
+                } else {
+                    ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
+                    vnfHierarchyMap.put(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
+                    logger.error("Failed in getVnfHierarchy, Error retrieving Vserver details. Error message: "
+                            + vmCtx.getAttribute(GET_RESOURCE_RESULT));
+                    logger.warn("Incorrect or Incomplete VNF Hierarchy");
+                    throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
+                }
+                vmCount++;
+            }
+        }
+    }
+
     @Override
     public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
         if (logger.isDebugEnabled()) {
@@ -174,136 +293,34 @@ public class ExecuteNodeActionImpl implements ExecuteNodeAction {
         paramsVnf.put(PREFIX_PARAM, "vnfRetrived");
         paramsVnf.put(RESOURCE_KEY_PARAM, retrivalVnfKey);
         logger.debug("Retrieving VNF details from A&AI");
-        //Retrive all the relations of VNF
+        // Retrive all the relations of VNF
         SvcLogicContext vnfCtx = new SvcLogicContext();
         getResource(paramsVnf, vnfCtx);
         if (vnfCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
             trySetHeatStackIDAttribute(ctx, vnfCtx);
             ctx.setAttribute("vnf.type", vnfCtx.getAttribute("vnfRetrived.vnf-type"));
-            Map<String, String> vnfHierarchyMap = new ConcurrentHashMap<>();
-
-            Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
-            int vmCount = 0;
-            Set<String> vmSet;
-            String vmURL;
-            logger.debug("Parsing Vserver details from VNF relations");
-
-            //loop through relationship-list data, to get vserver relations
-            for (String ctxKeySet : vnfCtx.getAttributeKeySet()) {
-                if (ctxKeySet.startsWith("vnfRetrived.") && "vserver"
-                    .equalsIgnoreCase(vnfCtx.getAttribute(ctxKeySet))) {
-                    String vmKey = ctxKeySet.substring(0, ctxKeySet.length() - "related-to".length());
-                    String vserverID = null;
-                    String tenantID = null;
-                    String cloudOwner = null;
-                    String cloudRegionId = null;
-                    int relationshipLength = getAttribute(vnfCtx, vmKey, RELATIONSHIP_DATA_LEN_PARAM);
-
-                    for (int j = 0; j
-                        < relationshipLength;
-                        j++) {      //loop inside relationship data, to get vserver-id and tenant-id
-                        String key = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
-                        String value = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
-                        vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
-                        if ("vserver.vserver-id".equals(key)) {
-                            vserverID = value;
-                        }
-                        if ("tenant.tenant-id".equals(key)) {
-                            tenantID = value;
-                        }
-                        if ("cloud-region.cloud-owner".equals(key)) {
-                            cloudOwner = value;
-                        }
-                        if ("cloud-region.cloud-region-id".equals(key)) {
-                            cloudRegionId = value;
-                        }
-                    }
-                    int relatedPropertyLength = getAttribute(vnfCtx, vmKey, RELATED_TO_PROPERTY_LEN_PARAM);
-                    for (int j = 0;
-                        j < relatedPropertyLength; j++) {   //loop inside related-to-property data, to get vserver-name
-                        String key = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-key");
-                        String value = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-value");
-                        vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
-                    }
-                    //Retrive VM relations to find vnfc's
-                    //VM to VNFC is 1 to 1 relation
-                    String vmRetrivalKey = "vserver.vserver-id = '" + vserverID
-                        + "' AND tenant.tenant_id = '" + tenantID
-                        + "'" + "' AND cloud-region.cloud-owner = '" + cloudOwner
-                        + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
-                    Map<String, String> paramsVm = new HashMap<>();
-                    paramsVm.put(RESOURCE_TYPE_PARAM, "vserver");
-                    paramsVm.put(PREFIX_PARAM, "vmRetrived");
-                    paramsVm.put(RESOURCE_KEY_PARAM, vmRetrivalKey);
-                    SvcLogicContext vmCtx = new SvcLogicContext();
-
-                    logger.debug("Retrieving VM details from A&AI");
-                    getResource(paramsVm, vmCtx);
-                    if (vmCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
-                        if (logger.isDebugEnabled()) {
-                            logger.debug("Parsing VNFC details from VM relations");
-                        }
-                        vmURL = vmCtx.getAttribute("vmRetrived.vserver-selflink");
-                        vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].URL", vmURL);
 
-                        //loop through relationship-list data, to get vnfc relations
-                        for (String ctxVnfcKeySet : vmCtx.getAttributeKeySet()) {
-                            if (ctxVnfcKeySet.startsWith("vmRetrived.")
-                                && "vnfc".equalsIgnoreCase(vmCtx.getAttribute(ctxVnfcKeySet))) {
-
-                                String vnfcKey =
-                                    ctxVnfcKeySet.substring(0, ctxVnfcKeySet.length() - "related-to".length());
-
-                                relationshipLength = getAttribute(vmCtx, vnfcKey, RELATIONSHIP_DATA_LEN_PARAM);
-
-                                for (int j = 0; j
-                                    < relationshipLength;
-                                    j++) {          //loop through relationship data, to get vnfc name
-                                    String key = vmCtx.getAttribute(
-                                        vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
-                                    String value = vmCtx.getAttribute(
-                                        vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
-                                    if ("vnfc.vnfc-name".equalsIgnoreCase(key)) {
-                                        vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].VNFC", value);
-                                        vmSet = resolveVmSet(vnfcHierarchyMap, value);
-                                        vmSet.add(vmURL);
-                                        vnfcHierarchyMap.put(value, vmSet);
-                                        break; //VM to VNFC is 1 to 1 relation, once we got the VNFC name we can break the loop
-                                    }
-                                }
-                            }
-                        }
-                    } else {
-                        ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
-                        vnfHierarchyMap.put(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
-                        logger.error("Failed in getVnfHierarchy, Error retrieving Vserver details. Error message: "
-                            + vmCtx.getAttribute(GET_RESOURCE_RESULT));
-                        logger.warn("Incorrect or Incomplete VNF Hierarchy");
-                        throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
-                    }
-                    vmCount++;
-                }
-            }
+            // loop through relationship-list data, to get vserver relations
+            getVserverRelations(vnfCtx, ctx);
             vnfHierarchyMap.put("VNF.VMCount", Integer.toString(vmCount));
             if (vmCount == 0) {
                 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, "VM count is 0");
             }
-            //code changes for getting vnfcs hirearchy
+            // code changes for getting vnfcs hirearchy
             populateVnfcsDetailsinContext(vnfcHierarchyMap, ctx);
-            //vnf,vnfcCount
-            ctx.setAttribute("VNF.VNFCCount",
-                Integer.toString(vnfcHierarchyMap.size()));
-            //code changes for getting vnfcs hirearchy
+            // vnf,vnfcCount
+            ctx.setAttribute("VNF.VNFCCount", Integer.toString(vnfcHierarchyMap.size()));
+            // code changes for getting vnfcs hirearchy
             ctx.setAttribute(GET_VNF_HIERARCHY_RESULT_PARAM, SUCCESS_PARAM);
-            //Finally set all attributes to ctx
-            for (Entry<String, String> entry: vnfHierarchyMap.entrySet()) {
+            // Finally set all attributes to ctx
+            for (Entry<String, String> entry : vnfHierarchyMap.entrySet()) {
                 ctx.setAttribute(entry.getKey(), entry.getValue());
             }
         } else {
             ctx.setAttribute(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
             ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
-            logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: " + ctx
-                .getAttribute(GET_RESOURCE_RESULT));
+            logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: "
+                    + ctx.getAttribute(GET_RESOURCE_RESULT));
             logger.warn("Incorrect or Incomplete VNF Hierarchy");
             throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
         }
@@ -330,13 +347,13 @@ public class ExecuteNodeActionImpl implements ExecuteNodeAction {
 
     private int getAttribute(SvcLogicContext ctx, String key, String param) {
         if (ctx.getAttributeKeySet().contains(key + param)) {
-           return Integer.parseInt(ctx.getAttribute(key + param));
+            return Integer.parseInt(ctx.getAttribute(key + param));
         }
         return 0;
     }
 
     void populateVnfcsDetailsinContext(Map<String, Set<String>> vnfcHierarchyMap, SvcLogicContext ctx)
-        throws APPCException {
+            throws APPCException {
         SvcLogicContext vnfcCtx = new SvcLogicContext();
         int vnfcCounter = 0;
         for (Entry<String, Set<String>> entry : vnfcHierarchyMap.entrySet()) {
@@ -349,23 +366,20 @@ public class ExecuteNodeActionImpl implements ExecuteNodeAction {
             logger.debug("Retrieving VM details from A&AI");
             getResource(paramsVnfc, vnfcCtx);
             if (vnfcCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
-                if (logger.isDebugEnabled()){
+                if (logger.isDebugEnabled()) {
                     logger.debug("Parsing VNFC details from VM relations");
                 }
-                //putting required values in the map
-                //vnf.vnfc[vnfcIndex].type
-                ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].TYPE",
-                    vnfcCtx.getAttribute("vnfcRetrived.vnfc-type"));
+                // putting required values in the map
+                // vnf.vnfc[vnfcIndex].type
+                ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].TYPE", vnfcCtx.getAttribute("vnfcRetrived.vnfc-type"));
 
                 // vnf.vnfc[vnfcIndex].name
-                ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].NAME",
-                    vnfcCtx.getAttribute("vnfcRetrived.vnfc-name"));
+                ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].NAME", vnfcCtx.getAttribute("vnfcRetrived.vnfc-name"));
 
-                //vnf.vnfc[vnfcIndex].vmCount
+                // vnf.vnfc[vnfcIndex].vmCount
                 Set<String> vmSet = entry.getValue();
                 String vmCountinVnfcs = Integer.toString(vmSet.size());
-                ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM_COUNT",
-                    vmCountinVnfcs);
+                ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM_COUNT", vmCountinVnfcs);
                 int vmCount = 0;
                 for (String vmURL : vmSet) {
                     ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM[" + vmCount++ + "].URL", vmURL);