1 package org.openecomp.sdc.translator.services.heattotosca.helper;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.LinkedList;
9 public class ResourceTranslationNeutronPortHelper {
11 public static final String IP_COUNT_REQUIRED = "ip_count_required";
12 public static final String FLOATING_IP_COUNT_REQUIRED = "floating_ip_count_required";
13 public static final String NETWORK = "network";
14 public static final String NETWORK_ROLE_TAG = "network_role_tag";
15 public static final String FIXED_IPS = "fixed_ips";
16 public static final String IP_VERSION = "ip_version";
17 public static final String IP_ADDRESS = "ip_address";
18 public static final String GET_INPUT = "get_input";
19 public static final String ALLOWED_ADDRESS_PAIRS = "allowed_address_pairs";
20 public static final String FLOATING_IP = "_floating_ip";
21 public static final String FLOATING_V6_IP = "_floating_v6_ip";
22 public static final String IPS = "_ips";
23 public static final String V6_IPS = "_v6_ips";
24 public static final String NET_NAME = "_net_name";
25 public static final String NET_ID = "_net_id";
26 public static final String NET_FQDN = "_net_fqdn";
27 public static final String IPV4_REGEX = "\\w*_ip_\\d+";
28 public static final String IPV6_REGEX = "\\w*_v6_ip_\\d+";
29 public static final String MAC_COUNT_REQUIRED = "mac_count_required";
30 public static final String MAC_ADDRESS = "mac_address";
31 public static final String IS_REQUIRED = "is_required";
32 public static final String IP_REQUIREMENTS = "ip_requirements";
33 public static final String MAC_REQUIREMENTS = "mac_requirements";
35 public void setAdditionalProperties(Map<String, Object> properties) {
36 setNetworkRoleTag(properties);
37 Map<String, Object> ipRequirements = new HashMap();
38 Map<String, Object> macRequirements = new HashMap();
39 Map<String, Object> isRequired = new HashMap();
40 Map<String, Object> floatingIsRequired = new HashMap();
41 Map<String, Object> macIsRequired = new HashMap();
43 isRequired.put(IS_REQUIRED, Boolean.FALSE);
44 floatingIsRequired.put(IS_REQUIRED, Boolean.FALSE);
45 macIsRequired.put(IS_REQUIRED, Boolean.FALSE);
47 ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
48 ipRequirements.put(FLOATING_IP_COUNT_REQUIRED, floatingIsRequired);
49 ipRequirements.put(IP_VERSION, 4);
50 macRequirements.put(MAC_COUNT_REQUIRED, macIsRequired);
52 List<Map<String, Object>> ipRequirementsList = new ArrayList<>();
53 ipRequirementsList.add(ipRequirements);
54 properties.put(IP_REQUIREMENTS , ipRequirementsList);
56 properties.put(MAC_REQUIREMENTS , macRequirements);
58 setIpVersion(properties);
59 setFloatingIpVersion(properties);
61 setMacCount(properties);
64 private void setMacCount(Map<String, Object> properties) {
65 if(properties.containsKey(MAC_ADDRESS)) {
66 Map<String, Object> macRequirements = (Map<String, Object>) properties.get(MAC_REQUIREMENTS);
67 Map<String, Object> macIsRequired = new HashMap();
68 macIsRequired.put(IS_REQUIRED, Boolean.TRUE);
69 macRequirements.put(MAC_COUNT_REQUIRED, macIsRequired);
70 properties.put(MAC_REQUIREMENTS, macRequirements);
74 private void setFloatingIpVersion(Map<String, Object> properties) {
75 List<Map<String, Object>> ipRequirementsList =
76 (List<Map<String, Object>>) properties.get(IP_REQUIREMENTS);
77 Map<String, Object> ipRequirements = ipRequirementsList.get(0);
79 Map<String, Object> isRequired = new HashMap();
80 isRequired.put(IS_REQUIRED, Boolean.TRUE);
82 propertyValue = properties.get(ALLOWED_ADDRESS_PAIRS);
83 if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
84 Map.Entry<String, Object> mapEntry =
85 (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next();
86 if (getFloatingIpVersion(mapEntry.getValue()) != null) {
87 ipRequirements.put(IP_VERSION, getFloatingIpVersion(mapEntry.getValue()));
88 ipRequirements.put(FLOATING_IP_COUNT_REQUIRED, isRequired);
91 else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
92 for (int i = 0; i < ((List) propertyValue).size(); i++) {
93 Object ipMap = ((List) propertyValue).get(i);
94 if(ipMap instanceof Map && !((Map) ipMap).isEmpty()) {
95 Object ipAddressMap = ((Map) ipMap).get(IP_ADDRESS);
96 if (ipAddressMap instanceof Map && !((Map) ipAddressMap).isEmpty()) {
97 Object ipList = ((Map) ipAddressMap).get(GET_INPUT);
98 if (ipList instanceof String && !((String) ipList).isEmpty()) {
99 if (getFloatingIpVersion(ipList) != null) {
100 ipRequirements.put(IP_VERSION, getFloatingIpVersion(ipList));
101 ipRequirements.put(FLOATING_IP_COUNT_REQUIRED, isRequired);
110 private void setIpVersion(Map<String, Object> properties) {
111 List<Map<String, Object>> ipRequirementsList =
112 (List<Map<String, Object>>) properties.get(IP_REQUIREMENTS);
113 Map<String, Object> ipRequirements = ipRequirementsList.get(0);
114 Object propertyValue;
115 Map<String, Object> isRequired = new HashMap();
116 isRequired.put(IS_REQUIRED, Boolean.TRUE);
118 propertyValue = properties.get(FIXED_IPS);
119 if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
120 Map.Entry<String, Object> mapEntry =
121 (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next();
122 if (getIpVersion(mapEntry.getValue()) != null) {
123 ipRequirements.put(IP_VERSION, getIpVersion(mapEntry.getValue()));
124 ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
127 else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
128 for (int i = 0; i < ((List) propertyValue).size(); i++) {
129 Object ipMap = ((List) propertyValue).get(i);
130 if(ipMap instanceof Map && !((Map) ipMap).isEmpty()) {
131 Object ipAddressMap = ((Map) ipMap).get(IP_ADDRESS);
132 if (ipAddressMap instanceof Map && !((Map) ipAddressMap).isEmpty()) {
133 Object ipList = ((Map) ipAddressMap).get(GET_INPUT);
134 if (ipList instanceof List && !((List) ipList).isEmpty()) {
135 if (getIpVersion(((List) ipList).get(0)) != null) {
136 ipRequirements.put(IP_VERSION, getIpVersion(((List) ipList).get(0)));
137 ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
140 else if (ipList instanceof String && !((String) ipList).isEmpty()) {
141 if (getIpVersion(ipList) != null) {
142 ipRequirements.put(IP_VERSION, getIpVersion(ipList));
143 ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
152 private void setNetworkRoleTag(Map<String, Object> properties) {
153 Object propertyValue = properties.get(NETWORK);
154 if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
155 Map.Entry<String, String> mapEntry =
156 (Map.Entry<String, String>) ((Map) propertyValue).entrySet().iterator().next();
157 if (mapEntry.getValue() instanceof String && getNetworkRole(mapEntry.getValue())!=null) {
158 properties.put(NETWORK_ROLE_TAG, getNetworkRole(mapEntry.getValue()));
163 private Object getFloatingIpVersion(Object value) {
164 Object ipVersion = null;
165 if(value instanceof String) {
166 if (((String) value).endsWith(FLOATING_V6_IP)) {
176 private Object getIpVersion(Object value) {
177 Object ipVersion = null;
178 if(value instanceof String) {
179 if (((String) value).endsWith(V6_IPS) || ((String) value).matches(IPV6_REGEX)) {
189 private Object getNetworkRole(String value) {
190 Object networkRole = null;
191 if(value.endsWith(NET_NAME)) {
192 networkRole = (Object) value.substring(0, value.length() - NET_NAME.length());
194 else if(value.endsWith(NET_ID)) {
195 networkRole = (Object) value.substring(0, value.length() - NET_ID.length());
197 else if(value.endsWith(NET_FQDN)) {
198 networkRole = (Object) value.substring(0, value.length() - NET_FQDN.length());