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