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