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