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.
//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);
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");
}
}
- 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()) {
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());
}
+ @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 {