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