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