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