5f922e0c99956401428c91659f1acf9c9be37780
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.onap.so.client.adapter.vnf.mapper;
22
23 import static java.util.Arrays.asList;
24
25 import java.io.IOException;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32
33 import javax.annotation.PostConstruct;
34
35 import org.onap.sdnc.northbound.client.model.GenericResourceApiParam;
36 import org.onap.sdnc.northbound.client.model.GenericResourceApiParamParam;
37 import org.onap.sdnc.northbound.client.model.GenericResourceApiSubInterfaceNetworkData;
38 import org.onap.sdnc.northbound.client.model.GenericResourceApiSubinterfacenetworkdataSubInterfaceNetworkData;
39 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfmoduleassignmentsVfModuleAssignments;
40 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfmoduleassignmentsVfmoduleassignmentsVms;
41 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfmoduletopologyVfModuleTopology;
42 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmNetworkData;
43 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmTopologyData;
44 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmnetworkdataFloatingIps;
45 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmnetworkdataInterfaceRoutePrefixes;
46 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmnetworkdataNetworkInformationItems;
47 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmnetworkdataNetworkinformationitemsNetworkInformationItem;
48 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmnetworkdataNetworkinformationitemsNetworkinformationitemNetworkIps;
49 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmnetworkdataSriovParameters;
50 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmnetworkdataSriovparametersHeatVlanFilters;
51 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmtopologydataVmNames;
52 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmtopologydataVmNetworks;
53 import org.onap.sdnc.northbound.client.model.GenericResourceApiVmtopologydataVmnamesVnfcNames;
54 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfNetworkData;
55 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfcNetworkData;
56 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfcnetworkdataVnfcNetworkData;
57 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfcnetworkdataVnfcnetworkdataVnfcPorts;
58 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfcnetworkdataVnfcnetworkdataVnfcportsVnfcPort;
59 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfresourceassignmentsVnfResourceAssignments;
60 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsAvailabilityZones;
61 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks;
62 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnftopologyVnfTopology;
63 import org.onap.so.adapters.vnfrest.CreateVfModuleRequest;
64 import org.onap.so.adapters.vnfrest.DeleteVfModuleRequest;
65 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
66 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
67 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
68 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
69 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
70 import org.onap.so.bpmn.servicedecomposition.generalobjects.OrchestrationContext;
71 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
72 import org.onap.so.entity.MsoRequest;
73 import org.onap.so.jsonpath.JsonPathUtil;
74 import org.springframework.beans.factory.annotation.Autowired;
75 import org.springframework.stereotype.Component;
76
77 import com.fasterxml.jackson.core.JsonParseException;
78 import com.fasterxml.jackson.core.JsonProcessingException;
79 import com.fasterxml.jackson.databind.DeserializationFeature;
80 import com.fasterxml.jackson.databind.JsonMappingException;
81 import com.fasterxml.jackson.databind.ObjectMapper;
82
83 import jersey.repackaged.com.google.common.base.Joiner;
84
85 @Component
86 public class VnfAdapterVfModuleObjectMapper {
87         @Autowired
88         protected VnfAdapterObjectMapperUtils vnfAdapterObjectMapperUtils;
89         private static List<String> sdncResponseParamsToSkip = asList("vnf_id", "vf_module_id", "vnf_name", "vf_module_name");
90         
91         private ObjectMapper mapper = new ObjectMapper();
92         private static final JsonPathUtil jsonPath = JsonPathUtil.getInstance();
93         private static final String SUB_INT = "subint";
94         private static final String SUBNET_ID = "_subnet_id";
95         private static final String V6_SUBNET_ID = "_v6_subnet_id";
96         private static final String PORT = "port";
97         private static final String SUB_INT_COUNT = "_subintcount";
98         private static final String VLAN_IDS = "_vlan_ids";
99         private static final String NET_NAMES = "_net_names";
100         private static final String NET_IDS = "_net_ids";
101         private static final String IP = "_ip";
102         private static final String V6_IP = "_v6_ip";
103         private static final String FLOATING_IP = "_floating_ip";
104         private static final String FLOATING_V6_IP = "_floating_v6_ip";
105         private static final String UNDERSCORE = "_";
106         
107         @PostConstruct
108         public void init () {
109                 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
110         }
111         
112         public CreateVfModuleRequest createVfModuleRequestMapper(RequestContext requestContext, CloudRegion cloudRegion, OrchestrationContext orchestrationContext, ServiceInstance serviceInstance, GenericVnf genericVnf, 
113                                 VfModule vfModule, VolumeGroup volumeGroup, String sdncVnfQueryResponse, String sdncVfModuleQueryResponse) throws JsonParseException, JsonMappingException, IOException {
114                 CreateVfModuleRequest createVfModuleRequest = new CreateVfModuleRequest();
115                 
116                 createVfModuleRequest.setCloudSiteId(cloudRegion.getLcpCloudRegionId());
117                 createVfModuleRequest.setTenantId(cloudRegion.getTenantId());
118                 createVfModuleRequest.setVfModuleId(vfModule.getVfModuleId());
119                 createVfModuleRequest.setVfModuleName(vfModule.getVfModuleName());
120                 createVfModuleRequest.setVnfId(genericVnf.getVnfId());
121                 createVfModuleRequest.setVnfType(genericVnf.getVnfType());              
122                 createVfModuleRequest.setVnfVersion(serviceInstance.getModelInfoServiceInstance().getModelVersion());
123                 createVfModuleRequest.setVfModuleType(vfModule.getModelInfoVfModule().getModelName());
124                 createVfModuleRequest.setModelCustomizationUuid(vfModule.getModelInfoVfModule().getModelCustomizationUUID());
125                 if (volumeGroup != null) {
126                         createVfModuleRequest.setVolumeGroupId(volumeGroup.getVolumeGroupId());
127                         createVfModuleRequest.setVolumeGroupStackId(volumeGroup.getHeatStackId());
128                 }
129                 VfModule baseVfModule = getBaseVfModule(genericVnf);            
130                 if (baseVfModule != null) {
131                         createVfModuleRequest.setBaseVfModuleId(baseVfModule.getVfModuleId());
132                         createVfModuleRequest.setBaseVfModuleStackId(baseVfModule.getHeatStackId());
133                 }
134                 createVfModuleRequest.setVfModuleParams(buildVfModuleParamsMap(requestContext, serviceInstance, genericVnf, vfModule, sdncVnfQueryResponse, sdncVfModuleQueryResponse));
135                 
136                 createVfModuleRequest.setSkipAAI(true);         
137                 createVfModuleRequest.setBackout(orchestrationContext.getIsRollbackEnabled());
138                 createVfModuleRequest.setFailIfExists(true);
139                 
140                 MsoRequest msoRequest = buildMsoRequest(requestContext, serviceInstance);               
141                 createVfModuleRequest.setMsoRequest(msoRequest);
142                 
143                 String messageId = vnfAdapterObjectMapperUtils.getRandomUuid();
144                 createVfModuleRequest.setMessageId(messageId);
145                 createVfModuleRequest.setNotificationUrl(vnfAdapterObjectMapperUtils.createCallbackUrl("VNFAResponse", messageId));
146                 
147                 return createVfModuleRequest;
148         }
149         
150         private MsoRequest buildMsoRequest(RequestContext requestContext,ServiceInstance serviceInstance) {
151                 MsoRequest msoRequest = new MsoRequest();
152                 msoRequest.setRequestId(requestContext.getMsoRequestId());
153                 msoRequest.setServiceInstanceId(serviceInstance.getServiceInstanceId());
154                 return msoRequest;              
155         }
156         
157         private Map<String,String> buildVfModuleParamsMap(RequestContext requestContext, ServiceInstance serviceInstance, GenericVnf genericVnf, 
158                                 VfModule vfModule, String sdncVnfQueryResponse, String sdncVfModuleQueryResponse) throws JsonParseException, JsonMappingException, IOException {
159                 
160                 GenericResourceApiVnftopologyVnfTopology vnfTopology = mapper.readValue(sdncVnfQueryResponse, GenericResourceApiVnftopologyVnfTopology.class);
161                 GenericResourceApiVfmoduletopologyVfModuleTopology vfModuleTopology = mapper.readValue(sdncVfModuleQueryResponse, GenericResourceApiVfmoduletopologyVfModuleTopology.class);
162                 Map<String,String> paramsMap = new HashMap<>();
163
164                 if(vnfTopology.getSdncGeneratedCloudResources() && vfModuleTopology.getSdncGeneratedCloudResources()) {
165                         buildParamsMapFromVfModuleSdncResponse(paramsMap, vfModuleTopology, true);
166                         buildParamsMapFromVnfSdncResponse(paramsMap, vnfTopology, null, true);
167                 }
168                 else {
169                         Map<String,String> networkRoleMap = buildNetworkRoleMap(vfModuleTopology);
170                         buildParamsMapFromVfModuleSdncResponse(paramsMap, vfModuleTopology, false);
171                         buildParamsMapFromVnfSdncResponse(paramsMap, vnfTopology, networkRoleMap, false);
172                 }
173                 
174                 buildMandatoryParamsMap(paramsMap, serviceInstance, genericVnf, vfModule);
175                 
176                 // Parameters received from the request should overwrite any parameters received from SDNC
177                 
178                 if (requestContext.getUserParams() != null) {
179                         paramsMap.putAll(requestContext.getUserParams());
180                 }
181                 return paramsMap;
182         }
183         
184         private void  buildMandatoryParamsMap(Map<String,String> paramsMap, ServiceInstance serviceInstance, GenericVnf genericVnf, VfModule vfModule) {                
185                 paramsMap.put("vnf_id", genericVnf.getVnfId());
186                 paramsMap.put("vnf_name", genericVnf.getVnfName());
187                 paramsMap.put("vf_module_id", vfModule.getVfModuleId());
188                 paramsMap.put("vf_module_name", vfModule.getVfModuleName());
189                 paramsMap.put("environment_context",serviceInstance.getModelInfoServiceInstance().getEnvironmentContext());
190                 paramsMap.put("workload_context", serviceInstance.getModelInfoServiceInstance().getWorkloadContext());
191                 Integer vfModuleIndex = vfModule.getModuleIndex();
192                 if (vfModuleIndex != null) {
193                         paramsMap.put("vf_module_index", vfModuleIndex.toString());
194                 }               
195         }
196         
197         private void buildParamsMapFromVnfSdncResponse(Map<String,String> paramsMap, GenericResourceApiVnftopologyVnfTopology vnfTopology, Map<String,String> networkRoleMap, boolean skipVnfResourceAssignments) throws JsonParseException, JsonMappingException, IOException {                                
198                 // Get VNF parameters from SDNC response
199                 GenericResourceApiParam vnfParametersData = vnfTopology.getVnfParametersData();
200                 buildParamsMapFromSdncParams(paramsMap, vnfParametersData);
201                 
202                 if(!skipVnfResourceAssignments) {
203                         GenericResourceApiVnfresourceassignmentsVnfResourceAssignments vnfResourceAssignments = vnfTopology.getVnfResourceAssignments();                
204                         if (vnfResourceAssignments != null) {
205                                 // Availability Zones
206                                 buildAvailabilityZones(paramsMap, vnfResourceAssignments);
207                                 // VNF Networks
208                                 buildVnfNetworks(paramsMap, vnfResourceAssignments, networkRoleMap);                    
209                         }
210                 }
211         }
212         
213         private void buildAvailabilityZones (Map<String,String> paramsMap, GenericResourceApiVnfresourceassignmentsVnfResourceAssignments vnfResourceAssignments) {             
214                 GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsAvailabilityZones availabilityZones = vnfResourceAssignments.getAvailabilityZones();
215                 if (availabilityZones != null) {
216                         List<String> availabilityZonesList = availabilityZones.getAvailabilityZone();
217                         if (availabilityZonesList != null) {
218                                 for(int i = 0; i < availabilityZonesList.size(); i++) {
219                                         paramsMap.put("availability_zone_" + i, availabilityZonesList.get(i));
220                                 }
221                         }
222                 }
223         }
224         
225         private void buildVnfNetworks (Map<String,String> paramsMap, GenericResourceApiVnfresourceassignmentsVnfResourceAssignments vnfResourceAssignments, Map<String,String> networkRoleMap) {
226                 GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks vnfNetworks = vnfResourceAssignments.getVnfNetworks();
227                 if (vnfNetworks != null) {
228                         List<GenericResourceApiVnfNetworkData> vnfNetworksList = vnfNetworks.getVnfNetwork();
229                         if (vnfNetworksList != null) {
230                                 for (int i = 0; i < vnfNetworksList.size(); i++) {
231                                         GenericResourceApiVnfNetworkData vnfNetwork = vnfNetworksList.get(i);
232                                         String networkRole = vnfNetwork.getNetworkRole();
233                                         String vnfNetworkKey = networkRoleMap.get(networkRole);
234                                         if (vnfNetworkKey == null || vnfNetworkKey.isEmpty()) {
235                                                 vnfNetworkKey = networkRole;
236                                         }               
237                                         
238                                         String vnfNetworkNeutronIdValue = vnfNetwork.getNeutronId();
239                                         paramsMap.put(vnfNetworkKey + "_net_id", vnfNetworkNeutronIdValue);
240                                         String vnfNetworkNetNameValue = vnfNetwork.getNetworkName();
241                                         paramsMap.put(vnfNetworkKey + "_net_name", vnfNetworkNetNameValue);
242                                         String vnfNetworkNetFqdnValue = vnfNetwork.getContrailNetworkFqdn();
243                                         paramsMap.put(vnfNetworkKey + "_net_fqdn", vnfNetworkNetFqdnValue);
244                                 
245                                         buildVnfNetworkSubnets(paramsMap, vnfNetwork, vnfNetworkKey);
246                                         
247                                 }
248                         }
249                 }
250         }
251         
252         private void buildVnfNetworkSubnets(Map<String,String> paramsMap, GenericResourceApiVnfNetworkData vnfNetwork, String vnfNetworkKey) {
253                 String vnfNetworkString = convertToString(vnfNetwork);
254                 Optional<String> ipv4Ips = jsonPath.locateResult(vnfNetworkString, "$.subnets-data.subnet-data[*].[?(@.ip-version == 'ipv4' && @.dhcp-enabled == 'Y')].subnet-id");
255                 if(ipv4Ips.isPresent())
256                         addPairToMap(paramsMap, vnfNetworkKey, SUBNET_ID, ipv4Ips.get());
257                 
258                 Optional<String> ipv6Ips = jsonPath.locateResult(vnfNetworkString, "$.subnets-data.subnet-data[*].[?(@.ip-version == 'ipv6' && @.dhcp-enabled == 'Y')].subnet-id");
259                 if(ipv6Ips.isPresent())
260                         addPairToMap(paramsMap, vnfNetworkKey, V6_SUBNET_ID, ipv6Ips.get());
261         }       
262
263         private void buildParamsMapFromVfModuleSdncResponse(Map<String,String> paramsMap, GenericResourceApiVfmoduletopologyVfModuleTopology vfModuleTopology, boolean skipVfModuleAssignments) throws JsonParseException, JsonMappingException, IOException {          
264                 // Get VF Module parameters from SDNC response
265                 GenericResourceApiParam vfModuleParametersData = vfModuleTopology.getVfModuleParameters();
266                 buildParamsMapFromSdncParams(paramsMap, vfModuleParametersData);
267                 
268                 if(!skipVfModuleAssignments) {          
269                         GenericResourceApiVfmoduleassignmentsVfModuleAssignments vfModuleAssignments = vfModuleTopology.getVfModuleAssignments();
270                         if (vfModuleAssignments != null) {                      
271                                 // VNF-VMS
272                                 GenericResourceApiVfmoduleassignmentsVfmoduleassignmentsVms vms = vfModuleAssignments.getVms();
273                                 if (vms != null) {
274                                 List<GenericResourceApiVmTopologyData> vmsList = vms.getVm();
275                                         if (vmsList != null) {
276                                                 for (GenericResourceApiVmTopologyData vm : vmsList){
277                                                         String key = vm.getVmType();
278                                                         buildVfModuleVmNames(paramsMap, vm, key);                                                                               
279                                                         GenericResourceApiVmtopologydataVmNetworks vmNetworks = vm.getVmNetworks();
280                                                         if (vmNetworks != null) {
281                                                                 List<GenericResourceApiVmNetworkData> vmNetworksList = vmNetworks.getVmNetwork();
282                                                                 if (vmNetworksList != null) {
283                                                                                 for(int n = 0; n < vmNetworksList.size(); n++){
284                                                                                 GenericResourceApiVmNetworkData network = vmNetworksList.get(n);
285                                                                                 network.getNetworkRoleTag();
286                                                                                 String networkKey = network.getNetworkRole();   
287                                                                                 // Floating IPs
288                                                                                 buildVfModuleFloatingIps(paramsMap, network, key, networkKey);                                                                  
289                                                                                 // Interface Route Prefixes
290                                                                                 buildVfModuleInterfaceRoutePrefixes(paramsMap, network, key, networkKey);                                                                       
291                                                                                 // SRIOV Parameters
292                                                                                 buildVfModuleSriovParameters(paramsMap, network, networkKey);                                                                   
293                                                                                 // IPV4 and IPV6 Addresses
294                                                                                 buildVfModuleNetworkInformation(paramsMap, network, key, networkKey);
295                                                                         }
296                                                                 }
297                                                         }
298                                                         
299                                                         buildParamsMapFromVfModuleForHeatTemplate(paramsMap, vm);
300                                                 }
301                                         }                               
302                                 }               
303                         }
304                 }
305         }
306         
307         private void buildVfModuleVmNames(Map<String,String> paramsMap, GenericResourceApiVmTopologyData vm, String key) {
308                 String values = "";
309                 GenericResourceApiVmtopologydataVmNames vmNames = vm.getVmNames();
310                 if (vmNames != null) {
311                         List<String> valueList = vmNames.getVmName();
312                         if (valueList != null) {
313                                 for(int i = 0; i < valueList.size(); i++){
314                                         String value = valueList.get(i);                                        
315                                         if (i != valueList.size() - 1) {
316                                                 values += value + ",";
317                                         }
318                                         else {
319                                                 values += value;
320                                         }
321                                         paramsMap.put(key + "_name_" + i, value);
322                                 }
323                                 paramsMap.put(key + "_names", values);
324                         }
325                 }
326         }
327         
328         private void buildVfModuleFloatingIps(Map<String,String> paramsMap, GenericResourceApiVmNetworkData network, String key, String networkKey) {
329                 GenericResourceApiVmnetworkdataFloatingIps floatingIps = network.getFloatingIps();
330                 if (floatingIps != null) {
331                         List<String> floatingIpV4List = floatingIps.getFloatingIpV4();
332                         if (floatingIpV4List != null) {
333                                 // add only one ipv4 floating ip for now
334                                 String floatingIPKey = key + UNDERSCORE + networkKey + FLOATING_IP;
335                                 String floatingIPKeyValue = floatingIpV4List.get(0);
336                                 if (floatingIPKeyValue != null && !floatingIPKeyValue.isEmpty()) {
337                                         paramsMap.put(floatingIPKey, floatingIPKeyValue);
338                                 }
339                         }
340                         // add only one ipv6 floating ip for now
341                         List<String> floatingIpV6List = floatingIps.getFloatingIpV6();
342                         if (floatingIpV6List != null) {
343                                 String floatingIPV6Key = key + UNDERSCORE + networkKey + FLOATING_V6_IP;
344                                 String floatingIPV6KeyValue = floatingIpV6List.get(0);
345                                 if (floatingIPV6KeyValue != null && !floatingIPV6KeyValue.isEmpty()) {
346                                         paramsMap.put(floatingIPV6Key, floatingIPV6KeyValue);
347                                 }
348                         }
349                 }
350         }
351         
352         private void buildVfModuleInterfaceRoutePrefixes(Map<String,String> paramsMap, GenericResourceApiVmNetworkData network, String key, String networkKey) {
353                 GenericResourceApiVmnetworkdataInterfaceRoutePrefixes interfaceRoutePrefixes = network.getInterfaceRoutePrefixes();
354                 if (interfaceRoutePrefixes != null) {
355                         List<String> interfaceRoutePrefixesList = interfaceRoutePrefixes.getInterfaceRoutePrefix();
356                         StringBuilder sbInterfaceRoutePrefixes = new StringBuilder();
357                         sbInterfaceRoutePrefixes.append("[");
358                         if (interfaceRoutePrefixesList != null) {
359                                 for(int a = 0; a < interfaceRoutePrefixesList.size(); a++){
360                                         String interfaceRoutePrefixValue = interfaceRoutePrefixesList.get(a);                                           
361                                         if (a != interfaceRoutePrefixesList.size() - 1) {
362                                                 sbInterfaceRoutePrefixes.append("{\"interface_route_table_routes_route_prefix\": \"" + interfaceRoutePrefixValue + "\"}" + ",");
363                                         }
364                                         else {
365                                                 sbInterfaceRoutePrefixes.append("{\"interface_route_table_routes_route_prefix\": \"" + interfaceRoutePrefixValue + "\"}");                                              
366                                         }
367                                 }                                                       
368                                 sbInterfaceRoutePrefixes.append("]");
369                                 if (interfaceRoutePrefixesList.size() > 0) {
370                                         paramsMap.put(key + UNDERSCORE + networkKey + "_route_prefixes", sbInterfaceRoutePrefixes.toString());
371                                 }
372                         }
373                 }
374         }
375         
376         private void buildVfModuleSriovParameters(Map<String,String> paramsMap, GenericResourceApiVmNetworkData network, String networkKey) {
377                 // SRIOV Parameters
378                 GenericResourceApiVmnetworkdataSriovParameters sriovParameters = network.getSriovParameters();
379                 if (sriovParameters != null) {
380                         GenericResourceApiVmnetworkdataSriovparametersHeatVlanFilters heatVlanFilters = sriovParameters.getHeatVlanFilters();
381                         if (heatVlanFilters != null) {
382                                 List<String> heatVlanFiltersList = heatVlanFilters.getHeatVlanFilter();
383                                 StringBuilder sriovFilterBuf = new StringBuilder();
384                                 if (heatVlanFiltersList != null) {
385                                         for(int a = 0; a < heatVlanFiltersList.size(); a++){
386                                                 String heatVlanFilterValue = heatVlanFiltersList.get(a);                                                
387                                                 if (a != heatVlanFiltersList.size() - 1) {
388                                                         sriovFilterBuf.append(heatVlanFilterValue).append(",");
389                                                 }
390                                                 else {
391                                                         sriovFilterBuf.append(heatVlanFilterValue);                                             
392                                                 }
393                                         }                                                       
394                                         if (heatVlanFiltersList.size() > 0) {
395                                                 paramsMap.put(networkKey + "_ATT_VF_VLAN_FILTER", sriovFilterBuf.toString());
396                                         }
397                                 }
398                         }
399                 }
400                 
401         }
402         
403         private void buildVfModuleNetworkInformation(Map<String,String> paramsMap, GenericResourceApiVmNetworkData network, String key, String networkKey) {
404                 
405                 GenericResourceApiVmnetworkdataNetworkInformationItems networkInformationItems = network.getNetworkInformationItems();
406                 StringBuilder sbIpv4Ips = new StringBuilder();
407                 StringBuilder sbIpv6Ips = new StringBuilder();
408                 
409                 if (networkInformationItems != null) {
410                         List<GenericResourceApiVmnetworkdataNetworkinformationitemsNetworkInformationItem> networkInformationItemList = networkInformationItems.getNetworkInformationItem();
411                         if (networkInformationItemList != null) {
412                                 for(int a = 0; a < networkInformationItemList.size(); a++){
413                                         GenericResourceApiVmnetworkdataNetworkinformationitemsNetworkInformationItem ipAddress = networkInformationItemList.get(a);
414                                         if (ipAddress != null) {
415                                                 GenericResourceApiVmnetworkdataNetworkinformationitemsNetworkinformationitemNetworkIps ips = ipAddress.getNetworkIps();
416                                                 if (ips != null) {
417                                                         List<String> ipsList = ips.getNetworkIp();
418                                                         if (ipsList != null) {                                                  
419                                                                 String ipVersion = ipAddress.getIpVersion();
420                                                                 for (int b = 0; b < ipsList.size(); b++) {
421                                                                         String ipAddressValue = ipsList.get(b);
422                                                                         if (ipVersion.equals("ipv4")) {
423                                                                                 if (b != ipsList.size() - 1) {
424                                                                                         sbIpv4Ips.append(ipAddressValue + ",");
425                                                                                 }
426                                                                                 else {
427                                                                                         sbIpv4Ips.append(ipAddressValue);
428                                                                                 }
429                                                                                 paramsMap.put(key + UNDERSCORE + networkKey + IP + UNDERSCORE + b, ipAddressValue);                                             
430                                                                         }
431                                                                         else if (ipVersion.equals("ipv6")) {
432                                                                                 if (b != ipsList.size() - 1) {
433                                                                                         sbIpv6Ips.append(ipAddressValue + ",");
434                                                                                 }
435                                                                                 else {
436                                                                                         sbIpv6Ips.append(ipAddressValue);
437                                                                                 }
438                                                                                 paramsMap.put(key + UNDERSCORE + networkKey + V6_IP + UNDERSCORE + b, ipAddressValue);                                                          
439                                                                         }                                                       
440                                                                 }
441                                                                 paramsMap.put(key + UNDERSCORE + networkKey + "_ips", sbIpv4Ips.toString());
442                                                                 paramsMap.put(key + UNDERSCORE + networkKey + "_v6_ips", sbIpv6Ips.toString());                                                         
443                                                         }
444                                                 }
445                                         }
446                                 }
447                         }
448                 }
449         }
450         
451         /*
452          * Build Mapping from GenericResourceApi SDNC for Heat Template so that AIC - PO gets accurate requests for vf module assignments.
453          * Build Count of SubInterfaces, VLAN Tag, network_name, network_id,
454          * ip_address (V4 and V6) and Floating IPs Addresses (V4 and V6) for Heat Template
455          */     
456         private void buildParamsMapFromVfModuleForHeatTemplate(Map<String,String> paramsMap, GenericResourceApiVmTopologyData vm) {
457                 GenericResourceApiVmtopologydataVmNames vmNames = vm.getVmNames();
458         
459                 if (vmNames != null) {
460                 
461                         List<GenericResourceApiVmtopologydataVmnamesVnfcNames> vnfcNamesList = vmNames.getVnfcNames();
462                         if (vnfcNamesList != null) {
463                                 
464                                 for(int i = 0; i < vnfcNamesList.size(); i++){
465                                         
466                                         GenericResourceApiVmtopologydataVmnamesVnfcNames vnfcNames = vnfcNamesList.get(i);
467                                         parseVnfcNamesData(paramsMap, vnfcNames);
468                                 }
469                         }
470                 }
471         }
472         
473         /*
474          * Parse vnfcNames data to build Mapping from GenericResourceApi SDNC for Heat Template.
475          */     
476         private void parseVnfcNamesData(Map<String,String> paramsMap, GenericResourceApiVmtopologydataVmnamesVnfcNames vnfcNames) {
477                 
478                 if (vnfcNames != null) {
479                         GenericResourceApiVnfcNetworkData vnfcNetworks = vnfcNames.getVnfcNetworks();
480                         if (vnfcNetworks != null) {
481                                 List<GenericResourceApiVnfcnetworkdataVnfcNetworkData> vnfcNetworkdataList = vnfcNetworks.getVnfcNetworkData();
482                                 
483                                 if (vnfcNetworkdataList != null) {
484                                         
485                                         for(int networkDataIdx = 0; networkDataIdx < vnfcNetworkdataList.size(); networkDataIdx++){
486                                         
487                                                 GenericResourceApiVnfcnetworkdataVnfcNetworkData vnfcNetworkdata = vnfcNetworkdataList.get(networkDataIdx);
488                                                 parseVnfcNetworkData(paramsMap, vnfcNetworkdata, networkDataIdx);
489                                         }
490                                 }
491                         }
492                 }
493         }
494         
495         /*
496          * Parse VnfcNetworkData to build Mapping from GenericResourceApi SDNC for Heat Template.
497          * Build Count of SubInterfaces, VLAN Tag, network_name, network_id,
498          * ip_address (V4 and V6) and Floating IPs Addresses (V4 and V6) for Heat Template
499          */     
500         private void parseVnfcNetworkData(Map<String,String> paramsMap, GenericResourceApiVnfcnetworkdataVnfcNetworkData vnfcNetworkdata, int networkDataIdx) {
501                 
502                 String vmTypeKey = vnfcNetworkdata.getVnfcType();
503                 GenericResourceApiVnfcnetworkdataVnfcnetworkdataVnfcPorts vnfcPorts = vnfcNetworkdata.getVnfcPorts();
504                 List<GenericResourceApiVnfcnetworkdataVnfcnetworkdataVnfcportsVnfcPort> vnfcPortList = vnfcPorts.getVnfcPort();
505                 if (vnfcPortList != null) {                                                                     
506                         for(int portIdx = 0; portIdx < vnfcPortList.size(); portIdx++){
507                         
508                                 GenericResourceApiVnfcnetworkdataVnfcnetworkdataVnfcportsVnfcPort vnfcPort = vnfcPortList.get(portIdx);
509                                 GenericResourceApiSubInterfaceNetworkData vnicSubInterfaces = vnfcPort.getVnicSubInterfaces();
510                         
511                                 String vnicSubInterfacesString = convertToString(vnicSubInterfaces);
512                                 String  networkRoleKey = vnfcPort.getCommonSubInterfaceRole();  
513                                 String subInterfaceKey = createVnfcSubInterfaceKey(vmTypeKey, networkDataIdx, networkRoleKey, portIdx);
514                                 String globalSubInterfaceKey = createGlobalVnfcSubInterfaceKey(vmTypeKey, networkRoleKey, portIdx);
515                                 
516                                 buildVfModuleSubInterfacesCount(paramsMap, globalSubInterfaceKey, vnicSubInterfaces);
517                         
518                                 buildVfModuleVlanTag(paramsMap, subInterfaceKey, vnicSubInterfacesString);
519                         
520                                 buildVfModuleNetworkName(paramsMap, subInterfaceKey, vnicSubInterfacesString);
521                         
522                                 buildVfModuleNetworkId(paramsMap, subInterfaceKey, vnicSubInterfacesString);
523
524                                 buildVfModuleIpV4AddressHeatTemplate(paramsMap, subInterfaceKey, vnicSubInterfacesString);
525                         
526                                 buildVfModuleIpV6AddressHeatTemplate(paramsMap, subInterfaceKey, vnicSubInterfacesString);
527                         
528                                 buildVfModuleFloatingIpV4HeatTemplate(paramsMap, globalSubInterfaceKey, vnicSubInterfacesString);
529
530                                 buildVfModuleFloatingIpV6HeatTemplate(paramsMap, globalSubInterfaceKey, vnicSubInterfacesString);
531                         }
532                 }
533         }
534         
535         /*
536          * Build "count" (calculating the total number of sub-interfaces) for Heat Template
537          * Building Criteria : {vm-type}_subint_{network-role}_port_{index}_subintcount
538          * vmTypeKey = vm-type, networkRoleKey = common-sub-interface-role
539          * Example: fw_subint_ctrl_port_0_subintcount
540          * 
541          */
542         private void buildVfModuleSubInterfacesCount(Map<String,String> paramsMap, String keyPrefix, GenericResourceApiSubInterfaceNetworkData vnicSubInterfaces) {
543
544                 List<GenericResourceApiSubinterfacenetworkdataSubInterfaceNetworkData> subInterfaceNetworkDataList = vnicSubInterfaces.getSubInterfaceNetworkData();
545                 
546                 if ( (subInterfaceNetworkDataList != null) && !subInterfaceNetworkDataList.isEmpty() ) {
547                         addPairToMap(paramsMap, keyPrefix, SUB_INT_COUNT, String.valueOf(subInterfaceNetworkDataList.size()));
548                 }
549     }
550         
551         protected String createVnfcSubInterfaceKey(String vmTypeKey, int networkDataIdx, String networkRoleKey, int portIdx) {
552                 
553                 return Joiner.on(UNDERSCORE).join(Arrays.asList(vmTypeKey, networkDataIdx, SUB_INT, networkRoleKey, PORT, portIdx));
554         }
555         
556         protected String createGlobalVnfcSubInterfaceKey(String vmTypeKey,String networkRoleKey, int portIdx) {
557                 
558                 return Joiner.on(UNDERSCORE).join(Arrays.asList(vmTypeKey, SUB_INT, networkRoleKey, PORT, portIdx));
559         }
560
561         /*
562          * Build VLAN Tag for Heat Template
563          * Building Criteria : {vm-type}_{index}_subint_{network-role}_port_{index}_vlan_ids
564          * vmTypeKey = vm-type, networkRoleKey = common-sub-interface-role
565          * Example: fw_0_subint_ctrl_port_0_vlan_ids
566          * 
567          */
568         protected void buildVfModuleVlanTag(Map<String,String> paramsMap, String keyPrefix, String vnicSubInterfaces) {
569                 
570                 List<String> vlanTagIds = jsonPath.locateResultList(vnicSubInterfaces, "$.sub-interface-network-data[*].vlan-tag-id");
571
572                 addPairToMap(paramsMap, keyPrefix, VLAN_IDS, vlanTagIds);
573         }
574         /*
575          * Build "network_name" for Heat Template
576          * Building Criteria : {vm-type}_{index}_subint_{network-role}_port_{index}_net_names
577          * vmTypeKey = vm-type, networkRoleKey = common-sub-interface-role
578          * Example: fw_0_subint_ctrl_port_0_net_names
579          * 
580          */
581         protected void buildVfModuleNetworkName(Map<String,String> paramsMap, String keyPrefix, String vnicSubInterfaces) {
582                 
583                 List<String> neworkNames = jsonPath.locateResultList(vnicSubInterfaces, "$.sub-interface-network-data[*].network-name");
584                 
585                 addPairToMap(paramsMap, keyPrefix, NET_NAMES, neworkNames);
586         }
587
588         /*
589          * Build "network_id" for Heat Template
590          * Building Criteria : {vm-type}_{index}_subint_{network-role}_port_{index}_net_ids
591          * vmTypeKey = vm-type, networkRoleKey = common-sub-interface-role
592          * Example: fw_0_subint_ctrl_port_0_net_ids
593          * 
594          */
595         protected void buildVfModuleNetworkId(Map<String,String> paramsMap, String keyPrefix, String vnicSubInterfaces) {
596                 
597                 List<String> neworkIds = jsonPath.locateResultList(vnicSubInterfaces, "$.sub-interface-network-data[*].network-id");
598                 
599                 addPairToMap(paramsMap, keyPrefix, NET_IDS, neworkIds);
600         }
601         
602         /*
603          * Build ip_address for V4 for Heat Template
604          * Building Criteria : 
605          * {vm-type}_{index}_subint_{network-role}_port_{index}_ip_{index}  -- for ipV4
606          * key = vm-type, networkRoleKey = NetWork-Role
607          */
608         protected void buildVfModuleIpV4AddressHeatTemplate(Map<String,String> paramsMap, String keyPrefix, String vnicSubInterfaces) {
609                 
610                 List<String> ipv4Ips = jsonPath.locateResultList(vnicSubInterfaces, "$.sub-interface-network-data[*].network-information-items.network-information-item[?(@.ip-version == 'ipv4')].network-ips.network-ip[*]");
611                 
612                 addPairToMap(paramsMap, keyPrefix, IP, ipv4Ips);
613
614                 for (int i = 0; i < ipv4Ips.size(); i++) {
615                         addPairToMap(paramsMap, keyPrefix, IP + UNDERSCORE + i, ipv4Ips.get(i));
616                 }
617
618         }
619         
620         /*
621          * Build ip_address for Heat Template
622          * Building Criteria : 
623          * {vm-type}_{index}_subint_{network-role}_port_{index}_v6_ip_{index} -- for ipV6
624          * key = vm-type, networkRoleKey = NetWork-Role
625          */
626         protected void buildVfModuleIpV6AddressHeatTemplate(Map<String,String> paramsMap, String keyPrefix, String vnicSubInterfaces) {
627                 
628                 List<String> ipv6Ips = jsonPath.locateResultList(vnicSubInterfaces, "$.sub-interface-network-data[*].network-information-items.network-information-item[?(@.ip-version == 'ipv6')].network-ips.network-ip[*]");
629
630                 addPairToMap(paramsMap, keyPrefix, V6_IP, ipv6Ips);
631
632                 for (int i = 0; i < ipv6Ips.size(); i++) {
633                         addPairToMap(paramsMap, keyPrefix, V6_IP + UNDERSCORE + i, ipv6Ips.get(i));
634                 }
635     }
636         
637         /*
638          * Build floatingip_address for Heat Template
639          * Building Criteria : 
640          * {vm-type}_subint_{network-role}_port_{index}_floating_ip  -- for ipV4
641          */
642         protected void buildVfModuleFloatingIpV4HeatTemplate(Map<String,String> paramsMap, String keyPrefix, String vnicSubInterfaces) {
643                 
644                 List<String> floatingV4 = jsonPath.locateResultList(vnicSubInterfaces, "$.sub-interface-network-data[*].floating-ips.floating-ip-v4[*]");
645
646                 if (!floatingV4.isEmpty()) {
647                         floatingV4 = Collections.singletonList(floatingV4.get(0));
648                 }
649                 addPairToMap(paramsMap, keyPrefix, FLOATING_IP, floatingV4);
650
651         }
652         
653         /*
654          * Build floatingip_address for Heat Template
655          * Building Criteria : 
656          * {vm-type}_subint_{network-role}_port_{index}_floating_v6_ip -- for ipV6
657          */
658         protected void buildVfModuleFloatingIpV6HeatTemplate(Map<String,String> paramsMap, String keyPrefix, String vnicSubInterfaces) {
659                                 
660                 List<String> floatingV6 = jsonPath.locateResultList(vnicSubInterfaces, "$.sub-interface-network-data[*].floating-ips.floating-ip-v6[*]");
661                 
662                 if (!floatingV6.isEmpty()) {
663                         floatingV6 = Collections.singletonList(floatingV6.get(0));
664                 }
665                 addPairToMap(paramsMap, keyPrefix, FLOATING_V6_IP, floatingV6);
666         }
667         
668         protected void addPairToMap(Map<String, String> paramsMap, String keyPrefix, String key, String value) {
669                 
670                 addPairToMap(paramsMap, keyPrefix, key, Collections.singletonList(value));
671         }
672
673         protected void addPairToMap(Map<String, String> paramsMap, String keyPrefix, String key, List<String> value) {
674                 
675                 if (!value.isEmpty()) {
676                         paramsMap.put(keyPrefix + key, Joiner.on(",").join(value));
677                 }
678         }
679         
680         private void buildParamsMapFromSdncParams(Map<String,String> paramsMap, GenericResourceApiParam parametersData) {               
681                 if (parametersData != null) {
682                         List<GenericResourceApiParamParam> paramsList = parametersData.getParam();
683                         if (paramsList != null) {
684                                 for (int i = 0; i < paramsList.size(); i++) {
685                                         GenericResourceApiParamParam param = paramsList.get(i);
686                                         String parameterName = param.getName();
687                                         if (!sdncResponseParamsToSkip.contains(parameterName)) {
688                                                 String parameterValue = param.getValue();
689                                                 paramsMap.put(parameterName, parameterValue);
690                                         }
691                                 }                               
692                         }
693                 }                                               
694         }
695         
696         private Map<String,String> buildNetworkRoleMap(GenericResourceApiVfmoduletopologyVfModuleTopology vfModuleTopology) throws JsonParseException, JsonMappingException, IOException {
697                 Map<String, String> networkRoleMap = new HashMap<>();           
698                 GenericResourceApiVfmoduleassignmentsVfModuleAssignments vfModuleAssignments = vfModuleTopology.getVfModuleAssignments();
699                 if (vfModuleAssignments != null) {                      
700                         GenericResourceApiVfmoduleassignmentsVfmoduleassignmentsVms vms = vfModuleAssignments.getVms();
701                         if (vms != null) {
702                         List<GenericResourceApiVmTopologyData> vmsList = vms.getVm();
703                         if (vmsList != null) {
704                                         for (GenericResourceApiVmTopologyData vm : vmsList){                                                    
705                                                 GenericResourceApiVmtopologydataVmNetworks vmNetworks = vm.getVmNetworks();
706                                                 if (vmNetworks != null) {
707                                                         List<GenericResourceApiVmNetworkData> vmNetworksList = vmNetworks.getVmNetwork();
708                                                         if (vmNetworksList != null) {
709                                                                         for(int n = 0; n < vmNetworksList.size(); n++){
710                                                                                 GenericResourceApiVmNetworkData network = vmNetworksList.get(n);
711                                                                                 String networkRole = network.getNetworkRole();
712                                                                                 String networkRoleValue = network.getNetworkRoleTag();
713                                                                                 if (networkRoleValue ==  null || networkRoleValue.isEmpty()) {
714                                                                                         networkRoleValue = networkRole;
715                                                                                 }
716                                                                                 networkRoleMap.put(networkRole, networkRoleValue);
717                                                                         }
718                                                                 }
719                                                         }
720                                                 }
721                                         }
722                                 }
723                         }
724                         return networkRoleMap;
725                 }
726         
727         public DeleteVfModuleRequest deleteVfModuleRequestMapper(RequestContext requestContext,CloudRegion cloudRegion, 
728                         ServiceInstance serviceInstance, GenericVnf genericVnf,
729                         VfModule vfModule) throws IOException {
730                 DeleteVfModuleRequest deleteVfModuleRequest = new DeleteVfModuleRequest();
731                 deleteVfModuleRequest.setCloudSiteId(cloudRegion.getLcpCloudRegionId());
732                 deleteVfModuleRequest.setTenantId(cloudRegion.getTenantId());
733                 deleteVfModuleRequest.setVnfId(genericVnf.getVnfId());
734                 deleteVfModuleRequest.setVfModuleId(vfModule.getVfModuleId());
735                 deleteVfModuleRequest.setVfModuleStackId(vfModule.getHeatStackId());//DoDVfMod_heatStackId
736                 deleteVfModuleRequest.setSkipAAI(true);                 
737                 String messageId = vnfAdapterObjectMapperUtils.getRandomUuid();
738                 deleteVfModuleRequest.setMessageId(messageId);          
739                 deleteVfModuleRequest.setNotificationUrl(vnfAdapterObjectMapperUtils.createCallbackUrl("VNFAResponse", messageId));
740                 MsoRequest msoRequest = buildMsoRequest(requestContext, serviceInstance);       
741                 deleteVfModuleRequest.setMsoRequest(msoRequest);
742                 return deleteVfModuleRequest;
743         }
744         
745         private String convertToString(Object obj) {
746                 String json;
747                 try {
748                         json = mapper.writeValueAsString(obj);
749                 } catch (JsonProcessingException e) {
750                         json = "{}";
751                 }
752                 
753                 return json;
754         }
755         
756         private VfModule getBaseVfModule(GenericVnf genericVnf) {
757                 List<VfModule> vfModules = genericVnf.getVfModules();
758                 VfModule baseVfModule = null;
759                 if (vfModules != null) {
760                         for(int i = 0; i < vfModules.size(); i++) {
761                                 if (vfModules.get(i).getModelInfoVfModule().getIsBaseBoolean()) {
762                                         baseVfModule = vfModules.get(i);
763                                         break;                                  
764                                 }
765                         }
766                 }
767                 return baseVfModule;
768         }
769 }