a44b0196d47d15a5778db323943c6e254e9b4ca1
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.heat.services;
18
19 import java.util.Objects;
20 import java.util.Optional;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
25
26 public class HeatResourceUtil {
27
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;
37
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;
48
49             String portNetworkRole = getNetworkRole(resourceId, portResourceIdRegex);
50             String portIntNetworkRole = getNetworkRole(resourceId, portIntResourceIdRegex);
51
52             return Optional.ofNullable(Objects.nonNull(portNetworkRole)
53                     ? portNetworkRole : portIntNetworkRole);
54         }
55         return Optional.empty();
56     }
57
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);
65         }
66         return Optional.empty();
67     }
68
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;
76
77             return Optional.ofNullable(getNetworkRole(resourceId, subInterfaceResourceIdRegex));
78         }
79         return Optional.empty();
80     }
81
82     private enum PortType {
83         PORT("port"),
84         VMI("vmi");
85
86         private String portTypeName;
87
88         PortType(String portTypeName) {
89             this.portTypeName = portTypeName;
90         }
91
92         public String getPortTypeName() {
93             return portTypeName;
94         }
95     }
96
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);
105             }
106         }
107         return null;
108     }
109
110 }