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