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