Enable DeleteChildService functionality
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / ebb / loader / ServiceEBBLoaderTest.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 com.fasterxml.jackson.databind.ObjectMapper;
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
29 import org.javatuples.Pair;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.mockito.Mock;
35 import org.onap.aai.domain.yang.ComposedResource;
36 import org.onap.aai.domain.yang.ComposedResources;
37 import org.onap.aai.domain.yang.RelatedToProperty;
38 import org.onap.aai.domain.yang.Relationship;
39 import org.onap.aai.domain.yang.RelationshipData;
40 import org.onap.aai.domain.yang.RelationshipList;
41 import org.onap.aai.domain.yang.ServiceInstance;
42 import org.onap.aaiclient.client.aai.entities.Relationships;
43 import org.onap.so.bpmn.BaseTaskTest;
44 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
45 import org.onap.so.bpmn.infrastructure.workflow.tasks.VrfBondingServiceException;
46 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
49 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
50 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
51 import org.onap.so.client.exception.ExceptionBuilder;
52 import org.onap.so.client.orchestration.AAIConfigurationResources;
53 import org.onap.so.db.catalog.beans.ConfigurationResourceCustomization;
54 import org.onap.so.db.catalog.beans.Service;
55 import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
56 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
57 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
58 import org.onap.so.db.catalog.beans.CollectionResource;
59 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
60 import org.onap.so.db.catalog.beans.InstanceGroup;
61 import org.onap.so.db.catalog.client.CatalogDbClient;
62 import org.onap.so.serviceinstancebeans.RelatedInstance;
63 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
64 import java.io.IOException;
65 import java.nio.file.Files;
66 import java.nio.file.Paths;
67 import java.util.ArrayList;
68 import java.util.List;
69 import java.util.Optional;
70 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
71 import static org.junit.Assert.assertEquals;
72 import static org.junit.Assert.assertNull;
73 import static org.junit.Assert.assertTrue;
74 import static org.junit.Assert.assertFalse;
75 import static org.junit.Assert.assertNotNull;
76 import static org.junit.Assert.assertThat;
77 import static org.junit.Assert.fail;
78 import static org.mockito.Mockito.mock;
79 import static org.mockito.Mockito.doReturn;
80 import static org.mockito.Mockito.any;
81 import static org.mockito.Mockito.anyList;
82 import static org.mockito.Mockito.anyString;
83
84 public class ServiceEBBLoaderTest extends BaseTaskTest {
85
86     private static final String MACRO_ACTIVATE_DELETE_UNASSIGN_JSON = "Macro/ServiceMacroActivateDeleteUnassign.json";
87     private static final String MACRO_ASSIGN_JSON = "Macro/ServiceMacroAssign.json";
88
89     @Rule
90     public ExpectedException thrown = ExpectedException.none();
91
92     @Mock
93     protected Relationships relationships;
94
95     private DelegateExecution execution;
96     private ServiceEBBLoader serviceEBBLoader;
97     private UserParamsServiceTraversal mockUserParamsServiceTraversal;
98     private CatalogDbClient mockCatalogDbClient;
99     private VrfValidation mockVrfValidation;
100     private AAIConfigurationResources mockAaiConfigurationResources;
101     private WorkflowActionExtractResourcesAAI mockWorkflowActionExtractResourcesAAI;
102     private BBInputSetupUtils mockBbInputSetupUtils;
103     private BBInputSetup mockBbInputSetup;
104
105     @Before
106     public void before() throws Exception {
107         execution = new DelegateExecutionFake();
108         mockUserParamsServiceTraversal = mock(UserParamsServiceTraversal.class);
109         mockCatalogDbClient = mock(CatalogDbClient.class);
110         mockVrfValidation = mock(VrfValidation.class);
111         mockAaiConfigurationResources = mock(AAIConfigurationResources.class);
112         mockWorkflowActionExtractResourcesAAI = mock(WorkflowActionExtractResourcesAAI.class);
113         mockBbInputSetupUtils = mock(BBInputSetupUtils.class);
114         mockBbInputSetup = mock(BBInputSetup.class);
115         serviceEBBLoader = new ServiceEBBLoader(mockUserParamsServiceTraversal, mockCatalogDbClient, mockVrfValidation,
116                 mockAaiConfigurationResources, mockWorkflowActionExtractResourcesAAI, mockBbInputSetupUtils,
117                 mockBbInputSetup, mock(ExceptionBuilder.class));
118     }
119
120
121     @Test
122     public void getResourceListForServiceWithRequestActionAssignInstance()
123             throws IOException, VrfBondingServiceException {
124         String bpmnRequest = readBpmnRequestFromFile(MACRO_ASSIGN_JSON);
125         ObjectMapper mapper = new ObjectMapper();
126         ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class);
127         String requestAction = "assignInstance";
128         String serviceInstanceId = "123";
129         String resourceId = "si0";
130         List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
131         doReturn(prepareListWithResources()).when(mockUserParamsServiceTraversal).getResourceListFromUserParams(any(),
132                 anyList(), anyString(), anyString());
133         List<Resource> resources = serviceEBBLoader.getResourceListForService(sIRequest, requestAction, execution,
134                 serviceInstanceId, resourceId, aaiResourceIds);
135         assertNotNull(resources);
136         assertEquals(resources.size(), 6);
137     }
138
139     @Test
140     public void findCatalogNetworkCollectionTest() {
141         Service service = new Service();
142         NetworkCollectionResourceCustomization networkCustomization = new NetworkCollectionResourceCustomization();
143         networkCustomization.setModelCustomizationUUID("123");
144         service.getCollectionResourceCustomizations().add(networkCustomization);
145         doReturn(networkCustomization).when(mockCatalogDbClient).getNetworkCollectionResourceCustomizationByID("123");
146         CollectionResourceCustomization customization =
147                 serviceEBBLoader.findCatalogNetworkCollection(execution, service);
148         assertNotNull(customization);
149     }
150
151     @Test
152     public void findCatalogNetworkCollectionEmptyTest() {
153         Service service = new Service();
154         NetworkCollectionResourceCustomization networkCustomization = new NetworkCollectionResourceCustomization();
155         networkCustomization.setModelCustomizationUUID("123");
156         service.getCollectionResourceCustomizations().add(networkCustomization);
157         CollectionResourceCustomization customization =
158                 serviceEBBLoader.findCatalogNetworkCollection(execution, service);
159         assertNull(customization);
160     }
161
162     @Test
163     public void findCatalogNetworkCollectionMoreThanOneTest() {
164         Service service = new Service();
165         NetworkCollectionResourceCustomization networkCustomization1 = new NetworkCollectionResourceCustomization();
166         networkCustomization1.setModelCustomizationUUID("123");
167         NetworkCollectionResourceCustomization networkCustomization2 = new NetworkCollectionResourceCustomization();
168         networkCustomization2.setModelCustomizationUUID("321");
169         service.getCollectionResourceCustomizations().add(networkCustomization1);
170         service.getCollectionResourceCustomizations().add(networkCustomization2);
171         doReturn(networkCustomization1).when(mockCatalogDbClient).getNetworkCollectionResourceCustomizationByID("123");
172         doReturn(networkCustomization2).when(mockCatalogDbClient).getNetworkCollectionResourceCustomizationByID("321");
173         serviceEBBLoader.findCatalogNetworkCollection(execution, service);
174         assertEquals("Found multiple Network Collections in the Service model, only one per Service is supported.",
175                 execution.getVariable("WorkflowActionErrorMessage"));
176     }
177
178     @Test
179     public void foundRelatedTest() {
180         List<Resource> resourceList = new ArrayList<>();
181         resourceList.add(new Resource(WorkflowType.PNF, "model customization id", false, null));
182         resourceList.add(new Resource(WorkflowType.VNF, "model customization id", false, null));
183         resourceList.add(new Resource(WorkflowType.NETWORK, "model customization id", false, null));
184         resourceList.add(new Resource(WorkflowType.NETWORKCOLLECTION, "model customization id", false, null));
185
186         assertTrue(serviceEBBLoader.foundRelated(resourceList));
187     }
188
189     @Test
190     public void containsWorkflowTypeTest() {
191         List<Resource> resourceList = new ArrayList<>();
192         resourceList.add(new Resource(WorkflowType.PNF, "resource id", false, null));
193         resourceList.add(new Resource(WorkflowType.VNF, "model customization id", false, null));
194         resourceList.add(new Resource(WorkflowType.NETWORK, "model customization id", false, null));
195         resourceList.add(new Resource(WorkflowType.NETWORKCOLLECTION, "model customization id", false, null));
196
197         assertTrue(serviceEBBLoader.containsWorkflowType(resourceList, WorkflowType.PNF));
198         assertTrue(serviceEBBLoader.containsWorkflowType(resourceList, WorkflowType.VNF));
199         assertTrue(serviceEBBLoader.containsWorkflowType(resourceList, WorkflowType.NETWORK));
200         assertTrue(serviceEBBLoader.containsWorkflowType(resourceList, WorkflowType.NETWORKCOLLECTION));
201         assertFalse(serviceEBBLoader.containsWorkflowType(resourceList, WorkflowType.CONFIGURATION));
202     }
203
204     @Test
205     public void traverseAAIServiceTest() {
206         List<Resource> resourceCounter = new ArrayList<>();
207         String resourceId = "si0";
208         List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
209
210         ServiceInstance serviceInstanceAAI = new ServiceInstance();
211         serviceInstanceAAI.setServiceInstanceId(resourceId);
212
213         org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstance = setServiceInstance();
214         GenericVnf genericVnf = setGenericVnf();
215         setVfModule(true);
216         setVolumeGroup();
217         setL3Network();
218         setCollection();
219         setConfiguration();
220
221         org.onap.aai.domain.yang.GenericVnf genericVnfAai = new org.onap.aai.domain.yang.GenericVnf();
222         genericVnfAai.setModelCustomizationId(genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid());
223
224         Configuration config = new Configuration();
225         config.setConfigurationId("testConfigurationId2");
226         serviceInstance.getConfigurations().add(config);
227
228         Relationship relationship1 = new Relationship();
229         relationship1.setRelatedTo("vnfc");
230         RelationshipList relationshipList1 = new RelationshipList();
231         relationshipList1.getRelationship().add(relationship1);
232
233         Relationship relationship2 = new Relationship();
234         relationship2.setRelatedTo("vpn-binding");
235         RelationshipList relationshipList2 = new RelationshipList();
236         relationshipList2.getRelationship().add(relationship2);
237
238         org.onap.aai.domain.yang.Configuration aaiConfiguration1 = new org.onap.aai.domain.yang.Configuration();
239         aaiConfiguration1.setConfigurationId("testConfigurationId");
240         aaiConfiguration1.setRelationshipList(relationshipList1);
241
242         org.onap.aai.domain.yang.Configuration aaiConfiguration2 = new org.onap.aai.domain.yang.Configuration();
243         aaiConfiguration2.setConfigurationId("testConfigurationId2");
244         aaiConfiguration2.setRelationshipList(relationshipList1);
245
246         try {
247             doReturn(genericVnfAai).when(mockBbInputSetupUtils).getAAIGenericVnf(genericVnf.getVnfId());
248             doReturn(serviceInstanceAAI).when(mockBbInputSetupUtils).getAAIServiceInstanceById(resourceId);
249             doReturn(serviceInstance).when(mockBbInputSetup).getExistingServiceInstance(serviceInstanceAAI);
250             doReturn(Optional.of(aaiConfiguration1)).when(mockAaiConfigurationResources)
251                     .getConfiguration("testConfigurationId");
252             doReturn(Optional.of(aaiConfiguration2)).when(mockAaiConfigurationResources)
253                     .getConfiguration("testConfigurationId2");
254             serviceEBBLoader.traverseAAIService(execution, resourceCounter, resourceId, aaiResourceIds);
255             assertEquals(8, resourceCounter.size());
256             assertTrue(resourceCounter.get(2).isBaseVfModule());
257             assertThat(aaiResourceIds, sameBeanAs(getExpectedResourceIds()));
258         } catch (Exception e) {
259             fail("Unexpected exception was thrown.");
260         }
261     }
262
263     @Test
264     public void traverseVrfConfigurationTest() throws VrfBondingServiceException, JsonProcessingException {
265         List<Resource> resource = new ArrayList<>();
266         List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
267
268         Service service = new Service();
269         List<ConfigurationResourceCustomization> resourceCustomizations = new ArrayList<>();
270
271         ConfigurationResourceCustomization configuration = new ConfigurationResourceCustomization();
272         configuration.setModelCustomizationUUID("123");
273         resourceCustomizations.add(configuration);
274         service.setConfigurationCustomizations(resourceCustomizations);
275
276         Relationship relationship = new Relationship();
277         relationship.setRelatedTo("vpn-binding");
278
279         RelationshipList relationshipList = new RelationshipList();
280         relationshipList.getRelationship().add(relationship);
281
282         org.onap.aai.domain.yang.L3Network aaiLocalNetwork = new org.onap.aai.domain.yang.L3Network();
283         aaiLocalNetwork.setNetworkId("localNetworkId");
284         aaiLocalNetwork.setRelationshipList(relationshipList);
285
286         RelatedInstance relatedVpnBinding = new RelatedInstance();
287         relatedVpnBinding.setInstanceId("vpnBindingInstanceId");
288         RelatedInstance relatedLocalNetwork = new RelatedInstance();
289         relatedLocalNetwork.setInstanceId("localNetworkInstanceId");
290
291
292         doReturn(aaiLocalNetwork).when(mockBbInputSetupUtils).getAAIL3Network("localNetworkInstanceId");
293
294         Resource serviceResource = new Resource(WorkflowType.SERVICE, "1", false, null);
295         serviceEBBLoader.traverseVrfConfiguration(aaiResourceIds, resource, serviceResource, service, relatedVpnBinding,
296                 relatedLocalNetwork);
297         assertEquals(resource.size(), 1);
298         assertEquals(aaiResourceIds.size(), 0);
299     }
300
301     private List<Pair<WorkflowType, String>> getExpectedResourceIds() {
302         List<Pair<WorkflowType, String>> resourceIds = new ArrayList<>();
303         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VNF, "testVnfId1"));
304         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VFMODULE, "testVfModuleId1"));
305         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VOLUMEGROUP, "testVolumeGroupId1"));
306         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.NETWORK, "testNetworkId1"));
307         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.NETWORKCOLLECTION, "testId"));
308         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.CONFIGURATION, "testConfigurationId"));
309         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.CONFIGURATION, "testConfigurationId2"));
310         return resourceIds;
311     }
312
313     @Test
314     public void traverseCatalogDbServiceMultipleNetworkTest() throws IOException, VrfBondingServiceException {
315         execution.setVariable("testProcessKey", "testProcessKeyValue");
316         Service service = new Service();
317         List<NetworkResourceCustomization> networkCustomizations = new ArrayList<>();
318         NetworkResourceCustomization networkCust = new NetworkResourceCustomization();
319         networkCust.setModelCustomizationUUID("123");
320         networkCustomizations.add(networkCust);
321         service.setNetworkCustomizations(networkCustomizations);
322         NetworkCollectionResourceCustomization collectionResourceCustomization =
323                 new NetworkCollectionResourceCustomization();
324         collectionResourceCustomization.setModelCustomizationUUID("123");
325         CollectionResource collectionResource = new CollectionResource();
326         collectionResource.setToscaNodeType("NetworkCollection");
327         InstanceGroup instanceGroup = new InstanceGroup();
328         List<CollectionResourceInstanceGroupCustomization> collectionInstanceGroupCustomizations = new ArrayList<>();
329         CollectionResourceInstanceGroupCustomization collectionInstanceGroupCustomization =
330                 new CollectionResourceInstanceGroupCustomization();
331         collectionInstanceGroupCustomization.setSubInterfaceNetworkQuantity(3);
332         collectionInstanceGroupCustomizations.add(collectionInstanceGroupCustomization);
333         instanceGroup.setCollectionInstanceGroupCustomizations(collectionInstanceGroupCustomizations);
334         collectionResource.setInstanceGroup(instanceGroup);
335         collectionResourceCustomization.setCollectionResource(collectionResource);;
336         service.setModelUUID("abc");
337         service.getCollectionResourceCustomizations().add(collectionResourceCustomization);
338         service.getCollectionResourceCustomizations().add(collectionResourceCustomization);
339
340
341         doReturn(service).when(mockCatalogDbClient).getServiceByID("3c40d244-808e-42ca-b09a-256d83d19d0a");
342         doReturn(collectionResourceCustomization).when(mockCatalogDbClient)
343                 .getNetworkCollectionResourceCustomizationByID("123");
344         String bpmnRequest = readBpmnRequestFromFile(MACRO_ACTIVATE_DELETE_UNASSIGN_JSON);
345         ObjectMapper mapper = new ObjectMapper();
346         ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class);
347
348         List<Resource> resource = new ArrayList<>();
349         List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
350
351         serviceEBBLoader.traverseCatalogDbService(execution, sIRequest, resource, aaiResourceIds);
352         assertEquals(resource.size(), 2);
353     }
354
355     private String readBpmnRequestFromFile(String fileName) throws IOException {
356         return new String(Files.readAllBytes(Paths.get("src/test/resources/__files/" + fileName)));
357     }
358
359     private List<Resource> prepareListWithResources() {
360         List<Resource> resourceList = new ArrayList<>();
361         Resource r1 = new Resource(WorkflowType.SERVICE, "3c40d244-808e-42ca-b09a-256d83d19d0a", false, null);
362         resourceList.add(r1);
363         Resource r2 = new Resource(WorkflowType.VNF, "ab153b6e-c364-44c0-bef6-1f2982117f04", false, r1);
364         resourceList.add(r2);
365         resourceList.add(new Resource(WorkflowType.VOLUMEGROUP, "a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f", false, r2));
366         resourceList.add(new Resource(WorkflowType.VFMODULE, "72d9d1cd-f46d-447a-abdb-451d6fb05fa8", false, r2));
367         resourceList.add(new Resource(WorkflowType.VFMODULE, "3c40d244-808e-42ca-b09a-256d83d19d0a", false, r2));
368         resourceList.add(new Resource(WorkflowType.VFMODULE, "72d9d1cd-f46d-447a-abdb-451d6fb05fa8", false, r2));
369         return resourceList;
370     }
371
372     @Test
373     public void traverseServiceInstanceChildServiceTest() {
374         List<Resource> resourceList = new ArrayList<>();
375         Resource parentResource = new Resource(WorkflowType.SERVICE, "parentId", false, null);
376         String resourceId = "siP";
377         ServiceInstance serviceInstanceAAI = new ServiceInstance();
378         serviceInstanceAAI.setServiceInstanceId(resourceId);
379
380         RelationshipData relationshipData = new RelationshipData();
381         relationshipData.setRelationshipKey("service-instance.service-instance-id");
382         relationshipData.setRelationshipValue("80ced9d5-666e-406b-88f0-a05d31328b70");
383         RelatedToProperty relatedToProperty = new RelatedToProperty();
384         relatedToProperty.setPropertyKey("service-instance.service-instance-name");
385         relatedToProperty.setPropertyValue("child_euler_002");
386
387         RelationshipData relationshipData1 = new RelationshipData();
388         relationshipData1.setRelationshipKey("service-instance.service-instance-id");
389         relationshipData1.setRelationshipValue("fa5640af-c827-4372-baae-7f1c50fdb5ed");
390         RelatedToProperty relatedToProperty1 = new RelatedToProperty();
391         relatedToProperty1.setPropertyKey("service-instance.service-instance-name");
392         relatedToProperty.setPropertyValue("child_euler_001");
393
394
395         Relationship relationship = new Relationship();
396         Relationship relationship1 = new Relationship();
397         relationship.setRelatedTo("service-instance");
398         relationship1.setRelatedTo("service-instance");
399         relationship.getRelationshipData().add(relationshipData);
400         relationship.getRelatedToProperty().add(relatedToProperty);
401         relationship1.getRelationshipData().add(relationshipData1);
402         relationship1.getRelatedToProperty().add(relatedToProperty1);
403
404         RelationshipList relationshipList = new RelationshipList();
405         RelationshipList relationshipList1 = new RelationshipList();
406         relationshipList.getRelationship().add(relationship);
407         relationshipList1.getRelationship().add(relationship1);
408
409         ComposedResource composedResource = new ComposedResource();
410         composedResource.setRelationshipList(relationshipList);
411         ComposedResource composedResource1 = new ComposedResource();
412         composedResource1.setRelationshipList(relationshipList);
413
414         ComposedResources composedResources = new ComposedResources();
415         composedResources.getComposedResource().add(composedResource);
416         composedResources.getComposedResource().add(composedResource1);
417
418         serviceInstanceAAI.setComposedResources(composedResources);
419
420         serviceEBBLoader.traverseServiceInstanceChildService(resourceList, parentResource, serviceInstanceAAI);
421         assertEquals(2, resourceList.size());
422     }
423 }