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