Merge "Support activate, terminate feature for NSMF based TN slices"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / ebb / loader / ServiceEBBLoader.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (c) 2020 Nokia
6  * ================================================================================
7  * Modifications Copyright (c) 2021 Orange
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.workflow.tasks.ebb.loader;
24
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import org.camunda.bpm.engine.delegate.DelegateExecution;
27 import org.javatuples.Pair;
28 import org.onap.aai.domain.yang.GenericVnf;
29 import org.onap.aai.domain.yang.Relationship;
30 import org.onap.aai.domain.yang.ServiceInstance;
31 import org.onap.aai.domain.yang.VpnBinding;
32 import org.onap.aaiclient.client.aai.AAICommonObjectMapperProvider;
33 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
34 import org.onap.aaiclient.client.aai.entities.Relationships;
35 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
36 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
37 import org.onap.so.bpmn.infrastructure.workflow.tasks.VrfBondingServiceException;
38 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
41 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
42 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
43 import org.onap.so.client.exception.ExceptionBuilder;
44 import org.onap.so.client.orchestration.AAIConfigurationResources;
45 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
46 import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
47 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
48 import org.onap.so.db.catalog.beans.InstanceGroup;
49 import org.onap.so.db.catalog.client.CatalogDbClient;
50 import org.onap.so.serviceinstancebeans.ModelType;
51 import org.onap.so.serviceinstancebeans.RelatedInstance;
52 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.stereotype.Component;
56 import java.io.IOException;
57 import java.util.ArrayList;
58 import java.util.List;
59 import java.util.Map;
60 import java.util.Optional;
61 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.ACTIVATE_INSTANCE;
62 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.DEACTIVATE_INSTANCE;
63 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.DELETE_INSTANCE;
64 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.UNASSIGN_INSTANCE;
65 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.UPGRADE_INSTANCE;
66 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.WORKFLOW_ACTION_ERROR_MESSAGE;
67 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.CREATE_INSTANCE;
68 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.FABRIC_CONFIGURATION;
69 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.NETWORKCOLLECTION;
70 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.USER_PARAM_SERVICE;
71 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.ASSIGN_INSTANCE;
72
73
74 @Component
75 public class ServiceEBBLoader {
76
77     private static final Logger logger = LoggerFactory.getLogger(ServiceEBBLoader.class);
78
79     private final UserParamsServiceTraversal userParamsServiceTraversal;
80     private final CatalogDbClient catalogDbClient;
81     private final VrfValidation vrfValidation;
82     private final AAIConfigurationResources aaiConfigurationResources;
83     private final WorkflowActionExtractResourcesAAI workflowActionUtils;
84     private final BBInputSetupUtils bbInputSetupUtils;
85     private final BBInputSetup bbInputSetup;
86     private final ExceptionBuilder exceptionBuilder;
87
88     public ServiceEBBLoader(UserParamsServiceTraversal userParamsServiceTraversal, CatalogDbClient catalogDbClient,
89             VrfValidation vrfValidation, AAIConfigurationResources aaiConfigurationResources,
90             WorkflowActionExtractResourcesAAI workflowActionUtils, BBInputSetupUtils bbInputSetupUtils,
91             BBInputSetup bbInputSetup, ExceptionBuilder exceptionBuilder) {
92         this.userParamsServiceTraversal = userParamsServiceTraversal;
93         this.catalogDbClient = catalogDbClient;
94         this.vrfValidation = vrfValidation;
95         this.aaiConfigurationResources = aaiConfigurationResources;
96         this.workflowActionUtils = workflowActionUtils;
97         this.bbInputSetupUtils = bbInputSetupUtils;
98         this.bbInputSetup = bbInputSetup;
99         this.exceptionBuilder = exceptionBuilder;
100     }
101
102     public List<Resource> getResourceListForService(ServiceInstancesRequest sIRequest, String requestAction,
103             DelegateExecution execution, String serviceInstanceId, String resourceId,
104             List<Pair<WorkflowType, String>> aaiResourceIds) throws IOException, VrfBondingServiceException {
105         boolean containsService = false;
106         List<Resource> resourceList = new ArrayList<>();
107         List<Map<String, Object>> userParams = sIRequest.getRequestDetails().getRequestParameters().getUserParams();
108         if (requestAction.equalsIgnoreCase(ASSIGN_INSTANCE)) {
109             // SERVICE-MACRO-ASSIGN will always get user params with a
110             // service.
111
112             if (userParams != null) {
113                 containsService = isContainsService(sIRequest);
114                 if (containsService) {
115                     resourceList = userParamsServiceTraversal.getResourceListFromUserParams(execution, userParams,
116                             serviceInstanceId, requestAction);
117                 }
118             } else {
119                 buildAndThrowException(execution,
120                         "Service-Macro-Assign request details must contain user params with a service");
121             }
122         } else if (requestAction.equalsIgnoreCase(CREATE_INSTANCE)) {
123             // SERVICE-MACRO-CREATE will get user params with a service,
124             // a service with a network, a service with a
125             // network collection, OR an empty service.
126             // If user params is just a service or null and macro
127             // queries the SI and finds a VNF, macro fails.
128
129             if (userParams != null) {
130                 containsService = isContainsService(sIRequest);
131             }
132             if (containsService) {
133                 resourceList = userParamsServiceTraversal.getResourceListFromUserParams(execution, userParams,
134                         serviceInstanceId, requestAction);
135             }
136             if (!foundRelated(resourceList)) {
137                 traverseCatalogDbService(execution, sIRequest, resourceList, aaiResourceIds);
138             }
139         } else if ((ACTIVATE_INSTANCE.equalsIgnoreCase(requestAction)
140                 || UNASSIGN_INSTANCE.equalsIgnoreCase(requestAction) || DELETE_INSTANCE.equalsIgnoreCase(requestAction)
141                 || UPGRADE_INSTANCE.equalsIgnoreCase(requestAction)
142                 || requestAction.equalsIgnoreCase("activate" + FABRIC_CONFIGURATION))) {
143             // SERVICE-MACRO-ACTIVATE, SERVICE-MACRO-UNASSIGN, and
144             // SERVICE-MACRO-DELETE
145             // Will never get user params with service, macro will have
146             // to query the SI in AAI to find related instances.
147             traverseAAIService(execution, resourceList, resourceId, aaiResourceIds);
148         } else if (DEACTIVATE_INSTANCE.equalsIgnoreCase(requestAction)) {
149             resourceList.add(new Resource(WorkflowType.SERVICE, "", false, null));
150         }
151         return resourceList;
152     }
153
154     private boolean isContainsService(ServiceInstancesRequest sIRequest) {
155         boolean containsService;
156         List<Map<String, Object>> userParams = sIRequest.getRequestDetails().getRequestParameters().getUserParams();
157         containsService = userParams.stream().anyMatch(param -> param.containsKey(USER_PARAM_SERVICE));
158         return containsService;
159     }
160
161     public void traverseCatalogDbService(DelegateExecution execution, ServiceInstancesRequest sIRequest,
162             List<Resource> resourceList, List<Pair<WorkflowType, String>> aaiResourceIds)
163             throws JsonProcessingException, VrfBondingServiceException {
164         String modelUUID = sIRequest.getRequestDetails().getModelInfo().getModelVersionId();
165         org.onap.so.db.catalog.beans.Service service = catalogDbClient.getServiceByID(modelUUID);
166
167         if (service == null) {
168             buildAndThrowException(execution, "Could not find the service model in catalog db.");
169         } else {
170             Resource serviceResource = new Resource(WorkflowType.SERVICE, service.getModelUUID(), false, null);
171             resourceList.add(serviceResource);
172             RelatedInstance relatedVpnBinding =
173                     bbInputSetupUtils.getRelatedInstanceByType(sIRequest.getRequestDetails(), ModelType.vpnBinding);
174             RelatedInstance relatedLocalNetwork =
175                     bbInputSetupUtils.getRelatedInstanceByType(sIRequest.getRequestDetails(), ModelType.network);
176
177             if (relatedVpnBinding != null && relatedLocalNetwork != null) {
178                 traverseVrfConfiguration(aaiResourceIds, resourceList, serviceResource, service, relatedVpnBinding,
179                         relatedLocalNetwork);
180             } else {
181                 traverseNetworkCollection(execution, resourceList, serviceResource, service);
182             }
183         }
184     }
185
186     public boolean foundRelated(List<Resource> resourceList) {
187         return (containsWorkflowType(resourceList, WorkflowType.VNF)
188                 || containsWorkflowType(resourceList, WorkflowType.PNF)
189                 || containsWorkflowType(resourceList, WorkflowType.NETWORK)
190                 || containsWorkflowType(resourceList, WorkflowType.NETWORKCOLLECTION));
191     }
192
193     public void traverseAAIService(DelegateExecution execution, List<Resource> resourceList, String resourceId,
194             List<Pair<WorkflowType, String>> aaiResourceIds) {
195         try {
196             ServiceInstance serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceById(resourceId);
197             org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstanceMSO =
198                     bbInputSetup.getExistingServiceInstance(serviceInstanceAAI);
199             var serviceResource =
200                     new Resource(WorkflowType.SERVICE, serviceInstanceMSO.getServiceInstanceId(), false, null);
201             serviceResource.setModelInvariantId(serviceInstanceAAI.getModelInvariantId());
202             resourceList.add(serviceResource);
203             traverseServiceInstanceMSOVnfs(resourceList, serviceResource, aaiResourceIds, serviceInstanceMSO);
204             traverseServiceInstanceMSOPnfs(resourceList, serviceResource, aaiResourceIds, serviceInstanceMSO);
205             if (serviceInstanceMSO.getNetworks() != null) {
206                 for (org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network network : serviceInstanceMSO
207                         .getNetworks()) {
208                     aaiResourceIds.add(new Pair<>(WorkflowType.NETWORK, network.getNetworkId()));
209                     resourceList
210                             .add(new Resource(WorkflowType.NETWORK, network.getNetworkId(), false, serviceResource));
211                 }
212             }
213             if (serviceInstanceMSO.getCollection() != null) {
214                 logger.debug("found networkcollection");
215                 aaiResourceIds
216                         .add(new Pair<>(WorkflowType.NETWORKCOLLECTION, serviceInstanceMSO.getCollection().getId()));
217                 resourceList.add(new Resource(WorkflowType.NETWORKCOLLECTION,
218                         serviceInstanceMSO.getCollection().getId(), false, serviceResource));
219             }
220             if (serviceInstanceMSO.getConfigurations() != null) {
221                 for (Configuration config : serviceInstanceMSO.getConfigurations()) {
222                     Optional<org.onap.aai.domain.yang.Configuration> aaiConfig =
223                             aaiConfigurationResources.getConfiguration(config.getConfigurationId());
224                     if (aaiConfig.isPresent() && aaiConfig.get().getRelationshipList() != null) {
225                         for (Relationship relationship : aaiConfig.get().getRelationshipList().getRelationship()) {
226                             if (relationship.getRelatedTo().contains("vnfc")
227                                     || relationship.getRelatedTo().contains("vpn-binding")) {
228                                 aaiResourceIds.add(new Pair<>(WorkflowType.CONFIGURATION, config.getConfigurationId()));
229                                 resourceList.add(new Resource(WorkflowType.CONFIGURATION, config.getConfigurationId(),
230                                         false, serviceResource));
231                                 break;
232                             }
233                         }
234                     }
235                 }
236             }
237         } catch (Exception ex) {
238             logger.error("Exception in traverseAAIService", ex);
239             buildAndThrowException(execution,
240                     "Could not find existing Service Instance or related Instances to execute the request on.");
241         }
242     }
243
244     private void traverseServiceInstanceMSOVnfs(List<Resource> resourceList, Resource serviceResource,
245             List<Pair<WorkflowType, String>> aaiResourceIds,
246             org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstanceMSO) {
247         if (serviceInstanceMSO.getVnfs() == null) {
248             return;
249         }
250         for (org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf vnf : serviceInstanceMSO.getVnfs()) {
251             aaiResourceIds.add(new Pair<>(WorkflowType.VNF, vnf.getVnfId()));
252             GenericVnf genericVnf = bbInputSetupUtils.getAAIGenericVnf(vnf.getVnfId());
253             Resource vnfResource = new Resource(WorkflowType.VNF, vnf.getVnfId(), false, serviceResource);
254             vnfResource.setVnfCustomizationId(genericVnf.getModelCustomizationId());
255             resourceList.add(vnfResource);
256             traverseVnfModules(resourceList, vnfResource, aaiResourceIds, vnf);
257             if (vnf.getVolumeGroups() != null) {
258                 for (org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup volumeGroup : vnf.getVolumeGroups()) {
259                     aaiResourceIds.add(new Pair<>(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId()));
260                     resourceList.add(
261                             new Resource(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId(), false, vnfResource));
262                 }
263             }
264         }
265     }
266
267     private void traverseServiceInstanceMSOPnfs(List<Resource> resourceList, Resource serviceResource,
268             List<Pair<WorkflowType, String>> aaiResourceIds,
269             org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstanceMSO) {
270         if (serviceInstanceMSO.getPnfs() == null) {
271             return;
272         }
273         for (org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf pnf : serviceInstanceMSO.getPnfs()) {
274             aaiResourceIds.add(new Pair<>(WorkflowType.PNF, pnf.getPnfId()));
275             resourceList.add(new Resource(WorkflowType.PNF, pnf.getPnfId(), false, serviceResource));
276         }
277     }
278
279     protected void traverseVrfConfiguration(List<Pair<WorkflowType, String>> aaiResourceIds,
280             List<Resource> resourceList, Resource serviceResource, org.onap.so.db.catalog.beans.Service service,
281             RelatedInstance relatedVpnBinding, RelatedInstance relatedLocalNetwork)
282             throws VrfBondingServiceException, JsonProcessingException {
283         org.onap.aai.domain.yang.L3Network aaiLocalNetwork =
284                 bbInputSetupUtils.getAAIL3Network(relatedLocalNetwork.getInstanceId());
285         vrfValidation.vrfServiceValidation(service);
286         vrfValidation.vrfCatalogDbChecks(service);
287         vrfValidation.aaiVpnBindingValidation(relatedVpnBinding.getInstanceId(),
288                 bbInputSetupUtils.getAAIVpnBinding(relatedVpnBinding.getInstanceId()));
289         vrfValidation.aaiNetworkValidation(relatedLocalNetwork.getInstanceId(), aaiLocalNetwork);
290         vrfValidation.aaiSubnetValidation(aaiLocalNetwork);
291         vrfValidation.aaiAggregateRouteValidation(aaiLocalNetwork);
292         vrfValidation.aaiRouteTargetValidation(aaiLocalNetwork);
293         String existingAAIVrfConfiguration = getExistingAAIVrfConfiguration(relatedVpnBinding, aaiLocalNetwork);
294         if (existingAAIVrfConfiguration != null) {
295             aaiResourceIds.add(new Pair<>(WorkflowType.CONFIGURATION, existingAAIVrfConfiguration));
296         }
297         resourceList.add(new Resource(WorkflowType.CONFIGURATION,
298                 service.getConfigurationCustomizations().get(0).getModelCustomizationUUID(), false, serviceResource));
299
300     }
301
302     protected void traverseNetworkCollection(DelegateExecution execution, List<Resource> resourceList,
303             Resource serviceResource, org.onap.so.db.catalog.beans.Service service) {
304         if (isVnfCustomizationsInTheService(service)) {
305             buildAndThrowException(execution,
306                     "Cannot orchestrate Service-Macro-Create without user params with a vnf. Please update ASDC model for new macro orchestration support or add service_recipe records to route to old macro flows");
307         }
308         if (isPnfCustomizationsInTheService(service)) {
309             buildAndThrowException(execution,
310                     "Cannot orchestrate Service-Macro-Create without user params with a pnf. Please update ASDC model for new macro orchestration support or add service_recipe records to route to old macro flows");
311         }
312         List<CollectionResourceCustomization> customizations = service.getCollectionResourceCustomizations();
313         if (customizations.isEmpty()) {
314             logger.debug("No Collections found. CollectionResourceCustomization list is empty.");
315         } else {
316             CollectionResourceCustomization collectionResourceCustomization =
317                     findCatalogNetworkCollection(execution, service);
318             traverseNetworkCollectionResourceCustomization(resourceList, serviceResource,
319                     collectionResourceCustomization);
320         }
321         traverseNetworkCollectionCustomization(resourceList, serviceResource, service);
322     }
323
324     private void traverseNetworkCollectionResourceCustomization(List<Resource> resourceList, Resource serviceResource,
325             CollectionResourceCustomization collectionResourceCustomization) {
326         if (collectionResourceCustomizationShouldNotBeProcessed(resourceList, serviceResource,
327                 collectionResourceCustomization))
328             return;
329         int minNetworks = 0;
330         org.onap.so.db.catalog.beans.InstanceGroup instanceGroup =
331                 collectionResourceCustomization.getCollectionResource().getInstanceGroup();
332         CollectionResourceInstanceGroupCustomization collectionInstCust = null;
333         if (!instanceGroup.getCollectionInstanceGroupCustomizations().isEmpty()) {
334             for (CollectionResourceInstanceGroupCustomization collectionInstanceGroupTemp : instanceGroup
335                     .getCollectionInstanceGroupCustomizations()) {
336                 if (collectionInstanceGroupTemp.getModelCustomizationUUID()
337                         .equalsIgnoreCase(collectionResourceCustomization.getModelCustomizationUUID())) {
338                     collectionInstCust = collectionInstanceGroupTemp;
339                     break;
340                 }
341             }
342             if (interfaceNetworkQuantityIsAvailableInCollection(collectionInstCust)) {
343                 minNetworks = collectionInstCust.getSubInterfaceNetworkQuantity();
344             }
345         }
346         logger.debug("minNetworks: {}", minNetworks);
347         CollectionNetworkResourceCustomization collectionNetworkResourceCust =
348                 getCollectionNetworkResourceCustomization(collectionResourceCustomization, instanceGroup);
349         for (int i = 0; i < minNetworks; i++) {
350             if (collectionNetworkResourceCust != null) {
351                 Resource resource = new Resource(WorkflowType.VIRTUAL_LINK,
352                         collectionNetworkResourceCust.getModelCustomizationUUID(), false, serviceResource);
353                 resource.setVirtualLinkKey(Integer.toString(i));
354                 resourceList.add(resource);
355             }
356         }
357     }
358
359     private CollectionNetworkResourceCustomization getCollectionNetworkResourceCustomization(
360             CollectionResourceCustomization collectionResourceCustomization, InstanceGroup instanceGroup) {
361         CollectionNetworkResourceCustomization collectionNetworkResourceCust = null;
362         for (CollectionNetworkResourceCustomization collectionNetworkTemp : instanceGroup
363                 .getCollectionNetworkResourceCustomizations()) {
364             if (collectionNetworkTemp.getNetworkResourceCustomization().getModelCustomizationUUID()
365                     .equalsIgnoreCase(collectionResourceCustomization.getModelCustomizationUUID())) {
366                 collectionNetworkResourceCust = collectionNetworkTemp;
367                 break;
368             }
369         }
370         return collectionNetworkResourceCust;
371     }
372
373     private boolean collectionResourceCustomizationShouldNotBeProcessed(List<Resource> resourceList,
374             Resource serviceResource, CollectionResourceCustomization collectionResourceCustomization) {
375         if (collectionResourceCustomization == null) {
376             logger.debug("No Network Collection Customization found");
377             return true;
378         }
379         resourceList.add(new Resource(WorkflowType.NETWORKCOLLECTION,
380                 collectionResourceCustomization.getModelCustomizationUUID(), false, serviceResource));
381         logger.debug("Found a network collection");
382         if (collectionResourceCustomization.getCollectionResource() == null) {
383             logger.debug("No Network Collection found. collectionResource is null");
384             return true;
385         }
386         if (collectionResourceCustomization.getCollectionResource().getInstanceGroup() == null) {
387             logger.debug("No Instance Group found for network collection.");
388             return true;
389         }
390         String toscaNodeType =
391                 collectionResourceCustomization.getCollectionResource().getInstanceGroup().getToscaNodeType();
392         if (!toscaNodeTypeHasNetworkCollection(toscaNodeType)) {
393             logger.debug("Instance Group tosca node type does not contain NetworkCollection:  {}", toscaNodeType);
394             return true;
395         }
396         return false;
397     }
398
399     private boolean interfaceNetworkQuantityIsAvailableInCollection(
400             CollectionResourceInstanceGroupCustomization collectionInstCust) {
401         return collectionInstCust != null && collectionInstCust.getSubInterfaceNetworkQuantity() != null;
402     }
403
404     private boolean toscaNodeTypeHasNetworkCollection(String toscaNodeType) {
405         return toscaNodeType != null && toscaNodeType.contains(NETWORKCOLLECTION);
406     }
407
408     private void traverseNetworkCollectionCustomization(List<Resource> resourceList, Resource serviceResource,
409             org.onap.so.db.catalog.beans.Service service) {
410         if (isNetworkCollectionInTheResourceList(resourceList)) {
411             return;
412         }
413         if (service.getNetworkCustomizations() == null) {
414             logger.debug("No networks were found on this service model");
415             return;
416         }
417         for (int i = 0; i < service.getNetworkCustomizations().size(); i++) {
418             resourceList.add(new Resource(WorkflowType.NETWORK,
419                     service.getNetworkCustomizations().get(i).getModelCustomizationUUID(), false, serviceResource));
420         }
421     }
422
423     private boolean isVnfCustomizationsInTheService(org.onap.so.db.catalog.beans.Service service) {
424         return !(service.getVnfCustomizations() == null || service.getVnfCustomizations().isEmpty());
425     }
426
427     private boolean isPnfCustomizationsInTheService(org.onap.so.db.catalog.beans.Service service) {
428         return !(service.getPnfCustomizations() == null || service.getPnfCustomizations().isEmpty());
429     }
430
431     private void traverseVnfModules(List<Resource> resourceList, Resource vnfResource,
432             List<Pair<WorkflowType, String>> aaiResourceIds,
433             org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf vnf) {
434         if (vnf.getVfModules() == null) {
435             return;
436         }
437         for (VfModule vfModule : vnf.getVfModules()) {
438             aaiResourceIds.add(new Pair<>(WorkflowType.VFMODULE, vfModule.getVfModuleId()));
439             Resource resource = new Resource(WorkflowType.VFMODULE, vfModule.getVfModuleId(), false, vnfResource);
440             resource.setBaseVfModule(vfModule.getModelInfoVfModule().getIsBaseBoolean());
441             resourceList.add(resource);
442         }
443     }
444
445
446     protected String getExistingAAIVrfConfiguration(RelatedInstance relatedVpnBinding,
447             org.onap.aai.domain.yang.L3Network aaiLocalNetwork)
448             throws JsonProcessingException, VrfBondingServiceException {
449         Optional<Relationships> relationshipsOp = new AAIResultWrapper(
450                 new AAICommonObjectMapperProvider().getMapper().writeValueAsString(aaiLocalNetwork)).getRelationships();
451         if (relationshipsOp.isPresent()) {
452             List<AAIResultWrapper> configurationsRelatedToLocalNetwork =
453                     relationshipsOp.get().getByType(AAIFluentTypeBuilder.Types.CONFIGURATION);
454             if (configurationsRelatedToLocalNetwork.size() > 1) {
455                 throw new VrfBondingServiceException(
456                         "Network: " + aaiLocalNetwork.getNetworkId() + " has more than 1 configuration related to it");
457             }
458             if (configurationsRelatedToLocalNetwork.size() == 1) {
459                 AAIResultWrapper configWrapper = configurationsRelatedToLocalNetwork.get(0);
460                 Optional<Configuration> relatedConfiguration = configWrapper.asBean(Configuration.class);
461                 if (relatedConfiguration.isPresent() && vrfConfigurationAlreadyExists(relatedVpnBinding,
462                         relatedConfiguration.get(), configWrapper)) {
463                     return relatedConfiguration.get().getConfigurationId();
464                 }
465             }
466         }
467         return null;
468     }
469
470     protected boolean vrfConfigurationAlreadyExists(RelatedInstance relatedVpnBinding, Configuration vrfConfiguration,
471             AAIResultWrapper configWrapper) throws VrfBondingServiceException {
472         if ("VRF-ENTRY".equalsIgnoreCase(vrfConfiguration.getConfigurationType())) {
473             Optional<Relationships> relationshipsConfigOp = configWrapper.getRelationships();
474             if (relationshipsConfigOp.isPresent()) {
475                 Optional<VpnBinding> relatedInfraVpnBindingOp =
476                         workflowActionUtils.extractRelationshipsVpnBinding(relationshipsConfigOp.get());
477                 if (relatedInfraVpnBindingOp.isPresent()) {
478                     VpnBinding relatedInfraVpnBinding = relatedInfraVpnBindingOp.get();
479                     if (!relatedInfraVpnBinding.getVpnId().equalsIgnoreCase(relatedVpnBinding.getInstanceId())) {
480                         throw new VrfBondingServiceException("Configuration: " + vrfConfiguration.getConfigurationId()
481                                 + " is not connected to the same vpn binding id provided in request: "
482                                 + relatedVpnBinding.getInstanceId());
483                     } else {
484                         return true;
485                     }
486                 }
487             }
488         }
489         return false;
490     }
491
492     public boolean containsWorkflowType(List<Resource> resourceList, WorkflowType workflowType) {
493         return resourceList.stream().anyMatch(resource -> resource.getResourceType().equals(workflowType));
494     }
495
496     public boolean isNetworkCollectionInTheResourceList(List<Resource> resourceList) {
497         return resourceList.stream().anyMatch(x -> WorkflowType.NETWORKCOLLECTION == x.getResourceType());
498     }
499
500     public CollectionResourceCustomization findCatalogNetworkCollection(DelegateExecution execution,
501             org.onap.so.db.catalog.beans.Service service) {
502         CollectionResourceCustomization networkCollection = null;
503         int count = 0;
504         for (CollectionResourceCustomization collectionCust : service.getCollectionResourceCustomizations()) {
505             if (catalogDbClient.getNetworkCollectionResourceCustomizationByID(
506                     collectionCust.getModelCustomizationUUID()) != null) {
507                 networkCollection = collectionCust;
508                 count++;
509             }
510         }
511         if (count == 0) {
512             return null;
513         } else if (count > 1) {
514             buildAndThrowException(execution,
515                     "Found multiple Network Collections in the Service model, only one per Service is supported.");
516         }
517         return networkCollection;
518     }
519
520     protected void buildAndThrowException(DelegateExecution execution, String msg) {
521         logger.error(msg);
522         execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
523         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
524     }
525 }