Added oparent to sdc main
[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 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.translator.services.heattotosca.helper;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 public class ResourceTranslationNeutronPortHelper {
27
28     public static final String IP_COUNT_REQUIRED = "ip_count_required";
29     public static final String FLOATING_IP_COUNT_REQUIRED = "floating_ip_count_required";
30     public static final String NETWORK = "network";
31     public static final String NETWORK_ROLE_TAG = "network_role_tag";
32     public static final String FIXED_IPS = "fixed_ips";
33     public static final String IP_VERSION = "ip_version";
34     public static final String IP_ADDRESS = "ip_address";
35     public static final String GET_INPUT = "get_input";
36     public static final String ALLOWED_ADDRESS_PAIRS = "allowed_address_pairs";
37     public static final String FLOATING_IP = "_floating_ip";
38     public static final String FLOATING_V6_IP = "_floating_v6_ip";
39     public static final String IPS = "_ips";
40     public static final String V6_IPS = "_v6_ips";
41     public static final String NET_NAME = "_net_name";
42     public static final String NET_ID = "_net_id";
43     public static final String NET_FQDN = "_net_fqdn";
44     public static final String IPV4_REGEX = "\\w*_ip_\\d+";
45     public static final String IPV6_REGEX = "\\w*_v6_ip_\\d+";
46     public static final String MAC_COUNT_REQUIRED = "mac_count_required";
47     public static final String MAC_ADDRESS = "mac_address";
48     public static final String IS_REQUIRED = "is_required";
49     public static final String IP_REQUIREMENTS = "ip_requirements";
50     public static final String MAC_REQUIREMENTS = "mac_requirements";
51     public static final int DEFAULT_IP_VERSION = 4;
52
53     public void setAdditionalProperties(Map<String, Object> properties) {
54         setRequirements(properties);
55         setNetworkRoleTag(properties);
56     }
57
58     private void setRequirements(Map<String, Object> properties) {
59         properties.putAll(addRequirements());
60         setIpRequirements(properties);
61         setMacRequirements(properties);
62     }
63
64     private void setIpRequirements(Map<String, Object> properties) {
65         setFixedIpCount(properties);
66         setFloatingIpCount(properties);
67         addDefaultIpRequirement(properties);
68     }
69
70     private Map<String, Object> addRequirements() {
71         Map<String, Object> properties = new HashMap<>();
72         List<Map<String, Object>> ipRequirementsList = new ArrayList<>();
73         properties.put(IP_REQUIREMENTS, ipRequirementsList);
74         properties.put(MAC_REQUIREMENTS, createMacRequirement());
75         return properties;
76
77     }
78
79     private Map<String, Object> createMacRequirement() {
80         Map<String, Object> macRequirements = new HashMap<>();
81         Map<String, Object> macIsRequired = new HashMap<>();
82         macIsRequired.put(IS_REQUIRED, Boolean.FALSE);
83         macRequirements.put(MAC_COUNT_REQUIRED, macIsRequired);
84         return macRequirements;
85     }
86
87     private void setMacRequirements(Map<String, Object> properties) {
88             updateMacCountRequired(properties);
89     }
90
91     private void updateMacCountRequired(Map<String, Object> properties) {
92         if (properties.containsKey(MAC_ADDRESS)) {
93             Map<String, Object> macRequirements = (Map<String, Object>) properties.get(MAC_REQUIREMENTS);
94             Map<String, Object> macIsRequired = (Map<String, Object>) macRequirements.get(MAC_COUNT_REQUIRED);
95             macIsRequired.put(IS_REQUIRED, Boolean.TRUE);
96         }
97     }
98
99     private void setFloatingIpCount(Map<String, Object> properties) {
100         handleIpCountRequired(properties, ALLOWED_ADDRESS_PAIRS, FLOATING_IP_COUNT_REQUIRED );
101     }
102
103     private void setFixedIpCount(Map<String, Object> properties) {
104         handleIpCountRequired(properties, FIXED_IPS, IP_COUNT_REQUIRED );
105     }
106
107
108     private void addDefaultIpRequirement(Map<String, Object> properties) {
109         List<Map<String, Object>> ipRequirementsList = ((List<Map<String, Object>>) properties.get(IP_REQUIREMENTS));
110         if(ipRequirementsList.isEmpty()) {
111             ipRequirementsList.add(createIPRequirement(DEFAULT_IP_VERSION));
112         }
113     }
114
115     private Map<String, Object> createIPRequirement(Object version) {
116         Map<String, Object> ipRequirements = new HashMap<>();
117         Map<String, Object> isRequired = new HashMap<>();
118         Map<String, Object> floatingIsRequired = new HashMap<>();
119         isRequired.put(IS_REQUIRED, Boolean.FALSE);
120         floatingIsRequired.put(IS_REQUIRED, Boolean.FALSE);
121         ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
122         ipRequirements.put(FLOATING_IP_COUNT_REQUIRED, floatingIsRequired);
123         ipRequirements.put(IP_VERSION, version);
124         return ipRequirements;
125     }
126
127     private void handleIpCountRequired(Map<String, Object> properties, String ipType, String ipCountRequired ){
128
129         Object propertyValue = properties.get(ipType);
130         if(propertyValue == null){
131             return;
132         }
133
134         if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
135             handleMapProperty(ipType, ipCountRequired, properties, (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next());
136         }
137         else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
138             handleListProperty(ipType, ipCountRequired, properties,  (List) propertyValue);
139         }
140
141     }
142
143     private void handleListProperty(String ipType, String ipCountRequired, Map<String, Object> properties,  List propertyValue) {
144         for (int i = 0; i < propertyValue.size(); i++) {
145             handleIpAddress(ipType, ipCountRequired, properties,  propertyValue.get(i));
146         }
147     }
148
149     private void handleMapProperty(String ipType, String ipCountRequired, Map<String, Object> properties, Map.Entry<String, Object> mapEntry) {
150         updateIpCountRequired(ipType, ipCountRequired, properties,  mapEntry.getValue());
151     }
152
153     private void handleIpAddress(String ipType, String ipCountRequired, Map<String, Object> properties, Object ipMap) {
154         if(ipMap instanceof Map && !((Map) ipMap).isEmpty()) {
155             Object ipAddressMap = ((Map) ipMap).get(IP_ADDRESS);
156             if (ipAddressMap instanceof Map && !((Map) ipAddressMap).isEmpty()) {
157                 Object ipInput = ((Map) ipAddressMap).get(GET_INPUT);
158                 updateIpCountRequired(ipType, ipCountRequired, properties,  ipInput);
159             }
160         }
161     }
162
163     private void updateIpCountRequired(String ipType, String ipCountRequired, Map<String, Object> properties, Object ipInput) {
164         Object ipVersion = getVersion(ipInput, ipType);
165         updateIpCountRequiredForVersion(ipCountRequired, properties,  ipVersion);
166     }
167
168     private void updateIpCountRequiredForVersion(String ipCountRequired, Map<String, Object> properties,   Object ipVersion) {
169         if (ipVersion != null) {
170             HashMap<Object, Map<String, Object>> ipRequirementsMap = getIPRequirements(properties);
171             Map<String, Object>  ipRequirement = ipRequirementsMap.get(ipVersion);
172             if (ipRequirement == null){
173                 ipRequirement = addIPRequirement(properties, ipVersion);
174             }
175             updateIpCountRequired(ipCountRequired, ipRequirement);
176         }
177     }
178
179     private Map<String, Object> addIPRequirement(Map<String, Object> properties, Object ipVersion) {
180         List<Map<String, Object>> ipRequirementsList = ((List<Map<String,Object>>) properties.get(IP_REQUIREMENTS));
181         Map<String, Object> newIpRequirement = createIPRequirement(ipVersion);
182         ipRequirementsList.add(newIpRequirement);
183         return newIpRequirement;
184     }
185
186     private void updateIpCountRequired(String ipCountRequired, Map<String, Object> ipRequirement) {
187         Map<String, Object> isIPCountRequired = (Map<String, Object>)ipRequirement.get(ipCountRequired);
188         isIPCountRequired.put(IS_REQUIRED, Boolean.TRUE);
189     }
190
191     private HashMap <Object, Map<String, Object>> getIPRequirements (Map<String, Object> properties) {
192         HashMap<Object, Map<String, Object>> ipRequirementsMap = new HashMap<>();
193         List<Map<String, Object>> ipRequirementsList = ((List<Map<String,Object>>) properties.get(IP_REQUIREMENTS));
194         ipRequirementsList.stream().forEach(e->ipRequirementsMap.put(e.get(IP_VERSION),e));
195         return ipRequirementsMap;
196     }
197
198     private void setNetworkRoleTag(Map<String, Object> properties) {
199         Object propertyValue = properties.get(NETWORK);
200         if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
201             Map.Entry<String, String> mapEntry =
202                     (Map.Entry<String, String>) ((Map) propertyValue).entrySet().iterator().next();
203             if (mapEntry.getValue() instanceof String && getNetworkRole(mapEntry.getValue())!=null) {
204                 properties.put(NETWORK_ROLE_TAG, getNetworkRole(mapEntry.getValue()));
205             }
206         }
207     }
208
209     private Object getVersion(Object value, String type) {
210
211         Object version = null;
212         if(type.equals(FIXED_IPS)){
213             version =  getIpVersion(value);
214         }
215         else if(type.equals(ALLOWED_ADDRESS_PAIRS)){
216             version =  getFloatingIpVersion(value);
217         }
218         return version;
219     }
220
221     private Object getFloatingIpVersion(Object value) {
222         Object ipVersion = null;
223
224         // Allowed ONLY String parameter
225         if(value instanceof String) {
226             if (((String) value).endsWith(FLOATING_V6_IP)) {
227                 ipVersion = 6;
228             }
229             else if (((String) value).endsWith(FLOATING_IP)){
230                 ipVersion = 4;
231             }
232         }
233         return ipVersion;
234     }
235
236     private Object getIpVersion(Object value) {
237
238         // Allowed List or String parameter
239         Object ipVersion = null;
240         if (value instanceof List && !((List) value).isEmpty()){
241             value = ((List) value).get(0);
242         }
243
244         if(value instanceof String) {
245             if (((String) value).endsWith(V6_IPS) || ((String) value).matches(IPV6_REGEX)) {
246                 ipVersion = 6;
247             }
248             else {
249                 ipVersion = 4;
250             }
251         }
252         return ipVersion;
253     }
254
255     private Object getNetworkRole(String value) {
256         Object networkRole = null;
257         if(value.endsWith(NET_NAME)) {
258             networkRole = value.substring(0, value.length() - NET_NAME.length());
259         }
260         else if(value.endsWith(NET_ID)) {
261             networkRole = value.substring(0, value.length() - NET_ID.length());
262         }
263         else if(value.endsWith(NET_FQDN)) {
264             networkRole = value.substring(0, value.length() - NET_FQDN.length());
265         }
266         return networkRole;
267     }
268 }