Fixed SONAR issues
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / helper / ResourceTranslationNeutronPortHelper.java
1 package org.openecomp.sdc.translator.services.heattotosca.helper;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 public class ResourceTranslationNeutronPortHelper {
9
10   public static final String IP_COUNT_REQUIRED = "ip_count_required";
11   public static final String FLOATING_IP_COUNT_REQUIRED = "floating_ip_count_required";
12   public static final String NETWORK = "network";
13   public static final String NETWORK_ROLE_TAG = "network_role_tag";
14   public static final String FIXED_IPS = "fixed_ips";
15   public static final String IP_VERSION = "ip_version";
16   public static final String IP_ADDRESS = "ip_address";
17   public static final String GET_INPUT = "get_input";
18   public static final String ALLOWED_ADDRESS_PAIRS = "allowed_address_pairs";
19   public static final String FLOATING_IP = "_floating_ip";
20   public static final String FLOATING_V6_IP = "_floating_v6_ip";
21   public static final String IPS = "_ips";
22   public static final String V6_IPS = "_v6_ips";
23   public static final String NET_NAME = "_net_name";
24   public static final String NET_ID = "_net_id";
25   public static final String NET_FQDN = "_net_fqdn";
26   public static final String IPV4_REGEX = "\\w*_ip_\\d+";
27   public static final String IPV6_REGEX = "\\w*_v6_ip_\\d+";
28   public static final String MAC_COUNT_REQUIRED = "mac_count_required";
29   public static final String MAC_ADDRESS = "mac_address";
30   public static final String IS_REQUIRED = "is_required";
31   public static final String IP_REQUIREMENTS = "ip_requirements";
32   public static final String MAC_REQUIREMENTS = "mac_requirements";
33
34   public void setAdditionalProperties(Map<String, Object> properties) {
35     setNetworkRoleTag(properties);
36     Map<String, Object> ipRequirements = new HashMap();
37     Map<String, Object> macRequirements = new HashMap();
38     Map<String, Object> isRequired = new HashMap();
39     Map<String, Object> floatingIsRequired = new HashMap();
40     Map<String, Object> macIsRequired = new HashMap();
41
42     isRequired.put(IS_REQUIRED, Boolean.FALSE);
43     floatingIsRequired.put(IS_REQUIRED, Boolean.FALSE);
44     macIsRequired.put(IS_REQUIRED, Boolean.FALSE);
45
46     ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
47     ipRequirements.put(FLOATING_IP_COUNT_REQUIRED, floatingIsRequired);
48     ipRequirements.put(IP_VERSION, 4);
49     macRequirements.put(MAC_COUNT_REQUIRED, macIsRequired);
50
51     List<Map<String, Object>> ipRequirementsList = new ArrayList<>();
52     ipRequirementsList.add(ipRequirements);
53     properties.put(IP_REQUIREMENTS , ipRequirementsList);
54
55     properties.put(MAC_REQUIREMENTS , macRequirements);
56
57     setIpVersion(properties);
58     setFloatingIpVersion(properties);
59
60     setMacCount(properties);
61   }
62
63   private void setMacCount(Map<String, Object> properties) {
64     if(properties.containsKey(MAC_ADDRESS)) {
65       Map<String, Object> macRequirements = (Map<String, Object>) properties.get(MAC_REQUIREMENTS);
66       Map<String, Object> macIsRequired = new HashMap();
67       macIsRequired.put(IS_REQUIRED, Boolean.TRUE);
68       macRequirements.put(MAC_COUNT_REQUIRED, macIsRequired);
69       properties.put(MAC_REQUIREMENTS, macRequirements);
70     }
71   }
72
73   private void setFloatingIpVersion(Map<String, Object> properties) {
74     List<Map<String, Object>> ipRequirementsList =
75         (List<Map<String, Object>>) properties.get(IP_REQUIREMENTS);
76     Map<String, Object> ipRequirements = ipRequirementsList.get(0);
77     Object propertyValue;
78     Map<String, Object> isRequired = new HashMap();
79     isRequired.put(IS_REQUIRED, Boolean.TRUE);
80
81     propertyValue = properties.get(ALLOWED_ADDRESS_PAIRS);
82     if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
83       Map.Entry<String, Object> mapEntry =
84           (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next();
85       if (getFloatingIpVersion(mapEntry.getValue()) != null) {
86         ipRequirements.put(IP_VERSION, getFloatingIpVersion(mapEntry.getValue()));
87         ipRequirements.put(FLOATING_IP_COUNT_REQUIRED, isRequired);
88       }
89     }
90     else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
91       for (int i = 0; i < ((List) propertyValue).size(); i++) {
92         Object ipMap = ((List) propertyValue).get(i);
93         if(ipMap instanceof Map && !((Map) ipMap).isEmpty()) {
94           Object ipAddressMap = ((Map) ipMap).get(IP_ADDRESS);
95           if (ipAddressMap instanceof Map && !((Map) ipAddressMap).isEmpty()) {
96             Object ipList = ((Map) ipAddressMap).get(GET_INPUT);
97             if (ipList instanceof String && !((String) ipList).isEmpty()) {
98               if (getFloatingIpVersion(ipList) != null) {
99                 ipRequirements.put(IP_VERSION, getFloatingIpVersion(ipList));
100                 ipRequirements.put(FLOATING_IP_COUNT_REQUIRED, isRequired);
101               }
102             }
103           }
104         }
105       }
106     }
107   }
108
109   private void setIpVersion(Map<String, Object> properties) {
110     List<Map<String, Object>> ipRequirementsList =
111         (List<Map<String, Object>>) properties.get(IP_REQUIREMENTS);
112     Map<String, Object> ipRequirements = ipRequirementsList.get(0);
113     Object propertyValue;
114     Map<String, Object> isRequired = new HashMap();
115     isRequired.put(IS_REQUIRED, Boolean.TRUE);
116
117     propertyValue = properties.get(FIXED_IPS);
118     if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
119       Map.Entry<String, Object> mapEntry =
120           (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next();
121       if (getIpVersion(mapEntry.getValue()) != null) {
122         ipRequirements.put(IP_VERSION, getIpVersion(mapEntry.getValue()));
123         ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
124       }
125     }
126     else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
127       for (int i = 0; i < ((List) propertyValue).size(); i++) {
128         Object ipMap = ((List) propertyValue).get(i);
129         if(ipMap instanceof Map && !((Map) ipMap).isEmpty()) {
130           Object ipAddressMap = ((Map) ipMap).get(IP_ADDRESS);
131           if (ipAddressMap instanceof Map && !((Map) ipAddressMap).isEmpty()) {
132             Object ipList = ((Map) ipAddressMap).get(GET_INPUT);
133             if (ipList instanceof List && !((List) ipList).isEmpty()) {
134               if (getIpVersion(((List) ipList).get(0)) != null) {
135                 ipRequirements.put(IP_VERSION, getIpVersion(((List) ipList).get(0)));
136                 ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
137               }
138             }
139             else if (ipList instanceof String && !((String) ipList).isEmpty()) {
140               if (getIpVersion(ipList) != null) {
141                 ipRequirements.put(IP_VERSION, getIpVersion(ipList));
142                 ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
143               }
144             }
145           }
146         }
147       }
148     }
149   }
150
151   private void setNetworkRoleTag(Map<String, Object> properties) {
152     Object propertyValue = properties.get(NETWORK);
153     if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
154       Map.Entry<String, String> mapEntry =
155           (Map.Entry<String, String>) ((Map) propertyValue).entrySet().iterator().next();
156       if (mapEntry.getValue() instanceof String && getNetworkRole(mapEntry.getValue())!=null) {
157         properties.put(NETWORK_ROLE_TAG, getNetworkRole(mapEntry.getValue()));
158       }
159     }
160   }
161
162   private Object getFloatingIpVersion(Object value) {
163     Object ipVersion = null;
164     if(value instanceof String) {
165       if (((String) value).endsWith(FLOATING_V6_IP)) {
166         ipVersion = 6;
167       }
168       else {
169         ipVersion = 4;
170       }
171     }
172     return ipVersion;
173   }
174
175   private Object getIpVersion(Object value) {
176     Object ipVersion = null;
177     if(value instanceof String) {
178       if (((String) value).endsWith(V6_IPS) || ((String) value).matches(IPV6_REGEX)) {
179         ipVersion = 6;
180       }
181       else {
182         ipVersion = 4;
183       }
184     }
185     return ipVersion;
186   }
187
188   private Object getNetworkRole(String value) {
189     Object networkRole = null;
190     if(value.endsWith(NET_NAME)) {
191       networkRole = (Object) value.substring(0, value.length() - NET_NAME.length());
192     }
193     else if(value.endsWith(NET_ID)) {
194       networkRole = (Object) value.substring(0, value.length() - NET_ID.length());
195     }
196     else if(value.endsWith(NET_FQDN)) {
197       networkRole = (Object) value.substring(0, value.length() - NET_FQDN.length());
198     }
199     return networkRole;
200   }
201 }