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 lombok.AllArgsConstructor;
26 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
28 public class HeatResourceUtil {
30 private static final String UNDERSCORE = "_";
31 private static final String WORDS_REGEX = "(\\w+)";
32 private static final String PORT_RESOURCE_ID_REGEX_SUFFIX = "(_\\d+)*";
33 private static final String PORT_RESOURCE_ID_REGEX_PREFIX =
34 WORDS_REGEX + PORT_RESOURCE_ID_REGEX_SUFFIX;
35 private static final String PORT_INT_RESOURCE_ID_REGEX_PREFIX = PORT_RESOURCE_ID_REGEX_PREFIX
36 + UNDERSCORE + "int_" + WORDS_REGEX + UNDERSCORE;
37 private static final String SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX =
38 PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + "subint_" + WORDS_REGEX + UNDERSCORE;
40 public static Optional<String> evaluateNetworkRoleFromResourceId(String resourceId,
41 String resourceType) {
42 Optional<PortType> portType = getPortType(resourceType);
43 if (portType.isPresent()) {
44 String portResourceIdRegex =
45 PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + WORDS_REGEX + UNDERSCORE
46 + portType.get().getPortTypeName() + PORT_RESOURCE_ID_REGEX_SUFFIX;
47 String portIntResourceIdRegex =
48 PORT_INT_RESOURCE_ID_REGEX_PREFIX + portType.get().getPortTypeName()
49 + PORT_RESOURCE_ID_REGEX_SUFFIX;
51 String portNetworkRole = getNetworkRole(resourceId, portResourceIdRegex);
52 String portIntNetworkRole = getNetworkRole(resourceId, portIntResourceIdRegex);
54 return Optional.ofNullable(Objects.nonNull(portNetworkRole)
55 ? portNetworkRole : portIntNetworkRole);
57 return Optional.empty();
60 private static Optional<PortType> getPortType(String resourceType) {
61 if (HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource()
62 .equals(resourceType)) {
63 return Optional.of(PortType.VMI);
64 } else if (HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource().equals(resourceType)) {
65 return Optional.of(PortType.PORT);
67 return Optional.empty();
71 * Extract network role from sub interface id optional.
73 * @param resourceId the resource id
74 * @param resourceType the resource type
75 * @return the optional
77 public static Optional<String> extractNetworkRoleFromSubInterfaceId(String resourceId,
78 String resourceType) {
79 Optional<PortType> portType = getPortType(resourceType);
80 if (portType.isPresent()) {
81 String subInterfaceResourceIdRegex =
82 SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX + portType.get().getPortTypeName()
83 + PORT_RESOURCE_ID_REGEX_SUFFIX;
85 return Optional.ofNullable(getNetworkRole(resourceId, subInterfaceResourceIdRegex));
87 return Optional.empty();
92 private enum PortType {
96 private String portTypeName;
99 private static String getNetworkRole(String portResourceId, String portIdRegex) {
100 Pattern pattern = Pattern.compile(portIdRegex);
101 Matcher matcher = pattern.matcher(portResourceId);
102 if (matcher.matches()) {
103 String networkRole = matcher.group(3);
104 //Assuming network role will not contain ONLY digits
105 if (!networkRole.matches("\\d+")) {
106 return matcher.group(3);