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