4563e686e50538e270b9bcb49ffe2e426b3481d8
[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 lombok.AllArgsConstructor;
25 import lombok.Getter;
26 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
27
28 public class HeatResourceUtil {
29
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;
39
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;
50
51             String portNetworkRole = getNetworkRole(resourceId, portResourceIdRegex);
52             String portIntNetworkRole = getNetworkRole(resourceId, portIntResourceIdRegex);
53
54             return Optional.ofNullable(Objects.nonNull(portNetworkRole)
55                     ? portNetworkRole : portIntNetworkRole);
56         }
57         return Optional.empty();
58     }
59
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);
66         }
67         return Optional.empty();
68     }
69
70     /**
71      * Extract network role from sub interface id optional.
72      *
73      * @param resourceId   the resource id
74      * @param resourceType the resource type
75      * @return the optional
76      */
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;
84
85             return Optional.ofNullable(getNetworkRole(resourceId, subInterfaceResourceIdRegex));
86         }
87         return Optional.empty();
88     }
89
90     @AllArgsConstructor
91     @Getter
92     private enum PortType {
93         PORT("port"),
94         VMI("vmi");
95
96         private String portTypeName;
97     }
98
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);
107             }
108         }
109         return null;
110     }
111
112 }