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