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