Merge "fix lookup of si if customer exists"
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / servicedecomposition / tasks / BBInputSetup.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.bpmn.servicedecomposition.tasks;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.UUID;
29
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.camunda.bpm.engine.delegate.JavaDelegate;
32 import org.javatuples.Pair;
33 import org.onap.so.bpmn.common.BuildingBlockExecution;
34 import org.onap.so.bpmn.common.DelegateExecutionImpl;
35 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
36 import org.onap.so.bpmn.servicedecomposition.bbobjects.Collection;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.LineOfBusiness;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.NetworkPolicy;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.OwningEntity;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.Platform;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.Project;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.RouteTableReference;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
49 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceSubscription;
50 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
51 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
52 import org.onap.so.bpmn.servicedecomposition.entities.ConfigurationResourceKeys;
53 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
54 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
55 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
56 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
57 import org.onap.so.bpmn.servicedecomposition.generalobjects.OrchestrationContext;
58 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
59 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
60 import org.onap.so.client.aai.AAIObjectType;
61 import org.onap.so.client.aai.entities.AAIResultWrapper;
62 import org.onap.so.client.aai.entities.Relationships;
63 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
64 import org.onap.so.client.exception.ExceptionBuilder;
65 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
66 import org.onap.so.db.catalog.beans.CollectionResource;
67 import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
68 import org.onap.so.db.catalog.beans.ConfigurationResourceCustomization;
69 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
70 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
71 import org.onap.so.db.catalog.beans.OrchestrationStatus;
72 import org.onap.so.db.catalog.beans.Service;
73 import org.onap.so.db.catalog.beans.VfModuleCustomization;
74 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
75 import org.onap.so.db.catalog.beans.VnfVfmoduleCvnfcConfigurationCustomization;
76 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
77 import org.onap.so.db.request.beans.InfraActiveRequests;
78 import org.onap.so.logger.MsoLogger;
79 import org.onap.so.serviceinstancebeans.CloudConfiguration;
80 import org.onap.so.serviceinstancebeans.ModelInfo;
81 import org.onap.so.serviceinstancebeans.ModelType;
82 import org.onap.so.serviceinstancebeans.Networks;
83 import org.onap.so.serviceinstancebeans.RelatedInstance;
84 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
85 import org.onap.so.serviceinstancebeans.RequestDetails;
86 import org.onap.so.serviceinstancebeans.RequestParameters;
87 import org.onap.so.serviceinstancebeans.Resources;
88 import org.onap.so.serviceinstancebeans.VfModules;
89 import org.onap.so.serviceinstancebeans.Vnfs;
90 import org.springframework.beans.factory.annotation.Autowired;
91 import org.springframework.stereotype.Component;
92
93 import com.fasterxml.jackson.core.JsonProcessingException;
94 import com.fasterxml.jackson.databind.ObjectMapper;
95 import com.fasterxml.jackson.databind.SerializationFeature;
96
97 @Component("BBInputSetup")
98 public class BBInputSetup implements JavaDelegate {
99
100         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, BBInputSetup.class);
101         private static final String FLOW_VAR_NAME = "flowToBeCalled";
102         private static final String LOOKUP_KEY_MAP_VAR_NAME = "lookupKeyMap";
103         private static final String GBB_INPUT_VAR_NAME = "gBBInput";
104         private static final String EXECUTE_BB_VAR_NAME = "buildingBlock";
105         private static final String VOLUME_GROUP = "VolumeGroup";
106         private static final String VF_MODULE = "VfModule";
107         private static final String NETWORK = "Network";
108         private static final String VNF = "Vnf";
109         private static final String NETWORK_COLLECTION = "NetworkCollection";
110
111         @Autowired
112         private BBInputSetupUtils bbInputSetupUtils;
113
114         @Autowired
115         private BBInputSetupMapperLayer mapperLayer;
116         
117         @Autowired
118         private CloudInfoFromAAI cloudInfoFromAAI;
119
120         @Autowired
121         private ExceptionBuilder exceptionUtil;
122
123         private ObjectMapper mapper = new ObjectMapper();
124
125         public BBInputSetupUtils getBbInputSetupUtils() {
126                 return bbInputSetupUtils;
127         }
128         
129         public void setCloudInfoFromAAI(CloudInfoFromAAI cloudInfoFromAAI) {
130                 this.cloudInfoFromAAI = cloudInfoFromAAI;
131         }
132
133         public void setBbInputSetupUtils(BBInputSetupUtils bbInputSetupUtils) {
134                 this.bbInputSetupUtils = bbInputSetupUtils;
135         }
136
137         public BBInputSetupMapperLayer getMapperLayer() {
138                 return mapperLayer;
139         }
140
141         public void setMapperLayer(BBInputSetupMapperLayer mapperLayer) {
142                 this.mapperLayer = mapperLayer;
143         }
144
145         @Override
146         public void execute(DelegateExecution execution) throws Exception {
147                 try {
148                         GeneralBuildingBlock outputBB = null;
149                         ExecuteBuildingBlock executeBB = this.getExecuteBBFromExecution(execution);
150                         String resourceId = executeBB.getResourceId();
151                         String requestAction = executeBB.getRequestAction();
152                         String vnfType = executeBB.getVnfType();
153                         boolean aLaCarte = Boolean.TRUE.equals(executeBB.isaLaCarte());
154                         boolean homing = Boolean.TRUE.equals(executeBB.isHoming());
155                         Map<ResourceKey, String> lookupKeyMap = new HashMap<>();
156                         outputBB = this.getGBB(executeBB, lookupKeyMap, requestAction, aLaCarte, resourceId, vnfType);
157                         ObjectMapper mapper = new ObjectMapper();
158                         mapper.enable(SerializationFeature.INDENT_OUTPUT);
159                         msoLogger.debug("GeneralBB: " + mapper.writeValueAsString(outputBB));
160
161                         setHomingFlag(outputBB, homing, lookupKeyMap);
162
163                         execution.setVariable(FLOW_VAR_NAME, executeBB.getBuildingBlock().getBpmnFlowName());
164                         execution.setVariable(GBB_INPUT_VAR_NAME, outputBB);
165                         execution.setVariable(LOOKUP_KEY_MAP_VAR_NAME, lookupKeyMap);
166
167                         BuildingBlockExecution gBuildingBlockExecution = new DelegateExecutionImpl(execution);
168                         execution.setVariable("gBuildingBlockExecution", gBuildingBlockExecution);
169                         execution.setVariable("RetryCount", 1);
170                         execution.setVariable("handlingCode", "Success");
171                 } catch (Exception e) {
172                         msoLogger.error(e);
173                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e.getMessage());
174                 }
175         }
176
177         protected void setHomingFlag(GeneralBuildingBlock outputBB, boolean homing, Map<ResourceKey, String> lookupKeyMap) {
178
179                 if (lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID) != null && homing && outputBB != null) {
180                         for (GenericVnf vnf : outputBB.getCustomer().getServiceSubscription().getServiceInstances().get(0).getVnfs()) {
181                                 if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
182                                         vnf.setCallHoming(homing);
183                                 }
184                         }
185                 }
186         }
187
188         protected ExecuteBuildingBlock getExecuteBBFromExecution(DelegateExecution execution) {
189                 return (ExecuteBuildingBlock) execution.getVariable(EXECUTE_BB_VAR_NAME);
190         }
191
192         protected GeneralBuildingBlock getGBB(ExecuteBuildingBlock executeBB, Map<ResourceKey, String> lookupKeyMap,
193                         String requestAction, boolean aLaCarte, String resourceId, String vnfType) throws Exception {
194                 String requestId = executeBB.getRequestId();
195                 this.populateLookupKeyMapWithIds(executeBB.getWorkflowResourceIds(), lookupKeyMap);
196                 RequestDetails requestDetails = executeBB.getRequestDetails();
197                 if(requestDetails == null) {
198                         requestDetails = bbInputSetupUtils.getRequestDetails(requestId);
199                 }
200                 if (requestDetails.getModelInfo() == null) {
201                         return this.getGBBCM(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId);
202                 }
203                 else {
204                         ModelType modelType = requestDetails.getModelInfo().getModelType();
205                         if (aLaCarte && modelType.equals(ModelType.service)) {
206                                 return this.getGBBALaCarteService(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId);
207                         } else if (aLaCarte && !modelType.equals(ModelType.service)) {
208                                 return this.getGBBALaCarteNonService(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId,
209                                                 vnfType);
210                         } else {
211                                 return this.getGBBMacro(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId, vnfType);
212                         }
213                 }
214         }
215
216         protected void populateLookupKeyMapWithIds(WorkflowResourceIds workflowResourceIds,
217                         Map<ResourceKey, String> lookupKeyMap) {
218                 lookupKeyMap.put(ResourceKey.SERVICE_INSTANCE_ID, workflowResourceIds.getServiceInstanceId());
219                 lookupKeyMap.put(ResourceKey.NETWORK_ID, workflowResourceIds.getNetworkId());
220                 lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, workflowResourceIds.getVnfId());
221                 lookupKeyMap.put(ResourceKey.VF_MODULE_ID, workflowResourceIds.getVfModuleId());
222                 lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, workflowResourceIds.getVolumeGroupId());
223                 lookupKeyMap.put(ResourceKey.CONFIGURATION_ID, workflowResourceIds.getConfigurationId());
224         }
225
226         protected GeneralBuildingBlock getGBBALaCarteNonService(ExecuteBuildingBlock executeBB,
227                         RequestDetails requestDetails, Map<ResourceKey, String> lookupKeyMap, String requestAction,
228                         String resourceId, String vnfType) throws Exception {
229                 String bbName = executeBB.getBuildingBlock().getBpmnFlowName();
230                 String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
231                 org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
232                 if (serviceInstanceId != null) {
233                         aaiServiceInstance = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
234                 }
235                 Service service = null;
236                 if (aaiServiceInstance != null) {
237                         service = bbInputSetupUtils.getCatalogServiceByModelUUID(aaiServiceInstance.getModelVersionId());
238                 }
239                 if (aaiServiceInstance != null && service != null) {
240                         ServiceInstance serviceInstance = this.getExistingServiceInstance(aaiServiceInstance);
241                         serviceInstance.setModelInfoServiceInstance(this.mapperLayer.mapCatalogServiceIntoServiceInstance(service));
242                         this.populateObjectsOnAssignAndCreateFlows(requestDetails, service, bbName, serviceInstance, lookupKeyMap,
243                                         resourceId, vnfType);
244                         return this.populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, null);
245                 } else {
246                         msoLogger.debug("Related Service Instance from AAI: " + aaiServiceInstance);
247                         msoLogger.debug("Related Service Instance Model Info from AAI: " + service);
248                         throw new Exception("Could not find relevant information for related Service Instance");
249                 }
250         }
251         
252         protected GeneralBuildingBlock getGBBCM(ExecuteBuildingBlock executeBB,
253                         RequestDetails requestDetails, Map<ResourceKey, String> lookupKeyMap, String requestAction,
254                         String resourceId) throws Exception {           
255                 ServiceInstance serviceInstance = new ServiceInstance();
256                 String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
257                 serviceInstance.setServiceInstanceId(serviceInstanceId);
258                 
259                 List<GenericVnf> genericVnfs = serviceInstance.getVnfs();
260                 
261                 String vnfId = lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID);
262                 org.onap.aai.domain.yang.GenericVnf aaiGenericVnf = bbInputSetupUtils.getAAIGenericVnf(vnfId);
263                 
264                 GenericVnf genericVnf = this.mapperLayer.mapAAIGenericVnfIntoGenericVnf(aaiGenericVnf);
265                 genericVnfs.add(genericVnf);            
266                 
267                 return this.populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, new Customer());
268                 
269         }
270
271         protected void populateObjectsOnAssignAndCreateFlows(RequestDetails requestDetails, Service service, String bbName,
272                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap, String resourceId, String vnfType)
273                         throws Exception {
274                 ModelInfo modelInfo = requestDetails.getModelInfo();
275                 String instanceName = requestDetails.getRequestInfo().getInstanceName();
276                 ModelType modelType = modelInfo.getModelType();
277                 RelatedInstanceList[] relatedInstanceList = requestDetails.getRelatedInstanceList();
278
279                 org.onap.so.serviceinstancebeans.Platform platform = requestDetails.getPlatform();
280                 org.onap.so.serviceinstancebeans.LineOfBusiness lineOfBusiness = requestDetails.getLineOfBusiness();
281
282                 if (modelType.equals(ModelType.network)) {
283                         lookupKeyMap.put(ResourceKey.NETWORK_ID, resourceId);
284                         this.populateL3Network(instanceName, modelInfo, service, bbName, serviceInstance, lookupKeyMap, resourceId, null);
285                 } else if (modelType.equals(ModelType.vnf)) {
286                         lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, resourceId);
287                         this.populateGenericVnf(modelInfo, instanceName, platform, lineOfBusiness, service, bbName, serviceInstance,
288                                         lookupKeyMap, relatedInstanceList, resourceId, vnfType, null);
289                 } else if (modelType.equals(ModelType.volumeGroup)) {
290                         lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, resourceId);
291                         this.populateVolumeGroup(modelInfo, service, bbName, serviceInstance, lookupKeyMap, resourceId,
292                                         relatedInstanceList, instanceName, vnfType, null);
293                 } else if (modelType.equals(ModelType.vfModule)) {
294                         lookupKeyMap.put(ResourceKey.VF_MODULE_ID, resourceId);
295                         this.populateVfModule(modelInfo, service, bbName, serviceInstance, lookupKeyMap, resourceId,
296                                         relatedInstanceList, instanceName, null, requestDetails.getCloudConfiguration());
297                 } else {
298                         return;
299                 }
300         }
301
302         protected void populateConfiguration(ModelInfo modelInfo, Service service, String bbName,
303                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap, String resourceId, String instanceName, ConfigurationResourceKeys configurationResourceKeys) {
304                 Configuration configuration = null;
305                 for (Configuration configurationTemp : serviceInstance.getConfigurations()) {
306                         if (lookupKeyMap.get(ResourceKey.CONFIGURATION_ID) != null
307                                         && configurationTemp.getConfigurationId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.CONFIGURATION_ID))) {
308                                 configuration = configurationTemp;
309                                 org.onap.aai.domain.yang.Configuration aaiConfiguration = bbInputSetupUtils.getAAIConfiguration(configuration.getConfigurationId());
310                                 if(aaiConfiguration!=null){
311                                         modelInfo.setModelCustomizationUuid(aaiConfiguration.getModelCustomizationId());
312                                 }
313                         }
314                 }
315                 if (configuration == null && bbName.equalsIgnoreCase(AssignFlows.FABRIC_CONFIGURATION.toString())) {
316                         configuration = this.createConfiguration(lookupKeyMap, instanceName, resourceId);
317                         serviceInstance.getConfigurations().add(configuration);
318                 }
319                 if(configuration != null) {
320                         this.mapCatalogConfiguration(configuration, modelInfo, service, configurationResourceKeys);
321                 }
322         }
323
324         protected Configuration createConfiguration(Map<ResourceKey, String> lookupKeyMap,
325                         String instanceName, String resourceId) {
326                 lookupKeyMap.put(ResourceKey.CONFIGURATION_ID, resourceId);
327                 Configuration configuration = new Configuration();
328                 configuration.setConfigurationId(resourceId);
329                 configuration.setConfigurationName(instanceName);
330                 configuration.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
331                 return configuration;
332         }
333
334         protected void mapCatalogConfiguration(Configuration configuration, ModelInfo modelInfo, Service service, ConfigurationResourceKeys configurationResourceKeys) {
335                 ConfigurationResourceCustomization configurationResourceCustomization = findConfigurationResourceCustomization(modelInfo, service);
336                 VnfVfmoduleCvnfcConfigurationCustomization vnfVfmoduleCvnfcConfigurationCustomization = 
337                                 findVnfVfmoduleCvnfcConfigurationCustomization(configurationResourceKeys.getVfModuleCustomizationUUID(),
338                                                 configurationResourceKeys.getVnfResourceCustomizationUUID(), configurationResourceKeys.getCvnfcCustomizationUUID(), configurationResourceCustomization);
339                 if (configurationResourceCustomization != null && vnfVfmoduleCvnfcConfigurationCustomization != null) {
340                         configuration.setModelInfoConfiguration(this.mapperLayer.mapCatalogConfigurationToConfiguration(configurationResourceCustomization
341                                         , vnfVfmoduleCvnfcConfigurationCustomization));
342                 }
343         }
344
345         protected VnfVfmoduleCvnfcConfigurationCustomization findVnfVfmoduleCvnfcConfigurationCustomization(String vfModuleCustomizationUUID, 
346                         String vnfResourceCustomizationUUID, String cvnfcCustomizationUUID, ConfigurationResourceCustomization configurationResourceCustomization) {
347
348                 if(configurationResourceCustomization.getConfigurationResource() != null)
349                         for(VnfVfmoduleCvnfcConfigurationCustomization vnfVfmoduleCvnfcConfigurationCustomization : 
350                                 configurationResourceCustomization.getConfigurationResource().getVnfVfmoduleCvnfcConfigurationCustomization()) {
351                                 if(vnfVfmoduleCvnfcConfigurationCustomization.getVfModuleCustomization().getModelCustomizationUUID().equalsIgnoreCase(vfModuleCustomizationUUID)
352                                                 && vnfVfmoduleCvnfcConfigurationCustomization.getVnfResourceCustomization().getModelCustomizationUUID().equalsIgnoreCase(vnfResourceCustomizationUUID)
353                                                 && vnfVfmoduleCvnfcConfigurationCustomization.getCvnfcCustomization().getModelCustomizationUUID().equalsIgnoreCase(cvnfcCustomizationUUID)) {
354                                         return vnfVfmoduleCvnfcConfigurationCustomization;
355                                 }
356                         }
357                 return null;
358         }
359
360         protected ConfigurationResourceCustomization findConfigurationResourceCustomization(ModelInfo modelInfo, Service service) {
361                 for (ConfigurationResourceCustomization resourceCust : service.getConfigurationCustomizations()) {
362                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(modelInfo.getModelCustomizationUuid())) {
363                                 return resourceCust;
364                         }
365                 }
366                 return null;
367         }
368
369         protected void populateVfModule(ModelInfo modelInfo, Service service, String bbName,
370                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap, String resourceId,
371                         RelatedInstanceList[] relatedInstanceList, String instanceName, List<Map<String, String>> instanceParams, CloudConfiguration cloudConfiguration) throws Exception {
372                 String vnfModelCustomizationUUID = null;
373                 if (relatedInstanceList != null) {
374                         for (RelatedInstanceList relatedInstList : relatedInstanceList) {
375                                 RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
376                                 if (relatedInstance.getModelInfo().getModelType().equals(ModelType.vnf)) {
377                                         vnfModelCustomizationUUID = relatedInstance.getModelInfo().getModelCustomizationId();
378                                 }
379                                 if (relatedInstance.getModelInfo().getModelType().equals(ModelType.volumeGroup)) {
380                                         lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, relatedInstance.getInstanceId());
381                                 }
382                         }
383                 }
384                 GenericVnf vnf = null;
385                 for (GenericVnf tempVnf : serviceInstance.getVnfs()) {
386                         if (tempVnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
387                                 vnf = tempVnf;
388                                 vnfModelCustomizationUUID = this.bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId())
389                                                 .getModelCustomizationId();
390                                 ModelInfo vnfModelInfo = new ModelInfo();
391                                 vnfModelInfo.setModelCustomizationUuid(vnfModelCustomizationUUID);
392                                 this.mapCatalogVnf(tempVnf, vnfModelInfo, service);
393                                 Optional<String> volumeGroupIdOp = getVolumeGroupIdRelatedToVfModule(tempVnf, modelInfo, cloudConfiguration.getCloudOwner(), 
394                                                 cloudConfiguration.getLcpCloudRegionId(), lookupKeyMap);
395                                 if(volumeGroupIdOp.isPresent()) {
396                                         lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, volumeGroupIdOp.get());
397                                 }
398                                 break;
399                         }
400                 }
401                 if (vnf != null) {
402                         VfModule vfModule = null;
403                         for (VfModule vfModuleTemp : vnf.getVfModules()) {
404                                 if (lookupKeyMap.get(ResourceKey.VF_MODULE_ID) != null
405                                                 && vfModuleTemp.getVfModuleId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.VF_MODULE_ID))) {
406                                         vfModule = vfModuleTemp;
407                                         String vfModuleCustId = bbInputSetupUtils.getAAIVfModule(vnf.getVnfId(), vfModule.getVfModuleId()).getModelCustomizationId();
408                                         modelInfo.setModelCustomizationId(vfModuleCustId);
409                                         break;
410                                 }
411                         }
412                         if (vfModule == null && bbName.equalsIgnoreCase(AssignFlows.VF_MODULE.toString())) {
413                                 vfModule = createVfModule(lookupKeyMap,
414                                                 resourceId, instanceName, instanceParams);
415                                 vnf.getVfModules().add(vfModule);
416                         }
417                         if(vfModule != null) {
418                                 mapCatalogVfModule(vfModule, modelInfo, service, vnfModelCustomizationUUID);
419                         }
420                 } else {
421                         msoLogger.debug("Related VNF instance Id not found:  " + lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID));
422                         throw new Exception("Could not find relevant information for related VNF");
423                 }
424         }
425         
426         protected Optional<String> getVolumeGroupIdRelatedToVfModule(GenericVnf vnf, ModelInfo modelInfo, 
427                         String cloudOwner, String cloudRegionId, Map<ResourceKey, String> lookupKeyMap) {
428                 if (lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID) == null) {
429                         for(VolumeGroup volumeGroup : vnf.getVolumeGroups()) {
430                                 String volumeGroupCustId = 
431                                                 bbInputSetupUtils.getAAIVolumeGroup(cloudOwner, 
432                                                                 cloudRegionId, volumeGroup.getVolumeGroupId()).getModelCustomizationId();
433                                 if(modelInfo.getModelCustomizationId().equalsIgnoreCase(volumeGroupCustId)) {
434                                         return Optional.of(volumeGroup.getVolumeGroupId());
435                                 }
436                         }
437                 }
438                 return Optional.empty();
439         }
440
441         protected void mapCatalogVfModule(VfModule vfModule, ModelInfo modelInfo, Service service,
442                         String vnfModelCustomizationUUID) {
443                 if(modelInfo.getModelCustomizationUuid() != null) {
444                         modelInfo.setModelCustomizationId(modelInfo.getModelCustomizationUuid());
445                 }
446                 VnfResourceCustomization vnfResourceCustomization = null;
447                 for (VnfResourceCustomization resourceCust : service.getVnfCustomizations()) {
448                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(vnfModelCustomizationUUID)) {
449                                 vnfResourceCustomization = resourceCust;
450                                 break;
451                         }
452                 }
453                 if (vnfResourceCustomization != null) {
454                         VfModuleCustomization vfResourceCustomization = vnfResourceCustomization.getVfModuleCustomizations()
455                                         .stream() // Convert to steam
456                                         .filter(x -> modelInfo.getModelCustomizationId().equalsIgnoreCase(x.getModelCustomizationUUID()))// find
457                                                                                                                                                                                                                                                 // what
458                                                                                                                                                                                                                                                 // we
459                                                                                                                                                                                                                                                 // want
460                                         .findAny() // If 'findAny' then return found
461                                         .orElse(null);
462                         if (vfResourceCustomization != null) {
463                                 vfModule.setModelInfoVfModule(this.mapperLayer.mapCatalogVfModuleToVfModule(vfResourceCustomization));
464                         }
465                 }
466         }
467
468         protected VfModule createVfModule(Map<ResourceKey, String> lookupKeyMap, String vfModuleId, String instanceName, List<Map<String, String>> instanceParams) {
469                 lookupKeyMap.put(ResourceKey.VF_MODULE_ID, vfModuleId);
470                 VfModule vfModule = new VfModule();
471                 vfModule.setVfModuleId(vfModuleId);
472                 vfModule.setVfModuleName(instanceName);
473                 vfModule.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
474                 if(instanceParams != null) {
475                         for(Map<String, String> params : instanceParams) {
476                                 vfModule.getCloudParams().putAll(params);
477                         }
478                 }
479                 return vfModule;
480         }
481
482         protected void populateVolumeGroup(ModelInfo modelInfo, Service service, String bbName,
483                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap, String resourceId,
484                         RelatedInstanceList[] relatedInstanceList, String instanceName, String vnfType, List<Map<String, String>> instanceParams) throws Exception {
485                 VolumeGroup volumeGroup = null;
486                 GenericVnf vnf = null;
487                 String vnfModelCustomizationUUID = null;
488                 String generatedVnfType = vnfType;
489                 if (generatedVnfType == null || generatedVnfType.isEmpty()) {
490                         generatedVnfType = service.getModelName() + "/" + modelInfo.getModelCustomizationName();
491                 }
492                 if (relatedInstanceList != null) {
493                         for (RelatedInstanceList relatedInstList : relatedInstanceList) {
494                                 RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
495                                 if (relatedInstance.getModelInfo().getModelType().equals(ModelType.vnf)) {
496                                         vnfModelCustomizationUUID = relatedInstance.getModelInfo().getModelCustomizationUuid();
497                                         break;
498                                 }
499                         }
500                 }
501                 for (GenericVnf tempVnf : serviceInstance.getVnfs()) {
502                         if (tempVnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
503                                 vnf = tempVnf;
504                                 vnfModelCustomizationUUID = bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId())
505                                                 .getModelCustomizationId();
506                                 ModelInfo vnfModelInfo = new ModelInfo();
507                                 vnfModelInfo.setModelCustomizationUuid(vnfModelCustomizationUUID);
508                                 mapCatalogVnf(tempVnf, vnfModelInfo, service);
509                                 break;
510                         }
511                 }
512                 if (vnf != null && vnfModelCustomizationUUID != null) {
513                         for (VolumeGroup volumeGroupTemp : vnf.getVolumeGroups()) {
514                                 if (lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID) != null && volumeGroupTemp.getVolumeGroupId()
515                                                 .equalsIgnoreCase(lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID))) {
516                                         volumeGroup = volumeGroupTemp;
517                                         break;
518                                 }
519                         }
520                         if (volumeGroup == null && bbName.equalsIgnoreCase(AssignFlows.VOLUME_GROUP.toString())) {
521                                 volumeGroup = createVolumeGroup(lookupKeyMap, resourceId, instanceName, generatedVnfType, instanceParams);
522                                 vnf.getVolumeGroups().add(volumeGroup);
523                         }
524                         if(volumeGroup != null) {
525                                 mapCatalogVolumeGroup(volumeGroup, modelInfo, service, vnfModelCustomizationUUID);
526                         }
527                 } else {
528                         msoLogger.debug("Related VNF instance Id not found:  " + lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID));
529                         throw new Exception("Could not find relevant information for related VNF");
530                 }
531         }
532
533         protected VolumeGroup createVolumeGroup(Map<ResourceKey, String> lookupKeyMap, String volumeGroupId, String instanceName, String vnfType, List<Map<String, String>> instanceParams) {
534                 lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, volumeGroupId);
535                 VolumeGroup volumeGroup = new VolumeGroup();
536                 volumeGroup.setVolumeGroupId(volumeGroupId);
537                 volumeGroup.setVolumeGroupName(instanceName);
538                 volumeGroup.setVnfType(vnfType);
539                 volumeGroup.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
540                 if(instanceParams != null) {
541                         for(Map<String, String> params : instanceParams) {
542                                 volumeGroup.getCloudParams().putAll(params);
543                         }
544                 }
545                 return volumeGroup;
546         }
547
548         protected void mapCatalogVolumeGroup(VolumeGroup volumeGroup, ModelInfo modelInfo, Service service,
549                         String vnfModelCustomizationUUID) {
550                 VfModuleCustomization vfResourceCustomization = getVfResourceCustomization(modelInfo, service,
551                                 vnfModelCustomizationUUID);
552                 if (vfResourceCustomization != null) {
553                         volumeGroup.setModelInfoVfModule(this.mapperLayer.mapCatalogVfModuleToVfModule(vfResourceCustomization));
554                 }
555         }
556
557         protected VfModuleCustomization getVfResourceCustomization(ModelInfo modelInfo, Service service,
558                         String vnfModelCustomizationUUID) {
559                 VnfResourceCustomization vnfResourceCustomization = null;
560                 for (VnfResourceCustomization resourceCust : service.getVnfCustomizations()) {
561                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(vnfModelCustomizationUUID)) {
562                                 vnfResourceCustomization = resourceCust;
563                                 break;
564                         }
565                 }
566                 if (vnfResourceCustomization != null) {
567                         for (VfModuleCustomization vfResourceCust : vnfResourceCustomization.getVfModuleCustomizations()) {
568                                 if (vfResourceCust.getModelCustomizationUUID()
569                                                 .equalsIgnoreCase(modelInfo.getModelCustomizationUuid())) {
570                                         return vfResourceCust;
571                                 }
572                         }
573
574                 }
575                 return null;
576         }
577
578         protected void populateGenericVnf(ModelInfo modelInfo, String instanceName,
579                         org.onap.so.serviceinstancebeans.Platform platform,
580                         org.onap.so.serviceinstancebeans.LineOfBusiness lineOfBusiness, Service service, String bbName,
581                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap,
582                         RelatedInstanceList[] relatedInstanceList, String resourceId, String vnfType, List<Map<String, String>> instanceParams) {
583                 GenericVnf vnf = null;
584                 ModelInfo instanceGroupModelInfo = null;
585                 String instanceGroupId = null;
586                 String generatedVnfType = vnfType;
587                 if (generatedVnfType == null || generatedVnfType.isEmpty()) {
588                         generatedVnfType = service.getModelName() + "/" + modelInfo.getModelCustomizationName();
589                 }
590                 if (relatedInstanceList != null) {
591                         for (RelatedInstanceList relatedInstList : relatedInstanceList) {
592                                 RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
593                                 if (relatedInstance.getModelInfo().getModelType().equals(ModelType.networkInstanceGroup)) {
594                                         instanceGroupModelInfo = relatedInstance.getModelInfo();
595                                         instanceGroupId = relatedInstance.getInstanceId();
596                                 }
597                         }
598                 }
599                 for (GenericVnf vnfTemp : serviceInstance.getVnfs()) {
600                         if (lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID) != null
601                                         && vnfTemp.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
602                                 String vnfModelCustId = bbInputSetupUtils.getAAIGenericVnf(vnfTemp.getVnfId()).getModelCustomizationId();
603                                 modelInfo.setModelCustomizationUuid(vnfModelCustId);
604                                 vnf = vnfTemp;
605                                 break;
606                         }
607                 }
608                 if (vnf == null && bbName.equalsIgnoreCase(AssignFlows.VNF.toString())) {
609                         vnf = createGenericVnf(lookupKeyMap, instanceName, platform, lineOfBusiness,
610                                         resourceId, generatedVnfType, instanceParams);
611                         serviceInstance.getVnfs().add(vnf);
612                         mapVnfcCollectionInstanceGroup(vnf, modelInfo, service);
613                 }
614                 if(vnf != null) {
615                         mapCatalogVnf(vnf, modelInfo, service);
616                         if (instanceGroupId != null && instanceGroupModelInfo != null
617                                         && instanceGroupModelInfo.getModelType().equals(ModelType.networkInstanceGroup)
618                                         && !instanceGroupInList(vnf, instanceGroupId)) {
619                                 mapNetworkCollectionInstanceGroup(vnf, instanceGroupId);
620                         }
621                 }
622         }
623
624         protected boolean instanceGroupInList(GenericVnf vnf, String instanceGroupId) {
625                 for(InstanceGroup instanceGroup : vnf.getInstanceGroups()) {
626                         if(instanceGroup.getId() != null && instanceGroup.getId().equalsIgnoreCase(instanceGroupId)) {
627                                 return true;
628                         }
629                 }
630                 return false;
631         }
632
633         protected void mapVnfcCollectionInstanceGroup(GenericVnf genericVnf, ModelInfo modelInfo, Service service) {
634                 VnfResourceCustomization vnfResourceCustomization = getVnfResourceCustomizationFromService(modelInfo, service);
635                 if(vnfResourceCustomization != null) {
636                 List<VnfcInstanceGroupCustomization> vnfcInstanceGroups = vnfResourceCustomization
637                                 .getVnfcInstanceGroupCustomizations();
638                 for (VnfcInstanceGroupCustomization vnfcInstanceGroupCust : vnfcInstanceGroups) {
639                         InstanceGroup instanceGroup = this.createInstanceGroup();
640                         org.onap.so.db.catalog.beans.InstanceGroup catalogInstanceGroup = bbInputSetupUtils.getCatalogInstanceGroup(vnfcInstanceGroupCust.getModelUUID());
641                         instanceGroup.setModelInfoInstanceGroup(this.mapperLayer
642                                         .mapCatalogInstanceGroupToInstanceGroup(null, catalogInstanceGroup));
643                         instanceGroup.getModelInfoInstanceGroup().setFunction(vnfcInstanceGroupCust.getFunction());
644                         instanceGroup.setDescription(vnfcInstanceGroupCust.getDescription());
645                         genericVnf.getInstanceGroups().add(instanceGroup);
646                 }
647         }
648         }
649
650         protected void mapNetworkCollectionInstanceGroup(GenericVnf genericVnf, String instanceGroupId) {
651                 org.onap.aai.domain.yang.InstanceGroup aaiInstanceGroup = bbInputSetupUtils
652                                 .getAAIInstanceGroup(instanceGroupId);
653                 InstanceGroup instanceGroup = this.mapperLayer.mapAAIInstanceGroupIntoInstanceGroup(aaiInstanceGroup);
654                 instanceGroup.setModelInfoInstanceGroup(this.mapperLayer.mapCatalogInstanceGroupToInstanceGroup(
655                                 null, this.bbInputSetupUtils.getCatalogInstanceGroup(aaiInstanceGroup.getModelVersionId())));
656                 genericVnf.getInstanceGroups().add(instanceGroup);
657         }
658
659         protected GenericVnf createGenericVnf(Map<ResourceKey, String> lookupKeyMap, String instanceName,
660                         org.onap.so.serviceinstancebeans.Platform platform,
661                         org.onap.so.serviceinstancebeans.LineOfBusiness lineOfBusiness, String vnfId, String vnfType, List<Map<String, String>> instanceParams) {
662                 lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, vnfId);
663                 GenericVnf genericVnf = new GenericVnf();
664                 genericVnf.setVnfId(vnfId);
665                 genericVnf.setVnfName(instanceName);
666                 genericVnf.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
667                 genericVnf.setVnfType(vnfType);
668                 if (platform != null) {
669                         genericVnf.setPlatform(this.mapperLayer.mapRequestPlatform(platform));
670                 }
671                 if (lineOfBusiness != null) {
672                         genericVnf.setLineOfBusiness(this.mapperLayer.mapRequestLineOfBusiness(lineOfBusiness));
673                 }
674                 if(instanceParams != null) {
675                         for(Map<String, String> params : instanceParams) {
676                                 genericVnf.getCloudParams().putAll(params);
677                         }
678                 }
679                 return genericVnf;
680         }
681
682         protected void mapCatalogVnf(GenericVnf genericVnf, ModelInfo modelInfo, Service service) {
683                 VnfResourceCustomization vnfResourceCustomization = getVnfResourceCustomizationFromService(modelInfo, service);
684                 if (vnfResourceCustomization != null) {
685                         genericVnf.setModelInfoGenericVnf(this.mapperLayer.mapCatalogVnfToVnf(vnfResourceCustomization));
686                 }
687         }
688
689         protected VnfResourceCustomization getVnfResourceCustomizationFromService(ModelInfo modelInfo, Service service) {
690                 VnfResourceCustomization vnfResourceCustomization = null;
691                 for (VnfResourceCustomization resourceCust : service.getVnfCustomizations()) {
692                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(modelInfo.getModelCustomizationUuid())) {
693                                 vnfResourceCustomization = resourceCust;
694                                 break;
695                         }
696                 }
697                 return vnfResourceCustomization;
698         }
699
700         protected void populateL3Network(String instanceName, ModelInfo modelInfo, Service service, String bbName,
701                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap, String resourceId, List<Map<String, String>> instanceParams) {
702                 L3Network network = null;
703                 for (L3Network networkTemp : serviceInstance.getNetworks()) {
704                         if (lookupKeyMap.get(ResourceKey.NETWORK_ID) != null
705                                         && networkTemp.getNetworkId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.NETWORK_ID))) {
706                                 network = networkTemp;
707                                 break;
708                         }
709                 }
710                 if (network == null
711                                 && (bbName.equalsIgnoreCase(AssignFlows.NETWORK_A_LA_CARTE.toString()) 
712                                                 || bbName.equalsIgnoreCase(AssignFlows.NETWORK_MACRO.toString()))) {
713                         network = createNetwork(lookupKeyMap, instanceName, resourceId, instanceParams);
714                         serviceInstance.getNetworks().add(network);
715                 }
716                 if(network != null) {
717                         mapCatalogNetwork(network, modelInfo, service);
718                 }
719         }
720
721         protected L3Network createNetwork(Map<ResourceKey, String> lookupKeyMap, String instanceName,
722                         String networkId, List<Map<String, String>> instanceParams) {
723                 lookupKeyMap.put(ResourceKey.NETWORK_ID, networkId);
724                 L3Network network = new L3Network();
725                 network.setNetworkId(networkId);
726                 network.setNetworkName(instanceName);
727                 network.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
728                 if(instanceParams != null) {
729                         for(Map<String, String> params : instanceParams) {
730                                 network.getCloudParams().putAll(params);
731                         }
732                 }
733                 return network;
734         }
735
736         protected void mapCatalogNetwork(L3Network network, ModelInfo modelInfo, Service service) {
737                 NetworkResourceCustomization networkResourceCustomization = null;
738                 for (NetworkResourceCustomization resourceCust : service.getNetworkCustomizations()) {
739                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(modelInfo.getModelCustomizationUuid())) {
740                                 networkResourceCustomization = resourceCust;
741                                 break;
742                         }
743                 }
744                 if (networkResourceCustomization != null) {
745                         network.setModelInfoNetwork(this.mapperLayer.mapCatalogNetworkToNetwork(networkResourceCustomization));
746                 }
747         }
748
749         protected GeneralBuildingBlock getGBBALaCarteService(ExecuteBuildingBlock executeBB, RequestDetails requestDetails,
750                         Map<ResourceKey, String> lookupKeyMap, String requestAction, String resourceId) throws Exception {
751                 Customer customer = getCustomerAndServiceSubscription(requestDetails, resourceId);
752                 if (customer != null) {
753                         Project project = null;
754                         OwningEntity owningEntity = null;
755
756                         if (requestDetails.getProject() != null)
757                                 project = mapperLayer.mapRequestProject(requestDetails.getProject());
758                         if (requestDetails.getOwningEntity() != null)
759                                 owningEntity = mapperLayer.mapRequestOwningEntity(requestDetails.getOwningEntity());
760
761
762                         Service service = bbInputSetupUtils
763                                         .getCatalogServiceByModelUUID(requestDetails.getModelInfo().getModelVersionId());
764                         if (service == null) {
765                                 service = bbInputSetupUtils.getCatalogServiceByModelVersionAndModelInvariantUUID(
766                                                 requestDetails.getModelInfo().getModelVersion(),
767                                                 requestDetails.getModelInfo().getModelInvariantId());
768                         if(service == null) {
769                                         throw new Exception("Could not find service for model version Id: "
770                                                         + requestDetails.getModelInfo().getModelVersionId() + " and for model invariant Id: "
771                                                         + requestDetails.getModelInfo().getModelInvariantId());
772                                 }
773                         }
774                         ServiceInstance serviceInstance = this.getALaCarteServiceInstance(service, requestDetails, customer,
775                                         project, owningEntity, lookupKeyMap, resourceId, Boolean.TRUE.equals(executeBB.isaLaCarte()),
776                                         executeBB.getBuildingBlock().getBpmnFlowName());
777                         return this.populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, customer);
778                 } else {
779                         throw new Exception("Could not find customer");
780                 }
781         }
782
783         protected Customer getCustomerAndServiceSubscription(RequestDetails requestDetails, String resourceId) {
784                 Customer customer;
785                 if (requestDetails.getSubscriberInfo() != null) {
786                         customer = this.getCustomerFromRequest(requestDetails);
787                 } else {
788                         customer = this.getCustomerFromURI(resourceId);
789                 }
790                 if (customer != null) {
791                         ServiceSubscription serviceSubscription = null;
792                         serviceSubscription = getServiceSubscription(requestDetails, customer);
793                         if (serviceSubscription == null) {
794                                 serviceSubscription = getServiceSubscriptionFromURI(resourceId, customer);
795                         }
796                         customer.setServiceSubscription(serviceSubscription);
797                         return customer;
798                 } else {
799                         return null;
800                 }
801         }
802
803         protected ServiceSubscription getServiceSubscriptionFromURI(String resourceId, Customer customer) {
804                 Map<String, String> uriKeys = bbInputSetupUtils.getURIKeysFromServiceInstance(resourceId);
805                 String subscriptionServiceType = uriKeys.get("service-type");
806                 org.onap.aai.domain.yang.ServiceSubscription serviceSubscriptionAAI = bbInputSetupUtils
807                                 .getAAIServiceSubscription(customer.getGlobalCustomerId(), subscriptionServiceType);
808                 if (serviceSubscriptionAAI != null) {
809                         return mapperLayer.mapAAIServiceSubscription(serviceSubscriptionAAI);
810                 } else {
811                         return null;
812                 }
813         }
814
815         protected Customer getCustomerFromURI(String resourceId) {
816                 Map<String, String> uriKeys = bbInputSetupUtils.getURIKeysFromServiceInstance(resourceId);
817                 String globalCustomerId = uriKeys.get("global-customer-id");
818                 org.onap.aai.domain.yang.Customer customerAAI = this.bbInputSetupUtils.getAAICustomer(globalCustomerId);
819                 if (customerAAI != null) {
820                         return mapperLayer.mapAAICustomer(customerAAI);
821                 } else {
822                         return null;
823                 }
824         }
825
826         protected GeneralBuildingBlock populateGBBWithSIAndAdditionalInfo(RequestDetails requestDetails,
827                         ServiceInstance serviceInstance, ExecuteBuildingBlock executeBB, String requestAction, Customer customer) {
828                 GeneralBuildingBlock outputBB = new GeneralBuildingBlock();
829                 OrchestrationContext orchContext = mapperLayer.mapOrchestrationContext(requestDetails);
830                 RequestContext requestContext = mapperLayer.mapRequestContext(requestDetails);
831                 requestContext.setAction(requestAction);
832                 requestContext.setMsoRequestId(executeBB.getRequestId());
833                 org.onap.aai.domain.yang.CloudRegion aaiCloudRegion = bbInputSetupUtils
834                                 .getCloudRegion(requestDetails.getCloudConfiguration());
835                 CloudRegion cloudRegion = mapperLayer.mapCloudRegion(requestDetails.getCloudConfiguration(), aaiCloudRegion);
836                 outputBB.setOrchContext(orchContext);
837                 outputBB.setRequestContext(requestContext);
838                 outputBB.setCloudRegion(cloudRegion);
839                 if(customer == null){
840                         Map<String, String> uriKeys = bbInputSetupUtils.getURIKeysFromServiceInstance(serviceInstance.getServiceInstanceId());
841                         String globalCustomerId = uriKeys.get("global-customer-id");
842                         String subscriptionServiceType = uriKeys.get("service-type");
843                         customer = mapCustomer(globalCustomerId, subscriptionServiceType);
844                 }
845                 outputBB.setServiceInstance(serviceInstance);
846                 if (customer.getServiceSubscription() != null) {
847                         customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
848                 }
849                 outputBB.setCustomer(customer);
850                 return outputBB;
851         }
852
853         protected ServiceSubscription getServiceSubscription(RequestDetails requestDetails, Customer customer) {
854                 org.onap.aai.domain.yang.ServiceSubscription aaiServiceSubscription = bbInputSetupUtils
855                                 .getAAIServiceSubscription(customer.getGlobalCustomerId(),
856                                                 requestDetails.getRequestParameters().getSubscriptionServiceType());
857                 if (aaiServiceSubscription != null) {
858                 return mapperLayer.mapAAIServiceSubscription(aaiServiceSubscription);
859                 } else {
860                         return null;
861                 }
862         }
863
864         protected Customer getCustomerFromRequest(RequestDetails requestDetails) {
865                 org.onap.aai.domain.yang.Customer aaiCustomer = bbInputSetupUtils
866                                 .getAAICustomer(requestDetails.getSubscriberInfo().getGlobalSubscriberId());
867                 if (aaiCustomer != null) {
868                 return mapperLayer.mapAAICustomer(aaiCustomer);
869                 } else {
870                         return null;
871                 }
872         }
873
874         protected ServiceInstance getALaCarteServiceInstance(Service service, RequestDetails requestDetails,
875                         Customer customer, Project project, OwningEntity owningEntity, Map<ResourceKey, String> lookupKeyMap,
876                         String serviceInstanceId, boolean aLaCarte, String bbName) throws Exception {
877                 ServiceInstance serviceInstance = this.getServiceInstanceHelper(requestDetails, customer, project, owningEntity,
878                                 lookupKeyMap, serviceInstanceId, aLaCarte, service, bbName);
879                 org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI = null;
880                 if(customer != null && customer.getServiceSubscription() != null) {
881                         serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceByIdAndCustomer(customer.getGlobalCustomerId(), 
882                                         customer.getServiceSubscription().getServiceType(), serviceInstanceId);
883                 } else {
884                         serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
885                 }
886                 if (serviceInstanceAAI != null
887                                 && !serviceInstanceAAI.getModelVersionId().equalsIgnoreCase(service.getModelUUID())) {
888                         Service tempService = this.bbInputSetupUtils
889                                         .getCatalogServiceByModelUUID(serviceInstanceAAI.getModelVersionId());
890                         if (tempService != null) {
891                                 serviceInstance
892                                                 .setModelInfoServiceInstance(mapperLayer.mapCatalogServiceIntoServiceInstance(tempService));
893                                 return serviceInstance;
894                         } else {
895                                 throw new Exception(
896                                                 "Could not find model of existing SI. Service Instance in AAI already exists with different model version id: "
897                                                                 + serviceInstanceAAI.getModelVersionId());
898                         }
899                 }
900                 serviceInstance.setModelInfoServiceInstance(mapperLayer.mapCatalogServiceIntoServiceInstance(service));
901                 return serviceInstance;
902         }
903
904         protected GeneralBuildingBlock getGBBMacro(ExecuteBuildingBlock executeBB, RequestDetails requestDetails,
905                         Map<ResourceKey, String> lookupKeyMap, String requestAction, String resourceId, String vnfType)
906                         throws Exception {
907                 String bbName = executeBB.getBuildingBlock().getBpmnFlowName();
908                 String key = executeBB.getBuildingBlock().getKey();
909                 
910                 if (requestAction.equalsIgnoreCase("deleteInstance")
911                                 || requestAction.equalsIgnoreCase("unassignInstance")
912                                 || requestAction.equalsIgnoreCase("activateInstance")
913                                 || requestAction.equalsIgnoreCase("activateFabricConfiguration")
914                                 || requestAction.equalsIgnoreCase("recreateInstance")
915                                 || requestAction.equalsIgnoreCase("replaceInstance")) {
916                         return getGBBMacroExistingService(executeBB, lookupKeyMap, bbName, requestAction,
917                                         requestDetails.getCloudConfiguration());
918                 }
919
920                 String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
921                 GeneralBuildingBlock gBB = this.getGBBALaCarteService(executeBB, requestDetails, lookupKeyMap, requestAction,
922                                 serviceInstanceId);
923                 RequestParameters requestParams = requestDetails.getRequestParameters();
924                 Service service = null;
925                 if (gBB != null && gBB.getServiceInstance() != null
926                                 && gBB.getServiceInstance().getModelInfoServiceInstance() != null
927                                 && gBB.getServiceInstance().getModelInfoServiceInstance().getModelUuid() != null) {
928                         service = bbInputSetupUtils.getCatalogServiceByModelUUID(
929                                         gBB.getServiceInstance().getModelInfoServiceInstance().getModelUuid());
930                 } else {
931                         throw new Exception("Could not get service instance for macro request");
932                 }
933                 if (requestParams != null && requestParams.getUserParams() != null) {
934                         for(Map<String, Object> userParams : requestParams.getUserParams()) {
935                                 if(userParams.containsKey("service")) {
936                                         String input = mapper.writeValueAsString(userParams.get("service"));
937                                         return getGBBMacroUserParams(executeBB, requestDetails, lookupKeyMap, vnfType, bbName, key, gBB,
938                                                         requestParams, service, input);
939                                 }
940                         }
941                 }
942                 if (requestAction.equalsIgnoreCase("deactivateInstance")) {
943                         return gBB;
944                 } else if (requestAction.equalsIgnoreCase("createInstance")) {
945                         return getGBBMacroNoUserParamsCreate(executeBB, lookupKeyMap, bbName, key, gBB, service);
946                 } else {
947                         throw new IllegalArgumentException(
948                                         "No user params on requestAction: assignInstance. Please specify user params.");
949                 }
950         }
951
952         protected GeneralBuildingBlock getGBBMacroNoUserParamsCreate(ExecuteBuildingBlock executeBB,
953                         Map<ResourceKey, String> lookupKeyMap, String bbName, String key, GeneralBuildingBlock gBB, Service service)
954                         throws Exception {
955                 ServiceInstance serviceInstance = gBB.getServiceInstance();
956                 if (bbName.contains(NETWORK) && !bbName.contains(NETWORK_COLLECTION)) {
957                         String networkId = lookupKeyMap.get(ResourceKey.NETWORK_ID);
958                         ModelInfo networkModelInfo = new ModelInfo();
959                         if((!Boolean.TRUE.equals(executeBB.getBuildingBlock().getIsVirtualLink()))) {
960                                 NetworkResourceCustomization networkCust = getNetworkCustomizationByKey(key, service);
961                                 if (networkCust != null) {
962                                         networkModelInfo.setModelCustomizationUuid(networkCust.getModelCustomizationUUID());
963                                         this.populateL3Network(null, networkModelInfo, service, bbName, serviceInstance, lookupKeyMap,
964                                                         networkId, null);
965                                 } else {
966                                         msoLogger.debug("Could not find a network customization with key: " + key);
967                                 }
968                         } else {
969                                 msoLogger.debug("Orchestrating on Collection Network Resource Customization");
970                                 CollectionNetworkResourceCustomization collectionNetworkResourceCust = bbInputSetupUtils.getCatalogCollectionNetworkResourceCustByID(key);
971                                 L3Network l3Network = getVirtualLinkL3Network(lookupKeyMap, bbName, key, networkId, collectionNetworkResourceCust, serviceInstance);
972                                 NetworkResourceCustomization networkResourceCustomization = 
973                                                 mapperLayer.mapCollectionNetworkResourceCustToNetworkResourceCust(collectionNetworkResourceCust);
974                                 if(l3Network != null) {
975                                         l3Network.setModelInfoNetwork(mapperLayer.mapCatalogNetworkToNetwork(networkResourceCustomization));
976                                 }
977                         }
978                 } else if(bbName.contains("Configuration")) {
979                         String configurationId = lookupKeyMap.get(ResourceKey.CONFIGURATION_ID);
980                         ModelInfo configurationModelInfo = new ModelInfo();
981                         configurationModelInfo.setModelCustomizationUuid(key);
982                         this.populateConfiguration(configurationModelInfo, service, bbName, serviceInstance, lookupKeyMap, configurationId, null, executeBB.getConfigurationResourceKeys());
983                 }
984                 if (executeBB.getWorkflowResourceIds() != null) {
985                         this.populateNetworkCollectionAndInstanceGroupAssign(service, bbName, serviceInstance,
986                                         executeBB.getWorkflowResourceIds().getNetworkCollectionId(), key);
987                 }
988                 return gBB;
989         }
990
991         protected L3Network getVirtualLinkL3Network(Map<ResourceKey, String> lookupKeyMap, String bbName, String key,
992                         String networkId, CollectionNetworkResourceCustomization collectionNetworkResourceCust, ServiceInstance serviceInstance) {
993                 if(collectionNetworkResourceCust != null) {
994                         if((bbName.equalsIgnoreCase(AssignFlows.NETWORK_A_LA_CARTE.toString())
995                                 || bbName.equalsIgnoreCase(AssignFlows.NETWORK_MACRO.toString()))) {
996                                 L3Network network = createNetwork(lookupKeyMap, null, networkId, null);                         
997                                 serviceInstance.getNetworks().add(network);
998                                 return network;
999                         } else {
1000                                 for (L3Network network : serviceInstance.getNetworks()) {
1001                                         if (network.getNetworkId().equalsIgnoreCase(networkId)) {
1002                                                 return network;
1003                                         }
1004                                 }
1005                         }
1006                 }
1007                 return null;
1008         }
1009
1010         protected NetworkResourceCustomization getNetworkCustomizationByKey(String key, Service service) {
1011                 for (NetworkResourceCustomization networkCust : service.getNetworkCustomizations()) {
1012                         if (networkCust.getModelCustomizationUUID().equalsIgnoreCase(key)) {
1013                                 return networkCust;
1014                         }
1015                 }
1016                 return null;
1017         }
1018
1019         protected GeneralBuildingBlock getGBBMacroExistingService(ExecuteBuildingBlock executeBB,
1020                         Map<ResourceKey, String> lookupKeyMap, String bbName, String requestAction, CloudConfiguration cloudConfiguration) throws Exception {
1021                 org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
1022                 String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
1023                 RequestDetails requestDetails = executeBB.getRequestDetails();
1024                 GeneralBuildingBlock gBB = null;
1025                 if (serviceInstanceId != null) {
1026                         aaiServiceInstance = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
1027                 }
1028                 Service service = null;
1029                 if (aaiServiceInstance != null) {
1030                         service = bbInputSetupUtils.getCatalogServiceByModelUUID(aaiServiceInstance.getModelVersionId());
1031                 }
1032                 if (aaiServiceInstance != null && service != null) {
1033                         ServiceInstance serviceInstance = this.getExistingServiceInstance(aaiServiceInstance);
1034                         serviceInstance.setModelInfoServiceInstance(this.mapperLayer.mapCatalogServiceIntoServiceInstance(service));
1035                         gBB = populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, null);
1036                 } else {
1037                         msoLogger.debug("Related Service Instance from AAI: " + aaiServiceInstance);
1038                         msoLogger.debug("Related Service Instance Model Info from AAI: " + service);
1039                         throw new Exception("Could not find relevant information for related Service Instance");
1040                 }
1041                 ServiceInstance serviceInstance = gBB.getServiceInstance();
1042                 CloudRegion cloudRegion = null;
1043                 if(cloudConfiguration == null) {
1044                         Optional<CloudRegion> cloudRegionOp = cloudInfoFromAAI.getCloudInfoFromAAI(serviceInstance);
1045                         if(cloudRegionOp.isPresent()) {
1046                                 cloudRegion = cloudRegionOp.get();
1047                         }
1048                 }
1049                 if (cloudConfiguration != null) {
1050                         org.onap.aai.domain.yang.CloudRegion aaiCloudRegion = bbInputSetupUtils.getCloudRegion(cloudConfiguration);
1051                         cloudRegion = mapperLayer.mapCloudRegion(cloudConfiguration, aaiCloudRegion);
1052                 }
1053                 gBB.setCloudRegion(cloudRegion);
1054                 if (bbName.contains(VNF)) {
1055                         for (GenericVnf genericVnf : serviceInstance.getVnfs()) {
1056                                 if (lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID) != null
1057                                                 && genericVnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
1058                                         org.onap.aai.domain.yang.GenericVnf vnf = bbInputSetupUtils.getAAIGenericVnf(genericVnf.getVnfId());
1059                                         ModelInfo modelInfo = new ModelInfo();
1060                                         if (vnf != null) {
1061                                                 modelInfo.setModelCustomizationUuid(vnf.getModelCustomizationId());
1062                                         }
1063                                         this.mapCatalogVnf(genericVnf, modelInfo, service);
1064                                 }
1065                         }
1066                 } else if (bbName.contains(VF_MODULE)) {
1067                         for (GenericVnf vnf : serviceInstance.getVnfs()) {
1068                                 for (VfModule vfModule : vnf.getVfModules()) {
1069                                         if (lookupKeyMap.get(ResourceKey.VF_MODULE_ID) != null
1070                                                         && vfModule.getVfModuleId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.VF_MODULE_ID))) {
1071                                                 String vnfModelCustomizationUUID = this.bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId())
1072                                                                 .getModelCustomizationId();
1073                                                 ModelInfo vnfModelInfo = new ModelInfo();
1074                                                 vnfModelInfo.setModelCustomizationUuid(vnfModelCustomizationUUID);
1075                                                 this.mapCatalogVnf(vnf, vnfModelInfo, service);
1076                                                 lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, vnf.getVnfId());
1077                                                 String vfModuleCustomizationUUID = this.bbInputSetupUtils
1078                                                                 .getAAIVfModule(vnf.getVnfId(), vfModule.getVfModuleId()).getModelCustomizationId();
1079                                                 ModelInfo vfModuleModelInfo = new ModelInfo();
1080                                                 vfModuleModelInfo.setModelCustomizationId(vfModuleCustomizationUUID);
1081                                                 this.mapCatalogVfModule(vfModule, vfModuleModelInfo, service, vnfModelCustomizationUUID);
1082                                                 if(cloudRegion != null) {
1083                                                         Optional<String> volumeGroupIdOp = getVolumeGroupIdRelatedToVfModule(vnf, vfModuleModelInfo, cloudRegion.getCloudOwner(), 
1084                                                                         cloudRegion.getLcpCloudRegionId(), lookupKeyMap);
1085                                                         if(volumeGroupIdOp.isPresent()) {
1086                                                                 lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, volumeGroupIdOp.get());
1087                                                         }
1088                                                 }
1089                                                 break;
1090                                         }
1091                                 }
1092                         }
1093                 } else if (bbName.contains(VOLUME_GROUP)) {
1094                         for (GenericVnf vnf : serviceInstance.getVnfs()) {
1095                                 for (VolumeGroup volumeGroup : vnf.getVolumeGroups()) {
1096                                         if (lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID) != null && volumeGroup.getVolumeGroupId()
1097                                                         .equalsIgnoreCase(lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID))) {
1098                                                 String vnfModelCustomizationUUID = this.bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId())
1099                                                                 .getModelCustomizationId();
1100                                                 ModelInfo vnfModelInfo = new ModelInfo();
1101                                                 vnfModelInfo.setModelCustomizationUuid(vnfModelCustomizationUUID);
1102                                                 this.mapCatalogVnf(vnf, vnfModelInfo, service);
1103                                                 lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, vnf.getVnfId());
1104                                                 if (cloudRegion != null) {
1105                                                         String volumeGroupCustomizationUUID = this.bbInputSetupUtils.getAAIVolumeGroup(cloudRegion.getCloudOwner(),
1106                                                                         cloudRegion.getLcpCloudRegionId(), volumeGroup.getVolumeGroupId())
1107                                                                         .getModelCustomizationId();
1108                                                         ModelInfo volumeGroupModelInfo = new ModelInfo();
1109                                                         volumeGroupModelInfo.setModelCustomizationId(volumeGroupCustomizationUUID);
1110                                                         this.mapCatalogVolumeGroup(volumeGroup, volumeGroupModelInfo, service,
1111                                                                         vnfModelCustomizationUUID);
1112                                                 }
1113                                                 break;
1114                                         }
1115                                 }
1116                         }
1117                 } else if (bbName.contains(NETWORK)) {
1118                         for (L3Network network : serviceInstance.getNetworks()) {
1119                                 if (lookupKeyMap.get(ResourceKey.NETWORK_ID) != null
1120                                                 && network.getNetworkId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.NETWORK_ID))) {
1121                                         String networkCustomizationUUID = this.bbInputSetupUtils.getAAIL3Network(network.getNetworkId())
1122                                                         .getModelCustomizationId();
1123                                         ModelInfo modelInfo = new ModelInfo();
1124                                         modelInfo.setModelCustomizationUuid(networkCustomizationUUID);
1125                                         this.mapCatalogNetwork(network, modelInfo, service);
1126                                         break;
1127                                 }
1128                         }
1129                 } else if (bbName.contains("Fabric")) {
1130                         for(Configuration configuration : serviceInstance.getConfigurations()) {
1131                                 if(lookupKeyMap.get(ResourceKey.CONFIGURATION_ID) != null
1132                                                 && configuration.getConfigurationId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.CONFIGURATION_ID))) {
1133                                         String configurationCustUUID = this.bbInputSetupUtils.getAAIConfiguration(configuration.getConfigurationId())
1134                                                         .getModelCustomizationId();
1135                                         ModelInfo modelInfo = new ModelInfo();
1136                                         modelInfo.setModelCustomizationUuid(configurationCustUUID);
1137                                         this.mapCatalogConfiguration(configuration, modelInfo, service, executeBB.getConfigurationResourceKeys());
1138                                         break;
1139                                 }
1140                         }
1141                 }
1142                 if (executeBB.getWorkflowResourceIds() != null) {
1143                         this.populateNetworkCollectionAndInstanceGroupAssign(service, bbName, serviceInstance,
1144                                         executeBB.getWorkflowResourceIds().getNetworkCollectionId(), executeBB.getBuildingBlock().getKey());
1145                 }
1146                 return gBB;
1147         }
1148
1149         protected GeneralBuildingBlock getGBBMacroUserParams(ExecuteBuildingBlock executeBB, RequestDetails requestDetails,
1150                         Map<ResourceKey, String> lookupKeyMap, String vnfType, String bbName, String key, GeneralBuildingBlock gBB,
1151                         RequestParameters requestParams, Service service, String input) throws Exception {
1152                 ServiceInstance serviceInstance = gBB.getServiceInstance();
1153                 org.onap.so.serviceinstancebeans.Service serviceMacro = mapper.readValue(input,
1154                                 org.onap.so.serviceinstancebeans.Service.class);
1155
1156                 Resources resources = serviceMacro.getResources();
1157                 Vnfs vnfs = null;
1158                 VfModules vfModules = null;
1159                 Networks networks = null;
1160                 CloudConfiguration cloudConfiguration = requestDetails.getCloudConfiguration();
1161                 CloudRegion cloudRegion = getCloudRegionFromMacroRequest(cloudConfiguration, resources);
1162                 gBB.setCloudRegion(cloudRegion);
1163                 if (bbName.contains(VNF)) {
1164                         vnfs = findVnfsByKey(key, resources, vnfs);
1165                         String vnfId = lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID);
1166                         // This stores the vnf id in request db to be retrieved later when
1167                         // working on a vf module or volume group
1168                         InfraActiveRequests request = this.bbInputSetupUtils.getInfraActiveRequest(executeBB.getRequestId());
1169                         if (request != null) {
1170                                 this.bbInputSetupUtils.updateInfraActiveRequestVnfId(request, vnfId);
1171                         }
1172                         this.populateGenericVnf(vnfs.getModelInfo(), vnfs.getInstanceName(), vnfs.getPlatform(),
1173                                         vnfs.getLineOfBusiness(), service, bbName, serviceInstance, lookupKeyMap, null, vnfId, vnfType, vnfs.getInstanceParams());
1174                 } else if (bbName.contains(VF_MODULE) || bbName.contains(VOLUME_GROUP)) {
1175                         Pair<Vnfs, VfModules> vnfsAndVfModules = getVfModulesAndItsVnfsByKey(key, resources);
1176                         vfModules = vnfsAndVfModules.getValue1();
1177                         vnfs = vnfsAndVfModules.getValue0();
1178                         lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, getVnfId(executeBB, lookupKeyMap));
1179                         if (vnfs == null) {
1180                                 throw new Exception("Could not find Vnf to orchestrate VfModule");
1181                         }
1182                         ModelInfo modelInfo = vfModules.getModelInfo();
1183                         if (bbName.contains(VOLUME_GROUP)) {
1184                                 String volumeGroupId = lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID);
1185                                 this.populateVolumeGroup(modelInfo, service, bbName, serviceInstance, lookupKeyMap, volumeGroupId, null,
1186                                                 vfModules.getVolumeGroupInstanceName(), vnfType, vfModules.getInstanceParams());
1187                         } else {
1188                                 String vfModuleId = lookupKeyMap.get(ResourceKey.VF_MODULE_ID);
1189                                 CloudConfiguration cloudConfig = new CloudConfiguration();
1190                                 cloudConfig.setLcpCloudRegionId(cloudRegion.getLcpCloudRegionId());
1191                                 cloudConfig.setCloudOwner(cloudRegion.getCloudOwner());
1192                                 this.populateVfModule(modelInfo, service, bbName, serviceInstance, lookupKeyMap, vfModuleId, null,
1193                                                 vfModules.getInstanceName(), vfModules.getInstanceParams(), cloudConfig);
1194                         }
1195                 } else if (bbName.contains(NETWORK)) {
1196                         networks = findNetworksByKey(key, resources);
1197                         String networkId = lookupKeyMap.get(ResourceKey.NETWORK_ID);
1198                         this.populateL3Network(networks.getInstanceName(), networks.getModelInfo(), service, bbName,
1199                                         serviceInstance, lookupKeyMap, networkId, networks.getInstanceParams());
1200                 } else if (bbName.contains("Configuration")) {
1201                         String configurationId = lookupKeyMap.get(ResourceKey.CONFIGURATION_ID);
1202                         ModelInfo configurationModelInfo = new ModelInfo();
1203                         configurationModelInfo.setModelCustomizationUuid(key);
1204                         ConfigurationResourceCustomization configurationCust = findConfigurationResourceCustomization(configurationModelInfo, service);
1205                         if(configurationCust != null) {
1206                                 this.populateConfiguration(configurationModelInfo, service, bbName, serviceInstance, lookupKeyMap, configurationId, null, executeBB.getConfigurationResourceKeys());
1207                         } else {
1208                                 msoLogger.debug("Could not find a configuration customization with key: " + key);
1209                         }
1210                 }
1211                 return gBB;
1212         }
1213         
1214         protected Networks findNetworksByKey(String key, Resources resources) {
1215                 for (Networks networks : resources.getNetworks()) {
1216                         if (networks.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
1217                                 return networks;
1218                         }
1219                 }
1220                 return null;
1221         }
1222
1223         protected Pair<Vnfs, VfModules> getVfModulesAndItsVnfsByKey(String key, Resources resources) {
1224                 for (Vnfs vnfs : resources.getVnfs()) {
1225                         for (VfModules vfModules : vnfs.getVfModules()) {
1226                                 if (vfModules.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
1227                                         return new Pair<Vnfs, VfModules>(vnfs, vfModules);
1228                                 }
1229                         }
1230                 }
1231                 return null;
1232         }
1233
1234         protected Vnfs findVnfsByKey(String key, Resources resources, Vnfs vnfs) {
1235                 for (Vnfs tempVnfs : resources.getVnfs()) {
1236                         if (tempVnfs.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
1237                                 vnfs = tempVnfs;
1238                                 break;
1239                         }
1240                 }
1241                 return vnfs;
1242         }
1243
1244         protected CloudRegion getCloudRegionFromMacroRequest(CloudConfiguration cloudConfiguration, Resources resources) {
1245                 if(cloudConfiguration == null) {
1246                         for(Vnfs vnfs : resources.getVnfs()) {
1247                                 if(cloudConfiguration == null) {
1248                                         cloudConfiguration = vnfs.getCloudConfiguration();
1249                                 } else {
1250                                         break;
1251                                 }
1252                                 for(VfModules vfModules : vnfs.getVfModules()) {
1253                                         if(cloudConfiguration == null) {
1254                                                 cloudConfiguration = vfModules.getCloudConfiguration();
1255                                         } else {
1256                                                 break;
1257                                         }
1258                                 }
1259                         }
1260                         for(Networks networks : resources.getNetworks()) {
1261                                 if(cloudConfiguration == null) {
1262                                 cloudConfiguration = networks.getCloudConfiguration();
1263                                 } else {
1264                                         break;
1265                         }
1266                 }
1267                 }
1268                 if(cloudConfiguration != null) {
1269                         org.onap.aai.domain.yang.CloudRegion aaiCloudRegion = bbInputSetupUtils.getCloudRegion(cloudConfiguration);
1270                         return mapperLayer.mapCloudRegion(cloudConfiguration, aaiCloudRegion);
1271                 } else {
1272                         msoLogger.debug("Could not find any cloud configuration for this request.");
1273                         return null;
1274                 }
1275         }
1276
1277         protected String getVnfId(ExecuteBuildingBlock executeBB, Map<ResourceKey, String> lookupKeyMap) {
1278                 String vnfId = lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID);
1279                 if (vnfId == null) {
1280                         InfraActiveRequests request = this.bbInputSetupUtils.getInfraActiveRequest(executeBB.getRequestId());
1281                         vnfId = request.getVnfId();
1282                 }
1283
1284                 return vnfId;
1285         }
1286
1287         protected String generateRandomUUID() {
1288                 return UUID.randomUUID().toString();
1289         }
1290
1291         protected ServiceInstance getServiceInstanceHelper(RequestDetails requestDetails, Customer customer,
1292                         Project project, OwningEntity owningEntity, Map<ResourceKey, String> lookupKeyMap, String serviceInstanceId,
1293                         boolean aLaCarte, Service service, String bbName) throws Exception {
1294                 if (requestDetails.getRequestInfo().getInstanceName() == null && aLaCarte
1295                                 && bbName.equalsIgnoreCase(AssignFlows.SERVICE_INSTANCE.toString())) {
1296                         throw new Exception("Request invalid missing: RequestInfo:InstanceName");
1297                 } else {
1298                         org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI = null;
1299                         if (aLaCarte && bbName.equalsIgnoreCase(AssignFlows.SERVICE_INSTANCE.toString())) {
1300                                 serviceInstanceAAI = bbInputSetupUtils
1301                                         .getAAIServiceInstanceByName(requestDetails.getRequestInfo().getInstanceName(), customer);
1302                         }
1303                         if (serviceInstanceId != null && serviceInstanceAAI == null) {
1304                                 serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
1305                         }
1306                         if (serviceInstanceAAI != null) {
1307                                 lookupKeyMap.put(ResourceKey.SERVICE_INSTANCE_ID, serviceInstanceId);
1308                                 return this.getExistingServiceInstance(serviceInstanceAAI);
1309                         } else {
1310                                 return createServiceInstance(requestDetails, project, owningEntity, lookupKeyMap,
1311                                                 serviceInstanceId);
1312                         }
1313                 }
1314         }
1315
1316         protected ServiceInstance createServiceInstance(RequestDetails requestDetails, Project project,
1317                                                                                                         OwningEntity owningEntity, Map<ResourceKey, String> lookupKeyMap, String serviceInstanceId) {
1318                 ServiceInstance serviceInstance = new ServiceInstance();
1319                 lookupKeyMap.put(ResourceKey.SERVICE_INSTANCE_ID, serviceInstanceId);
1320                 serviceInstance.setServiceInstanceId(serviceInstanceId);
1321                 if(requestDetails.getRequestInfo() != null) {
1322                 serviceInstance.setServiceInstanceName(requestDetails.getRequestInfo().getInstanceName());
1323                 }
1324                 serviceInstance.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
1325                 serviceInstance.setProject(project);
1326                 serviceInstance.setOwningEntity(owningEntity);
1327                 return serviceInstance;
1328         }
1329
1330         public ServiceInstance getExistingServiceInstance(org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI)
1331                         throws Exception {
1332                 ServiceInstance serviceInstance = mapperLayer.mapAAIServiceInstanceIntoServiceInstance(serviceInstanceAAI);
1333                 if (serviceInstanceAAI.getRelationshipList() != null
1334                                 && serviceInstanceAAI.getRelationshipList().getRelationship() != null
1335                                 && !serviceInstanceAAI.getRelationshipList().getRelationship().isEmpty()) {
1336                         addRelationshipsToSI(serviceInstanceAAI, serviceInstance);
1337                 }
1338                 return serviceInstance;
1339         }
1340
1341         protected void populateNetworkCollectionAndInstanceGroupAssign(Service service, String bbName,
1342                         ServiceInstance serviceInstance, String resourceId, String key) throws Exception {
1343                 if (serviceInstance.getCollection() == null
1344                                 && bbName.equalsIgnoreCase(AssignFlows.NETWORK_COLLECTION.toString())) {
1345                         Collection collection = this.createCollection(resourceId);
1346                         serviceInstance.setCollection(collection);
1347                         this.mapCatalogCollection(service, serviceInstance.getCollection(), key);
1348                         if(isVlanTagging(service, key)) {
1349                         InstanceGroup instanceGroup = this.createInstanceGroup();
1350                         serviceInstance.getCollection().setInstanceGroup(instanceGroup);
1351                                 this.mapCatalogNetworkCollectionInstanceGroup(service,
1352                                                 serviceInstance.getCollection().getInstanceGroup(), key);
1353                         }
1354                 }
1355         }
1356
1357         protected CollectionResourceCustomization findCatalogNetworkCollection(Service service, String key) {
1358                 for(CollectionResourceCustomization collectionCust : service.getCollectionResourceCustomizations()){
1359                         if(collectionCust.getModelCustomizationUUID().equalsIgnoreCase(key)) {
1360                                 return collectionCust;
1361                         }
1362                 }
1363                         return null;
1364         }
1365                         
1366         protected boolean isVlanTagging(Service service, String key) {
1367                 CollectionResourceCustomization collectionCust = findCatalogNetworkCollection(service, key);
1368                 if (collectionCust != null) {
1369                         CollectionResource collectionResource = collectionCust.getCollectionResource();
1370                         if (collectionResource != null
1371                                         && collectionResource.getInstanceGroup() != null
1372                                         && collectionResource.getInstanceGroup().getToscaNodeType() != null
1373                                         && collectionResource.getInstanceGroup().getToscaNodeType().contains("NetworkCollection")) {
1374                                 return true;
1375                         }
1376                 }
1377                 return false;
1378         }
1379
1380         protected void mapCatalogNetworkCollectionInstanceGroup(Service service, InstanceGroup instanceGroup, String key) {
1381                 CollectionResourceCustomization collectionCust = this.findCatalogNetworkCollection(service, key);
1382                 org.onap.so.db.catalog.beans.InstanceGroup catalogInstanceGroup = collectionCust.getCollectionResource().getInstanceGroup();
1383                 instanceGroup.setModelInfoInstanceGroup(
1384                                 mapperLayer.mapCatalogInstanceGroupToInstanceGroup(collectionCust, catalogInstanceGroup));
1385                 }
1386
1387         protected void mapCatalogCollection(Service service, Collection collection, String key) {
1388                 CollectionResourceCustomization collectionCust = findCatalogNetworkCollection(service, key);
1389                 if (collectionCust != null) {
1390                         CollectionResource collectionResource = collectionCust.getCollectionResource();
1391                         if (collectionResource != null) {
1392                                 collection.setModelInfoCollection(
1393                                                 mapperLayer.mapCatalogCollectionToCollection(collectionCust, collectionResource));
1394                         }
1395                 }
1396         }
1397
1398         protected Collection createCollection(String collectionId) {
1399                 Collection collection = new Collection();
1400                 collection.setId(collectionId);
1401                 collection.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
1402                 return collection;
1403         }
1404
1405         protected InstanceGroup createInstanceGroup() {
1406                 InstanceGroup instanceGroup = new InstanceGroup();
1407                 String instanceGroupId = this.generateRandomUUID();
1408                 instanceGroup.setId(instanceGroupId);
1409                 return instanceGroup;
1410         }
1411
1412         protected void addRelationshipsToSI(org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI,
1413                         ServiceInstance serviceInstance) throws Exception {
1414                 AAIResultWrapper serviceInstanceWrapper = new AAIResultWrapper(
1415                                 new AAICommonObjectMapperProvider().getMapper().writeValueAsString(serviceInstanceAAI));
1416                 Optional<Relationships> relationshipsOp = serviceInstanceWrapper.getRelationships();
1417                 Relationships relationships = null;
1418                 if (relationshipsOp.isPresent()) {
1419                         relationships = relationshipsOp.get();
1420                 } else {
1421                         return;
1422                 }
1423
1424                 this.mapProject(relationships.getByType(AAIObjectType.PROJECT), serviceInstance);
1425                 this.mapOwningEntity(relationships.getByType(AAIObjectType.OWNING_ENTITY), serviceInstance);
1426                 this.mapL3Networks(relationships.getRelatedAAIUris(AAIObjectType.L3_NETWORK), serviceInstance.getNetworks());
1427                 this.mapGenericVnfs(relationships.getRelatedAAIUris(AAIObjectType.GENERIC_VNF), serviceInstance.getVnfs());
1428                 this.mapCollection(relationships.getByType(AAIObjectType.COLLECTION), serviceInstance);
1429                 this.mapConfigurations(relationships.getRelatedAAIUris(AAIObjectType.CONFIGURATION), serviceInstance.getConfigurations());
1430         }
1431
1432         protected void mapConfigurations(List<AAIResourceUri> relatedAAIUris, List<Configuration> configurations) {
1433                 for (AAIResourceUri aaiResourceUri : relatedAAIUris) {
1434                         configurations.add(mapConfiguration(aaiResourceUri));
1435                 }
1436         }
1437
1438         protected Configuration mapConfiguration(AAIResourceUri aaiResourceUri) {
1439                 AAIResultWrapper aaiConfigurationWrapper = this.bbInputSetupUtils.getAAIResourceDepthOne(aaiResourceUri);
1440                 Optional<org.onap.aai.domain.yang.Configuration> aaiConfigurationOp = aaiConfigurationWrapper
1441                                 .asBean(org.onap.aai.domain.yang.Configuration.class);
1442                 if (!aaiConfigurationOp.isPresent()) {
1443                         return null;
1444                 }
1445
1446                 return this.mapperLayer.mapAAIConfiguration(aaiConfigurationOp.get());
1447         }
1448
1449         protected void mapGenericVnfs(List<AAIResourceUri> list, List<GenericVnf> genericVnfs) {
1450                 for (AAIResourceUri aaiResourceUri : list) {
1451                         genericVnfs.add(this.mapGenericVnf(aaiResourceUri));
1452                 }
1453         }
1454
1455         protected GenericVnf mapGenericVnf(AAIResourceUri aaiResourceUri) {
1456                 AAIResultWrapper aaiGenericVnfWrapper = this.bbInputSetupUtils.getAAIResourceDepthOne(aaiResourceUri);
1457                 Optional<org.onap.aai.domain.yang.GenericVnf> aaiGenericVnfOp = aaiGenericVnfWrapper
1458                                 .asBean(org.onap.aai.domain.yang.GenericVnf.class);
1459                 if (!aaiGenericVnfOp.isPresent()) {
1460                         return null;
1461                 }
1462
1463                 GenericVnf genericVnf = this.mapperLayer.mapAAIGenericVnfIntoGenericVnf(aaiGenericVnfOp.get());
1464
1465                 Optional<Relationships> relationshipsOp = aaiGenericVnfWrapper.getRelationships();
1466                 if (relationshipsOp.isPresent()) {
1467                         Relationships relationships = relationshipsOp.get();
1468                         this.mapPlatform(relationships.getByType(AAIObjectType.PLATFORM), genericVnf);
1469                         this.mapLineOfBusiness(relationships.getByType(AAIObjectType.LINE_OF_BUSINESS), genericVnf);
1470                         genericVnf.getVolumeGroups().addAll(mapVolumeGroups(relationships.getByType(AAIObjectType.VOLUME_GROUP)));
1471                         genericVnf.getInstanceGroups().addAll(mapInstanceGroups(relationships.getByType(AAIObjectType.INSTANCE_GROUP)));
1472                 }
1473
1474                 return genericVnf;
1475         }
1476
1477         protected List<InstanceGroup> mapInstanceGroups(List<AAIResultWrapper> instanceGroups) {
1478                 List<InstanceGroup> instanceGroupsList = new ArrayList<>();
1479                 for (AAIResultWrapper volumeGroupWrapper : instanceGroups) {
1480                         instanceGroupsList.add(this.mapInstanceGroup(volumeGroupWrapper));
1481                 }
1482                 return instanceGroupsList;
1483         }
1484
1485         protected InstanceGroup mapInstanceGroup(AAIResultWrapper instanceGroupWrapper) {
1486                 Optional<org.onap.aai.domain.yang.InstanceGroup> aaiInstanceGroupOp = instanceGroupWrapper
1487                                 .asBean(org.onap.aai.domain.yang.InstanceGroup.class);
1488                 org.onap.aai.domain.yang.InstanceGroup aaiInstanceGroup = null;
1489
1490                 if (!aaiInstanceGroupOp.isPresent()) {
1491                         return null;
1492                 }
1493
1494                 aaiInstanceGroup = aaiInstanceGroupOp.get();
1495                 InstanceGroup instanceGroup = this.mapperLayer.mapAAIInstanceGroupIntoInstanceGroup(aaiInstanceGroup);
1496                 instanceGroup.setModelInfoInstanceGroup(this.mapperLayer.mapCatalogInstanceGroupToInstanceGroup(null,
1497                                 this.bbInputSetupUtils.getCatalogInstanceGroup(aaiInstanceGroup.getModelVersionId())));
1498                 return instanceGroup;
1499         }
1500
1501         protected List<VolumeGroup> mapVolumeGroups(List<AAIResultWrapper> volumeGroups) {
1502                 List<VolumeGroup> volumeGroupsList = new ArrayList<>();
1503                 for (AAIResultWrapper volumeGroupWrapper : volumeGroups) {
1504                         volumeGroupsList.add(this.mapVolumeGroup(volumeGroupWrapper));
1505                 }
1506                 return volumeGroupsList;
1507         }
1508
1509         protected VolumeGroup mapVolumeGroup(AAIResultWrapper volumeGroupWrapper) {
1510                 Optional<org.onap.aai.domain.yang.VolumeGroup> aaiVolumeGroupOp = volumeGroupWrapper
1511                                 .asBean(org.onap.aai.domain.yang.VolumeGroup.class);
1512                 org.onap.aai.domain.yang.VolumeGroup aaiVolumeGroup = null;
1513
1514                 if (!aaiVolumeGroupOp.isPresent()) {
1515                         return null;
1516                 }
1517
1518                 aaiVolumeGroup = aaiVolumeGroupOp.get();
1519                 return this.mapperLayer.mapAAIVolumeGroup(aaiVolumeGroup);
1520         }
1521
1522         protected void mapLineOfBusiness(List<AAIResultWrapper> lineOfBusinesses, GenericVnf genericVnf) {
1523                 if (!lineOfBusinesses.isEmpty()) {
1524                         AAIResultWrapper lineOfBusinessWrapper = lineOfBusinesses.get(0);
1525                         Optional<org.onap.aai.domain.yang.LineOfBusiness> aaiLineOfBusinessOp = lineOfBusinessWrapper
1526                                         .asBean(org.onap.aai.domain.yang.LineOfBusiness.class);
1527                         org.onap.aai.domain.yang.LineOfBusiness aaiLineOfBusiness = null;
1528                         if (!aaiLineOfBusinessOp.isPresent()) {
1529                                 return;
1530                         }
1531                         aaiLineOfBusiness = aaiLineOfBusinessOp.get();
1532
1533                         LineOfBusiness lineOfBusiness = this.mapperLayer.mapAAILineOfBusiness(aaiLineOfBusiness);
1534                         genericVnf.setLineOfBusiness(lineOfBusiness);
1535                 }
1536         }
1537
1538         protected void mapPlatform(List<AAIResultWrapper> platforms, GenericVnf genericVnf) {
1539                 if (!platforms.isEmpty()) {
1540                         AAIResultWrapper platformWrapper = platforms.get(0);
1541                         Optional<org.onap.aai.domain.yang.Platform> aaiPlatformOp = platformWrapper
1542                                         .asBean(org.onap.aai.domain.yang.Platform.class);
1543                         org.onap.aai.domain.yang.Platform aaiPlatform = null;
1544                         if (!aaiPlatformOp.isPresent()) {
1545                                 return;
1546                         }
1547                         aaiPlatform = aaiPlatformOp.get();
1548
1549                         Platform platform = this.mapperLayer.mapAAIPlatform(aaiPlatform);
1550                         genericVnf.setPlatform(platform);
1551                 }
1552         }
1553
1554         protected void mapCollection(List<AAIResultWrapper> collections, ServiceInstance serviceInstance) {
1555                 if (!collections.isEmpty()) {
1556                         AAIResultWrapper collectionWrapper = collections.get(0);
1557                         Optional<org.onap.aai.domain.yang.Collection> aaiCollectionOp = collectionWrapper
1558                                         .asBean(org.onap.aai.domain.yang.Collection.class);
1559                         org.onap.aai.domain.yang.Collection aaiCollection = null;
1560                         if (!aaiCollectionOp.isPresent()) {
1561                                 return;
1562                         }
1563                         aaiCollection = aaiCollectionOp.get();
1564
1565                         Collection collection = this.mapperLayer.mapAAICollectionIntoCollection(aaiCollection);
1566                         NetworkCollectionResourceCustomization collectionResourceCust = 
1567                                         bbInputSetupUtils.getCatalogNetworkCollectionResourceCustByID(aaiCollection.getCollectionCustomizationId());
1568                         collection.setModelInfoCollection(mapperLayer.mapCatalogCollectionToCollection(collectionResourceCust, collectionResourceCust.getCollectionResource()));
1569                         Optional<Relationships> relationshipsOp = collectionWrapper.getRelationships();
1570                         Relationships relationships = null;
1571                         if (relationshipsOp.isPresent()) {
1572                                 relationships = relationshipsOp.get();
1573                         } else {
1574                                 serviceInstance.setCollection(collection);
1575                                 return;
1576                         }
1577                         List<InstanceGroup> instanceGroupsList = mapInstanceGroups(relationships.getByType(AAIObjectType.INSTANCE_GROUP));
1578                         if(!instanceGroupsList.isEmpty()) {
1579                                 collection.setInstanceGroup(instanceGroupsList.get(0));
1580                         }
1581                         serviceInstance.setCollection(collection);
1582                 }
1583         }
1584
1585         protected void mapL3Networks(List<AAIResourceUri> list, List<L3Network> l3Networks) {
1586                 for (AAIResourceUri aaiResourceUri : list) {
1587                         l3Networks.add(this.mapL3Network(aaiResourceUri));
1588                 }
1589         }
1590
1591         protected L3Network mapL3Network(AAIResourceUri aaiResourceUri) {
1592                 AAIResultWrapper aaiNetworkWrapper = this.bbInputSetupUtils.getAAIResourceDepthTwo(aaiResourceUri);
1593                 Optional<org.onap.aai.domain.yang.L3Network> aaiL3NetworkOp = aaiNetworkWrapper
1594                                 .asBean(org.onap.aai.domain.yang.L3Network.class);
1595                 org.onap.aai.domain.yang.L3Network aaiL3Network = null;
1596
1597                 if (!aaiL3NetworkOp.isPresent()) {
1598                         return null;
1599                 }
1600
1601                 aaiL3Network = aaiL3NetworkOp.get();
1602                 L3Network network = this.mapperLayer.mapAAIL3Network(aaiL3Network);
1603
1604                 Optional<Relationships> relationshipsOp = aaiNetworkWrapper.getRelationships();
1605                 if (relationshipsOp.isPresent()) {
1606                         Relationships relationships = relationshipsOp.get();
1607                         this.mapNetworkPolicies(relationships.getByType(AAIObjectType.NETWORK_POLICY),
1608                                         network.getNetworkPolicies());
1609                         mapRouteTableReferences(relationships.getByType(AAIObjectType.ROUTE_TABLE_REFERENCE),
1610                                         network.getContrailNetworkRouteTableReferences());
1611                 }
1612
1613                 return network;
1614         }
1615
1616         protected void mapNetworkPolicies(List<AAIResultWrapper> aaiNetworkPolicies, List<NetworkPolicy> networkPolicies) {
1617                 for (AAIResultWrapper networkPolicyWrapper : aaiNetworkPolicies) {
1618                         networkPolicies.add(this.mapNetworkPolicy(networkPolicyWrapper));
1619                 }
1620         }
1621
1622         protected NetworkPolicy mapNetworkPolicy(AAIResultWrapper networkPolicyWrapper) {
1623                 Optional<org.onap.aai.domain.yang.NetworkPolicy> aaiNetworkPolicyOp = networkPolicyWrapper
1624                                 .asBean(org.onap.aai.domain.yang.NetworkPolicy.class);
1625                 org.onap.aai.domain.yang.NetworkPolicy aaiNetworkPolicy = null;
1626
1627                 if (!aaiNetworkPolicyOp.isPresent()) {
1628                         return null;
1629                 }
1630
1631                 aaiNetworkPolicy = aaiNetworkPolicyOp.get();
1632                 return this.mapperLayer.mapAAINetworkPolicy(aaiNetworkPolicy);
1633         }
1634
1635         protected void mapRouteTableReferences(List<AAIResultWrapper> routeTableReferences,
1636                         List<RouteTableReference> contrailNetworkRouteTableReferences) {
1637                 for (AAIResultWrapper routeTableReferenceWrapper : routeTableReferences) {
1638                         contrailNetworkRouteTableReferences.add(this.mapRouteTableReference(routeTableReferenceWrapper));
1639                 }
1640         }
1641
1642         protected RouteTableReference mapRouteTableReference(AAIResultWrapper routeTableReferenceWrapper) {
1643                 Optional<org.onap.aai.domain.yang.RouteTableReference> aaiRouteTableReferenceOp = routeTableReferenceWrapper
1644                                 .asBean(org.onap.aai.domain.yang.RouteTableReference.class);
1645                 org.onap.aai.domain.yang.RouteTableReference aaiRouteTableReference = null;
1646
1647                 if (!aaiRouteTableReferenceOp.isPresent()) {
1648                         return null;
1649                 }
1650
1651                 aaiRouteTableReference = aaiRouteTableReferenceOp.get();
1652                 return this.mapperLayer.mapAAIRouteTableReferenceIntoRouteTableReference(aaiRouteTableReference);
1653         }
1654
1655         protected void mapOwningEntity(List<AAIResultWrapper> owningEntities, ServiceInstance serviceInstance) {
1656                 if (!owningEntities.isEmpty()) {
1657                         AAIResultWrapper owningEntityWrapper = owningEntities.get(0);
1658                         Optional<org.onap.aai.domain.yang.OwningEntity> aaiOwningEntityOp = owningEntityWrapper
1659                                         .asBean(org.onap.aai.domain.yang.OwningEntity.class);
1660                         org.onap.aai.domain.yang.OwningEntity aaiOwningEntity = null;
1661                         if (!aaiOwningEntityOp.isPresent()) {
1662                                 return;
1663                         }
1664                         aaiOwningEntity = aaiOwningEntityOp.get();
1665
1666                         OwningEntity owningEntity = this.mapperLayer.mapAAIOwningEntity(aaiOwningEntity);
1667                         serviceInstance.setOwningEntity(owningEntity);
1668                 }
1669         }
1670
1671         protected void mapProject(List<AAIResultWrapper> projects, ServiceInstance serviceInstance) {
1672                 if (!projects.isEmpty()) {
1673                         AAIResultWrapper projectWrapper = projects.get(0);
1674                         Optional<org.onap.aai.domain.yang.Project> aaiProjectOp = projectWrapper
1675                                         .asBean(org.onap.aai.domain.yang.Project.class);
1676                         org.onap.aai.domain.yang.Project aaiProject = null;
1677                         if (!aaiProjectOp.isPresent()) {
1678                                 return;
1679                         }
1680                         aaiProject = aaiProjectOp.get();
1681
1682                         Project project = this.mapperLayer.mapAAIProject(aaiProject);
1683                         serviceInstance.setProject(project);
1684                 }
1685         }
1686         protected Customer mapCustomer(String globalCustomerId, String subscriptionServiceType) {
1687                 org.onap.aai.domain.yang.Customer aaiCustomer = this.bbInputSetupUtils.getAAICustomer(globalCustomerId);
1688                 org.onap.aai.domain.yang.ServiceSubscription aaiServiceSubscription = this.bbInputSetupUtils
1689                                 .getAAIServiceSubscription(globalCustomerId, subscriptionServiceType);
1690                 Customer customer = this.mapperLayer.mapAAICustomer(aaiCustomer);
1691                 ServiceSubscription serviceSubscription = this.mapperLayer.mapAAIServiceSubscription(aaiServiceSubscription);
1692                 if(serviceSubscription != null){
1693                         customer.setServiceSubscription(serviceSubscription);
1694                 }
1695                 return customer;
1696         }
1697
1698
1699 }