aboutsummaryrefslogtreecommitdiffstats
path: root/ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncVFCModel.java
blob: fc288aaa9cc555d4cce75bcde9cb9546da578427 (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
/*-
 * ============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.IEntityDetails;
import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
import org.onap.sdc.tosca.parser.enums.SdcTypes;
import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
import org.onap.sdc.toscaparser.api.Property;
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, IEntityDetails entityDetails, DBResourceManager jdbcDataSource, SdncUebConfiguration config) throws IOException {

		super(sdcCsarHelper, entityDetails, jdbcDataSource, config);
		
		// extract properties
		addParameter("ecomp_generated_naming", extractBooleanValue (entityDetails, "nfc_naming", "ecomp_generated_naming"));
		addParameter("naming_policy", extractValue (entityDetails, "nfc_naming", "naming_policy"));
		vmCount = extractValue (entityDetails, "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 (entityDetails, 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 (entityDetails, "nfc_naming_code"));
		addParameter("nfc_function", extractValue (entityDetails, "nfc_function"));
		addParameter("high_availability", extractValue (entityDetails, "high_availablity"));
		addParameter("vm_image_name", extractValue (entityDetails, "vm_image_name"));
		addParameter("vm_flavor_name", extractValue (entityDetails, "vm_flavor_name"));
		addParameter("nfc_naming", extractValue (entityDetails, "nfc_naming"));
		addParameter("min_instances", extractValue (entityDetails, "min_instances"));
		addParameter("max_instances", extractValue (entityDetails, "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 (IEntityDetails cvfcEntity) throws IOException {		

		// Get the CPs on this VFC - using getEntity
		// 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
		
		String vfcCustomizationUuid = getCustomizationUUID().replace("\"", "");
		EntityQuery entityQueryCP = EntityQuery.newBuilder(SdcTypes.CP).build();
	    TopologyTemplateQuery topologyTemplateQueryVFC = TopologyTemplateQuery.newBuilder(SdcTypes.CVFC).customizationUUID(vfcCustomizationUuid).build();
	    List<IEntityDetails> cpEntities = sdcCsarHelper.getEntity(entityQueryCP, topologyTemplateQueryVFC, true);
		if (cpEntities == null || cpEntities.isEmpty()) {
			LOG.debug("insertVFCtoNetworkRoleMappingData: Could not find the nested CVFCs for: " + vfcCustomizationUuid);
			return;
		}
		
		// 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 can be multiple insertions per CP:
		// 		Insert once for each unique IP Version / Subnet Role combination per CP (network_role)
		for (IEntityDetails cpEntity : cpEntities) {
			
			// Extract common parameters 
			Map<String, String> commonParams = new HashMap<String, String>();  // non-IP Version specific parameters
			// Get vm_type from VFC node
			addParameter("vm_type", getVmType(), commonParams);
			
			// Extract non-IP Version specific parameters - outside the ip_requirements block
			String networkRole = extractValue(cpEntity, "network_role").isEmpty() ? "default-network-role" : extractValue(cpEntity, "network_role");  // set default-network-role?
			addParameter("network_role", networkRole, commonParams); // can not be null
			addParameter("network_role_tag", nullCheck(extractValue(cpEntity, "network_role_tag")), commonParams);
			addParameter("extcp_subnetpool_id", nullCheck(extractValue(cpEntity, "subnetpoolid")), commonParams);
			String subinterfaceIndicator = extractBooleanValue (cpEntity, "subinterface_indicator");
			addParameter("subinterface_indicator", subinterfaceIndicator, commonParams);
			
			// 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>>();
				
			// Extract IP Version specific parameters
			if (cpEntity.getProperties().containsKey("ip_requirements")) {
				
				ArrayList<Map<String, Object>>  ipPropsList = new ArrayList<Map<String, Object>>(); 
				ipPropsList =  (ArrayList<Map<String, Object>>) cpEntity.getProperties().get("ip_requirements").getValue();
				
				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);
					}		
				}
		    }
	    }
	}
	
	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, IEntityDetails cvfcEntity) throws IOException {
		
		// Get the CPs on this VFC - using getEntity
		
		String vfcCustomizationUuid = getCustomizationUUID().replace("\"", "");
		// Get the CPs on this VFC - ASDC suggests getNodeTemplateChildren
		//List<NodeTemplate> cpNodesList = sdcCsarHelper.getNodeTemplateChildren(vfcNode);
		EntityQuery entityQueryCP = EntityQuery.newBuilder(SdcTypes.CP).build();
	    TopologyTemplateQuery topologyTemplateQueryVFC = TopologyTemplateQuery.newBuilder(SdcTypes.CVFC).customizationUUID(vfcCustomizationUuid).build();
	    List<IEntityDetails> cpEntities = sdcCsarHelper.getEntity(entityQueryCP, topologyTemplateQueryVFC, true);
		if (cpEntities == null || cpEntities.isEmpty()) {
			LOG.debug("insertVFCRelatedNetworkRoleData: Could not find the nested CVFCs for: " + vfcCustomizationUuid);
			return;
		}
		
		try {
			cleanUpExistingToscaData("VFC_RELATED_NETWORK_ROLE", "vfc_customization_uuid", getCustomizationUUID());
		} catch (IOException e) {
			LOG.error("Could not clean up Tosca CSAR data in the VFC_RELATED_NETWORK_ROLE table");
			throw new IOException (e);
		}

		for (IEntityDetails cpEntity : cpEntities){
			String networkRole = extractValue(cpEntity, "network_role");
			Map<String, String> relatedNetworkRoleParams = new HashMap<String, String>();
			addParameter("vfc_customization_uuid", getCustomizationUUID(), relatedNetworkRoleParams);
			addParameter("vm_type", vmType, relatedNetworkRoleParams);
			addParameter("network_role", networkRole, relatedNetworkRoleParams);
				
			if (cpEntity.getProperties().containsKey("related_networks")) {
				
				Property relatedNetworksProperty = cpEntity.getProperties().get("related_networks");
				List<String> relatedNetworkRoles = relatedNetworksProperty.getLeafPropertyValue("related_network_role");
				
				for (String relatedNetworkRole : relatedNetworkRoles) {
					LOG.debug("CP [" + cpEntity.getName() + "], property [" + "related_network_role" + "] property value: " + relatedNetworkRole);
					
    				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", relatedNetworkRole, relatedNetworkRoleParamsCheck);
    					if (checkForExistingToscaData("VFC_RELATED_NETWORK_ROLE", relatedNetworkRoleParamsCheck) == false) {    					
        					LOG.info("Call insertToscaData for VFC_RELATED_NETWORK_ROLE where vfc_customization_uuid = " + getCustomizationUUID());
        					insertToscaData(buildSql("VFC_RELATED_NETWORK_ROLE", "related_network_role", "\"" + relatedNetworkRole + "\"", 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", relatedNetworkRole, 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", "\"" + relatedNetworkRole + "\"", 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);
    				}
				}				
			}			
		}
	}

	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;
	}

}