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