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