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