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