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