76b24caf09b5810f4b1c42fc6ebb03573e58083a
[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.NetworkCollectionResourceCustomization;
76 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
77 import org.onap.so.db.catalog.beans.OrchestrationStatus;
78 import org.onap.so.db.catalog.beans.Service;
79 import org.onap.so.db.catalog.beans.VfModuleCustomization;
80 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
81 import org.onap.so.db.catalog.beans.VnfVfmoduleCvnfcConfigurationCustomization;
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                 VnfVfmoduleCvnfcConfigurationCustomization vnfVfmoduleCvnfcConfigurationCustomization = 
417                                 findVnfVfmoduleCvnfcConfigurationCustomization(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(configurationResourceKeys.getVnfResourceCustomizationUUID(),
425                                         configurationResourceKeys.getVfModuleCustomizationUUID(), configurationResourceKeys.getCvnfcCustomizationUUID());
426                         if (vnfVfmoduleCvnfcConfigurationCustomization != null){
427                                 configuration.setModelInfoConfiguration(this.mapperLayer.mapCatalogConfigurationToConfiguration(vnfVfmoduleCvnfcConfigurationCustomization));
428                         }
429                 }
430         }
431
432         protected VnfVfmoduleCvnfcConfigurationCustomization findVnfVfmoduleCvnfcConfigurationCustomization(String vfModuleCustomizationUUID, 
433                         String vnfResourceCustomizationUUID, String cvnfcCustomizationUUID, ConfigurationResourceCustomization configurationResourceCustomization) {
434                 if(configurationResourceCustomization != null && configurationResourceCustomization.getConfigurationResource() != null)
435                         for(VnfVfmoduleCvnfcConfigurationCustomization vnfVfmoduleCvnfcConfigurationCustomization : 
436                                 configurationResourceCustomization.getConfigurationResource().getVnfVfmoduleCvnfcConfigurationCustomization()) {
437                                 if(vnfVfmoduleCvnfcConfigurationCustomization.getVfModuleCustomization().getModelCustomizationUUID().equalsIgnoreCase(vfModuleCustomizationUUID)
438                                                 && vnfVfmoduleCvnfcConfigurationCustomization.getVnfResourceCustomization().getModelCustomizationUUID().equalsIgnoreCase(vnfResourceCustomizationUUID)
439                                                 && vnfVfmoduleCvnfcConfigurationCustomization.getCvnfcCustomization().getModelCustomizationUUID().equalsIgnoreCase(cvnfcCustomizationUUID)) {
440                                         return vnfVfmoduleCvnfcConfigurationCustomization;
441                                 }
442                         }
443                 return null;
444         }
445
446         protected ConfigurationResourceCustomization findConfigurationResourceCustomization(ModelInfo modelInfo, Service service) {
447                 for (ConfigurationResourceCustomization resourceCust : service.getConfigurationCustomizations()) {
448                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(modelInfo.getModelCustomizationUuid())) {
449                                 return resourceCust;
450                         }
451                 }
452                 return null;
453         }
454         
455         protected VnfVfmoduleCvnfcConfigurationCustomization findVnfVfmoduleCvnfcConfigurationCustomization(String vnfResourceCustomizationUUID,
456                         String vfModuleCustomizationUUID, String cvnfcCustomizationUUID) {
457                 return bbInputSetupUtils.getVnfVfmoduleCvnfcConfigurationCustomizationByVnfCustomizationIdnAndVfModuleCustomizationIdAndCvnfcCustomizationId(vnfResourceCustomizationUUID,
458                                 vfModuleCustomizationUUID, cvnfcCustomizationUUID);
459
460         }
461
462         protected void populateVfModule(ModelInfo modelInfo, Service service, String bbName,
463                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap, String resourceId,
464                         RelatedInstanceList[] relatedInstanceList, String instanceName, List<Map<String, String>> instanceParams, CloudConfiguration cloudConfiguration) throws Exception {
465                 String vnfModelCustomizationUUID = null;
466                 if (relatedInstanceList != null) {
467                         for (RelatedInstanceList relatedInstList : relatedInstanceList) {
468                                 RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
469                                 if (relatedInstance.getModelInfo().getModelType().equals(ModelType.vnf)) {
470                                         vnfModelCustomizationUUID = relatedInstance.getModelInfo().getModelCustomizationId();
471                                 }
472                                 if (relatedInstance.getModelInfo().getModelType().equals(ModelType.volumeGroup)) {
473                                         lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, relatedInstance.getInstanceId());
474                                 }
475                         }
476                 }
477                 GenericVnf vnf = null;
478                 for (GenericVnf tempVnf : serviceInstance.getVnfs()) {
479                         if (tempVnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
480                                 vnf = tempVnf;
481                                 vnfModelCustomizationUUID = this.bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId())
482                                                 .getModelCustomizationId();
483                                 ModelInfo vnfModelInfo = new ModelInfo();
484                                 vnfModelInfo.setModelCustomizationUuid(vnfModelCustomizationUUID);
485                                 this.mapCatalogVnf(tempVnf, vnfModelInfo, service);
486                                 Optional<String> volumeGroupIdOp = getVolumeGroupIdRelatedToVfModule(tempVnf, modelInfo, cloudConfiguration.getCloudOwner(), 
487                                                 cloudConfiguration.getLcpCloudRegionId(), lookupKeyMap);
488                                 if(volumeGroupIdOp.isPresent()) {
489                                         lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, volumeGroupIdOp.get());
490                                 }
491                                 break;
492                         }
493                 }
494                 if (vnf != null) {
495                         VfModule vfModule = null;
496                         for (VfModule vfModuleTemp : vnf.getVfModules()) {
497                                 if (lookupKeyMap.get(ResourceKey.VF_MODULE_ID) != null
498                                                 && vfModuleTemp.getVfModuleId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.VF_MODULE_ID))) {
499                                         vfModule = vfModuleTemp;
500                                 }
501                                 String vfModuleCustId = bbInputSetupUtils.getAAIVfModule(vnf.getVnfId(), vfModuleTemp.getVfModuleId()).getModelCustomizationId();
502                                 ModelInfo modelInfoVfModule = new ModelInfo();
503                                 modelInfoVfModule.setModelCustomizationId(vfModuleCustId);
504                                 mapCatalogVfModule(vfModuleTemp, modelInfoVfModule, service, vnfModelCustomizationUUID);
505                         }
506                         if (vfModule == null && bbName.equalsIgnoreCase(AssignFlows.VF_MODULE.toString())) {
507                                 vfModule = createVfModule(lookupKeyMap,
508                                                 resourceId, instanceName, instanceParams);
509                                 vnf.getVfModules().add(vfModule);
510                                 mapCatalogVfModule(vfModule, modelInfo, service, vnfModelCustomizationUUID);
511                         }
512                 } else {
513                         logger.debug("Related VNF instance Id not found: {}", lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID));
514                         throw new Exception("Could not find relevant information for related VNF");
515                 }
516         }
517         
518         protected Optional<String> getVolumeGroupIdRelatedToVfModule(GenericVnf vnf, ModelInfo modelInfo, 
519                         String cloudOwner, String cloudRegionId, Map<ResourceKey, String> lookupKeyMap) {
520                 if (lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID) == null) {
521                         for(VolumeGroup volumeGroup : vnf.getVolumeGroups()) {
522                                 String volumeGroupCustId = 
523                                                 bbInputSetupUtils.getAAIVolumeGroup(cloudOwner, 
524                                                                 cloudRegionId, volumeGroup.getVolumeGroupId()).getModelCustomizationId();
525                                 if(modelInfo.getModelCustomizationId().equalsIgnoreCase(volumeGroupCustId)) {
526                                         return Optional.of(volumeGroup.getVolumeGroupId());
527                                 }
528                         }
529                 }
530                 return Optional.empty();
531         }
532
533         protected void mapCatalogVfModule(VfModule vfModule, ModelInfo modelInfo, Service service,
534                         String vnfModelCustomizationUUID) {
535                 if(modelInfo.getModelCustomizationUuid() != null) {
536                         modelInfo.setModelCustomizationId(modelInfo.getModelCustomizationUuid());
537                 }
538                 VnfResourceCustomization vnfResourceCustomization = null;
539                 for (VnfResourceCustomization resourceCust : service.getVnfCustomizations()) {
540                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(vnfModelCustomizationUUID)) {
541                                 vnfResourceCustomization = resourceCust;
542                                 break;
543                         }
544                 }
545                 if (vnfResourceCustomization != null) {
546                         VfModuleCustomization vfResourceCustomization = vnfResourceCustomization.getVfModuleCustomizations()
547                                         .stream() // Convert to steam
548                                         .filter(x -> modelInfo.getModelCustomizationId().equalsIgnoreCase(x.getModelCustomizationUUID()))// find
549                                                                                                                                                                                                                                                 // what
550                                                                                                                                                                                                                                                 // we
551                                                                                                                                                                                                                                                 // want
552                                         .findAny() // If 'findAny' then return found
553                                         .orElse(null);
554                         if (vfResourceCustomization != null) {
555                                 vfModule.setModelInfoVfModule(this.mapperLayer.mapCatalogVfModuleToVfModule(vfResourceCustomization));
556                         }
557                 }
558         }
559
560         protected VfModule createVfModule(Map<ResourceKey, String> lookupKeyMap, String vfModuleId, String instanceName, List<Map<String, String>> instanceParams) {
561                 lookupKeyMap.put(ResourceKey.VF_MODULE_ID, vfModuleId);
562                 VfModule vfModule = new VfModule();
563                 vfModule.setVfModuleId(vfModuleId);
564                 vfModule.setVfModuleName(instanceName);
565                 vfModule.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
566                 if(instanceParams != null) {
567                         for(Map<String, String> params : instanceParams) {
568                                 vfModule.getCloudParams().putAll(params);
569                         }
570                 }
571                 return vfModule;
572         }
573
574         protected void populateVolumeGroup(ModelInfo modelInfo, Service service, String bbName,
575                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap, String resourceId,
576                         RelatedInstanceList[] relatedInstanceList, String instanceName, String vnfType, List<Map<String, String>> instanceParams) throws Exception {
577                 VolumeGroup volumeGroup = null;
578                 GenericVnf vnf = null;
579                 String vnfModelCustomizationUUID = null;
580                 String generatedVnfType = vnfType;
581                 if (generatedVnfType == null || generatedVnfType.isEmpty()) {
582                         generatedVnfType = service.getModelName() + "/" + modelInfo.getModelCustomizationName();
583                 }
584                 if (relatedInstanceList != null) {
585                         for (RelatedInstanceList relatedInstList : relatedInstanceList) {
586                                 RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
587                                 if (relatedInstance.getModelInfo().getModelType().equals(ModelType.vnf)) {
588                                         vnfModelCustomizationUUID = relatedInstance.getModelInfo().getModelCustomizationUuid();
589                                         break;
590                                 }
591                         }
592                 }
593                 for (GenericVnf tempVnf : serviceInstance.getVnfs()) {
594                         if (tempVnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
595                                 vnf = tempVnf;
596                                 vnfModelCustomizationUUID = bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId())
597                                                 .getModelCustomizationId();
598                                 ModelInfo vnfModelInfo = new ModelInfo();
599                                 vnfModelInfo.setModelCustomizationUuid(vnfModelCustomizationUUID);
600                                 mapCatalogVnf(tempVnf, vnfModelInfo, service);
601                                 break;
602                         }
603                 }
604                 if (vnf != null && vnfModelCustomizationUUID != null) {
605                         for (VolumeGroup volumeGroupTemp : vnf.getVolumeGroups()) {
606                                 if (lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID) != null && volumeGroupTemp.getVolumeGroupId()
607                                                 .equalsIgnoreCase(lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID))) {
608                                         volumeGroup = volumeGroupTemp;
609                                         break;
610                                 }
611                         }
612                         if (volumeGroup == null && bbName.equalsIgnoreCase(AssignFlows.VOLUME_GROUP.toString())) {
613                                 volumeGroup = createVolumeGroup(lookupKeyMap, resourceId, instanceName, generatedVnfType, instanceParams);
614                                 vnf.getVolumeGroups().add(volumeGroup);
615                         }
616                         if(volumeGroup != null) {
617                                 mapCatalogVolumeGroup(volumeGroup, modelInfo, service, vnfModelCustomizationUUID);
618                         }
619                 } else {
620                         logger.debug("Related VNF instance Id not found: {}", lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID));
621                         throw new Exception("Could not find relevant information for related VNF");
622                 }
623         }
624
625         protected VolumeGroup createVolumeGroup(Map<ResourceKey, String> lookupKeyMap, String volumeGroupId, String instanceName, String vnfType, List<Map<String, String>> instanceParams) {
626                 lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, volumeGroupId);
627                 VolumeGroup volumeGroup = new VolumeGroup();
628                 volumeGroup.setVolumeGroupId(volumeGroupId);
629                 volumeGroup.setVolumeGroupName(instanceName);
630                 volumeGroup.setVnfType(vnfType);
631                 volumeGroup.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
632                 if(instanceParams != null) {
633                         for(Map<String, String> params : instanceParams) {
634                                 volumeGroup.getCloudParams().putAll(params);
635                         }
636                 }
637                 return volumeGroup;
638         }
639
640         protected void mapCatalogVolumeGroup(VolumeGroup volumeGroup, ModelInfo modelInfo, Service service,
641                         String vnfModelCustomizationUUID) {
642                 VfModuleCustomization vfResourceCustomization = getVfResourceCustomization(modelInfo, service,
643                                 vnfModelCustomizationUUID);
644                 if (vfResourceCustomization != null) {
645                         volumeGroup.setModelInfoVfModule(this.mapperLayer.mapCatalogVfModuleToVfModule(vfResourceCustomization));
646                 }
647         }
648
649         protected VfModuleCustomization getVfResourceCustomization(ModelInfo modelInfo, Service service,
650                         String vnfModelCustomizationUUID) {
651                 VnfResourceCustomization vnfResourceCustomization = null;
652                 for (VnfResourceCustomization resourceCust : service.getVnfCustomizations()) {
653                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(vnfModelCustomizationUUID)) {
654                                 vnfResourceCustomization = resourceCust;
655                                 break;
656                         }
657                 }
658                 if (vnfResourceCustomization != null) {
659                         for (VfModuleCustomization vfResourceCust : vnfResourceCustomization.getVfModuleCustomizations()) {
660                                 if (vfResourceCust.getModelCustomizationUUID()
661                                                 .equalsIgnoreCase(modelInfo.getModelCustomizationUuid())) {
662                                         return vfResourceCust;
663                                 }
664                         }
665
666                 }
667                 return null;
668         }
669
670         protected void populateGenericVnf(ModelInfo modelInfo, String instanceName,
671                         org.onap.so.serviceinstancebeans.Platform platform,
672                         org.onap.so.serviceinstancebeans.LineOfBusiness lineOfBusiness, Service service, String bbName,
673                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap,
674                         RelatedInstanceList[] relatedInstanceList, String resourceId, String vnfType, List<Map<String, String>> instanceParams,
675                         String productFamilyId) {
676                 GenericVnf vnf = null;
677                 ModelInfo instanceGroupModelInfo = null;
678                 String instanceGroupId = null;
679                 String generatedVnfType = vnfType;
680                 if (generatedVnfType == null || generatedVnfType.isEmpty()) {
681                         generatedVnfType = service.getModelName() + "/" + modelInfo.getModelCustomizationName();
682                 }
683                 if (relatedInstanceList != null) {
684                         for (RelatedInstanceList relatedInstList : relatedInstanceList) {
685                                 RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
686                                 if (relatedInstance.getModelInfo().getModelType().equals(ModelType.networkInstanceGroup)) {
687                                         instanceGroupModelInfo = relatedInstance.getModelInfo();
688                                         instanceGroupId = relatedInstance.getInstanceId();
689                                 }
690                         }
691                 }
692                 for (GenericVnf vnfTemp : serviceInstance.getVnfs()) {
693                         if (lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID) != null
694                                         && vnfTemp.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
695                                 String vnfModelCustId = bbInputSetupUtils.getAAIGenericVnf(vnfTemp.getVnfId()).getModelCustomizationId();
696                                 modelInfo.setModelCustomizationUuid(vnfModelCustId);
697                                 vnf = vnfTemp;
698                                 break;
699                         }
700                 }
701                 if (vnf == null && bbName.equalsIgnoreCase(AssignFlows.VNF.toString())) {
702                         vnf = createGenericVnf(lookupKeyMap, instanceName, platform, lineOfBusiness,
703                                         resourceId, generatedVnfType, instanceParams, productFamilyId);
704                         serviceInstance.getVnfs().add(vnf);
705                         mapVnfcCollectionInstanceGroup(vnf, modelInfo, service);
706                 }
707                 if(vnf != null) {
708                         mapCatalogVnf(vnf, modelInfo, service);
709                         if (instanceGroupId != null && instanceGroupModelInfo != null
710                                         && instanceGroupModelInfo.getModelType().equals(ModelType.networkInstanceGroup)
711                                         && !instanceGroupInList(vnf, instanceGroupId)) {
712                                 mapNetworkCollectionInstanceGroup(vnf, instanceGroupId);
713                         }
714                 }
715         }
716
717         protected boolean instanceGroupInList(GenericVnf vnf, String instanceGroupId) {
718                 for(InstanceGroup instanceGroup : vnf.getInstanceGroups()) {
719                         if(instanceGroup.getId() != null && instanceGroup.getId().equalsIgnoreCase(instanceGroupId)) {
720                                 return true;
721                         }
722                 }
723                 return false;
724         }
725
726         protected void mapVnfcCollectionInstanceGroup(GenericVnf genericVnf, ModelInfo modelInfo, Service service) {
727                 VnfResourceCustomization vnfResourceCustomization = getVnfResourceCustomizationFromService(modelInfo, service);
728                 if(vnfResourceCustomization != null) {
729                 List<VnfcInstanceGroupCustomization> vnfcInstanceGroups = vnfResourceCustomization
730                                 .getVnfcInstanceGroupCustomizations();
731                 for (VnfcInstanceGroupCustomization vnfcInstanceGroupCust : vnfcInstanceGroups) {
732                         InstanceGroup instanceGroup = this.createInstanceGroup();
733                         org.onap.so.db.catalog.beans.InstanceGroup catalogInstanceGroup = bbInputSetupUtils.getCatalogInstanceGroup(vnfcInstanceGroupCust.getModelUUID());
734                         instanceGroup.setModelInfoInstanceGroup(this.mapperLayer
735                                         .mapCatalogInstanceGroupToInstanceGroup(null, catalogInstanceGroup));
736                         instanceGroup.getModelInfoInstanceGroup().setFunction(vnfcInstanceGroupCust.getFunction());
737                         instanceGroup.setDescription(vnfcInstanceGroupCust.getDescription());
738                         genericVnf.getInstanceGroups().add(instanceGroup);
739                 }
740         }
741         }
742
743         protected void mapNetworkCollectionInstanceGroup(GenericVnf genericVnf, String instanceGroupId) {
744                 org.onap.aai.domain.yang.InstanceGroup aaiInstanceGroup = bbInputSetupUtils
745                                 .getAAIInstanceGroup(instanceGroupId);
746                 InstanceGroup instanceGroup = this.mapperLayer.mapAAIInstanceGroupIntoInstanceGroup(aaiInstanceGroup);
747                 instanceGroup.setModelInfoInstanceGroup(this.mapperLayer.mapCatalogInstanceGroupToInstanceGroup(
748                                 null, this.bbInputSetupUtils.getCatalogInstanceGroup(aaiInstanceGroup.getModelVersionId())));
749                 genericVnf.getInstanceGroups().add(instanceGroup);
750         }
751
752         protected GenericVnf createGenericVnf(Map<ResourceKey, String> lookupKeyMap, String instanceName,
753                         org.onap.so.serviceinstancebeans.Platform platform,
754                         org.onap.so.serviceinstancebeans.LineOfBusiness lineOfBusiness, String vnfId, String vnfType, List<Map<String, String>> instanceParams,
755                         String productFamilyId) {
756                 lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, vnfId);
757                 GenericVnf genericVnf = new GenericVnf();
758                 genericVnf.setVnfId(vnfId);
759                 genericVnf.setVnfName(instanceName);
760                 genericVnf.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
761                 genericVnf.setVnfType(vnfType);
762                 genericVnf.setProvStatus(PREPROV);
763                 genericVnf.setServiceId(productFamilyId);
764                 if (platform != null) {
765                         genericVnf.setPlatform(this.mapperLayer.mapRequestPlatform(platform));
766                 }
767                 if (lineOfBusiness != null) {
768                         genericVnf.setLineOfBusiness(this.mapperLayer.mapRequestLineOfBusiness(lineOfBusiness));
769                 }
770                 if(instanceParams != null) {
771                         for(Map<String, String> params : instanceParams) {
772                                 genericVnf.getCloudParams().putAll(params);
773                         }
774                 }
775                 return genericVnf;
776         }
777
778         protected void mapCatalogVnf(GenericVnf genericVnf, ModelInfo modelInfo, Service service) {
779                 VnfResourceCustomization vnfResourceCustomization = getVnfResourceCustomizationFromService(modelInfo, service);
780                 if (vnfResourceCustomization != null) {
781                         genericVnf.setModelInfoGenericVnf(this.mapperLayer.mapCatalogVnfToVnf(vnfResourceCustomization));
782                 }
783         }
784
785         protected VnfResourceCustomization getVnfResourceCustomizationFromService(ModelInfo modelInfo, Service service) {
786                 VnfResourceCustomization vnfResourceCustomization = null;
787                 for (VnfResourceCustomization resourceCust : service.getVnfCustomizations()) {
788                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(modelInfo.getModelCustomizationUuid())) {
789                                 vnfResourceCustomization = resourceCust;
790                                 break;
791                         }
792                 }
793                 return vnfResourceCustomization;
794         }
795
796         protected void populateL3Network(String instanceName, ModelInfo modelInfo, Service service, String bbName,
797                         ServiceInstance serviceInstance, Map<ResourceKey, String> lookupKeyMap, String resourceId, List<Map<String, String>> instanceParams) {
798                 L3Network network = null;
799                 for (L3Network networkTemp : serviceInstance.getNetworks()) {
800                         if (lookupKeyMap.get(ResourceKey.NETWORK_ID) != null
801                                         && networkTemp.getNetworkId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.NETWORK_ID))) {
802                                 network = networkTemp;
803                                 break;
804                         }
805                 }
806                 if (network == null
807                                 && (bbName.equalsIgnoreCase(AssignFlows.NETWORK_A_LA_CARTE.toString()) 
808                                                 || bbName.equalsIgnoreCase(AssignFlows.NETWORK_MACRO.toString()))) {
809                         network = createNetwork(lookupKeyMap, instanceName, resourceId, instanceParams);
810                         serviceInstance.getNetworks().add(network);
811                 }
812                 if(network != null) {
813                         mapCatalogNetwork(network, modelInfo, service);
814                 }
815         }
816
817         protected L3Network createNetwork(Map<ResourceKey, String> lookupKeyMap, String instanceName,
818                         String networkId, List<Map<String, String>> instanceParams) {
819                 lookupKeyMap.put(ResourceKey.NETWORK_ID, networkId);
820                 L3Network network = new L3Network();
821                 network.setNetworkId(networkId);
822                 network.setNetworkName(instanceName);
823                 network.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
824                 if(instanceParams != null) {
825                         for(Map<String, String> params : instanceParams) {
826                                 network.getCloudParams().putAll(params);
827                         }
828                 }
829                 return network;
830         }
831
832         protected void mapCatalogNetwork(L3Network network, ModelInfo modelInfo, Service service) {
833                 NetworkResourceCustomization networkResourceCustomization = null;
834                 for (NetworkResourceCustomization resourceCust : service.getNetworkCustomizations()) {
835                         if (resourceCust.getModelCustomizationUUID().equalsIgnoreCase(modelInfo.getModelCustomizationUuid())) {
836                                 networkResourceCustomization = resourceCust;
837                                 break;
838                         }
839                 }
840                 if (networkResourceCustomization != null) {
841                         network.setModelInfoNetwork(this.mapperLayer.mapCatalogNetworkToNetwork(networkResourceCustomization));
842                 }
843         }
844
845         protected GeneralBuildingBlock getGBBALaCarteService(ExecuteBuildingBlock executeBB, RequestDetails requestDetails,
846                         Map<ResourceKey, String> lookupKeyMap, String requestAction, String resourceId) throws Exception {
847                 Customer customer = getCustomerAndServiceSubscription(requestDetails, resourceId);
848                 if (customer != null) {
849                         Project project = null;
850                         OwningEntity owningEntity = null;
851
852                         if (requestDetails.getProject() != null)
853                                 project = mapperLayer.mapRequestProject(requestDetails.getProject());
854                         if (requestDetails.getOwningEntity() != null)
855                                 owningEntity = mapperLayer.mapRequestOwningEntity(requestDetails.getOwningEntity());
856
857
858                         Service service = bbInputSetupUtils
859                                         .getCatalogServiceByModelUUID(requestDetails.getModelInfo().getModelVersionId());
860                         if (service == null) {
861                                 service = bbInputSetupUtils.getCatalogServiceByModelVersionAndModelInvariantUUID(
862                                                 requestDetails.getModelInfo().getModelVersion(),
863                                                 requestDetails.getModelInfo().getModelInvariantId());
864                         if(service == null) {
865                                         throw new Exception("Could not find service for model version Id: "
866                                                         + requestDetails.getModelInfo().getModelVersionId() + " and for model invariant Id: "
867                                                         + requestDetails.getModelInfo().getModelInvariantId());
868                                 }
869                         }
870                         ServiceInstance serviceInstance = this.getALaCarteServiceInstance(service, requestDetails, customer,
871                                         project, owningEntity, lookupKeyMap, resourceId, Boolean.TRUE.equals(executeBB.isaLaCarte()),
872                                         executeBB.getBuildingBlock().getBpmnFlowName());
873                         return this.populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, customer);
874                 } else {
875                         throw new Exception("Could not find customer");
876                 }
877         }
878
879         protected Customer getCustomerAndServiceSubscription(RequestDetails requestDetails, String resourceId) {
880                 Customer customer;
881                 if (requestDetails.getSubscriberInfo() != null) {
882                         customer = this.getCustomerFromRequest(requestDetails);
883                 } else {
884                         customer = this.getCustomerFromURI(resourceId);
885                 }
886                 if (customer != null) {
887                         ServiceSubscription serviceSubscription = null;
888                         serviceSubscription = getServiceSubscription(requestDetails, customer);
889                         if (serviceSubscription == null) {
890                                 serviceSubscription = getServiceSubscriptionFromURI(resourceId, customer);
891                         }
892                         customer.setServiceSubscription(serviceSubscription);
893                         return customer;
894                 } else {
895                         return null;
896                 }
897         }
898
899         protected ServiceSubscription getServiceSubscriptionFromURI(String resourceId, Customer customer) {
900                 Map<String, String> uriKeys = bbInputSetupUtils.getURIKeysFromServiceInstance(resourceId);
901                 String subscriptionServiceType = uriKeys.get("service-type");
902                 org.onap.aai.domain.yang.ServiceSubscription serviceSubscriptionAAI = bbInputSetupUtils
903                                 .getAAIServiceSubscription(customer.getGlobalCustomerId(), subscriptionServiceType);
904                 if (serviceSubscriptionAAI != null) {
905                         return mapperLayer.mapAAIServiceSubscription(serviceSubscriptionAAI);
906                 } else {
907                         return null;
908                 }
909         }
910
911         protected Customer getCustomerFromURI(String resourceId) {
912                 Map<String, String> uriKeys = bbInputSetupUtils.getURIKeysFromServiceInstance(resourceId);
913                 String globalCustomerId = uriKeys.get("global-customer-id");
914                 org.onap.aai.domain.yang.Customer customerAAI = this.bbInputSetupUtils.getAAICustomer(globalCustomerId);
915                 if (customerAAI != null) {
916                         return mapperLayer.mapAAICustomer(customerAAI);
917                 } else {
918                         return null;
919                 }
920         }
921
922         protected GeneralBuildingBlock populateGBBWithSIAndAdditionalInfo(RequestDetails requestDetails,
923                         ServiceInstance serviceInstance, ExecuteBuildingBlock executeBB, String requestAction, Customer customer) throws Exception {
924                 GeneralBuildingBlock outputBB = new GeneralBuildingBlock();
925                 OrchestrationContext orchContext = mapperLayer.mapOrchestrationContext(requestDetails);
926                 RequestContext requestContext = mapperLayer.mapRequestContext(requestDetails);
927                 requestContext.setAction(requestAction);
928                 requestContext.setMsoRequestId(executeBB.getRequestId());
929                 org.onap.aai.domain.yang.CloudRegion aaiCloudRegion = bbInputSetupUtils
930                                 .getCloudRegion(requestDetails.getCloudConfiguration());
931                 CloudRegion cloudRegion = mapperLayer.mapCloudRegion(requestDetails.getCloudConfiguration(), aaiCloudRegion);
932                 Tenant tenant = getTenant(requestDetails.getCloudConfiguration(), aaiCloudRegion);
933                 outputBB.setOrchContext(orchContext);
934                 outputBB.setRequestContext(requestContext);
935                 outputBB.setCloudRegion(cloudRegion);
936                 outputBB.setTenant(tenant);
937                 if(customer == null){
938                         Map<String, String> uriKeys = bbInputSetupUtils.getURIKeysFromServiceInstance(serviceInstance.getServiceInstanceId());
939                         String globalCustomerId = uriKeys.get("global-customer-id");
940                         String subscriptionServiceType = uriKeys.get("service-type");
941                         customer = mapCustomer(globalCustomerId, subscriptionServiceType);
942                 }
943                 outputBB.setServiceInstance(serviceInstance);
944                 if (customer.getServiceSubscription() != null) {
945                         customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
946                 }
947                 outputBB.setCustomer(customer);
948                 return outputBB;
949         }
950
951         protected Tenant getTenant(CloudConfiguration cloudConfiguration, org.onap.aai.domain.yang.CloudRegion aaiCloudRegion) throws Exception {
952                 Tenant tenant = new Tenant();
953                 if(cloudConfiguration != null && cloudConfiguration.getTenantId() != null 
954                                 && aaiCloudRegion != null && aaiCloudRegion.getTenants() != null) {
955                         for(org.onap.aai.domain.yang.Tenant aaiTenant : aaiCloudRegion.getTenants().getTenant()) {
956                                 if(aaiTenant.getTenantId().equalsIgnoreCase(cloudConfiguration.getTenantId())) {
957                                         tenant = mapperLayer.mapTenant(aaiTenant);
958                                 }
959                         }
960                         if(tenant.getTenantId() == null || tenant.getTenantName() == null) { 
961                                 throw new Exception("Invalid tenant information retrieved: tenantId = " + tenant.getTenantId() 
962                                 + " tenantName = " + tenant.getTenantName()); 
963                         }
964                 }
965                 return tenant;
966         }
967
968         protected ServiceSubscription getServiceSubscription(RequestDetails requestDetails, Customer customer) {
969                 org.onap.aai.domain.yang.ServiceSubscription aaiServiceSubscription = bbInputSetupUtils
970                                 .getAAIServiceSubscription(customer.getGlobalCustomerId(),
971                                                 requestDetails.getRequestParameters().getSubscriptionServiceType());
972                 if (aaiServiceSubscription != null) {
973                 return mapperLayer.mapAAIServiceSubscription(aaiServiceSubscription);
974                 } else {
975                         return null;
976                 }
977         }
978
979         protected Customer getCustomerFromRequest(RequestDetails requestDetails) {
980                 org.onap.aai.domain.yang.Customer aaiCustomer = bbInputSetupUtils
981                                 .getAAICustomer(requestDetails.getSubscriberInfo().getGlobalSubscriberId());
982                 if (aaiCustomer != null) {
983                 return mapperLayer.mapAAICustomer(aaiCustomer);
984                 } else {
985                         return null;
986                 }
987         }
988
989         protected ServiceInstance getALaCarteServiceInstance(Service service, RequestDetails requestDetails,
990                         Customer customer, Project project, OwningEntity owningEntity, Map<ResourceKey, String> lookupKeyMap,
991                         String serviceInstanceId, boolean aLaCarte, String bbName) throws Exception {
992                 ServiceInstance serviceInstance = this.getServiceInstanceHelper(requestDetails, customer, project, owningEntity,
993                                 lookupKeyMap, serviceInstanceId, aLaCarte, service, bbName);
994                 org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI = null;
995                 if(customer != null && customer.getServiceSubscription() != null) {
996                         serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceByIdAndCustomer(customer.getGlobalCustomerId(), 
997                                         customer.getServiceSubscription().getServiceType(), serviceInstanceId);
998                 } else {
999                         serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
1000                 }
1001                 if (serviceInstanceAAI != null
1002                                 && !serviceInstanceAAI.getModelVersionId().equalsIgnoreCase(service.getModelUUID())) {
1003                         Service tempService = this.bbInputSetupUtils
1004                                         .getCatalogServiceByModelUUID(serviceInstanceAAI.getModelVersionId());
1005                         if (tempService != null) {
1006                                 serviceInstance
1007                                                 .setModelInfoServiceInstance(mapperLayer.mapCatalogServiceIntoServiceInstance(tempService));
1008                                 return serviceInstance;
1009                         } else {
1010                                 throw new Exception(
1011                                                 "Could not find model of existing SI. Service Instance in AAI already exists with different model version id: "
1012                                                                 + serviceInstanceAAI.getModelVersionId());
1013                         }
1014                 }
1015                 serviceInstance.setModelInfoServiceInstance(mapperLayer.mapCatalogServiceIntoServiceInstance(service));
1016                 return serviceInstance;
1017         }
1018
1019         protected GeneralBuildingBlock getGBBMacro(ExecuteBuildingBlock executeBB, RequestDetails requestDetails,
1020                         Map<ResourceKey, String> lookupKeyMap, String requestAction, String resourceId, String vnfType)
1021                         throws Exception {
1022                 String bbName = executeBB.getBuildingBlock().getBpmnFlowName();
1023                 String key = executeBB.getBuildingBlock().getKey();
1024                 
1025                 if (requestAction.equalsIgnoreCase("deleteInstance")
1026                                 || requestAction.equalsIgnoreCase("unassignInstance")
1027                                 || requestAction.equalsIgnoreCase("activateInstance")
1028                                 || requestAction.equalsIgnoreCase("activateFabricConfiguration")
1029                                 || requestAction.equalsIgnoreCase("recreateInstance")
1030                                 || requestAction.equalsIgnoreCase("replaceInstance")) {
1031                         return getGBBMacroExistingService(executeBB, lookupKeyMap, bbName, requestAction,
1032                                         requestDetails.getCloudConfiguration());
1033                 }
1034
1035                 String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
1036                 GeneralBuildingBlock gBB = this.getGBBALaCarteService(executeBB, requestDetails, lookupKeyMap, requestAction,
1037                                 serviceInstanceId);
1038                 RequestParameters requestParams = requestDetails.getRequestParameters();
1039                 Service service = null;
1040                 if (gBB != null && gBB.getServiceInstance() != null
1041                                 && gBB.getServiceInstance().getModelInfoServiceInstance() != null
1042                                 && gBB.getServiceInstance().getModelInfoServiceInstance().getModelUuid() != null) {
1043                         service = bbInputSetupUtils.getCatalogServiceByModelUUID(
1044                                         gBB.getServiceInstance().getModelInfoServiceInstance().getModelUuid());
1045                 } else {
1046                         throw new Exception("Could not get service instance for macro request");
1047                 }
1048                 if (requestParams != null && requestParams.getUserParams() != null) {
1049                         for(Map<String, Object> userParams : requestParams.getUserParams()) {
1050                                 if(userParams.containsKey("service")) {
1051                                         String input = mapper.writeValueAsString(userParams.get("service"));
1052                                         return getGBBMacroUserParams(executeBB, requestDetails, lookupKeyMap, vnfType, bbName, key, gBB,
1053                                                         requestParams, service, input);
1054                                 }
1055                         }
1056                 }
1057                 if (requestAction.equalsIgnoreCase("deactivateInstance")) {
1058                         return gBB;
1059                 } else if (requestAction.equalsIgnoreCase("createInstance")) {
1060                         return getGBBMacroNoUserParamsCreate(executeBB, lookupKeyMap, bbName, key, gBB, service);
1061                 } else {
1062                         throw new IllegalArgumentException(
1063                                         "No user params on requestAction: assignInstance. Please specify user params.");
1064                 }
1065         }
1066
1067         protected GeneralBuildingBlock getGBBMacroNoUserParamsCreate(ExecuteBuildingBlock executeBB,
1068                         Map<ResourceKey, String> lookupKeyMap, String bbName, String key, GeneralBuildingBlock gBB, Service service)
1069                         throws Exception {
1070                 ServiceInstance serviceInstance = gBB.getServiceInstance();
1071                 if (bbName.contains(NETWORK) && !bbName.contains(NETWORK_COLLECTION)) {
1072                         String networkId = lookupKeyMap.get(ResourceKey.NETWORK_ID);
1073                         ModelInfo networkModelInfo = new ModelInfo();
1074                         if((!Boolean.TRUE.equals(executeBB.getBuildingBlock().getIsVirtualLink()))) {
1075                                 NetworkResourceCustomization networkCust = getNetworkCustomizationByKey(key, service);
1076                                 if (networkCust != null) {
1077                                         networkModelInfo.setModelCustomizationUuid(networkCust.getModelCustomizationUUID());
1078                                         this.populateL3Network(null, networkModelInfo, service, bbName, serviceInstance, lookupKeyMap,
1079                                                         networkId, null);
1080                                 } else {
1081                                         logger.debug("Could not find a network customization with key: {}", key);
1082                                 }
1083                         } else {
1084                                 logger.debug("Orchestrating on Collection Network Resource Customization");
1085                                 CollectionNetworkResourceCustomization collectionNetworkResourceCust = bbInputSetupUtils.getCatalogCollectionNetworkResourceCustByID(key);
1086                                 L3Network l3Network = getVirtualLinkL3Network(lookupKeyMap, bbName, key, networkId, collectionNetworkResourceCust, serviceInstance);
1087                                 NetworkResourceCustomization networkResourceCustomization = 
1088                                                 mapperLayer.mapCollectionNetworkResourceCustToNetworkResourceCust(collectionNetworkResourceCust);
1089                                 if(l3Network != null) {
1090                                         l3Network.setModelInfoNetwork(mapperLayer.mapCatalogNetworkToNetwork(networkResourceCustomization));
1091                                 }
1092                         }
1093                 } else if(bbName.contains("Configuration")) {
1094                         String configurationId = lookupKeyMap.get(ResourceKey.CONFIGURATION_ID);
1095                         ModelInfo configurationModelInfo = new ModelInfo();
1096                         configurationModelInfo.setModelCustomizationUuid(key);
1097                         this.populateConfiguration(configurationModelInfo, service, bbName, serviceInstance, lookupKeyMap, configurationId, null, executeBB.getConfigurationResourceKeys());
1098                 }
1099                 if (executeBB.getWorkflowResourceIds() != null) {
1100                         this.populateNetworkCollectionAndInstanceGroupAssign(service, bbName, serviceInstance,
1101                                         executeBB.getWorkflowResourceIds().getNetworkCollectionId(), key);
1102                 }
1103                 return gBB;
1104         }
1105
1106         protected L3Network getVirtualLinkL3Network(Map<ResourceKey, String> lookupKeyMap, String bbName, String key,
1107                         String networkId, CollectionNetworkResourceCustomization collectionNetworkResourceCust, ServiceInstance serviceInstance) {
1108                 if(collectionNetworkResourceCust != null) {
1109                         if((bbName.equalsIgnoreCase(AssignFlows.NETWORK_A_LA_CARTE.toString())
1110                                 || bbName.equalsIgnoreCase(AssignFlows.NETWORK_MACRO.toString()))) {
1111                                 L3Network network = createNetwork(lookupKeyMap, null, networkId, null);                         
1112                                 serviceInstance.getNetworks().add(network);
1113                                 return network;
1114                         } else {
1115                                 for (L3Network network : serviceInstance.getNetworks()) {
1116                                         if (network.getNetworkId().equalsIgnoreCase(networkId)) {
1117                                                 return network;
1118                                         }
1119                                 }
1120                         }
1121                 }
1122                 return null;
1123         }
1124
1125         protected NetworkResourceCustomization getNetworkCustomizationByKey(String key, Service service) {
1126                 for (NetworkResourceCustomization networkCust : service.getNetworkCustomizations()) {
1127                         if (networkCust.getModelCustomizationUUID().equalsIgnoreCase(key)) {
1128                                 return networkCust;
1129                         }
1130                 }
1131                 return null;
1132         }
1133
1134         protected GeneralBuildingBlock getGBBMacroExistingService(ExecuteBuildingBlock executeBB,
1135                         Map<ResourceKey, String> lookupKeyMap, String bbName, String requestAction, CloudConfiguration cloudConfiguration) throws Exception {
1136                 org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
1137                 String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
1138                 RequestDetails requestDetails = executeBB.getRequestDetails();
1139                 GeneralBuildingBlock gBB = null;
1140                 if (serviceInstanceId != null) {
1141                         aaiServiceInstance = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
1142                 }
1143                 Service service = null;
1144                 if (aaiServiceInstance != null) {
1145                         service = bbInputSetupUtils.getCatalogServiceByModelUUID(aaiServiceInstance.getModelVersionId());
1146                 }
1147                 if (aaiServiceInstance != null && service != null) {
1148                         ServiceInstance serviceInstance = this.getExistingServiceInstance(aaiServiceInstance);
1149                         serviceInstance.setModelInfoServiceInstance(this.mapperLayer.mapCatalogServiceIntoServiceInstance(service));
1150                         gBB = populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, null);
1151                 } else {
1152                         logger.debug("Related Service Instance from AAI: {}", aaiServiceInstance);
1153                         logger.debug("Related Service Instance Model Info from AAI: {}", service);
1154                         throw new Exception("Could not find relevant information for related Service Instance");
1155                 }
1156                 ServiceInstance serviceInstance = gBB.getServiceInstance();
1157                 CloudRegion cloudRegion = null;
1158                 if(cloudConfiguration == null) {
1159                         Optional<CloudRegion> cloudRegionOp = cloudInfoFromAAI.getCloudInfoFromAAI(serviceInstance);
1160                         if(cloudRegionOp.isPresent()) {
1161                                 cloudRegion = cloudRegionOp.get();
1162                         }
1163                 }
1164                 if (cloudConfiguration != null) {
1165                         org.onap.aai.domain.yang.CloudRegion aaiCloudRegion = bbInputSetupUtils.getCloudRegion(cloudConfiguration);
1166                         cloudRegion = mapperLayer.mapCloudRegion(cloudConfiguration, aaiCloudRegion);
1167                 }
1168                 gBB.setCloudRegion(cloudRegion);
1169                 if (bbName.contains(VNF)) {
1170                         for (GenericVnf genericVnf : serviceInstance.getVnfs()) {
1171                                 if (lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID) != null
1172                                                 && genericVnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
1173                                         org.onap.aai.domain.yang.GenericVnf vnf = bbInputSetupUtils.getAAIGenericVnf(genericVnf.getVnfId());
1174                                         ModelInfo modelInfo = new ModelInfo();
1175                                         if (vnf != null) {
1176                                                 modelInfo.setModelCustomizationUuid(vnf.getModelCustomizationId());
1177                                         }
1178                                         this.mapCatalogVnf(genericVnf, modelInfo, service);
1179                                 }
1180                         }
1181                 } else if (bbName.contains(VF_MODULE)) {
1182                         for (GenericVnf vnf : serviceInstance.getVnfs()) {
1183                                 for (VfModule vfModule : vnf.getVfModules()) {
1184                                         if (lookupKeyMap.get(ResourceKey.VF_MODULE_ID) != null
1185                                                         && vfModule.getVfModuleId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.VF_MODULE_ID))) {
1186                                                 String vnfModelCustomizationUUID = this.bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId())
1187                                                                 .getModelCustomizationId();
1188                                                 ModelInfo vnfModelInfo = new ModelInfo();
1189                                                 vnfModelInfo.setModelCustomizationUuid(vnfModelCustomizationUUID);
1190                                                 this.mapCatalogVnf(vnf, vnfModelInfo, service);
1191                                                 lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, vnf.getVnfId());
1192                                                 String vfModuleCustomizationUUID = this.bbInputSetupUtils
1193                                                                 .getAAIVfModule(vnf.getVnfId(), vfModule.getVfModuleId()).getModelCustomizationId();
1194                                                 ModelInfo vfModuleModelInfo = new ModelInfo();
1195                                                 vfModuleModelInfo.setModelCustomizationId(vfModuleCustomizationUUID);
1196                                                 this.mapCatalogVfModule(vfModule, vfModuleModelInfo, service, vnfModelCustomizationUUID);
1197                                                 if(cloudRegion != null) {
1198                                                         Optional<String> volumeGroupIdOp = getVolumeGroupIdRelatedToVfModule(vnf, vfModuleModelInfo, cloudRegion.getCloudOwner(), 
1199                                                                         cloudRegion.getLcpCloudRegionId(), lookupKeyMap);
1200                                                         if(volumeGroupIdOp.isPresent()) {
1201                                                                 lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, volumeGroupIdOp.get());
1202                                                         }
1203                                                 }
1204                                                 break;
1205                                         }
1206                                 }
1207                         }
1208                 } else if (bbName.contains(VOLUME_GROUP)) {
1209                         for (GenericVnf vnf : serviceInstance.getVnfs()) {
1210                                 for (VolumeGroup volumeGroup : vnf.getVolumeGroups()) {
1211                                         if (lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID) != null && volumeGroup.getVolumeGroupId()
1212                                                         .equalsIgnoreCase(lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID))) {
1213                                                 String vnfModelCustomizationUUID = this.bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId())
1214                                                                 .getModelCustomizationId();
1215                                                 ModelInfo vnfModelInfo = new ModelInfo();
1216                                                 vnfModelInfo.setModelCustomizationUuid(vnfModelCustomizationUUID);
1217                                                 this.mapCatalogVnf(vnf, vnfModelInfo, service);
1218                                                 lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, vnf.getVnfId());
1219                                                 if (cloudRegion != null) {
1220                                                         String volumeGroupCustomizationUUID = this.bbInputSetupUtils.getAAIVolumeGroup(cloudRegion.getCloudOwner(),
1221                                                                         cloudRegion.getLcpCloudRegionId(), volumeGroup.getVolumeGroupId())
1222                                                                         .getModelCustomizationId();
1223                                                         ModelInfo volumeGroupModelInfo = new ModelInfo();
1224                                                         volumeGroupModelInfo.setModelCustomizationId(volumeGroupCustomizationUUID);
1225                                                         this.mapCatalogVolumeGroup(volumeGroup, volumeGroupModelInfo, service,
1226                                                                         vnfModelCustomizationUUID);
1227                                                 }
1228                                                 break;
1229                                         }
1230                                 }
1231                         }
1232                 } else if (bbName.contains(NETWORK)) {
1233                         for (L3Network network : serviceInstance.getNetworks()) {
1234                                 if (lookupKeyMap.get(ResourceKey.NETWORK_ID) != null
1235                                                 && network.getNetworkId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.NETWORK_ID))) {
1236                                         String networkCustomizationUUID = this.bbInputSetupUtils.getAAIL3Network(network.getNetworkId())
1237                                                         .getModelCustomizationId();
1238                                         ModelInfo modelInfo = new ModelInfo();
1239                                         modelInfo.setModelCustomizationUuid(networkCustomizationUUID);
1240                                         this.mapCatalogNetwork(network, modelInfo, service);
1241                                         break;
1242                                 }
1243                         }
1244                 } else if (bbName.contains("Fabric")) {
1245                         for(Configuration configuration : serviceInstance.getConfigurations()) {
1246                                 if(lookupKeyMap.get(ResourceKey.CONFIGURATION_ID) != null
1247                                                 && configuration.getConfigurationId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.CONFIGURATION_ID))) {
1248                                         String configurationCustUUID = this.bbInputSetupUtils.getAAIConfiguration(configuration.getConfigurationId())
1249                                                         .getModelCustomizationId();
1250                                         ModelInfo modelInfo = new ModelInfo();
1251                                         modelInfo.setModelCustomizationUuid(configurationCustUUID);
1252                                         this.mapCatalogConfiguration(configuration, modelInfo, service, executeBB.getConfigurationResourceKeys());
1253                                         break;
1254                                 }
1255                         }
1256                 }
1257                 if (executeBB.getWorkflowResourceIds() != null) {
1258                         this.populateNetworkCollectionAndInstanceGroupAssign(service, bbName, serviceInstance,
1259                                         executeBB.getWorkflowResourceIds().getNetworkCollectionId(), executeBB.getBuildingBlock().getKey());
1260                 }
1261                 return gBB;
1262         }
1263
1264         protected GeneralBuildingBlock getGBBMacroUserParams(ExecuteBuildingBlock executeBB, RequestDetails requestDetails,
1265                         Map<ResourceKey, String> lookupKeyMap, String vnfType, String bbName, String key, GeneralBuildingBlock gBB,
1266                         RequestParameters requestParams, Service service, String input) throws Exception {
1267                 ServiceInstance serviceInstance = gBB.getServiceInstance();
1268                 org.onap.so.serviceinstancebeans.Service serviceMacro = mapper.readValue(input,
1269                                 org.onap.so.serviceinstancebeans.Service.class);
1270
1271                 Resources resources = serviceMacro.getResources();
1272                 Vnfs vnfs = null;
1273                 VfModules vfModules = null;
1274                 Networks networks = null;
1275                 CloudConfiguration cloudConfiguration = requestDetails.getCloudConfiguration();
1276                 CloudRegion cloudRegion = getCloudRegionFromMacroRequest(cloudConfiguration, resources);
1277                 gBB.setCloudRegion(cloudRegion);
1278                 if (bbName.contains(VNF)) {
1279                         vnfs = findVnfsByKey(key, resources, vnfs);
1280                         String vnfId = lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID);
1281                         // This stores the vnf id in request db to be retrieved later when
1282                         // working on a vf module or volume group
1283                         InfraActiveRequests request = this.bbInputSetupUtils.getInfraActiveRequest(executeBB.getRequestId());
1284                         if (request != null) {
1285                                 this.bbInputSetupUtils.updateInfraActiveRequestVnfId(request, vnfId);
1286                         }
1287                         String productFamilyId = requestDetails.getRequestInfo().getProductFamilyId();
1288                         this.populateGenericVnf(vnfs.getModelInfo(), vnfs.getInstanceName(), vnfs.getPlatform(),
1289                                         vnfs.getLineOfBusiness(), service, bbName, serviceInstance, lookupKeyMap, null, vnfId, vnfType, vnfs.getInstanceParams(),
1290                                         productFamilyId);
1291                 } else if (bbName.contains(VF_MODULE) || bbName.contains(VOLUME_GROUP)) {
1292                         Pair<Vnfs, VfModules> vnfsAndVfModules = getVfModulesAndItsVnfsByKey(key, resources);
1293                         vfModules = vnfsAndVfModules.getValue1();
1294                         vnfs = vnfsAndVfModules.getValue0();
1295                         lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, getVnfId(executeBB, lookupKeyMap));
1296                         if (vnfs == null) {
1297                                 throw new Exception("Could not find Vnf to orchestrate VfModule");
1298                         }
1299                         ModelInfo modelInfo = vfModules.getModelInfo();
1300                         if (bbName.contains(VOLUME_GROUP)) {
1301                                 String volumeGroupId = lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID);
1302                                 this.populateVolumeGroup(modelInfo, service, bbName, serviceInstance, lookupKeyMap, volumeGroupId, null,
1303                                                 vfModules.getVolumeGroupInstanceName(), vnfType, vfModules.getInstanceParams());
1304                         } else {
1305                                 String vfModuleId = lookupKeyMap.get(ResourceKey.VF_MODULE_ID);
1306                                 CloudConfiguration cloudConfig = new CloudConfiguration();
1307                                 cloudConfig.setLcpCloudRegionId(cloudRegion.getLcpCloudRegionId());
1308                                 cloudConfig.setCloudOwner(cloudRegion.getCloudOwner());
1309                                 this.populateVfModule(modelInfo, service, bbName, serviceInstance, lookupKeyMap, vfModuleId, null,
1310                                                 vfModules.getInstanceName(), vfModules.getInstanceParams(), cloudConfig);
1311                         }
1312                 } else if (bbName.contains(NETWORK)) {
1313                         networks = findNetworksByKey(key, resources);
1314                         String networkId = lookupKeyMap.get(ResourceKey.NETWORK_ID);
1315                         this.populateL3Network(networks.getInstanceName(), networks.getModelInfo(), service, bbName,
1316                                         serviceInstance, lookupKeyMap, networkId, networks.getInstanceParams());
1317                 } else if (bbName.contains("Configuration")) {
1318                         String configurationId = lookupKeyMap.get(ResourceKey.CONFIGURATION_ID);
1319                         ModelInfo configurationModelInfo = new ModelInfo();
1320                         configurationModelInfo.setModelCustomizationUuid(key);
1321                         ConfigurationResourceCustomization configurationCust = findConfigurationResourceCustomization(configurationModelInfo, service);
1322                         if(configurationCust != null) {
1323                                 this.populateConfiguration(configurationModelInfo, service, bbName, serviceInstance, lookupKeyMap, configurationId, null, executeBB.getConfigurationResourceKeys());
1324                         } else {
1325                                 logger.debug("Could not find a configuration customization with key: {}", key);
1326                         }
1327                 }
1328                 return gBB;
1329         }
1330         
1331         protected Networks findNetworksByKey(String key, Resources resources) {
1332                 for (Networks networks : resources.getNetworks()) {
1333                         if (networks.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
1334                                 return networks;
1335                         }
1336                 }
1337                 return null;
1338         }
1339
1340         protected Pair<Vnfs, VfModules> getVfModulesAndItsVnfsByKey(String key, Resources resources) {
1341                 for (Vnfs vnfs : resources.getVnfs()) {
1342                         for (VfModules vfModules : vnfs.getVfModules()) {
1343                                 if (vfModules.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
1344                                         return new Pair<Vnfs, VfModules>(vnfs, vfModules);
1345                                 }
1346                         }
1347                 }
1348                 return null;
1349         }
1350
1351         protected Vnfs findVnfsByKey(String key, Resources resources, Vnfs vnfs) {
1352                 for (Vnfs tempVnfs : resources.getVnfs()) {
1353                         if (tempVnfs.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
1354                                 vnfs = tempVnfs;
1355                                 break;
1356                         }
1357                 }
1358                 return vnfs;
1359         }
1360
1361         protected CloudRegion getCloudRegionFromMacroRequest(CloudConfiguration cloudConfiguration, Resources resources) {
1362                 if(cloudConfiguration == null) {
1363                         for(Vnfs vnfs : resources.getVnfs()) {
1364                                 if(cloudConfiguration == null) {
1365                                         cloudConfiguration = vnfs.getCloudConfiguration();
1366                                 } else {
1367                                         break;
1368                                 }
1369                                 for(VfModules vfModules : vnfs.getVfModules()) {
1370                                         if(cloudConfiguration == null) {
1371                                                 cloudConfiguration = vfModules.getCloudConfiguration();
1372                                         } else {
1373                                                 break;
1374                                         }
1375                                 }
1376                         }
1377                         for(Networks networks : resources.getNetworks()) {
1378                                 if(cloudConfiguration == null) {
1379                                 cloudConfiguration = networks.getCloudConfiguration();
1380                                 } else {
1381                                         break;
1382                         }
1383                 }
1384                 }
1385                 if(cloudConfiguration != null) {
1386                         org.onap.aai.domain.yang.CloudRegion aaiCloudRegion = bbInputSetupUtils.getCloudRegion(cloudConfiguration);
1387                         return mapperLayer.mapCloudRegion(cloudConfiguration, aaiCloudRegion);
1388                 } else {
1389                         logger.debug("Could not find any cloud configuration for this request.");
1390                         return null;
1391                 }
1392         }
1393
1394         protected String getVnfId(ExecuteBuildingBlock executeBB, Map<ResourceKey, String> lookupKeyMap) {
1395                 String vnfId = lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID);
1396                 if (vnfId == null) {
1397                         InfraActiveRequests request = this.bbInputSetupUtils.getInfraActiveRequest(executeBB.getRequestId());
1398                         vnfId = request.getVnfId();
1399                 }
1400
1401                 return vnfId;
1402         }
1403
1404         protected String generateRandomUUID() {
1405                 return UUID.randomUUID().toString();
1406         }
1407
1408         protected ServiceInstance getServiceInstanceHelper(RequestDetails requestDetails, Customer customer,
1409                         Project project, OwningEntity owningEntity, Map<ResourceKey, String> lookupKeyMap, String serviceInstanceId,
1410                         boolean aLaCarte, Service service, String bbName) throws Exception {
1411                 if (requestDetails.getRequestInfo().getInstanceName() == null && aLaCarte
1412                                 && bbName.equalsIgnoreCase(AssignFlows.SERVICE_INSTANCE.toString())) {
1413                         throw new Exception("Request invalid missing: RequestInfo:InstanceName");
1414                 } else {
1415                         org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI = null;
1416                         if (aLaCarte && bbName.equalsIgnoreCase(AssignFlows.SERVICE_INSTANCE.toString())) {
1417                                 serviceInstanceAAI = bbInputSetupUtils
1418                                         .getAAIServiceInstanceByName(requestDetails.getRequestInfo().getInstanceName(), customer);
1419                         }
1420                         if (serviceInstanceId != null && serviceInstanceAAI == null) {
1421                                 if(customer != null && customer.getServiceSubscription() != null) {
1422                                         serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceByIdAndCustomer(customer.getGlobalCustomerId(), 
1423                                                         customer.getServiceSubscription().getServiceType(), serviceInstanceId);
1424                                 } else {
1425                                         serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId);
1426                                 }
1427                         }
1428                         if (serviceInstanceAAI != null) {
1429                                 lookupKeyMap.put(ResourceKey.SERVICE_INSTANCE_ID, serviceInstanceId);
1430                                 return this.getExistingServiceInstance(serviceInstanceAAI);
1431                         } else {
1432                                 return createServiceInstance(requestDetails, project, owningEntity, lookupKeyMap,
1433                                                 serviceInstanceId);
1434                         }
1435                 }
1436         }
1437
1438         protected ServiceInstance createServiceInstance(RequestDetails requestDetails, Project project,
1439                                                                                                         OwningEntity owningEntity, Map<ResourceKey, String> lookupKeyMap, String serviceInstanceId) {
1440                 ServiceInstance serviceInstance = new ServiceInstance();
1441                 lookupKeyMap.put(ResourceKey.SERVICE_INSTANCE_ID, serviceInstanceId);
1442                 serviceInstance.setServiceInstanceId(serviceInstanceId);
1443                 if(requestDetails.getRequestInfo() != null) {
1444                 serviceInstance.setServiceInstanceName(requestDetails.getRequestInfo().getInstanceName());
1445                 }
1446                 serviceInstance.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
1447                 serviceInstance.setProject(project);
1448                 serviceInstance.setOwningEntity(owningEntity);
1449                 return serviceInstance;
1450         }
1451
1452         public ServiceInstance getExistingServiceInstance(org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI)
1453                         throws Exception {
1454                 ServiceInstance serviceInstance = mapperLayer.mapAAIServiceInstanceIntoServiceInstance(serviceInstanceAAI);
1455                 if (serviceInstanceAAI.getRelationshipList() != null
1456                                 && serviceInstanceAAI.getRelationshipList().getRelationship() != null
1457                                 && !serviceInstanceAAI.getRelationshipList().getRelationship().isEmpty()) {
1458                         addRelationshipsToSI(serviceInstanceAAI, serviceInstance);
1459                 }
1460                 return serviceInstance;
1461         }
1462
1463         protected void populateNetworkCollectionAndInstanceGroupAssign(Service service, String bbName,
1464                         ServiceInstance serviceInstance, String resourceId, String key) throws Exception {
1465                 if (serviceInstance.getCollection() == null
1466                                 && bbName.equalsIgnoreCase(AssignFlows.NETWORK_COLLECTION.toString())) {
1467                         Collection collection = this.createCollection(resourceId);
1468                         serviceInstance.setCollection(collection);
1469                         this.mapCatalogCollection(service, serviceInstance.getCollection(), key);
1470                         if(isVlanTagging(service, key)) {
1471                         InstanceGroup instanceGroup = this.createInstanceGroup();
1472                         serviceInstance.getCollection().setInstanceGroup(instanceGroup);
1473                                 this.mapCatalogNetworkCollectionInstanceGroup(service,
1474                                                 serviceInstance.getCollection().getInstanceGroup(), key);
1475                         }
1476                 }
1477         }
1478
1479         protected CollectionResourceCustomization findCatalogNetworkCollection(Service service, String key) {
1480                 for(CollectionResourceCustomization collectionCust : service.getCollectionResourceCustomizations()){
1481                         if(collectionCust.getModelCustomizationUUID().equalsIgnoreCase(key)) {
1482                                 return collectionCust;
1483                         }
1484                 }
1485                         return null;
1486         }
1487                         
1488         protected boolean isVlanTagging(Service service, String key) {
1489                 CollectionResourceCustomization collectionCust = findCatalogNetworkCollection(service, key);
1490                 if (collectionCust != null) {
1491                         CollectionResource collectionResource = collectionCust.getCollectionResource();
1492                         if (collectionResource != null
1493                                         && collectionResource.getInstanceGroup() != null
1494                                         && collectionResource.getInstanceGroup().getToscaNodeType() != null
1495                                         && collectionResource.getInstanceGroup().getToscaNodeType().contains("NetworkCollection")) {
1496                                 return true;
1497                         }
1498                 }
1499                 return false;
1500         }
1501
1502         protected void mapCatalogNetworkCollectionInstanceGroup(Service service, InstanceGroup instanceGroup, String key) {
1503                 CollectionResourceCustomization collectionCust = this.findCatalogNetworkCollection(service, key);
1504                 org.onap.so.db.catalog.beans.InstanceGroup catalogInstanceGroup = collectionCust.getCollectionResource().getInstanceGroup();
1505                 instanceGroup.setModelInfoInstanceGroup(
1506                                 mapperLayer.mapCatalogInstanceGroupToInstanceGroup(collectionCust, catalogInstanceGroup));
1507                 }
1508
1509         protected void mapCatalogCollection(Service service, Collection collection, String key) {
1510                 CollectionResourceCustomization collectionCust = findCatalogNetworkCollection(service, key);
1511                 if (collectionCust != null) {
1512                         CollectionResource collectionResource = collectionCust.getCollectionResource();
1513                         if (collectionResource != null) {
1514                                 collection.setModelInfoCollection(
1515                                                 mapperLayer.mapCatalogCollectionToCollection(collectionCust, collectionResource));
1516                         }
1517                 }
1518         }
1519
1520         protected Collection createCollection(String collectionId) {
1521                 Collection collection = new Collection();
1522                 collection.setId(collectionId);
1523                 collection.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
1524                 return collection;
1525         }
1526
1527         protected InstanceGroup createInstanceGroup() {
1528                 InstanceGroup instanceGroup = new InstanceGroup();
1529                 String instanceGroupId = this.generateRandomUUID();
1530                 instanceGroup.setId(instanceGroupId);
1531                 return instanceGroup;
1532         }
1533
1534         protected void addRelationshipsToSI(org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI,
1535                         ServiceInstance serviceInstance) throws Exception {
1536                 AAIResultWrapper serviceInstanceWrapper = new AAIResultWrapper(
1537                                 new AAICommonObjectMapperProvider().getMapper().writeValueAsString(serviceInstanceAAI));
1538                 Optional<Relationships> relationshipsOp = serviceInstanceWrapper.getRelationships();
1539                 Relationships relationships = null;
1540                 if (relationshipsOp.isPresent()) {
1541                         relationships = relationshipsOp.get();
1542                 } else {
1543                         return;
1544                 }
1545
1546                 this.mapProject(relationships.getByType(AAIObjectType.PROJECT), serviceInstance);
1547                 this.mapOwningEntity(relationships.getByType(AAIObjectType.OWNING_ENTITY), serviceInstance);
1548                 this.mapL3Networks(relationships.getRelatedAAIUris(AAIObjectType.L3_NETWORK), serviceInstance.getNetworks());
1549                 this.mapGenericVnfs(relationships.getRelatedAAIUris(AAIObjectType.GENERIC_VNF), serviceInstance.getVnfs());
1550                 this.mapCollection(relationships.getByType(AAIObjectType.COLLECTION), serviceInstance);
1551                 this.mapConfigurations(relationships.getRelatedAAIUris(AAIObjectType.CONFIGURATION), serviceInstance.getConfigurations());
1552         }
1553
1554         protected void mapConfigurations(List<AAIResourceUri> relatedAAIUris, List<Configuration> configurations) {
1555                 for (AAIResourceUri aaiResourceUri : relatedAAIUris) {
1556                         configurations.add(mapConfiguration(aaiResourceUri));
1557                 }
1558         }
1559
1560         protected Configuration mapConfiguration(AAIResourceUri aaiResourceUri) {
1561                 AAIResultWrapper aaiConfigurationWrapper = this.bbInputSetupUtils.getAAIResourceDepthOne(aaiResourceUri);
1562                 Optional<org.onap.aai.domain.yang.Configuration> aaiConfigurationOp = aaiConfigurationWrapper
1563                                 .asBean(org.onap.aai.domain.yang.Configuration.class);
1564                 if (!aaiConfigurationOp.isPresent()) {
1565                         return null;
1566                 }
1567
1568                 return this.mapperLayer.mapAAIConfiguration(aaiConfigurationOp.get());
1569         }
1570
1571         protected void mapGenericVnfs(List<AAIResourceUri> list, List<GenericVnf> genericVnfs) {
1572                 for (AAIResourceUri aaiResourceUri : list) {
1573                         genericVnfs.add(this.mapGenericVnf(aaiResourceUri));
1574                 }
1575         }
1576
1577         protected GenericVnf mapGenericVnf(AAIResourceUri aaiResourceUri) {
1578                 AAIResultWrapper aaiGenericVnfWrapper = this.bbInputSetupUtils.getAAIResourceDepthOne(aaiResourceUri);
1579                 Optional<org.onap.aai.domain.yang.GenericVnf> aaiGenericVnfOp = aaiGenericVnfWrapper
1580                                 .asBean(org.onap.aai.domain.yang.GenericVnf.class);
1581                 if (!aaiGenericVnfOp.isPresent()) {
1582                         return null;
1583                 }
1584
1585                 GenericVnf genericVnf = this.mapperLayer.mapAAIGenericVnfIntoGenericVnf(aaiGenericVnfOp.get());
1586
1587                 Optional<Relationships> relationshipsOp = aaiGenericVnfWrapper.getRelationships();
1588                 if (relationshipsOp.isPresent()) {
1589                         Relationships relationships = relationshipsOp.get();
1590                         this.mapPlatform(relationships.getByType(AAIObjectType.PLATFORM), genericVnf);
1591                         this.mapLineOfBusiness(relationships.getByType(AAIObjectType.LINE_OF_BUSINESS), genericVnf);
1592                         genericVnf.getVolumeGroups().addAll(mapVolumeGroups(relationships.getByType(AAIObjectType.VOLUME_GROUP)));
1593                         genericVnf.getInstanceGroups().addAll(mapInstanceGroups(relationships.getByType(AAIObjectType.INSTANCE_GROUP)));
1594                 }
1595
1596                 return genericVnf;
1597         }
1598
1599         protected List<InstanceGroup> mapInstanceGroups(List<AAIResultWrapper> instanceGroups) {
1600                 List<InstanceGroup> instanceGroupsList = new ArrayList<>();
1601                 for (AAIResultWrapper volumeGroupWrapper : instanceGroups) {
1602                         instanceGroupsList.add(this.mapInstanceGroup(volumeGroupWrapper));
1603                 }
1604                 return instanceGroupsList;
1605         }
1606
1607         protected InstanceGroup mapInstanceGroup(AAIResultWrapper instanceGroupWrapper) {
1608                 Optional<org.onap.aai.domain.yang.InstanceGroup> aaiInstanceGroupOp = instanceGroupWrapper
1609                                 .asBean(org.onap.aai.domain.yang.InstanceGroup.class);
1610                 org.onap.aai.domain.yang.InstanceGroup aaiInstanceGroup = null;
1611
1612                 if (!aaiInstanceGroupOp.isPresent()) {
1613                         return null;
1614                 }
1615
1616                 aaiInstanceGroup = aaiInstanceGroupOp.get();
1617                 InstanceGroup instanceGroup = this.mapperLayer.mapAAIInstanceGroupIntoInstanceGroup(aaiInstanceGroup);
1618                 instanceGroup.setModelInfoInstanceGroup(this.mapperLayer.mapCatalogInstanceGroupToInstanceGroup(null,
1619                                 this.bbInputSetupUtils.getCatalogInstanceGroup(aaiInstanceGroup.getModelVersionId())));
1620                 return instanceGroup;
1621         }
1622
1623         protected List<VolumeGroup> mapVolumeGroups(List<AAIResultWrapper> volumeGroups) {
1624                 List<VolumeGroup> volumeGroupsList = new ArrayList<>();
1625                 for (AAIResultWrapper volumeGroupWrapper : volumeGroups) {
1626                         volumeGroupsList.add(this.mapVolumeGroup(volumeGroupWrapper));
1627                 }
1628                 return volumeGroupsList;
1629         }
1630
1631         protected VolumeGroup mapVolumeGroup(AAIResultWrapper volumeGroupWrapper) {
1632                 Optional<org.onap.aai.domain.yang.VolumeGroup> aaiVolumeGroupOp = volumeGroupWrapper
1633                                 .asBean(org.onap.aai.domain.yang.VolumeGroup.class);
1634                 org.onap.aai.domain.yang.VolumeGroup aaiVolumeGroup = null;
1635
1636                 if (!aaiVolumeGroupOp.isPresent()) {
1637                         return null;
1638                 }
1639
1640                 aaiVolumeGroup = aaiVolumeGroupOp.get();
1641                 return this.mapperLayer.mapAAIVolumeGroup(aaiVolumeGroup);
1642         }
1643
1644         protected void mapLineOfBusiness(List<AAIResultWrapper> lineOfBusinesses, GenericVnf genericVnf) {
1645                 if (!lineOfBusinesses.isEmpty()) {
1646                         AAIResultWrapper lineOfBusinessWrapper = lineOfBusinesses.get(0);
1647                         Optional<org.onap.aai.domain.yang.LineOfBusiness> aaiLineOfBusinessOp = lineOfBusinessWrapper
1648                                         .asBean(org.onap.aai.domain.yang.LineOfBusiness.class);
1649                         org.onap.aai.domain.yang.LineOfBusiness aaiLineOfBusiness = null;
1650                         if (!aaiLineOfBusinessOp.isPresent()) {
1651                                 return;
1652                         }
1653                         aaiLineOfBusiness = aaiLineOfBusinessOp.get();
1654
1655                         LineOfBusiness lineOfBusiness = this.mapperLayer.mapAAILineOfBusiness(aaiLineOfBusiness);
1656                         genericVnf.setLineOfBusiness(lineOfBusiness);
1657                 }
1658         }
1659
1660         protected void mapPlatform(List<AAIResultWrapper> platforms, GenericVnf genericVnf) {
1661                 if (!platforms.isEmpty()) {
1662                         AAIResultWrapper platformWrapper = platforms.get(0);
1663                         Optional<org.onap.aai.domain.yang.Platform> aaiPlatformOp = platformWrapper
1664                                         .asBean(org.onap.aai.domain.yang.Platform.class);
1665                         org.onap.aai.domain.yang.Platform aaiPlatform = null;
1666                         if (!aaiPlatformOp.isPresent()) {
1667                                 return;
1668                         }
1669                         aaiPlatform = aaiPlatformOp.get();
1670
1671                         Platform platform = this.mapperLayer.mapAAIPlatform(aaiPlatform);
1672                         genericVnf.setPlatform(platform);
1673                 }
1674         }
1675
1676         protected void mapCollection(List<AAIResultWrapper> collections, ServiceInstance serviceInstance) {
1677                 if (!collections.isEmpty()) {
1678                         AAIResultWrapper collectionWrapper = collections.get(0);
1679                         Optional<org.onap.aai.domain.yang.Collection> aaiCollectionOp = collectionWrapper
1680                                         .asBean(org.onap.aai.domain.yang.Collection.class);
1681                         org.onap.aai.domain.yang.Collection aaiCollection = null;
1682                         if (!aaiCollectionOp.isPresent()) {
1683                                 return;
1684                         }
1685                         aaiCollection = aaiCollectionOp.get();
1686
1687                         Collection collection = this.mapperLayer.mapAAICollectionIntoCollection(aaiCollection);
1688                         NetworkCollectionResourceCustomization collectionResourceCust = 
1689                                         bbInputSetupUtils.getCatalogNetworkCollectionResourceCustByID(aaiCollection.getCollectionCustomizationId());
1690                         collection.setModelInfoCollection(mapperLayer.mapCatalogCollectionToCollection(collectionResourceCust, collectionResourceCust.getCollectionResource()));
1691                         Optional<Relationships> relationshipsOp = collectionWrapper.getRelationships();
1692                         Relationships relationships = null;
1693                         if (relationshipsOp.isPresent()) {
1694                                 relationships = relationshipsOp.get();
1695                         } else {
1696                                 serviceInstance.setCollection(collection);
1697                                 return;
1698                         }
1699                         List<InstanceGroup> instanceGroupsList = mapInstanceGroups(relationships.getByType(AAIObjectType.INSTANCE_GROUP));
1700                         if(!instanceGroupsList.isEmpty()) {
1701                                 collection.setInstanceGroup(instanceGroupsList.get(0));
1702                         }
1703                         serviceInstance.setCollection(collection);
1704                 }
1705         }
1706
1707         protected void mapL3Networks(List<AAIResourceUri> list, List<L3Network> l3Networks) {
1708                 for (AAIResourceUri aaiResourceUri : list) {
1709                         l3Networks.add(this.mapL3Network(aaiResourceUri));
1710                 }
1711         }
1712
1713         protected L3Network mapL3Network(AAIResourceUri aaiResourceUri) {
1714                 AAIResultWrapper aaiNetworkWrapper = this.bbInputSetupUtils.getAAIResourceDepthTwo(aaiResourceUri);
1715                 Optional<org.onap.aai.domain.yang.L3Network> aaiL3NetworkOp = aaiNetworkWrapper
1716                                 .asBean(org.onap.aai.domain.yang.L3Network.class);
1717                 org.onap.aai.domain.yang.L3Network aaiL3Network = null;
1718
1719                 if (!aaiL3NetworkOp.isPresent()) {
1720                         return null;
1721                 }
1722
1723                 aaiL3Network = aaiL3NetworkOp.get();
1724                 L3Network network = this.mapperLayer.mapAAIL3Network(aaiL3Network);
1725
1726                 Optional<Relationships> relationshipsOp = aaiNetworkWrapper.getRelationships();
1727                 if (relationshipsOp.isPresent()) {
1728                         Relationships relationships = relationshipsOp.get();
1729                         this.mapNetworkPolicies(relationships.getByType(AAIObjectType.NETWORK_POLICY),
1730                                         network.getNetworkPolicies());
1731                         mapRouteTableReferences(relationships.getByType(AAIObjectType.ROUTE_TABLE_REFERENCE),
1732                                         network.getContrailNetworkRouteTableReferences());
1733                 }
1734
1735                 return network;
1736         }
1737
1738         protected void mapNetworkPolicies(List<AAIResultWrapper> aaiNetworkPolicies, List<NetworkPolicy> networkPolicies) {
1739                 for (AAIResultWrapper networkPolicyWrapper : aaiNetworkPolicies) {
1740                         networkPolicies.add(this.mapNetworkPolicy(networkPolicyWrapper));
1741                 }
1742         }
1743
1744         protected NetworkPolicy mapNetworkPolicy(AAIResultWrapper networkPolicyWrapper) {
1745                 Optional<org.onap.aai.domain.yang.NetworkPolicy> aaiNetworkPolicyOp = networkPolicyWrapper
1746                                 .asBean(org.onap.aai.domain.yang.NetworkPolicy.class);
1747                 org.onap.aai.domain.yang.NetworkPolicy aaiNetworkPolicy = null;
1748
1749                 if (!aaiNetworkPolicyOp.isPresent()) {
1750                         return null;
1751                 }
1752
1753                 aaiNetworkPolicy = aaiNetworkPolicyOp.get();
1754                 return this.mapperLayer.mapAAINetworkPolicy(aaiNetworkPolicy);
1755         }
1756
1757         protected void mapRouteTableReferences(List<AAIResultWrapper> routeTableReferences,
1758                         List<RouteTableReference> contrailNetworkRouteTableReferences) {
1759                 for (AAIResultWrapper routeTableReferenceWrapper : routeTableReferences) {
1760                         contrailNetworkRouteTableReferences.add(this.mapRouteTableReference(routeTableReferenceWrapper));
1761                 }
1762         }
1763
1764         protected RouteTableReference mapRouteTableReference(AAIResultWrapper routeTableReferenceWrapper) {
1765                 Optional<org.onap.aai.domain.yang.RouteTableReference> aaiRouteTableReferenceOp = routeTableReferenceWrapper
1766                                 .asBean(org.onap.aai.domain.yang.RouteTableReference.class);
1767                 org.onap.aai.domain.yang.RouteTableReference aaiRouteTableReference = null;
1768
1769                 if (!aaiRouteTableReferenceOp.isPresent()) {
1770                         return null;
1771                 }
1772
1773                 aaiRouteTableReference = aaiRouteTableReferenceOp.get();
1774                 return this.mapperLayer.mapAAIRouteTableReferenceIntoRouteTableReference(aaiRouteTableReference);
1775         }
1776
1777         protected void mapOwningEntity(List<AAIResultWrapper> owningEntities, ServiceInstance serviceInstance) {
1778                 if (!owningEntities.isEmpty()) {
1779                         AAIResultWrapper owningEntityWrapper = owningEntities.get(0);
1780                         Optional<org.onap.aai.domain.yang.OwningEntity> aaiOwningEntityOp = owningEntityWrapper
1781                                         .asBean(org.onap.aai.domain.yang.OwningEntity.class);
1782                         org.onap.aai.domain.yang.OwningEntity aaiOwningEntity = null;
1783                         if (!aaiOwningEntityOp.isPresent()) {
1784                                 return;
1785                         }
1786                         aaiOwningEntity = aaiOwningEntityOp.get();
1787
1788                         OwningEntity owningEntity = this.mapperLayer.mapAAIOwningEntity(aaiOwningEntity);
1789                         serviceInstance.setOwningEntity(owningEntity);
1790                 }
1791         }
1792
1793         protected void mapProject(List<AAIResultWrapper> projects, ServiceInstance serviceInstance) {
1794                 if (!projects.isEmpty()) {
1795                         AAIResultWrapper projectWrapper = projects.get(0);
1796                         Optional<org.onap.aai.domain.yang.Project> aaiProjectOp = projectWrapper
1797                                         .asBean(org.onap.aai.domain.yang.Project.class);
1798                         org.onap.aai.domain.yang.Project aaiProject = null;
1799                         if (!aaiProjectOp.isPresent()) {
1800                                 return;
1801                         }
1802                         aaiProject = aaiProjectOp.get();
1803
1804                         Project project = this.mapperLayer.mapAAIProject(aaiProject);
1805                         serviceInstance.setProject(project);
1806                 }
1807         }
1808         protected Customer mapCustomer(String globalCustomerId, String subscriptionServiceType) {
1809                 org.onap.aai.domain.yang.Customer aaiCustomer = this.bbInputSetupUtils.getAAICustomer(globalCustomerId);
1810                 org.onap.aai.domain.yang.ServiceSubscription aaiServiceSubscription = this.bbInputSetupUtils
1811                                 .getAAIServiceSubscription(globalCustomerId, subscriptionServiceType);
1812                 Customer customer = this.mapperLayer.mapAAICustomer(aaiCustomer);
1813                 ServiceSubscription serviceSubscription = this.mapperLayer.mapAAIServiceSubscription(aaiServiceSubscription);
1814                 if(serviceSubscription != null){
1815                         customer.setServiceSubscription(serviceSubscription);
1816                 }
1817                 return customer;
1818         }
1819
1820
1821 }