aboutsummaryrefslogtreecommitdiffstats
path: root/ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncVFCModel.java
blob: 94392262e5282d35af60d7e4929ee44bcf830dd7 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.northbound.uebclient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SdncVFCModel extends SdncBaseModel {
	
	private static final Logger LOG = LoggerFactory
			.getLogger(SdncVFCModel.class);
	
	private String vmType = null;
	private String vmCount = null;

	public SdncVFCModel(ISdcCsarHelper sdcCsarHelper, NodeTemplate nodeTemplate, DBResourceManager jdbcDataSource) {

		super(sdcCsarHelper, nodeTemplate, jdbcDataSource);
		
		// extract properties
		addParameter("ecomp_generated_naming", extractBooleanValue (nodeTemplate, "nfc_naming#ecomp_generated_naming"));
		addParameter("naming_policy", extractValue (nodeTemplate, "nfc_naming#naming_policy"));
		vmCount = extractValue (nodeTemplate, "service_template_filter#count"); // need path to vm_count, extracted as service_template_filter#count
		if (vmCount.isEmpty()) {
			vmCount = "0"; // vm_count can not be null
		}
		vmType = extractValue (nodeTemplate, SdcPropertyNames.PROPERTY_NAME_VMTYPETAG);
		addParameter("vm_type", vmType); // populate vm_type with vm_type_tag value
		addParameter("vm_type_tag", vmType);
		addParameter("nfc_naming_code", extractValue (nodeTemplate, "nfc_naming_code"));
		addParameter("nfc_function", extractValue (nodeTemplate, "nfc_function"));
		addParameter("high_availability", extractValue (nodeTemplate, "high_availablity"));
		addParameter("vm_image_name", extractValue (nodeTemplate, "vm_image_name"));
		addParameter("vm_flavor_name", extractValue (nodeTemplate, "vm_flavor_name"));
		addParameter("nfc_naming", extractValue (nodeTemplate, "nfc_naming"));
		addParameter("min_instances", extractValue (nodeTemplate, "min_instances"));
		addParameter("max_instances", extractValue (nodeTemplate, "max_instances"));
	}

	public void insertVFCModelData () throws IOException {
		try {
			cleanUpExistingToscaData("VFC_MODEL", "customization_uuid", getCustomizationUUID());
			LOG.info("Call insertToscaData for VFC_MODEL where customization_uuid = " + getCustomizationUUID());
			insertToscaData(buildSql("VFC_MODEL", model_yaml), null);
		} catch (IOException e) {
			LOG.error("Could not insert Tosca CSAR data into the VFC_MODEL table");
			throw new IOException (e);
		}

	}
	
	public void insertVFCtoNetworkRoleMappingData (NodeTemplate vfcNode) throws IOException {
		
		// For each VFC node, get CP properties to insert into VFC_TO_NETWORK_ROLE_MAPPING
		// VFC_TO_NETWORK_ROLE_MAPPING: vfc_customization_uuid, network_role, network_role_tag, vm_type, ipv4_count, ipv6_count,
		// ipv4_use_dhcp, ipv6_use_dhcp, ipv4_ip_version, ipv6_ip_version, extcp_subnetpool_id
		Map<String,Map<String,Object>> cpPropertiesMap = sdcCsarHelper.getCpPropertiesFromVfcAsObject(vfcNode);
		
		// DEBUG only
		if (cpPropertiesMap != null && !cpPropertiesMap.toString().contentEquals("{}")) {
			LOG.info("getCpPropertiesFromVfcAsObject for vfc_customization_uuid " + this.getCustomizationUUID() + ": "  + cpPropertiesMap.toString());
		}
		
		// Clean up all VFC_TO_NETWORK_ROLE_MAPPING data for this VFC node
		try {
			cleanUpExistingToscaData("VFC_TO_NETWORK_ROLE_MAPPING", "vfc_customization_uuid", getCustomizationUUID());
		} catch (IOException e) {
			LOG.error("Could not clean up data in VFC_TO_NETWORK_ROLE_MAPPING table ", e);
		}
		
		// There will be a cpPropertiesMap entry for each CP which will contain a map of properties to be inserted into VFC_TO_NETWORK_ROLE_MAPPING
		// There can be multiple insertions per CP:
		// 		Insert once for each unique IP Version / Subnet Role combination per CP (network_role)
		//		If there are IPV4 and IPV6 ip_requirements elements that have the same subnet_role (within a CP) combine those parameters for one insert 
		for (String nodeMapKey :  cpPropertiesMap.keySet()) {  // there will be one entry in this map per CP (network_role)
			LOG.debug("node key = " + nodeMapKey);
			Map<String,Object>  propsMap = cpPropertiesMap.get(nodeMapKey);
			Map<String, String> commonParams = new HashMap<String, String>();	// non-IP Version specific parameters
			
			// Get vm_type from VFC node
			SdncBaseModel.addParameter("vm_type", getVmType(), commonParams);
			
			// Extract non-IP Version specific parameters
			String networkRole = nullCheck(propsMap.get("network_role")).isEmpty() ? "default-network-role" : nullCheck(propsMap.get("network_role"));
			SdncBaseModel.addParameter("network_role", networkRole, commonParams); // can not be null
			SdncBaseModel.addParameter("network_role_tag", nullCheck(propsMap.get("network_role_tag")), commonParams);
			SdncBaseModel.addParameter("extcp_subnetpool_id", nullCheck(propsMap.get("subnetpoolid")), commonParams);

			// Loop thru all CPs using getNodeTemplateChildren and match the network_role on the CP with network_role from 
			// getCpPropertiesFromVfcAsObject output, then get subinterface_indicator for this CP
			List<NodeTemplate> cpNodesList = sdcCsarHelper.getNodeTemplateChildren(vfcNode);
			for (NodeTemplate cpNode : cpNodesList){
				String cpNetworkRole = extractValue(cpNode, "network_role");
				
				if (cpNetworkRole == networkRole) {
					String subinterfaceIndicator = extractBooleanValue (cpNode, "subinterface_indicator");
					addParameter("subinterface_indicator", subinterfaceIndicator, commonParams);
				}									
			}

			// Extract IP Version specific parameters
			String ipRequirementsString = nullCheck(propsMap.get("ip_requirements"));
			//ArrayList<Map<String, Object>>  ipPropsList =  (ArrayList<Map<String, Object>>) propsMap.get("ip_requirements");
			ArrayList<Map<String, Object>>  ipPropsList = new ArrayList<Map<String, Object>>(); 

			if (!ipRequirementsString.equals("{}")) {
				ipPropsList =  (ArrayList<Map<String, Object>>) propsMap.get("ip_requirements");
			}
			
			// Build lists of all IPV4 and IPV6 ip_requirements elements
			ArrayList<Map<String, String>> ipv4PropParamsList = new ArrayList<Map<String, String>>();
			ArrayList<Map<String, String>> ipv6PropParamsList = new ArrayList<Map<String, String>>();
			
			if (ipPropsList != null) {
				for (Map<String, Object> ipPropMap :  ipPropsList) {
					//LOG.info("ip_requirements prop map = " + nullCheck(ipPropMap));
	
					String ipVersion = nullCheck(ipPropMap.get("ip_version"));
					if (ipVersion == null) {
						LOG.error("SdncVFCModel: ipVersion not included in ip_requirements element");	
						continue;
					}

					String subnetRole = nullCheck(ipPropMap.get("subnet_role"));
					
					if (ipVersion.contains("4")) {

						// If we have already encountered this subnetRole for IPV4, skip this ip_requirements element
						if (!ipPropParamsMapContainsSubnetRole (ipv4PropParamsList, subnetRole)) {
						
							Map<String, String> ipv4PropParams = new HashMap<String, String>();
							SdncBaseModel.addParameter("ipv4_ip_version", ipVersion, ipv4PropParams);
							SdncBaseModel.addParameter("ipv4_use_dhcp", nullCheck(ipPropMap.get("dhcp_enabled")).contains("true") ? "Y" : "N", ipv4PropParams);
							Map<String, Object> ipCountRequired = (Map<String, Object>)ipPropMap.get("ip_count_required");
							if (ipCountRequired != null && ipCountRequired.get("count") != null) {
								SdncBaseModel.addParameter("ipv4_count", nullCheck(ipCountRequired.get("count")), ipv4PropParams);
							}
							Map<String, Object> floatingIpCountRequired = (Map<String, Object>)ipPropMap.get("floating_ip_count_required");
							if (floatingIpCountRequired != null && floatingIpCountRequired.get("count") != null) {
								SdncBaseModel.addParameter("ipv4_floating_count", nullCheck(floatingIpCountRequired.get("count")), ipv4PropParams);
							}
							SdncBaseModel.addParameter("ipv4_address_plan_name", nullCheck(ipPropMap.get("ip_address_plan_name")), ipv4PropParams);
							SdncBaseModel.addParameter("ipv4_vrf_name", nullCheck(ipPropMap.get("vrf_name")), ipv4PropParams);
							SdncBaseModel.addParameter("subnet_role", nullCheck(ipPropMap.get("subnet_role")), ipv4PropParams);
							
							ipv4PropParamsList.add(ipv4PropParams);
							
						} else {
							LOG.error("SdncVFCModel: Additional V4 ip-requirements element encountered for this subnet_role: ", subnetRole);
						}

					} else if (ipVersion.contains("6")) {

						// If we have already encountered this subnetRole for IPV6, skip this ip_requirements element
						if (!ipPropParamsMapContainsSubnetRole (ipv6PropParamsList, subnetRole)) { 
						
							Map<String, String> ipv6PropParams = new HashMap<String, String>();
							SdncBaseModel.addParameter("ipv6_ip_version", ipVersion, ipv6PropParams);
							SdncBaseModel.addParameter("ipv6_use_dhcp", nullCheck(ipPropMap.get("dhcp_enabled")).contains("true") ? "Y" : "N", ipv6PropParams);
							Map<String, Object> ipCountRequired = (Map<String, Object>)ipPropMap.get("ip_count_required");
							if (ipCountRequired != null && ipCountRequired.get("count") != null) {
								SdncBaseModel.addParameter("ipv6_count", nullCheck(ipCountRequired.get("count")), ipv6PropParams);
							}
							Map<String, Object> floatingIpCountRequired = (Map<String, Object>)ipPropMap.get("floating_ip_count_required");
							if (floatingIpCountRequired != null && floatingIpCountRequired.get("count") != null) {
								SdncBaseModel.addParameter("ipv6_floating_count", nullCheck(floatingIpCountRequired.get("count")), ipv6PropParams);
							}
							SdncBaseModel.addParameter("ipv6_address_plan_name", nullCheck(ipPropMap.get("ip_address_plan_name")), ipv6PropParams);
							SdncBaseModel.addParameter("ipv6_vrf_name", nullCheck(ipPropMap.get("vrf_name")), ipv6PropParams);
							SdncBaseModel.addParameter("subnet_role", nullCheck(ipPropMap.get("subnet_role")), ipv6PropParams);
							
							ipv6PropParamsList.add(ipv6PropParams);
							
						} else {							
							LOG.error("SdncVFCModel: Additional V6 ip-requirements element encountered for this subnetRole: ", subnetRole);							
						}
							
					} else {
						LOG.error("SdncVFCModel: invalid IP version encountered: ", ipVersion);
					}					
					
				} // for each ip-requirements element
				
			} // ipPropsList null check		
			
			// After all Common and IP Version specific parameters are extracted, insert IPV4 and IPV6 data separately
			// Insert IPV4 data
			for (Map<String, String> ipv4PropParams: ipv4PropParamsList) {
				
				Map<String, String> mappingParams = new HashMap<String, String>();	// final list for single insertion
				addParamsToMap(commonParams, mappingParams);
				addParamsToMap(ipv4PropParams, mappingParams);
								
				// Insert ipv4PropParams into VFC_TO_NETWORK_ROLE_MAPPING
				try {
					LOG.info("Call insertToscaData for VFC_TO_NETWORK_ROLE_MAPPING where vfc_customization_uuid = " + getCustomizationUUID());
					addRequiredParameters(mappingParams);
					insertToscaData(SdncBaseModel.getSql("VFC_TO_NETWORK_ROLE_MAPPING", "vfc_customization_uuid", getCustomizationUUID(), "", mappingParams), null);
				} catch (IOException e) {
					LOG.error("Could not insert Tosca CSAR data into the VFC_TO_NETWORK_ROLE_MAPPING table");
					throw new IOException (e);
				}	

			}
			
			// Insert IPV6 data
			for (Map<String, String> ipv6PropParams: ipv6PropParamsList) {
				
				Map<String, String> mappingParams = new HashMap<String, String>();	// final list for single insertion
				addParamsToMap(commonParams, mappingParams);
				addParamsToMap(ipv6PropParams, mappingParams);
				
				// Insert ipv6PropParams into VFC_TO_NETWORK_ROLE_MAPPING
				try {
					LOG.info("Call insertToscaData for VFC_TO_NETWORK_ROLE_MAPPING where vfc_customization_uuid = " + getCustomizationUUID());
					addRequiredParameters(mappingParams);
					insertToscaData(SdncBaseModel.getSql("VFC_TO_NETWORK_ROLE_MAPPING", "vfc_customization_uuid", getCustomizationUUID(), "", mappingParams), null);
				} catch (IOException e) {
					LOG.error("Could not insert Tosca CSAR data into the VFC_TO_NETWORK_ROLE_MAPPING table");
					throw new IOException (e);
				}	
				
			}
			
		} // Outer map loop - one per ExtCP

	}
		
	protected boolean ipPropParamsMapContainsSubnetRole (ArrayList<Map<String, String>> ipPropParamsList, String subnetRole) {
		
		boolean subnetRoleFound = false;
		
		if (subnetRole != null && !subnetRole.isEmpty()) { 
			for (Map<String, String> ipPropMap :  ipPropParamsList) {
				if (ipPropMap.get("subnet_role").contentEquals(subnetRole)) {
					return true;
				}
			}
		}
		return subnetRoleFound;
	}
	
	private void addRequiredParameters (Map<String, String> mappingParams) {
		
		// Add parameters which can not be null if they have not already been added - network_role, ipv4_count, ipv6_count
		String ipvCountDefault = "0";
		if (!mappingParams.containsKey("ipv4_count")) {
			SdncBaseModel.addParameter("ipv4_count", ipvCountDefault, mappingParams);
		}
		if (!mappingParams.containsKey("ipv6_count")) {
			SdncBaseModel.addParameter("ipv6_count", ipvCountDefault, mappingParams);
		}
	}
	
	public void insertVFCRelatedNetworkRoleData (String vfCustomizationUUID, NodeTemplate vfcNode) throws IOException {
		
		// Get the CPs on this VFC - ASDC suggests getNodeTemplateChildren
		List<NodeTemplate> cpNodesList = sdcCsarHelper.getNodeTemplateChildren(vfcNode);
		
		String vfcCustomizationUuid = getCustomizationUUID();
		
		try {
			cleanUpExistingToscaData("VFC_RELATED_NETWORK_ROLE", "vfc_customization_uuid", vfcCustomizationUuid);
		} catch (IOException e) {
			LOG.error("Could not clean up Tosca CSAR data in the VFC_RELATED_NETWORK_ROLE table");
			throw new IOException (e);
		}

		for (NodeTemplate cpNode : cpNodesList){
			String networkRole = extractValue(cpNode, "network_role");
			Map<String, String> relatedNetworkRoleParams = new HashMap<String, String>();
			addParameter("vfc_customization_uuid", vfcCustomizationUuid, relatedNetworkRoleParams);
			addParameter("vm_type", vmType, relatedNetworkRoleParams);
			addParameter("network_role", networkRole, relatedNetworkRoleParams);
			
			final Object relatedNetworksPropertyValue = cpNode.getPropertyValue("related_networks");
			
			ArrayList<Map<String, String>> relatedNetworkList = (ArrayList)relatedNetworksPropertyValue;
			if (relatedNetworkList != null) {
				for (Map<String, String> relatedNetworkValue : relatedNetworkList) {
                			LOG.debug("CP [" + cpNode.getName() + "], property [" + "related_network_role" + "] property value: " + relatedNetworkValue.get("related_network_role"));               
    					String relatedNetworkRoleValue = relatedNetworkValue.get("related_network_role");
   			
    					try {
    						// Table cleanup for VFC_RELATED_NETWORK_ROLE occurs per vfc
    						// If cp related_network_role, cp network_role and vm_type for this vfc already exist in VFC_RELATED_NETWORK_ROLE,
    						// don't attempt insertion
    						Map<String, String> relatedNetworkRoleParamsCheck = new HashMap<String, String>();
    						addParamsToMap(relatedNetworkRoleParams, relatedNetworkRoleParamsCheck);
    						addParameter("related_network_role", relatedNetworkRoleValue, relatedNetworkRoleParamsCheck);
    						if (checkForExistingToscaData("VFC_RELATED_NETWORK_ROLE", relatedNetworkRoleParamsCheck) == false) {    					
        						LOG.info("Call insertToscaData for VFC_RELATED_NETWORK_ROLE where vfc_customization_uuid = " + vfcCustomizationUuid);
        						insertToscaData(buildSql("VFC_RELATED_NETWORK_ROLE", "related_network_role", "\"" + relatedNetworkRoleValue + "\"", model_yaml, relatedNetworkRoleParams), null);
    						}
    					
    						// Table cleanup for VNF_RELATED_NETWORK_ROLE occurs per vf (up one level)
    						// Insert same related_network_role data into VNF_RELATED_NETWORK_ROLE
    						Map<String, String> vfRelatedNetworkRoleParamsCheck = new HashMap<String, String>();
    						addParameter("vnf_customization_uuid", vfCustomizationUUID, vfRelatedNetworkRoleParamsCheck);
    						addParameter("network_role", networkRole, vfRelatedNetworkRoleParamsCheck);
    						addParameter("related_network_role", relatedNetworkRoleValue, vfRelatedNetworkRoleParamsCheck);
    						if (checkForExistingToscaData("VNF_RELATED_NETWORK_ROLE", vfRelatedNetworkRoleParamsCheck) == false) {
    							vfRelatedNetworkRoleParamsCheck.remove("related_network_role");
        						LOG.info("Call insertToscaData for VNF_RELATED_NETWORK_ROLE where vnf_customization_uuid = " + vfCustomizationUUID);
        						insertToscaData(buildSql("VNF_RELATED_NETWORK_ROLE", "related_network_role", "\"" + relatedNetworkRoleValue + "\"", model_yaml, vfRelatedNetworkRoleParamsCheck), null);
    						}    					

    					} catch (IOException e) {
    						LOG.error("Could not insert Tosca CSAR data into the VFC_RELATED_NETWORK_ROLE table");
    						throw new IOException (e);
    					}
				}
			} 
			else {
				LOG.debug("CP [" + cpNode.getName() + "], property [" + "related_networks" + "] property value: " + null);
			}	
		}
		
	}

	public String getVmType() {
		return vmType;
	}

	public void setVmType(String vmType) {
		this.vmType = vmType;
	}

	public String getVmCount() {
		return vmCount;
	}

	public void setVmCount(String vmCount) {
		this.vmCount = vmCount;
	}

}