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