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