blob: fa8845e3821bcdf6b89bf30a22a0eb68a5527d0d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/*
* Copyright © 2016-2018 European Support Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openecomp.sdc.heat.services;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
public class HeatResourceUtil {
private static final String UNDERSCORE = "_";
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 = 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;
public static Optional<String> evaluateNetworkRoleFromResourceId(String resourceId, String resourceType) {
Optional<PortType> portType = getPortType(resourceType);
if (portType.isPresent()) {
String portResourceIdRegex = PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + WORDS_REGEX + UNDERSCORE + portType.get().getPortTypeName()
+ PORT_RESOURCE_ID_REGEX_SUFFIX;
String portIntResourceIdRegex = PORT_INT_RESOURCE_ID_REGEX_PREFIX + portType.get().getPortTypeName() + PORT_RESOURCE_ID_REGEX_SUFFIX;
String portNetworkRole = getNetworkRole(resourceId, portResourceIdRegex);
String portIntNetworkRole = getNetworkRole(resourceId, portIntResourceIdRegex);
return Optional.ofNullable(Objects.nonNull(portNetworkRole) ? portNetworkRole : portIntNetworkRole);
}
return Optional.empty();
}
private static Optional<PortType> getPortType(String resourceType) {
if (HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource().equals(resourceType)) {
return Optional.of(PortType.VMI);
} else if (HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource().equals(resourceType)) {
return Optional.of(PortType.PORT);
}
return Optional.empty();
}
/**
* Extract network role from sub interface id optional.
*
* @param resourceId the resource id
* @param resourceType the resource type
* @return the optional
*/
public static Optional<String> extractNetworkRoleFromSubInterfaceId(String resourceId, String resourceType) {
Optional<PortType> portType = getPortType(resourceType);
if (portType.isPresent()) {
String subInterfaceResourceIdRegex =
SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX + portType.get().getPortTypeName() + PORT_RESOURCE_ID_REGEX_SUFFIX;
return Optional.ofNullable(getNetworkRole(resourceId, subInterfaceResourceIdRegex));
}
return Optional.empty();
}
private static String getNetworkRole(String portResourceId, String portIdRegex) {
Pattern pattern = Pattern.compile(portIdRegex);
Matcher matcher = pattern.matcher(portResourceId);
if (matcher.matches()) {
String networkRole = matcher.group(3);
//Assuming network role will not contain ONLY digits
if (!networkRole.matches("\\d+")) {
return matcher.group(3);
}
}
return null;
}
@AllArgsConstructor
@Getter
private enum PortType {
PORT("port"), VMI("vmi");
private String portTypeName;
}
}
|