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