2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.heat.services;
19 import java.util.Objects;
20 import java.util.Optional;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
26 public class HeatResourceUtil {
28 private static final String UNDERSCORE = "_";
29 private static final String WORDS_REGEX = "(\\w+)";
30 private static final String PORT_RESOURCE_ID_REGEX_SUFFIX = "(_\\d+)*";
31 private static final String PORT_RESOURCE_ID_REGEX_PREFIX =
32 WORDS_REGEX + PORT_RESOURCE_ID_REGEX_SUFFIX;
33 private static final String PORT_INT_RESOURCE_ID_REGEX_PREFIX = PORT_RESOURCE_ID_REGEX_PREFIX
34 + UNDERSCORE + "int_"+ WORDS_REGEX + UNDERSCORE;
35 private static final String SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX =
36 PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + "subint_"+ WORDS_REGEX + UNDERSCORE;
38 public static Optional<String> evaluateNetworkRoleFromResourceId(String resourceId,
39 String resourceType) {
40 Optional<PortType> portType = getPortType(resourceType);
41 if (portType.isPresent()) {
42 String portResourceIdRegex =
43 PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + WORDS_REGEX + UNDERSCORE
44 + portType.get().getPortTypeName() + PORT_RESOURCE_ID_REGEX_SUFFIX;
45 String portIntResourceIdRegex =
46 PORT_INT_RESOURCE_ID_REGEX_PREFIX + portType.get().getPortTypeName()
47 + PORT_RESOURCE_ID_REGEX_SUFFIX;
49 String portNetworkRole = getNetworkRole(resourceId, portResourceIdRegex);
50 String portIntNetworkRole = getNetworkRole(resourceId, portIntResourceIdRegex);
52 return Optional.ofNullable(Objects.nonNull(portNetworkRole)
53 ? portNetworkRole : portIntNetworkRole);
55 return Optional.empty();
58 private static Optional<PortType> getPortType(String resourceType) {
59 if (resourceType.equals(
60 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource())) {
61 return Optional.of(PortType.VMI);
62 } else if (resourceType.equals(
63 HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource())) {
64 return Optional.of(PortType.PORT);
66 return Optional.empty();
69 public static Optional<String> extractNetworkRoleFromSubInterfaceId(String resourceId,
70 String resourceType) {
71 Optional<PortType> portType = getPortType(resourceType);
72 if (portType.isPresent()) {
73 String subInterfaceResourceIdRegex =
74 SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX + portType.get().getPortTypeName()
75 + PORT_RESOURCE_ID_REGEX_SUFFIX;
77 return Optional.ofNullable(getNetworkRole(resourceId, subInterfaceResourceIdRegex));
79 return Optional.empty();
82 private enum PortType {
86 private String portTypeName;
88 PortType(String portTypeName) {
89 this.portTypeName = portTypeName;
92 public String getPortTypeName() {
97 private static String getNetworkRole(String portResourceId, String portIdRegex) {
98 Pattern pattern = Pattern.compile(portIdRegex);
99 Matcher matcher = pattern.matcher(portResourceId);
100 if (matcher.matches()) {
101 String networkRole = matcher.group(3);
102 //Assuming network role will not contain ONLY digits
103 if (!networkRole.matches("\\d+")) {
104 return matcher.group(3);