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