Code change to evaluate Network role 47/37347/2
authorsiddharth0905 <siddharth.singh4@amdocs.com>
Wed, 21 Mar 2018 11:26:35 +0000 (13:26 +0200)
committersiddharth0905 <siddharth.singh4@amdocs.com>
Thu, 22 Mar 2018 13:05:54 +0000 (18:35 +0530)
Code change to evaluate Network Role of Sub interface resource

Change-Id: I73b1cad7eb15c59153751a77a4bc78101042941d
Issue-ID: SDC-1072
Signed-off-by: siddharth0905 <siddharth.singh4@amdocs.com>
openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java
openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ResourceTranslationNestedImpl.java
openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ContrailV2VlanToInterfaceResourceConnectionTest.java

index 13d1a49..f965352 100644 (file)
@@ -109,11 +109,16 @@ public class HeatToToscaUtil {
   private static final String GET_ATTR = "get_attr";
   private static final String GET_RESOURCE = "get_resource";
   private static final String UNDERSCORE = "_";
-  private static final String PORT_RESOURCE_ID_REGEX_SUFFIX = "(_\\d)*";
+  private static final String WORDS_REGEX = "(\\w+)";
+  private static final String PORT_RESOURCE_ID_REGEX_SUFFIX = "(_\\d+)*";
   private static final String PORT_RESOURCE_ID_REGEX_PREFIX =
-      "(\\w+)" + PORT_RESOURCE_ID_REGEX_SUFFIX;
-  private static final String PORT_INT_RESOURCE_ID_REGEX_PREFIX =
-      PORT_RESOURCE_ID_REGEX_PREFIX + "_" + "int_(\\w+)_";
+      WORDS_REGEX + PORT_RESOURCE_ID_REGEX_SUFFIX;
+  private static final String PORT_INT_RESOURCE_ID_REGEX_PREFIX = PORT_RESOURCE_ID_REGEX_PREFIX
+      + UNDERSCORE + "int_"+ WORDS_REGEX + UNDERSCORE;
+  private static final String SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX =
+      PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + "subint_"+ WORDS_REGEX + UNDERSCORE;
+  private static final String SUB_INTERFACE_REGEX = WORDS_REGEX + PORT_RESOURCE_ID_REGEX_SUFFIX
+      + "_subint_(\\w_+)*vmi" + PORT_RESOURCE_ID_REGEX_SUFFIX;
 
   /**
    * Load and translate template data translator output.
@@ -1485,8 +1490,8 @@ public class HeatToToscaUtil {
 
   //Method evaluate the  network role from sub interface node template id, designed considering
   // only single sub interface present in nested file else it will return null
-  public static Optional<String> getNetworkRoleFromResource(Resource resource,
-                                                            TranslationContext translationContext) {
+  public static Optional<String> getNetworkRoleFromSubInterfaceId(Resource resource,
+                                                   TranslationContext translationContext) {
     Optional<String> networkRole = Optional.empty();
     Optional<String> nestedHeatFileName = HeatToToscaUtil.getNestedHeatFileName(resource);
 
@@ -1520,30 +1525,43 @@ public class HeatToToscaUtil {
                                                                    String resourceType) {
     if (resourceType.equals(
         HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource())) {
-      return Optional.ofNullable(extractNetworkRoleFromPortId(resourceId, PortType.VMI));
+      return Optional.ofNullable(extractNetworkRoleFromResourceId(resourceId, PortType.VMI));
     }
 
     if (resourceType.equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource())) {
-      return Optional.ofNullable(extractNetworkRoleFromPortId
-          (resourceId, PortType.PORT));
+      return Optional.ofNullable(extractNetworkRoleFromResourceId(resourceId, PortType.PORT));
     }
     return Optional.empty();
   }
 
-  private static String extractNetworkRoleFromPortId(String portResourceId, PortType portType) {
+  private static String extractNetworkRoleFromResourceId(String portResourceId, PortType portType) {
+
+    if (portResourceId.matches(SUB_INTERFACE_REGEX)) {
+      return extractNetworkRoleFromSubInterfaceId(portResourceId, portType);
+    }
+
     String portResourceIdRegex =
-        PORT_RESOURCE_ID_REGEX_PREFIX + "_(\\w+)_" + portType.getPortTypeName() +
-            PORT_RESOURCE_ID_REGEX_SUFFIX;
+        PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + WORDS_REGEX + UNDERSCORE + portType.getPortTypeName()
+            PORT_RESOURCE_ID_REGEX_SUFFIX;
     String portIntResourceIdRegex =
-        PORT_INT_RESOURCE_ID_REGEX_PREFIX + portType.getPortTypeName() +
-            PORT_RESOURCE_ID_REGEX_SUFFIX;
+        PORT_INT_RESOURCE_ID_REGEX_PREFIX + portType.getPortTypeName()
+            PORT_RESOURCE_ID_REGEX_SUFFIX;
 
-    String portNetworkRole = getPortNetworkRole(portResourceId, portResourceIdRegex);
-    String portIntNetworkRole = getPortNetworkRole(portResourceId, portIntResourceIdRegex);
+    String portNetworkRole = getNetworkRole(portResourceId, portResourceIdRegex);
+    String portIntNetworkRole = getNetworkRole(portResourceId, portIntResourceIdRegex);
 
     return Objects.nonNull(portNetworkRole) ? portNetworkRole : portIntNetworkRole;
   }
 
+  private static String extractNetworkRoleFromSubInterfaceId(String  resourceId,
+                                                             PortType portType) {
+    String subInterfaceResourceIdRegex =
+        SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX + portType.getPortTypeName()
+            + PORT_RESOURCE_ID_REGEX_SUFFIX;
+
+    return getNetworkRole(resourceId, subInterfaceResourceIdRegex);
+  }
+
   private enum PortType {
     PORT("port"),
     VMI("vmi");
@@ -1559,7 +1577,7 @@ public class HeatToToscaUtil {
     }
   }
 
-  private static String getPortNetworkRole(String portResourceId, String portIdRegex) {
+  private static String getNetworkRole(String portResourceId, String portIdRegex) {
     Pattern pattern = Pattern.compile(portIdRegex);
     Matcher matcher = pattern.matcher(portResourceId);
     if (matcher.matches()) {
index a3b8009..00acb66 100644 (file)
@@ -131,7 +131,7 @@ public class ResourceTranslationNestedImpl extends ResourceTranslationBase {
       return;
     }
     Optional<String> subInterfaceNetworkRole =
-        HeatToToscaUtil.getNetworkRoleFromResource(translateTo.getResource(), translateTo.getContext());
+        HeatToToscaUtil.getNetworkRoleFromSubInterfaceId(translateTo.getResource(), translateTo.getContext());
     subInterfaceNetworkRole.ifPresent(subInterfaceTemplateConsolidationData.get()::setNetworkRole);
     Object count = getSubInterfaceCountFromResourceProperties(translateTo);
     subInterfaceTemplateConsolidationData.get().setResourceGroupCount(count);
index c7c780c..7815824 100644 (file)
@@ -108,7 +108,7 @@ public class ContrailV2VlanToInterfaceResourceConnectionTest extends BaseResourc
     Resource targetResource = new Resource();
     targetResource.setType(NESTED_FILE_NAME_INOUT_ATTR_TEST);
 
-    Optional<String> networkRole = HeatToToscaUtil.getNetworkRoleFromResource(targetResource, this
+    Optional<String> networkRole = HeatToToscaUtil.getNetworkRoleFromSubInterfaceId(targetResource, this
         .translationContext);
 
     Assert.assertEquals(NETWORK_ROLE_INOUT_ATTR_TEST,networkRole.get());
@@ -131,6 +131,34 @@ public class ContrailV2VlanToInterfaceResourceConnectionTest extends BaseResourc
 
   }
 
+  @Test
+  public void testSubInterfaceResourceNetworkRolePositive() throws Exception {
+    List<String> subInterfaceResourceIds=Arrays.asList("vm_type_11_subint_networkrole_vmi_11",
+        "v_subint_networkrole_vmi", "v_1_subint_networkrole_vmi", "v_subint_networkrole_vmi_11",
+        "vm_type_subint_networkrole_vmi_11", "vm_type_11_subint_networkrole_vmi",
+        "vm_type_subint_networkrole_vmi");
+
+    subInterfaceResourceIds.forEach(resourceId -> {
+      Optional<String> networkRole=HeatToToscaUtil.evaluateNetworkRoleFromResourceId(resourceId,
+        HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource());
+      Assert.assertTrue(networkRole.isPresent()
+          && "networkrole".equals(networkRole.get()));
+      }
+    );
+  }
+
+  @Test
+  public void testSubInterfaceResourceNetworkRoleNegative() throws Exception {
+    List<String> subInterfaceResourceIds=Arrays.asList("vm_type_11_subint_vmi_11",
+        "vm_type_11_subint_11_vmi_11");
+
+    subInterfaceResourceIds.forEach(resourceId -> {
+        Optional<String> networkRole=HeatToToscaUtil.evaluateNetworkRoleFromResourceId(resourceId,
+          HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource());
+        Assert.assertFalse(networkRole.isPresent());
+      }
+    );
+  }
 
   @Test
   public void testIsSubInterfaceResourceUtil() throws Exception {