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