Merge branch 'recursive-orch'
[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         org.onap.aai.domain.yang.VfModule aaiVfModule = new org.onap.aai.domain.yang.VfModule();
247         aaiVfModule.setIsBaseVfModule(true);
248
249         try {
250             doReturn(genericVnfAai).when(mockBbInputSetupUtils).getAAIGenericVnf(genericVnf.getVnfId());
251             doReturn(serviceInstanceAAI).when(mockBbInputSetupUtils).getAAIServiceInstanceById(resourceId);
252             doReturn(serviceInstance).when(mockBbInputSetup).getExistingServiceInstance(serviceInstanceAAI);
253             doReturn(Optional.of(aaiConfiguration1)).when(mockAaiConfigurationResources)
254                     .getConfiguration("testConfigurationId");
255             doReturn(Optional.of(aaiConfiguration2)).when(mockAaiConfigurationResources)
256                     .getConfiguration("testConfigurationId2");
257             doReturn(aaiVfModule).when(mockBbInputSetupUtils).getAAIVfModule(any(), any());
258             serviceEBBLoader.traverseAAIService(execution, resourceCounter, resourceId, aaiResourceIds);
259             assertEquals(8, resourceCounter.size());
260             assertTrue(resourceCounter.get(2).isBaseVfModule());
261             assertThat(aaiResourceIds, sameBeanAs(getExpectedResourceIds()));
262         } catch (Exception e) {
263             fail("Unexpected exception was thrown.");
264         }
265     }
266
267     @Test
268     public void traverseVrfConfigurationTest() throws VrfBondingServiceException, JsonProcessingException {
269         List<Resource> resource = new ArrayList<>();
270         List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
271
272         Service service = new Service();
273         List<ConfigurationResourceCustomization> resourceCustomizations = new ArrayList<>();
274
275         ConfigurationResourceCustomization configuration = new ConfigurationResourceCustomization();
276         configuration.setModelCustomizationUUID("123");
277         resourceCustomizations.add(configuration);
278         service.setConfigurationCustomizations(resourceCustomizations);
279
280         Relationship relationship = new Relationship();
281         relationship.setRelatedTo("vpn-binding");
282
283         RelationshipList relationshipList = new RelationshipList();
284         relationshipList.getRelationship().add(relationship);
285
286         org.onap.aai.domain.yang.L3Network aaiLocalNetwork = new org.onap.aai.domain.yang.L3Network();
287         aaiLocalNetwork.setNetworkId("localNetworkId");
288         aaiLocalNetwork.setRelationshipList(relationshipList);
289
290         RelatedInstance relatedVpnBinding = new RelatedInstance();
291         relatedVpnBinding.setInstanceId("vpnBindingInstanceId");
292         RelatedInstance relatedLocalNetwork = new RelatedInstance();
293         relatedLocalNetwork.setInstanceId("localNetworkInstanceId");
294
295
296         doReturn(aaiLocalNetwork).when(mockBbInputSetupUtils).getAAIL3Network("localNetworkInstanceId");
297
298         Resource serviceResource = new Resource(WorkflowType.SERVICE, "1", false, null);
299         serviceEBBLoader.traverseVrfConfiguration(aaiResourceIds, resource, serviceResource, service, relatedVpnBinding,
300                 relatedLocalNetwork);
301         assertEquals(resource.size(), 1);
302         assertEquals(aaiResourceIds.size(), 0);
303     }
304
305     private List<Pair<WorkflowType, String>> getExpectedResourceIds() {
306         List<Pair<WorkflowType, String>> resourceIds = new ArrayList<>();
307         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VNF, "testVnfId1"));
308         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VFMODULE, "testVfModuleId1"));
309         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VOLUMEGROUP, "testVolumeGroupId1"));
310         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.NETWORK, "testNetworkId1"));
311         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.NETWORKCOLLECTION, "testId"));
312         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.CONFIGURATION, "testConfigurationId"));
313         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.CONFIGURATION, "testConfigurationId2"));
314         return resourceIds;
315     }
316
317     @Test
318     public void traverseCatalogDbServiceMultipleNetworkTest() throws IOException, VrfBondingServiceException {
319         execution.setVariable("testProcessKey", "testProcessKeyValue");
320         Service service = new Service();
321         List<NetworkResourceCustomization> networkCustomizations = new ArrayList<>();
322         NetworkResourceCustomization networkCust = new NetworkResourceCustomization();
323         networkCust.setModelCustomizationUUID("123");
324         networkCustomizations.add(networkCust);
325         service.setNetworkCustomizations(networkCustomizations);
326         NetworkCollectionResourceCustomization collectionResourceCustomization =
327                 new NetworkCollectionResourceCustomization();
328         collectionResourceCustomization.setModelCustomizationUUID("123");
329         CollectionResource collectionResource = new CollectionResource();
330         collectionResource.setToscaNodeType("NetworkCollection");
331         InstanceGroup instanceGroup = new InstanceGroup();
332         List<CollectionResourceInstanceGroupCustomization> collectionInstanceGroupCustomizations = new ArrayList<>();
333         CollectionResourceInstanceGroupCustomization collectionInstanceGroupCustomization =
334                 new CollectionResourceInstanceGroupCustomization();
335         collectionInstanceGroupCustomization.setSubInterfaceNetworkQuantity(3);
336         collectionInstanceGroupCustomizations.add(collectionInstanceGroupCustomization);
337         instanceGroup.setCollectionInstanceGroupCustomizations(collectionInstanceGroupCustomizations);
338         collectionResource.setInstanceGroup(instanceGroup);
339         collectionResourceCustomization.setCollectionResource(collectionResource);;
340         service.setModelUUID("abc");
341         service.getCollectionResourceCustomizations().add(collectionResourceCustomization);
342         service.getCollectionResourceCustomizations().add(collectionResourceCustomization);
343
344
345         doReturn(service).when(mockCatalogDbClient).getServiceByID("3c40d244-808e-42ca-b09a-256d83d19d0a");
346         doReturn(collectionResourceCustomization).when(mockCatalogDbClient)
347                 .getNetworkCollectionResourceCustomizationByID("123");
348         String bpmnRequest = readBpmnRequestFromFile(MACRO_ACTIVATE_DELETE_UNASSIGN_JSON);
349         ObjectMapper mapper = new ObjectMapper();
350         ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class);
351
352         List<Resource> resource = new ArrayList<>();
353         List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
354
355         serviceEBBLoader.traverseCatalogDbService(execution, sIRequest, resource, aaiResourceIds);
356         assertEquals(resource.size(), 2);
357     }
358
359     private String readBpmnRequestFromFile(String fileName) throws IOException {
360         return new String(Files.readAllBytes(Paths.get("src/test/resources/__files/" + fileName)));
361     }
362
363     private List<Resource> prepareListWithResources() {
364         List<Resource> resourceList = new ArrayList<>();
365         Resource r1 = new Resource(WorkflowType.SERVICE, "3c40d244-808e-42ca-b09a-256d83d19d0a", false, null);
366         resourceList.add(r1);
367         Resource r2 = new Resource(WorkflowType.VNF, "ab153b6e-c364-44c0-bef6-1f2982117f04", false, r1);
368         resourceList.add(r2);
369         resourceList.add(new Resource(WorkflowType.VOLUMEGROUP, "a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f", false, r2));
370         resourceList.add(new Resource(WorkflowType.VFMODULE, "72d9d1cd-f46d-447a-abdb-451d6fb05fa8", false, r2));
371         resourceList.add(new Resource(WorkflowType.VFMODULE, "3c40d244-808e-42ca-b09a-256d83d19d0a", false, r2));
372         resourceList.add(new Resource(WorkflowType.VFMODULE, "72d9d1cd-f46d-447a-abdb-451d6fb05fa8", false, r2));
373         return resourceList;
374     }
375
376     @Test
377     public void traverseServiceInstanceChildServiceTest() {
378         List<Resource> resourceList = new ArrayList<>();
379         Resource parentResource = new Resource(WorkflowType.SERVICE, "parentId", false, null);
380         String resourceId = "siP";
381         ServiceInstance serviceInstanceAAI = new ServiceInstance();
382         serviceInstanceAAI.setServiceInstanceId(resourceId);
383
384         RelationshipData relationshipData = new RelationshipData();
385         relationshipData.setRelationshipKey("service-instance.service-instance-id");
386         relationshipData.setRelationshipValue("80ced9d5-666e-406b-88f0-a05d31328b70");
387         RelatedToProperty relatedToProperty = new RelatedToProperty();
388         relatedToProperty.setPropertyKey("service-instance.service-instance-name");
389         relatedToProperty.setPropertyValue("child_euler_002");
390
391         RelationshipData relationshipData1 = new RelationshipData();
392         relationshipData1.setRelationshipKey("service-instance.service-instance-id");
393         relationshipData1.setRelationshipValue("fa5640af-c827-4372-baae-7f1c50fdb5ed");
394         RelatedToProperty relatedToProperty1 = new RelatedToProperty();
395         relatedToProperty1.setPropertyKey("service-instance.service-instance-name");
396         relatedToProperty.setPropertyValue("child_euler_001");
397
398
399         Relationship relationship = new Relationship();
400         Relationship relationship1 = new Relationship();
401         relationship.setRelatedTo("service-instance");
402         relationship1.setRelatedTo("service-instance");
403         relationship.getRelationshipData().add(relationshipData);
404         relationship.getRelatedToProperty().add(relatedToProperty);
405         relationship1.getRelationshipData().add(relationshipData1);
406         relationship1.getRelatedToProperty().add(relatedToProperty1);
407
408         RelationshipList relationshipList = new RelationshipList();
409         RelationshipList relationshipList1 = new RelationshipList();
410         relationshipList.getRelationship().add(relationship);
411         relationshipList1.getRelationship().add(relationship1);
412
413         ComposedResource composedResource = new ComposedResource();
414         composedResource.setRelationshipList(relationshipList);
415         ComposedResource composedResource1 = new ComposedResource();
416         composedResource1.setRelationshipList(relationshipList);
417
418         ComposedResources composedResources = new ComposedResources();
419         composedResources.getComposedResource().add(composedResource);
420         composedResources.getComposedResource().add(composedResource1);
421
422         serviceInstanceAAI.setComposedResources(composedResources);
423
424         serviceEBBLoader.traverseServiceInstanceChildService(resourceList, parentResource, serviceInstanceAAI);
425         assertEquals(2, resourceList.size());
426     }
427 }