Skip cloud regions and fix macro
[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                 }
613                 if(vnf != null) {
614                         mapCatalogVnf(vnf, modelInfo, service);
615                         mapVnfcCollectionInstanceGroup(vnf, modelInfo, service);
616                         if (instanceGroupId != null && instanceGroupModelInfo != null) {
617                                 mapNetworkCollectionInstanceGroup(vnf, instanceGroupId);
618                         }
619                 }
620         }
621
622         protected void mapVnfcCollectionInstanceGroup(GenericVnf genericVnf, ModelInfo modelInfo, Service service) {
623                 VnfResourceCustomization vnfResourceCustomization = getVnfResourceCustomizationFromService(modelInfo, service);
624                 if(vnfResourceCustomization != null) {
625                 List<VnfcInstanceGroupCustomization> vnfcInstanceGroups = vnfResourceCustomization
626                                 .getVnfcInstanceGroupCustomizations();
627                 for (VnfcInstanceGroupCustomization vnfcInstanceGroupCust : vnfcInstanceGroups) {
628                         InstanceGroup instanceGroup = this.createInstanceGroup();
629                         org.onap.so.db.catalog.beans.InstanceGroup catalogInstanceGroup = bbInputSetupUtils.getCatalogInstanceGroup(vnfcInstanceGroupCust.getModelUUID());
630                         instanceGroup.setModelInfoInstanceGroup(this.mapperLayer
631                                         .mapCatalogInstanceGroupToInstanceGroup(null, catalogInstanceGroup));
632                         instanceGroup.getModelInfoInstanceGroup().setFunction(vnfcInstanceGroupCust.getFunction());
633                         instanceGroup.setDescription(vnfcInstanceGroupCust.getDescription());
634                         genericVnf.getInstanceGroups().add(instanceGroup);
635                 }
636         }
637         }
638
639         protected void mapNetworkCollectionInstanceGroup(GenericVnf genericVnf, String instanceGroupId) {
640                 org.onap.aai.domain.yang.InstanceGroup aaiInstanceGroup = bbInputSetupUtils
641                                 .getAAIInstanceGroup(instanceGroupId);
642                 InstanceGroup instanceGroup = this.mapperLayer.mapAAIInstanceGroupIntoInstanceGroup(aaiInstanceGroup);
643                 instanceGroup.setModelInfoInstanceGroup(this.mapperLayer.mapCatalogInstanceGroupToInstanceGroup(
644                                 null, this.bbInputSetupUtils.getCatalogInstanceGroup(aaiInstanceGroup.getModelVersionId())));
645                 genericVnf.getInstanceGroups().add(instanceGroup);
646         }
647
648         protected GenericVnf createGenericVnf(Map<ResourceKey, String> lookupKeyMap, String instanceName,
649                         org.onap.so.serviceinstancebeans.Platform platform,
650                         org.onap.so.serviceinstancebeans.LineOfBusiness lineOfBusiness, String vnfId, String vnfType, List<Map<String, String>> instanceParams) {
651                 lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, vnfId);
652                 GenericVnf genericVnf = new GenericVnf();
653                 genericVnf.setVnfId(vnfId);
654                 genericVnf.setVnfName(instanceName);
655                 genericVnf.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
656                 genericVnf.setVnfType(vnfType);
657                 if (platform != null) {
658                         genericVnf.setPlatform(this.mapperLayer.mapRequestPlatform(platform));
659                 }
660                 if (lineOfBusiness != null) {
661                         genericVnf.setLineOfBusiness(this.mapperLayer.mapRequestLineOfBusiness(lineOfBusiness));
662                 }
663                 if(instanceParams != null) {
664                         for(Map<String, String> params : instanceParams) {
665                                 genericVnf.getCloudParams().putAll(params);
666                         }
667                 }
668                 return genericVnf;
669         }
670
671         protected void mapCatalogVnf(GenericVnf genericVnf, ModelInfo modelInfo, Service service) {
672                 VnfResourceCustomization vnfResourceCustomization = getVnfResourceCustomizationFromService(modelInfo, service);
673                 if (vnfResourceCustomization != null) {
674                         genericVnf.setModelInfoGenericVnf(this.mapperLayer.mapCatalogVnfToVnf(vnfResourceCustomization));
675                 }
676         }
677
678         protected VnfResourceCustomization getVnfResourceCustomizationFromService(ModelInfo modelInfo, Service service) {
679                 VnfResourceCustomization vnfResourceCustomization = null;
680                 for (VnfResourceCustomization resourceCust : service.getVnfCustomizations()) {
681                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(modelInfo.getModelCustomizationUuid())) {
682                                 vnfResourceCustomization = resourceCust;
683                                 break;
684                         }
685                 }
686                 return vnfResourceCustomization;
687         }
688
689         protected void populateL3Network(String instanceName, ModelInfo modelInfo, Service service, String bbName,
690                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap, String resourceId, List<Map<String, String>> instanceParams) {
691                 L3Network network = null;
692                 for (L3Network networkTemp : serviceInstance.getNetworks()) {
693                         if (lookupKeyMap.get(ResourceKey.NETWORK_ID) != null
694                                         && networkTemp.getNetworkId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.NETWORK_ID))) {
695                                 network = networkTemp;
696                                 break;
697                         }
698                 }
699                 if (network == null
700                                 && (bbName.equalsIgnoreCase(AssignFlows.NETWORK_A_LA_CARTE.toString()) 
701                                                 || bbName.equalsIgnoreCase(AssignFlows.NETWORK_MACRO.toString()))) {
702                         network = createNetwork(lookupKeyMap, instanceName, resourceId, instanceParams);
703                         serviceInstance.getNetworks().add(network);
704                 }
705                 if(network != null) {
706                         mapCatalogNetwork(network, modelInfo, service);
707                 }
708         }
709
710         protected L3Network createNetwork(Map<ResourceKey, String> lookupKeyMap, String instanceName,
711                         String networkId, List<Map<String, String>> instanceParams) {
712                 lookupKeyMap.put(ResourceKey.NETWORK_ID, networkId);
713                 L3Network network = new L3Network();
714                 network.setNetworkId(networkId);
715                 network.setNetworkName(instanceName);
716                 network.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
717                 if(instanceParams != null) {
718                         for(Map<String, String> params : instanceParams) {
719                                 network.getCloudParams().putAll(params);
720                         }
721                 }
722                 return network;
723         }
724
725         protected void mapCatalogNetwork(L3Network network, ModelInfo modelInfo, Service service) {
726                 NetworkResourceCustomization networkResourceCustomization = null;
727                 for (NetworkResourceCustomization resourceCust : service.getNetworkCustomizations()) {
728                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(modelInfo.getModelCustomizationUuid())) {
729                                 networkResourceCustomization = resourceCust;
730                                 break;
731                         }
732                 }
733                 if (networkResourceCustomization != null) {
734                         network.setModelInfoNetwork(this.mapperLayer.mapCatalogNetworkToNetwork(networkResourceCustomization));
735                 }
736         }
737
738         protected GeneralBuildingBlock getGBBALaCarteService(ExecuteBuildingBlock executeBB, RequestDetails requestDetails,
739                         Map<ResourceKey, String> lookupKeyMap, String requestAction, String resourceId) throws Exception {
740                 Customer customer = getCustomerAndServiceSubscription(requestDetails, resourceId);
741                 if (customer != null) {
742                         Project project = null;
743                         OwningEntity owningEntity = null;
744
745                         if (requestDetails.getProject() != null)
746                                 project = mapperLayer.mapRequestProject(requestDetails.getProject());
747                         if (requestDetails.getOwningEntity() != null)
748                                 owningEntity = mapperLayer.mapRequestOwningEntity(requestDetails.getOwningEntity());
749
750
751                         Service service = bbInputSetupUtils
752                                         .getCatalogServiceByModelUUID(requestDetails.getModelInfo().getModelVersionId());
753                         if (service == null) {
754                                 service = bbInputSetupUtils.getCatalogServiceByModelVersionAndModelInvariantUUID(
755                                                 requestDetails.getModelInfo().getModelVersion(),
756                                                 requestDetails.getModelInfo().getModelInvariantId());
757                         if(service == null) {
758                                         throw new Exception("Could not find service for model version Id: "
759                                                         + requestDetails.getModelInfo().getModelVersionId() + " and for model invariant Id: "
760                                                         + requestDetails.getModelInfo().getModelInvariantId());
761                                 }
762                         }
763                         ServiceInstance serviceInstance = this.getALaCarteServiceInstance(service, requestDetails, customer,
764                                         project, owningEntity, lookupKeyMap, resourceId, Boolean.TRUE.equals(executeBB.isaLaCarte()),
765                                         executeBB.getBuildingBlock().getBpmnFlowName());
766                         return this.populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, customer);
767                 } else {
768                         throw new Exception("Could not find customer");
769                 }
770         }
771
772         protected Customer getCustomerAndServiceSubscription(RequestDetails requestDetails, String resourceId) {
773                 Customer customer;
774                 if (requestDetails.getSubscriberInfo() != null) {
775                         customer = this.getCustomerFromRequest(requestDetails);
776                 } else {
777                         customer = this.getCustomerFromURI(resourceId);
778                 }
779                 if (customer != null) {
780                         ServiceSubscription serviceSubscription = null;
781                         serviceSubscription = getServiceSubscription(requestDetails, customer);
782                         if (serviceSubscription == null) {
783                                 serviceSubscription = getServiceSubscriptionFromURI(resourceId, customer);
784                         }
785                         customer.setServiceSubscription(serviceSubscription);
786                         return customer;
787                 } else {
788                         return null;
789                 }
790         }
791
792         protected ServiceSubscription getServiceSubscriptionFromURI(String resourceId, Customer customer) {
793                 Map<String, String> uriKeys = bbInputSetupUtils.getURIKeysFromServiceInstance(resourceId);
794                 String subscriptionServiceType = uriKeys.get("service-type");
795                 org.onap.aai.domain.yang.ServiceSubscription serviceSubscriptionAAI = bbInputSetupUtils
796                                 .getAAIServiceSubscription(customer.getGlobalCustomerId(), subscriptionServiceType);
797                 if (serviceSubscriptionAAI != null) {
798                         return mapperLayer.mapAAIServiceSubscription(serviceSubscriptionAAI);
799                 } else {
800                         return null;
801                 }
802         }
803
804         protected Customer getCustomerFromURI(String resourceId) {
805                 Map<String, String> uriKeys = bbInputSetupUtils.getURIKeysFromServiceInstance(resourceId);
806                 String globalCustomerId = uriKeys.get("global-customer-id");
807                 org.onap.aai.domain.yang.Customer customerAAI = this.bbInputSetupUtils.getAAICustomer(globalCustomerId);
808                 if (customerAAI != null) {
809                         return mapperLayer.mapAAICustomer(customerAAI);
810                 } else {
811                         return null;
812                 }
813         }
814
815         protected GeneralBuildingBlock populateGBBWithSIAndAdditionalInfo(RequestDetails requestDetails,
816                         ServiceInstance serviceInstance, ExecuteBuildingBlock executeBB, String requestAction, Customer customer) {
817                 GeneralBuildingBlock outputBB = new GeneralBuildingBlock();
818                 OrchestrationContext orchContext = mapperLayer.mapOrchestrationContext(requestDetails);
819                 RequestContext requestContext = mapperLayer.mapRequestContext(requestDetails);
820                 requestContext.setAction(requestAction);
821                 requestContext.setMsoRequestId(executeBB.getRequestId());
822                 org.onap.aai.domain.yang.CloudRegion aaiCloudRegion = bbInputSetupUtils
823                                 .getCloudRegion(requestDetails.getCloudConfiguration());
824                 CloudRegion cloudRegion = mapperLayer.mapCloudRegion(requestDetails.getCloudConfiguration(), aaiCloudRegion);
825                 outputBB.setOrchContext(orchContext);
826                 outputBB.setRequestContext(requestContext);
827                 outputBB.setCloudRegion(cloudRegion);
828                 if(customer == null){
829                         Map<String, String> uriKeys = bbInputSetupUtils.getURIKeysFromServiceInstance(serviceInstance.getServiceInstanceId());
830                         String globalCustomerId = uriKeys.get("global-customer-id");
831                         String subscriptionServiceType = uriKeys.get("service-type");
832                         customer = mapCustomer(globalCustomerId, subscriptionServiceType);
833                 }
834                 outputBB.setServiceInstance(serviceInstance);
835                 if (customer.getServiceSubscription() != null) {
836                         customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
837                 }
838                 outputBB.setCustomer(customer);
839                 return outputBB;
840         }
841
842         protected ServiceSubscription getServiceSubscription(RequestDetails requestDetails, Customer customer) {
843                 org.onap.aai.domain.yang.ServiceSubscription aaiServiceSubscription = bbInputSetupUtils
844                                 .getAAIServiceSubscription(customer.getGlobalCustomerId(),
845                                                 requestDetails.getRequestParameters().getSubscriptionServiceType());
846                 if (aaiServiceSubscription != null) {
847                 return mapperLayer.mapAAIServiceSubscription(aaiServiceSubscription);
848                 } else {
849                         return null;
850                 }
851         }
852
853         protected Customer getCustomerFromRequest(RequestDetails requestDetails) {
854                 org.onap.aai.domain.yang.Customer aaiCustomer = bbInputSetupUtils
855                                 .getAAICustomer(requestDetails.getSubscriberInfo().getGlobalSubscriberId());
856                 if (aaiCustomer != null) {
857                 return mapperLayer.mapAAICustomer(aaiCustomer);
858                 } else {
859                         return null;
860                 }
861         }
862
863         protected ServiceInstance getALaCarteServiceInstance(Service service, RequestDetails requestDetails,
864                         Customer customer, Project project, OwningEntity owningEntity, Map<ResourceKey, String> lookupKeyMap,
865                         String serviceInstanceId, boolean aLaCarte, String bbName) throws Exception {
866                 ServiceInstance serviceInstance = this.getServiceInstanceHelper(requestDetails, customer, project, owningEntity,
867                                 lookupKeyMap, serviceInstanceId, aLaCarte, service, bbName);
868                 org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI = this.bbInputSetupUtils
869                                 .getAAIServiceInstanceById(serviceInstanceId);
870                 if (serviceInstanceAAI != null
871                                 && !serviceInstanceAAI.getModelVersionId().equalsIgnoreCase(service.getModelUUID())) {
872                         Service tempService = this.bbInputSetupUtils
873                                         .getCatalogServiceByModelUUID(serviceInstanceAAI.getModelVersionId());
874                         if (tempService != null) {
875                                 serviceInstance
876                                                 .setModelInfoServiceInstance(mapperLayer.mapCatalogServiceIntoServiceInstance(tempService));
877                                 return serviceInstance;
878                         } else {
879                                 throw new Exception(
880                                                 "Could not find model of existing SI. Service Instance in AAI already exists with different model version id: "
881                                                                 + serviceInstanceAAI.getModelVersionId());
882                         }
883                 }
884                 serviceInstance.setModelInfoServiceInstance(mapperLayer.mapCatalogServiceIntoServiceInstance(service));
885                 return serviceInstance;
886         }
887
888         protected GeneralBuildingBlock getGBBMacro(ExecuteBuildingBlock executeBB, RequestDetails requestDetails,
889                         Map<ResourceKey, String> lookupKeyMap, String requestAction, String resourceId, String vnfType)
890                         throws Exception {
891                 String bbName = executeBB.getBuildingBlock().getBpmnFlowName();
892                 String key = executeBB.getBuildingBlock().getKey();
893                 
894                 if (requestAction.equalsIgnoreCase("deleteInstance")
895                                 || requestAction.equalsIgnoreCase("unassignInstance")
896                                 || requestAction.equalsIgnoreCase("activateInstance")
897                                 || requestAction.equalsIgnoreCase("activateFabricConfiguration")
898                                 || requestAction.equalsIgnoreCase("recreateInstance")
899                                 || requestAction.equalsIgnoreCase("replaceInstance")) {
900                         return getGBBMacroExistingService(executeBB, lookupKeyMap, bbName, requestAction,
901                                         requestDetails.getCloudConfiguration());
902                 }
903
904                 String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
905                 GeneralBuildingBlock gBB = this.getGBBALaCarteService(executeBB, requestDetails, lookupKeyMap, requestAction,
906                                 serviceInstanceId);
907                 RequestParameters requestParams = requestDetails.getRequestParameters();
908                 Service service = null;
909                 if (gBB != null && gBB.getServiceInstance() != null
910                                 && gBB.getServiceInstance().getModelInfoServiceInstance() != null
911                                 && gBB.getServiceInstance().getModelInfoServiceInstance().getModelUuid() != null) {
912                         service = bbInputSetupUtils.getCatalogServiceByModelUUID(
913                                         gBB.getServiceInstance().getModelInfoServiceInstance().getModelUuid());
914                 } else {
915                         throw new Exception("Could not get service instance for macro request");
916                 }
917                 if (requestParams != null && requestParams.getUserParams() != null) {
918                         for(Map<String, Object> userParams : requestParams.getUserParams()) {
919                                 if(userParams.containsKey("service")) {
920                                         String input = mapper.writeValueAsString(userParams.get("service"));
921                                         return getGBBMacroUserParams(executeBB, requestDetails, lookupKeyMap, vnfType, bbName, key, gBB,
922                                                         requestParams, service, input);
923                                 }
924                         }
925                 }
926                 if (requestAction.equalsIgnoreCase("deactivateInstance")) {
927                         return gBB;
928                 } else if (requestAction.equalsIgnoreCase("createInstance")) {
929                         return getGBBMacroNoUserParamsCreate(executeBB, lookupKeyMap, bbName, key, gBB, service);
930                 } else {
931                         throw new IllegalArgumentException(
932                                         "No user params on requestAction: assignInstance. Please specify user params.");
933                 }
934         }
935
936         protected GeneralBuildingBlock getGBBMacroNoUserParamsCreate(ExecuteBuildingBlock executeBB,
937                         Map<ResourceKey, String> lookupKeyMap, String bbName, String key, GeneralBuildingBlock gBB, Service service)
938                         throws Exception {
939                 ServiceInstance serviceInstance = gBB.getServiceInstance();
940                 if (bbName.contains(NETWORK) && !bbName.contains(NETWORK_COLLECTION)) {
941                         String networkId = lookupKeyMap.get(ResourceKey.NETWORK_ID);
942                         ModelInfo networkModelInfo = new ModelInfo();
943                         if((!Boolean.TRUE.equals(executeBB.getBuildingBlock().getIsVirtualLink()))) {
944                                 NetworkResourceCustomization networkCust = getNetworkCustomizationByKey(key, service);
945                                 if (networkCust != null) {
946                                         networkModelInfo.setModelCustomizationUuid(networkCust.getModelCustomizationUUID());
947                                         this.populateL3Network(null, networkModelInfo, service, bbName, serviceInstance, lookupKeyMap,
948                                                         networkId, null);
949                                 } else {
950                                         msoLogger.debug("Could not find a network customization with key: " + key);
951                                 }
952                         } else {
953                                 msoLogger.debug("Orchestrating on Collection Network Resource Customization");
954                                 CollectionNetworkResourceCustomization collectionNetworkResourceCust = bbInputSetupUtils.getCatalogCollectionNetworkResourceCustByID(key);
955                                 L3Network l3Network = getVirtualLinkL3Network(lookupKeyMap, bbName, key, networkId, collectionNetworkResourceCust, serviceInstance);
956                                 NetworkResourceCustomization networkResourceCustomization = 
957                                                 mapperLayer.mapCollectionNetworkResourceCustToNetworkResourceCust(collectionNetworkResourceCust);
958                                 if(l3Network != null) {
959                                         l3Network.setModelInfoNetwork(mapperLayer.mapCatalogNetworkToNetwork(networkResourceCustomization));
960                                 }
961                         }
962                 } else if(bbName.contains("Configuration")) {
963                         String configurationId = lookupKeyMap.get(ResourceKey.CONFIGURATION_ID);
964                         ModelInfo configurationModelInfo = new ModelInfo();
965                         configurationModelInfo.setModelCustomizationUuid(key);
966                         this.populateConfiguration(configurationModelInfo, service, bbName, serviceInstance, lookupKeyMap, configurationId, null, executeBB.getConfigurationResourceKeys());
967                 }
968                 if (executeBB.getWorkflowResourceIds() != null) {
969                         this.populateNetworkCollectionAndInstanceGroupAssign(service, bbName, serviceInstance,
970                                         executeBB.getWorkflowResourceIds().getNetworkCollectionId(), key);
971                 }
972                 return gBB;
973         }
974
975         protected L3Network getVirtualLinkL3Network(Map<ResourceKey, String> lookupKeyMap, String bbName, String key,
976                         String networkId, CollectionNetworkResourceCustomization collectionNetworkResourceCust, ServiceInstance serviceInstance) {
977                 if(collectionNetworkResourceCust != null) {
978                         if((bbName.equalsIgnoreCase(AssignFlows.NETWORK_A_LA_CARTE.toString())
979                                 || bbName.equalsIgnoreCase(AssignFlows.NETWORK_MACRO.toString()))) {
980                                 L3Network network = createNetwork(lookupKeyMap, null, networkId, null);                         
981                                 serviceInstance.getNetworks().add(network);
982                                 return network;
983                         } else {
984                                 for (L3Network network : serviceInstance.getNetworks()) {
985                                         if (network.getNetworkId().equalsIgnoreCase(networkId)) {
986                                                 return network;
987                                         }
988                                 }
989                         }
990                 }
991                 return null;
992         }
993
994         protected NetworkResourceCustomization getNetworkCustomizationByKey(String key, Service service) {
995                 for (NetworkResourceCustomization networkCust : service.getNetworkCustomizations()) {
996                         if (networkCust.getModelCustomizationUUID().equalsIgnoreCase(key)) {
997                                 return networkCust;
998                         }
999                 }
1000                 return null;
1001         }
1002
1003         protected GeneralBuildingBlock getGBBMacroExistingService(ExecuteBuildingBlock executeBB,
1004                         Map<ResourceKey, String> lookupKeyMap, String bbName, String requestAction, CloudConfiguration cloudConfiguration) throws Exception {
1005                 org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
1006                 String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
1007                 RequestDetails requestDetails = executeBB.getRequestDetails();
1008                 GeneralBuildingBlock gBB = null;
1009                 if (serviceInstanceId != null) {
1010                         aaiServiceInstance = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
1011                 }
1012                 Service service = null;
1013                 if (aaiServiceInstance != null) {
1014                         service = bbInputSetupUtils.getCatalogServiceByModelUUID(aaiServiceInstance.getModelVersionId());
1015                 }
1016                 if (aaiServiceInstance != null && service != null) {
1017                         ServiceInstance serviceInstance = this.getExistingServiceInstance(aaiServiceInstance);
1018                         serviceInstance.setModelInfoServiceInstance(this.mapperLayer.mapCatalogServiceIntoServiceInstance(service));
1019                         gBB = populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, null);
1020                 } else {
1021                         msoLogger.debug("Related Service Instance from AAI: " + aaiServiceInstance);
1022                         msoLogger.debug("Related Service Instance Model Info from AAI: " + service);
1023                         throw new Exception("Could not find relevant information for related Service Instance");
1024                 }
1025                 ServiceInstance serviceInstance = gBB.getServiceInstance();
1026                 CloudRegion cloudRegion = null;
1027                 if(cloudConfiguration == null) {
1028                         Optional<CloudRegion> cloudRegionOp = cloudInfoFromAAI.getCloudInfoFromAAI(serviceInstance);
1029                         if(cloudRegionOp.isPresent()) {
1030                                 cloudRegion = cloudRegionOp.get();
1031                         }
1032                 }
1033                 if (cloudConfiguration != null) {
1034                         org.onap.aai.domain.yang.CloudRegion aaiCloudRegion = bbInputSetupUtils.getCloudRegion(cloudConfiguration);
1035                         cloudRegion = mapperLayer.mapCloudRegion(cloudConfiguration, aaiCloudRegion);
1036                 }
1037                 gBB.setCloudRegion(cloudRegion);
1038                 if (bbName.contains(VNF)) {
1039                         for (GenericVnf genericVnf : serviceInstance.getVnfs()) {
1040                                 if (lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID) != null
1041                                                 && genericVnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
1042                                         org.onap.aai.domain.yang.GenericVnf vnf = bbInputSetupUtils.getAAIGenericVnf(genericVnf.getVnfId());
1043                                         ModelInfo modelInfo = new ModelInfo();
1044                                         if (vnf != null) {
1045                                                 modelInfo.setModelCustomizationUuid(vnf.getModelCustomizationId());
1046                                         }
1047                                         this.mapCatalogVnf(genericVnf, modelInfo, service);
1048                                 }
1049                         }
1050                 } else if (bbName.contains(VF_MODULE)) {
1051                         for (GenericVnf vnf : serviceInstance.getVnfs()) {
1052                                 for (VfModule vfModule : vnf.getVfModules()) {
1053                                         if (lookupKeyMap.get(ResourceKey.VF_MODULE_ID) != null
1054                                                         && vfModule.getVfModuleId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.VF_MODULE_ID))) {
1055                                                 String vnfModelCustomizationUUID = this.bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId())
1056                                                                 .getModelCustomizationId();
1057                                                 ModelInfo vnfModelInfo = new ModelInfo();
1058                                                 vnfModelInfo.setModelCustomizationUuid(vnfModelCustomizationUUID);
1059                                                 this.mapCatalogVnf(vnf, vnfModelInfo, service);
1060                                                 lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, vnf.getVnfId());
1061                                                 String vfModuleCustomizationUUID = this.bbInputSetupUtils
1062                                                                 .getAAIVfModule(vnf.getVnfId(), vfModule.getVfModuleId()).getModelCustomizationId();
1063                                                 ModelInfo vfModuleModelInfo = new ModelInfo();
1064                                                 vfModuleModelInfo.setModelCustomizationId(vfModuleCustomizationUUID);
1065                                                 this.mapCatalogVfModule(vfModule, vfModuleModelInfo, service, vnfModelCustomizationUUID);
1066                                                 if(cloudRegion != null) {
1067                                                         Optional<String> volumeGroupIdOp = getVolumeGroupIdRelatedToVfModule(vnf, vfModuleModelInfo, cloudRegion.getCloudOwner(), 
1068                                                                         cloudRegion.getLcpCloudRegionId(), lookupKeyMap);
1069                                                         if(volumeGroupIdOp.isPresent()) {
1070                                                                 lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, volumeGroupIdOp.get());
1071                                                         }
1072                                                 }
1073                                                 break;
1074                                         }
1075                                 }
1076                         }
1077                 } else if (bbName.contains(VOLUME_GROUP)) {
1078                         for (GenericVnf vnf : serviceInstance.getVnfs()) {
1079                                 for (VolumeGroup volumeGroup : vnf.getVolumeGroups()) {
1080                                         if (lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID) != null && volumeGroup.getVolumeGroupId()
1081                                                         .equalsIgnoreCase(lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID))) {
1082                                                 String vnfModelCustomizationUUID = this.bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId())
1083                                                                 .getModelCustomizationId();
1084                                                 ModelInfo vnfModelInfo = new ModelInfo();
1085                                                 vnfModelInfo.setModelCustomizationUuid(vnfModelCustomizationUUID);
1086                                                 this.mapCatalogVnf(vnf, vnfModelInfo, service);
1087                                                 lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, vnf.getVnfId());
1088                                                 if (cloudRegion != null) {
1089                                                         String volumeGroupCustomizationUUID = this.bbInputSetupUtils.getAAIVolumeGroup(cloudRegion.getCloudOwner(),
1090                                                                         cloudRegion.getLcpCloudRegionId(), volumeGroup.getVolumeGroupId())
1091                                                                         .getModelCustomizationId();
1092                                                         ModelInfo volumeGroupModelInfo = new ModelInfo();
1093                                                         volumeGroupModelInfo.setModelCustomizationId(volumeGroupCustomizationUUID);
1094                                                         this.mapCatalogVolumeGroup(volumeGroup, volumeGroupModelInfo, service,
1095                                                                         vnfModelCustomizationUUID);
1096                                                 }
1097                                                 break;
1098                                         }
1099                                 }
1100                         }
1101                 } else if (bbName.contains(NETWORK)) {
1102                         for (L3Network network : serviceInstance.getNetworks()) {
1103                                 if (lookupKeyMap.get(ResourceKey.NETWORK_ID) != null
1104                                                 && network.getNetworkId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.NETWORK_ID))) {
1105                                         String networkCustomizationUUID = this.bbInputSetupUtils.getAAIL3Network(network.getNetworkId())
1106                                                         .getModelCustomizationId();
1107                                         ModelInfo modelInfo = new ModelInfo();
1108                                         modelInfo.setModelCustomizationUuid(networkCustomizationUUID);
1109                                         this.mapCatalogNetwork(network, modelInfo, service);
1110                                         break;
1111                                 }
1112                         }
1113                 } else if (bbName.contains("Fabric")) {
1114                         for(Configuration configuration : serviceInstance.getConfigurations()) {
1115                                 if(lookupKeyMap.get(ResourceKey.CONFIGURATION_ID) != null
1116                                                 && configuration.getConfigurationId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.CONFIGURATION_ID))) {
1117                                         String configurationCustUUID = this.bbInputSetupUtils.getAAIConfiguration(configuration.getConfigurationId())
1118                                                         .getModelCustomizationId();
1119                                         ModelInfo modelInfo = new ModelInfo();
1120                                         modelInfo.setModelCustomizationUuid(configurationCustUUID);
1121                                         this.mapCatalogConfiguration(configuration, modelInfo, service, executeBB.getConfigurationResourceKeys());
1122                                         break;
1123                                 }
1124                         }
1125                 }
1126                 if (executeBB.getWorkflowResourceIds() != null) {
1127                         this.populateNetworkCollectionAndInstanceGroupAssign(service, bbName, serviceInstance,
1128                                         executeBB.getWorkflowResourceIds().getNetworkCollectionId(), executeBB.getBuildingBlock().getKey());
1129                 }
1130                 return gBB;
1131         }
1132
1133         protected GeneralBuildingBlock getGBBMacroUserParams(ExecuteBuildingBlock executeBB, RequestDetails requestDetails,
1134                         Map<ResourceKey, String> lookupKeyMap, String vnfType, String bbName, String key, GeneralBuildingBlock gBB,
1135                         RequestParameters requestParams, Service service, String input) throws Exception {
1136                 ServiceInstance serviceInstance = gBB.getServiceInstance();
1137                 org.onap.so.serviceinstancebeans.Service serviceMacro = mapper.readValue(input,
1138                                 org.onap.so.serviceinstancebeans.Service.class);
1139
1140                 Resources resources = serviceMacro.getResources();
1141                 Vnfs vnfs = null;
1142                 VfModules vfModules = null;
1143                 Networks networks = null;
1144                 CloudConfiguration cloudConfiguration = requestDetails.getCloudConfiguration();
1145                 CloudRegion cloudRegion = getCloudRegionFromMacroRequest(cloudConfiguration, resources);
1146                 gBB.setCloudRegion(cloudRegion);
1147                 if (bbName.contains(VNF)) {
1148                         vnfs = findVnfsByKey(key, resources, vnfs);
1149                         String vnfId = lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID);
1150                         // This stores the vnf id in request db to be retrieved later when
1151                         // working on a vf module or volume group
1152                         InfraActiveRequests request = this.bbInputSetupUtils.getInfraActiveRequest(executeBB.getRequestId());
1153                         if (request != null) {
1154                                 this.bbInputSetupUtils.updateInfraActiveRequestVnfId(request, vnfId);
1155                         }
1156                         this.populateGenericVnf(vnfs.getModelInfo(), vnfs.getInstanceName(), vnfs.getPlatform(),
1157                                         vnfs.getLineOfBusiness(), service, bbName, serviceInstance, lookupKeyMap, null, vnfId, vnfType, vnfs.getInstanceParams());
1158                 } else if (bbName.contains(VF_MODULE) || bbName.contains(VOLUME_GROUP)) {
1159                         Pair<Vnfs, VfModules> vnfsAndVfModules = getVfModulesAndItsVnfsByKey(key, resources);
1160                         vfModules = vnfsAndVfModules.getValue1();
1161                         vnfs = vnfsAndVfModules.getValue0();
1162                         lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, getVnfId(executeBB, lookupKeyMap));
1163                         if (vnfs == null) {
1164                                 throw new Exception("Could not find Vnf to orchestrate VfModule");
1165                         }
1166                         ModelInfo modelInfo = vfModules.getModelInfo();
1167                         if (bbName.contains(VOLUME_GROUP)) {
1168                                 String volumeGroupId = lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID);
1169                                 this.populateVolumeGroup(modelInfo, service, bbName, serviceInstance, lookupKeyMap, volumeGroupId, null,
1170                                                 vfModules.getVolumeGroupInstanceName(), vnfType, vfModules.getInstanceParams());
1171                         } else {
1172                                 String vfModuleId = lookupKeyMap.get(ResourceKey.VF_MODULE_ID);
1173                                 CloudConfiguration cloudConfig = new CloudConfiguration();
1174                                 cloudConfig.setLcpCloudRegionId(cloudRegion.getLcpCloudRegionId());
1175                                 cloudConfig.setCloudOwner(cloudRegion.getCloudOwner());
1176                                 this.populateVfModule(modelInfo, service, bbName, serviceInstance, lookupKeyMap, vfModuleId, null,
1177                                                 vfModules.getInstanceName(), vfModules.getInstanceParams(), cloudConfig);
1178                         }
1179                 } else if (bbName.contains(NETWORK)) {
1180                         networks = findNetworksByKey(key, resources);
1181                         String networkId = lookupKeyMap.get(ResourceKey.NETWORK_ID);
1182                         this.populateL3Network(networks.getInstanceName(), networks.getModelInfo(), service, bbName,
1183                                         serviceInstance, lookupKeyMap, networkId, networks.getInstanceParams());
1184                 } else if (bbName.contains("Configuration")) {
1185                         String configurationId = lookupKeyMap.get(ResourceKey.CONFIGURATION_ID);
1186                         ModelInfo configurationModelInfo = new ModelInfo();
1187                         configurationModelInfo.setModelCustomizationUuid(key);
1188                         ConfigurationResourceCustomization configurationCust = findConfigurationResourceCustomization(configurationModelInfo, service);
1189                         if(configurationCust != null) {
1190                                 this.populateConfiguration(configurationModelInfo, service, bbName, serviceInstance, lookupKeyMap, configurationId, null, executeBB.getConfigurationResourceKeys());
1191                         } else {
1192                                 msoLogger.debug("Could not find a configuration customization with key: " + key);
1193                         }
1194                 }
1195                 return gBB;
1196         }
1197         
1198         protected Networks findNetworksByKey(String key, Resources resources) {
1199                 for (Networks networks : resources.getNetworks()) {
1200                         if (networks.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
1201                                 return networks;
1202                         }
1203                 }
1204                 return null;
1205         }
1206
1207         protected Pair<Vnfs, VfModules> getVfModulesAndItsVnfsByKey(String key, Resources resources) {
1208                 for (Vnfs vnfs : resources.getVnfs()) {
1209                         for (VfModules vfModules : vnfs.getVfModules()) {
1210                                 if (vfModules.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
1211                                         return new Pair<Vnfs, VfModules>(vnfs, vfModules);
1212                                 }
1213                         }
1214                 }
1215                 return null;
1216         }
1217
1218         protected Vnfs findVnfsByKey(String key, Resources resources, Vnfs vnfs) {
1219                 for (Vnfs tempVnfs : resources.getVnfs()) {
1220                         if (tempVnfs.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
1221                                 vnfs = tempVnfs;
1222                                 break;
1223                         }
1224                 }
1225                 return vnfs;
1226         }
1227
1228         protected CloudRegion getCloudRegionFromMacroRequest(CloudConfiguration cloudConfiguration, Resources resources) {
1229                 if(cloudConfiguration == null) {
1230                         for(Vnfs vnfs : resources.getVnfs()) {
1231                                 if(cloudConfiguration == null) {
1232                                         cloudConfiguration = vnfs.getCloudConfiguration();
1233                                 } else {
1234                                         break;
1235                                 }
1236                                 for(VfModules vfModules : vnfs.getVfModules()) {
1237                                         if(cloudConfiguration == null) {
1238                                                 cloudConfiguration = vfModules.getCloudConfiguration();
1239                                         } else {
1240                                                 break;
1241                                         }
1242                                 }
1243                         }
1244                         for(Networks networks : resources.getNetworks()) {
1245                                 if(cloudConfiguration == null) {
1246                                 cloudConfiguration = networks.getCloudConfiguration();
1247                                 } else {
1248                                         break;
1249                         }
1250                 }
1251                 }
1252                 if(cloudConfiguration != null) {
1253                         org.onap.aai.domain.yang.CloudRegion aaiCloudRegion = bbInputSetupUtils.getCloudRegion(cloudConfiguration);
1254                         return mapperLayer.mapCloudRegion(cloudConfiguration, aaiCloudRegion);
1255                 } else {
1256                         msoLogger.debug("Could not find any cloud configuration for this request.");
1257                         return null;
1258                 }
1259         }
1260
1261         protected String getVnfId(ExecuteBuildingBlock executeBB, Map<ResourceKey, String> lookupKeyMap) {
1262                 String vnfId = lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID);
1263                 if (vnfId == null) {
1264                         InfraActiveRequests request = this.bbInputSetupUtils.getInfraActiveRequest(executeBB.getRequestId());
1265                         vnfId = request.getVnfId();
1266                 }
1267
1268                 return vnfId;
1269         }
1270
1271         protected String generateRandomUUID() {
1272                 return UUID.randomUUID().toString();
1273         }
1274
1275         protected ServiceInstance getServiceInstanceHelper(RequestDetails requestDetails, Customer customer,
1276                         Project project, OwningEntity owningEntity, Map<ResourceKey, String> lookupKeyMap, String serviceInstanceId,
1277                         boolean aLaCarte, Service service, String bbName) throws Exception {
1278                 if (requestDetails.getRequestInfo().getInstanceName() == null && aLaCarte
1279                                 && bbName.equalsIgnoreCase(AssignFlows.SERVICE_INSTANCE.toString())) {
1280                         throw new Exception("Request invalid missing: RequestInfo:InstanceName");
1281                 } else {
1282                         org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI = null;
1283                         if (aLaCarte && bbName.equalsIgnoreCase(AssignFlows.SERVICE_INSTANCE.toString())) {
1284                                 serviceInstanceAAI = bbInputSetupUtils
1285                                         .getAAIServiceInstanceByName(requestDetails.getRequestInfo().getInstanceName(), customer);
1286                         }
1287                         if (serviceInstanceId != null && serviceInstanceAAI == null) {
1288                                 serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
1289                         }
1290                         if (serviceInstanceAAI != null) {
1291                                 lookupKeyMap.put(ResourceKey.SERVICE_INSTANCE_ID, serviceInstanceId);
1292                                 return this.getExistingServiceInstance(serviceInstanceAAI);
1293                         } else {
1294                                 return createServiceInstance(requestDetails, project, owningEntity, lookupKeyMap,
1295                                                 serviceInstanceId);
1296                         }
1297                 }
1298         }
1299
1300         protected ServiceInstance createServiceInstance(RequestDetails requestDetails, Project project,
1301                                                                                                         OwningEntity owningEntity, Map<ResourceKey, String> lookupKeyMap, String serviceInstanceId) {
1302                 ServiceInstance serviceInstance = new ServiceInstance();
1303                 lookupKeyMap.put(ResourceKey.SERVICE_INSTANCE_ID, serviceInstanceId);
1304                 serviceInstance.setServiceInstanceId(serviceInstanceId);
1305                 if(requestDetails.getRequestInfo() != null) {
1306                 serviceInstance.setServiceInstanceName(requestDetails.getRequestInfo().getInstanceName());
1307                 }
1308                 serviceInstance.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
1309                 serviceInstance.setProject(project);
1310                 serviceInstance.setOwningEntity(owningEntity);
1311                 return serviceInstance;
1312         }
1313
1314         public ServiceInstance getExistingServiceInstance(org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI)
1315                         throws Exception {
1316                 ServiceInstance serviceInstance = mapperLayer.mapAAIServiceInstanceIntoServiceInstance(serviceInstanceAAI);
1317                 if (serviceInstanceAAI.getRelationshipList() != null
1318                                 && serviceInstanceAAI.getRelationshipList().getRelationship() != null
1319                                 && !serviceInstanceAAI.getRelationshipList().getRelationship().isEmpty()) {
1320                         addRelationshipsToSI(serviceInstanceAAI, serviceInstance);
1321                 }
1322                 return serviceInstance;
1323         }
1324
1325         protected void populateNetworkCollectionAndInstanceGroupAssign(Service service, String bbName,
1326                         ServiceInstance serviceInstance, String resourceId, String key) throws Exception {
1327                 if (serviceInstance.getCollection() == null
1328                                 && bbName.equalsIgnoreCase(AssignFlows.NETWORK_COLLECTION.toString())) {
1329                         Collection collection = this.createCollection(resourceId);
1330                         serviceInstance.setCollection(collection);
1331                         this.mapCatalogCollection(service, serviceInstance.getCollection(), key);
1332                         if(isVlanTagging(service, key)) {
1333                         InstanceGroup instanceGroup = this.createInstanceGroup();
1334                         serviceInstance.getCollection().setInstanceGroup(instanceGroup);
1335                                 this.mapCatalogNetworkCollectionInstanceGroup(service,
1336                                                 serviceInstance.getCollection().getInstanceGroup(), key);
1337                         }
1338                 }
1339         }
1340
1341         protected CollectionResourceCustomization findCatalogNetworkCollection(Service service, String key) {
1342                 for(CollectionResourceCustomization collectionCust : service.getCollectionResourceCustomizations()){
1343                         if(collectionCust.getModelCustomizationUUID().equalsIgnoreCase(key)) {
1344                                 return collectionCust;
1345                         }
1346                 }
1347                         return null;
1348         }
1349                         
1350         protected boolean isVlanTagging(Service service, String key) {
1351                 CollectionResourceCustomization collectionCust = findCatalogNetworkCollection(service, key);
1352                 if (collectionCust != null) {
1353                         CollectionResource collectionResource = collectionCust.getCollectionResource();
1354                         if (collectionResource != null
1355                                         && collectionResource.getInstanceGroup() != null
1356                                         && collectionResource.getInstanceGroup().getToscaNodeType() != null
1357                                         && collectionResource.getInstanceGroup().getToscaNodeType().contains("NetworkCollection")) {
1358                                 return true;
1359                         }
1360                 }
1361                 return false;
1362         }
1363
1364         protected void mapCatalogNetworkCollectionInstanceGroup(Service service, InstanceGroup instanceGroup, String key) {
1365                 CollectionResourceCustomization collectionCust = this.findCatalogNetworkCollection(service, key);
1366                 org.onap.so.db.catalog.beans.InstanceGroup catalogInstanceGroup = collectionCust.getCollectionResource().getInstanceGroup();
1367                 instanceGroup.setModelInfoInstanceGroup(
1368                                 mapperLayer.mapCatalogInstanceGroupToInstanceGroup(collectionCust, catalogInstanceGroup));
1369                 }
1370
1371         protected void mapCatalogCollection(Service service, Collection collection, String key) {
1372                 CollectionResourceCustomization collectionCust = findCatalogNetworkCollection(service, key);
1373                 if (collectionCust != null) {
1374                         CollectionResource collectionResource = collectionCust.getCollectionResource();
1375                         if (collectionResource != null) {
1376                                 collection.setModelInfoCollection(
1377                                                 mapperLayer.mapCatalogCollectionToCollection(collectionCust, collectionResource));
1378                         }
1379                 }
1380         }
1381
1382         protected Collection createCollection(String collectionId) {
1383                 Collection collection = new Collection();
1384                 collection.setId(collectionId);
1385                 collection.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
1386                 return collection;
1387         }
1388
1389         protected InstanceGroup createInstanceGroup() {
1390                 InstanceGroup instanceGroup = new InstanceGroup();
1391                 String instanceGroupId = this.generateRandomUUID();
1392                 instanceGroup.setId(instanceGroupId);
1393                 return instanceGroup;
1394         }
1395
1396         protected void addRelationshipsToSI(org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI,
1397                         ServiceInstance serviceInstance) throws Exception {
1398                 AAIResultWrapper serviceInstanceWrapper = new AAIResultWrapper(
1399                                 new AAICommonObjectMapperProvider().getMapper().writeValueAsString(serviceInstanceAAI));
1400                 Optional<Relationships> relationshipsOp = serviceInstanceWrapper.getRelationships();
1401                 Relationships relationships = null;
1402                 if (relationshipsOp.isPresent()) {
1403                         relationships = relationshipsOp.get();
1404                 } else {
1405                         return;
1406                 }
1407
1408                 this.mapProject(relationships.getByType(AAIObjectType.PROJECT), serviceInstance);
1409                 this.mapOwningEntity(relationships.getByType(AAIObjectType.OWNING_ENTITY), serviceInstance);
1410                 this.mapL3Networks(relationships.getRelatedAAIUris(AAIObjectType.L3_NETWORK), serviceInstance.getNetworks());
1411                 this.mapGenericVnfs(relationships.getRelatedAAIUris(AAIObjectType.GENERIC_VNF), serviceInstance.getVnfs());
1412                 this.mapCollection(relationships.getByType(AAIObjectType.COLLECTION), serviceInstance);
1413                 this.mapConfigurations(relationships.getRelatedAAIUris(AAIObjectType.CONFIGURATION), serviceInstance.getConfigurations());
1414         }
1415
1416         protected void mapConfigurations(List<AAIResourceUri> relatedAAIUris, List<Configuration> configurations) {
1417                 for (AAIResourceUri aaiResourceUri : relatedAAIUris) {
1418                         configurations.add(mapConfiguration(aaiResourceUri));
1419                 }
1420         }
1421
1422         protected Configuration mapConfiguration(AAIResourceUri aaiResourceUri) {
1423                 AAIResultWrapper aaiConfigurationWrapper = this.bbInputSetupUtils.getAAIResourceDepthOne(aaiResourceUri);
1424                 Optional<org.onap.aai.domain.yang.Configuration> aaiConfigurationOp = aaiConfigurationWrapper
1425                                 .asBean(org.onap.aai.domain.yang.Configuration.class);
1426                 if (!aaiConfigurationOp.isPresent()) {
1427                         return null;
1428                 }
1429
1430                 return this.mapperLayer.mapAAIConfiguration(aaiConfigurationOp.get());
1431         }
1432
1433         protected void mapGenericVnfs(List<AAIResourceUri> list, List<GenericVnf> genericVnfs) {
1434                 for (AAIResourceUri aaiResourceUri : list) {
1435                         genericVnfs.add(this.mapGenericVnf(aaiResourceUri));
1436                 }
1437         }
1438
1439         protected GenericVnf mapGenericVnf(AAIResourceUri aaiResourceUri) {
1440                 AAIResultWrapper aaiGenericVnfWrapper = this.bbInputSetupUtils.getAAIResourceDepthOne(aaiResourceUri);
1441                 Optional<org.onap.aai.domain.yang.GenericVnf> aaiGenericVnfOp = aaiGenericVnfWrapper
1442                                 .asBean(org.onap.aai.domain.yang.GenericVnf.class);
1443                 if (!aaiGenericVnfOp.isPresent()) {
1444                         return null;
1445                 }
1446
1447                 GenericVnf genericVnf = this.mapperLayer.mapAAIGenericVnfIntoGenericVnf(aaiGenericVnfOp.get());
1448
1449                 Optional<Relationships> relationshipsOp = aaiGenericVnfWrapper.getRelationships();
1450                 if (relationshipsOp.isPresent()) {
1451                         Relationships relationships = relationshipsOp.get();
1452                         this.mapPlatform(relationships.getByType(AAIObjectType.PLATFORM), genericVnf);
1453                         this.mapLineOfBusiness(relationships.getByType(AAIObjectType.LINE_OF_BUSINESS), genericVnf);
1454                         genericVnf.getVolumeGroups().addAll(mapVolumeGroups(relationships.getByType(AAIObjectType.VOLUME_GROUP)));
1455                         genericVnf.getInstanceGroups().addAll(mapInstanceGroups(relationships.getByType(AAIObjectType.INSTANCE_GROUP)));
1456                 }
1457
1458                 return genericVnf;
1459         }
1460
1461         protected List<InstanceGroup> mapInstanceGroups(List<AAIResultWrapper> instanceGroups) {
1462                 List<InstanceGroup> instanceGroupsList = new ArrayList<>();
1463                 for (AAIResultWrapper volumeGroupWrapper : instanceGroups) {
1464                         instanceGroupsList.add(this.mapInstanceGroup(volumeGroupWrapper));
1465                 }
1466                 return instanceGroupsList;
1467         }
1468
1469         protected InstanceGroup mapInstanceGroup(AAIResultWrapper instanceGroupWrapper) {
1470                 Optional<org.onap.aai.domain.yang.InstanceGroup> aaiInstanceGroupOp = instanceGroupWrapper
1471                                 .asBean(org.onap.aai.domain.yang.InstanceGroup.class);
1472                 org.onap.aai.domain.yang.InstanceGroup aaiInstanceGroup = null;
1473
1474                 if (!aaiInstanceGroupOp.isPresent()) {
1475                         return null;
1476                 }
1477
1478                 aaiInstanceGroup = aaiInstanceGroupOp.get();
1479                 InstanceGroup instanceGroup = this.mapperLayer.mapAAIInstanceGroupIntoInstanceGroup(aaiInstanceGroup);
1480                 instanceGroup.setModelInfoInstanceGroup(this.mapperLayer.mapCatalogInstanceGroupToInstanceGroup(null,
1481                                 this.bbInputSetupUtils.getCatalogInstanceGroup(aaiInstanceGroup.getModelVersionId())));
1482                 return instanceGroup;
1483         }
1484
1485         protected List<VolumeGroup> mapVolumeGroups(List<AAIResultWrapper> volumeGroups) {
1486                 List<VolumeGroup> volumeGroupsList = new ArrayList<>();
1487                 for (AAIResultWrapper volumeGroupWrapper : volumeGroups) {
1488                         volumeGroupsList.add(this.mapVolumeGroup(volumeGroupWrapper));
1489                 }
1490                 return volumeGroupsList;
1491         }
1492
1493         protected VolumeGroup mapVolumeGroup(AAIResultWrapper volumeGroupWrapper) {
1494                 Optional<org.onap.aai.domain.yang.VolumeGroup> aaiVolumeGroupOp = volumeGroupWrapper
1495                                 .asBean(org.onap.aai.domain.yang.VolumeGroup.class);
1496                 org.onap.aai.domain.yang.VolumeGroup aaiVolumeGroup = null;
1497
1498                 if (!aaiVolumeGroupOp.isPresent()) {
1499                         return null;
1500                 }
1501
1502                 aaiVolumeGroup = aaiVolumeGroupOp.get();
1503                 return this.mapperLayer.mapAAIVolumeGroup(aaiVolumeGroup);
1504         }
1505
1506         protected void mapLineOfBusiness(List<AAIResultWrapper> lineOfBusinesses, GenericVnf genericVnf) {
1507                 if (!lineOfBusinesses.isEmpty()) {
1508                         AAIResultWrapper lineOfBusinessWrapper = lineOfBusinesses.get(0);
1509                         Optional<org.onap.aai.domain.yang.LineOfBusiness> aaiLineOfBusinessOp = lineOfBusinessWrapper
1510                                         .asBean(org.onap.aai.domain.yang.LineOfBusiness.class);
1511                         org.onap.aai.domain.yang.LineOfBusiness aaiLineOfBusiness = null;
1512                         if (!aaiLineOfBusinessOp.isPresent()) {
1513                                 return;
1514                         }
1515                         aaiLineOfBusiness = aaiLineOfBusinessOp.get();
1516
1517                         LineOfBusiness lineOfBusiness = this.mapperLayer.mapAAILineOfBusiness(aaiLineOfBusiness);
1518                         genericVnf.setLineOfBusiness(lineOfBusiness);
1519                 }
1520         }
1521
1522         protected void mapPlatform(List<AAIResultWrapper> platforms, GenericVnf genericVnf) {
1523                 if (!platforms.isEmpty()) {
1524                         AAIResultWrapper platformWrapper = platforms.get(0);
1525                         Optional<org.onap.aai.domain.yang.Platform> aaiPlatformOp = platformWrapper
1526                                         .asBean(org.onap.aai.domain.yang.Platform.class);
1527                         org.onap.aai.domain.yang.Platform aaiPlatform = null;
1528                         if (!aaiPlatformOp.isPresent()) {
1529                                 return;
1530                         }
1531                         aaiPlatform = aaiPlatformOp.get();
1532
1533                         Platform platform = this.mapperLayer.mapAAIPlatform(aaiPlatform);
1534                         genericVnf.setPlatform(platform);
1535                 }
1536         }
1537
1538         protected void mapCollection(List<AAIResultWrapper> collections, ServiceInstance serviceInstance) {
1539                 if (!collections.isEmpty()) {
1540                         AAIResultWrapper collectionWrapper = collections.get(0);
1541                         Optional<org.onap.aai.domain.yang.Collection> aaiCollectionOp = collectionWrapper
1542                                         .asBean(org.onap.aai.domain.yang.Collection.class);
1543                         org.onap.aai.domain.yang.Collection aaiCollection = null;
1544                         if (!aaiCollectionOp.isPresent()) {
1545                                 return;
1546                         }
1547                         aaiCollection = aaiCollectionOp.get();
1548
1549                         Collection collection = this.mapperLayer.mapAAICollectionIntoCollection(aaiCollection);
1550                         NetworkCollectionResourceCustomization collectionResourceCust = 
1551                                         bbInputSetupUtils.getCatalogNetworkCollectionResourceCustByID(aaiCollection.getCollectionCustomizationId());
1552                         collection.setModelInfoCollection(mapperLayer.mapCatalogCollectionToCollection(collectionResourceCust, collectionResourceCust.getCollectionResource()));
1553                         Optional<Relationships> relationshipsOp = collectionWrapper.getRelationships();
1554                         Relationships relationships = null;
1555                         if (relationshipsOp.isPresent()) {
1556                                 relationships = relationshipsOp.get();
1557                         } else {
1558                                 serviceInstance.setCollection(collection);
1559                                 return;
1560                         }
1561                         List<InstanceGroup> instanceGroupsList = mapInstanceGroups(relationships.getByType(AAIObjectType.INSTANCE_GROUP));
1562                         if(!instanceGroupsList.isEmpty()) {
1563                                 collection.setInstanceGroup(instanceGroupsList.get(0));
1564                         }
1565                         serviceInstance.setCollection(collection);
1566                 }
1567         }
1568
1569         protected void mapL3Networks(List<AAIResourceUri> list, List<L3Network> l3Networks) {
1570                 for (AAIResourceUri aaiResourceUri : list) {
1571                         l3Networks.add(this.mapL3Network(aaiResourceUri));
1572                 }
1573         }
1574
1575         protected L3Network mapL3Network(AAIResourceUri aaiResourceUri) {
1576                 AAIResultWrapper aaiNetworkWrapper = this.bbInputSetupUtils.getAAIResourceDepthTwo(aaiResourceUri);
1577                 Optional<org.onap.aai.domain.yang.L3Network> aaiL3NetworkOp = aaiNetworkWrapper
1578                                 .asBean(org.onap.aai.domain.yang.L3Network.class);
1579                 org.onap.aai.domain.yang.L3Network aaiL3Network = null;
1580
1581                 if (!aaiL3NetworkOp.isPresent()) {
1582                         return null;
1583                 }
1584
1585                 aaiL3Network = aaiL3NetworkOp.get();
1586                 L3Network network = this.mapperLayer.mapAAIL3Network(aaiL3Network);
1587
1588                 Optional<Relationships> relationshipsOp = aaiNetworkWrapper.getRelationships();
1589                 if (relationshipsOp.isPresent()) {
1590                         Relationships relationships = relationshipsOp.get();
1591                         this.mapNetworkPolicies(relationships.getByType(AAIObjectType.NETWORK_POLICY),
1592                                         network.getNetworkPolicies());
1593                         mapRouteTableReferences(relationships.getByType(AAIObjectType.ROUTE_TABLE_REFERENCE),
1594                                         network.getContrailNetworkRouteTableReferences());
1595                 }
1596
1597                 return network;
1598         }
1599
1600         protected void mapNetworkPolicies(List<AAIResultWrapper> aaiNetworkPolicies, List<NetworkPolicy> networkPolicies) {
1601                 for (AAIResultWrapper networkPolicyWrapper : aaiNetworkPolicies) {
1602                         networkPolicies.add(this.mapNetworkPolicy(networkPolicyWrapper));
1603                 }
1604         }
1605
1606         protected NetworkPolicy mapNetworkPolicy(AAIResultWrapper networkPolicyWrapper) {
1607                 Optional<org.onap.aai.domain.yang.NetworkPolicy> aaiNetworkPolicyOp = networkPolicyWrapper
1608                                 .asBean(org.onap.aai.domain.yang.NetworkPolicy.class);
1609                 org.onap.aai.domain.yang.NetworkPolicy aaiNetworkPolicy = null;
1610
1611                 if (!aaiNetworkPolicyOp.isPresent()) {
1612                         return null;
1613                 }
1614
1615                 aaiNetworkPolicy = aaiNetworkPolicyOp.get();
1616                 return this.mapperLayer.mapAAINetworkPolicy(aaiNetworkPolicy);
1617         }
1618
1619         protected void mapRouteTableReferences(List<AAIResultWrapper> routeTableReferences,
1620                         List<RouteTableReference> contrailNetworkRouteTableReferences) {
1621                 for (AAIResultWrapper routeTableReferenceWrapper : routeTableReferences) {
1622                         contrailNetworkRouteTableReferences.add(this.mapRouteTableReference(routeTableReferenceWrapper));
1623                 }
1624         }
1625
1626         protected RouteTableReference mapRouteTableReference(AAIResultWrapper routeTableReferenceWrapper) {
1627                 Optional<org.onap.aai.domain.yang.RouteTableReference> aaiRouteTableReferenceOp = routeTableReferenceWrapper
1628                                 .asBean(org.onap.aai.domain.yang.RouteTableReference.class);
1629                 org.onap.aai.domain.yang.RouteTableReference aaiRouteTableReference = null;
1630
1631                 if (!aaiRouteTableReferenceOp.isPresent()) {
1632                         return null;
1633                 }
1634
1635                 aaiRouteTableReference = aaiRouteTableReferenceOp.get();
1636                 return this.mapperLayer.mapAAIRouteTableReferenceIntoRouteTableReference(aaiRouteTableReference);
1637         }
1638
1639         protected void mapOwningEntity(List<AAIResultWrapper> owningEntities, ServiceInstance serviceInstance) {
1640                 if (!owningEntities.isEmpty()) {
1641                         AAIResultWrapper owningEntityWrapper = owningEntities.get(0);
1642                         Optional<org.onap.aai.domain.yang.OwningEntity> aaiOwningEntityOp = owningEntityWrapper
1643                                         .asBean(org.onap.aai.domain.yang.OwningEntity.class);
1644                         org.onap.aai.domain.yang.OwningEntity aaiOwningEntity = null;
1645                         if (!aaiOwningEntityOp.isPresent()) {
1646                                 return;
1647                         }
1648                         aaiOwningEntity = aaiOwningEntityOp.get();
1649
1650                         OwningEntity owningEntity = this.mapperLayer.mapAAIOwningEntity(aaiOwningEntity);
1651                         serviceInstance.setOwningEntity(owningEntity);
1652                 }
1653         }
1654
1655         protected void mapProject(List<AAIResultWrapper> projects, ServiceInstance serviceInstance) {
1656                 if (!projects.isEmpty()) {
1657                         AAIResultWrapper projectWrapper = projects.get(0);
1658                         Optional<org.onap.aai.domain.yang.Project> aaiProjectOp = projectWrapper
1659                                         .asBean(org.onap.aai.domain.yang.Project.class);
1660                         org.onap.aai.domain.yang.Project aaiProject = null;
1661                         if (!aaiProjectOp.isPresent()) {
1662                                 return;
1663                         }
1664                         aaiProject = aaiProjectOp.get();
1665
1666                         Project project = this.mapperLayer.mapAAIProject(aaiProject);
1667                         serviceInstance.setProject(project);
1668                 }
1669         }
1670         protected Customer mapCustomer(String globalCustomerId, String subscriptionServiceType) {
1671                 org.onap.aai.domain.yang.Customer aaiCustomer = this.bbInputSetupUtils.getAAICustomer(globalCustomerId);
1672                 org.onap.aai.domain.yang.ServiceSubscription aaiServiceSubscription = this.bbInputSetupUtils
1673                                 .getAAIServiceSubscription(globalCustomerId, subscriptionServiceType);
1674                 Customer customer = this.mapperLayer.mapAAICustomer(aaiCustomer);
1675                 ServiceSubscription serviceSubscription = this.mapperLayer.mapAAIServiceSubscription(aaiServiceSubscription);
1676                 if(serviceSubscription != null){
1677                         customer.setServiceSubscription(serviceSubscription);
1678                 }
1679                 return customer;
1680         }
1681
1682
1683 }