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