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