2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.infrastructure.workflow.tasks.ebb.loader;
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;
80 public class ServiceEBBLoaderTest extends BaseTaskTest {
82 private static final String MACRO_ACTIVATE_DELETE_UNASSIGN_JSON = "Macro/ServiceMacroActivateDeleteUnassign.json";
83 private static final String MACRO_ASSIGN_JSON = "Macro/ServiceMacroAssign.json";
86 public ExpectedException thrown = ExpectedException.none();
89 protected Relationships relationships;
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;
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));
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);
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);
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);
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"));
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));
182 assertTrue(serviceEBBLoader.foundRelated(resourceList));
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));
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));
201 public void traverseAAIServiceTest() {
202 List<Resource> resourceCounter = new ArrayList<>();
203 String resourceId = "si0";
204 List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
206 ServiceInstance serviceInstanceAAI = new ServiceInstance();
207 serviceInstanceAAI.setServiceInstanceId(resourceId);
209 org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstance = setServiceInstance();
210 GenericVnf genericVnf = setGenericVnf();
217 org.onap.aai.domain.yang.GenericVnf genericVnfAai = new org.onap.aai.domain.yang.GenericVnf();
218 genericVnfAai.setModelCustomizationId(genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid());
220 Configuration config = new Configuration();
221 config.setConfigurationId("testConfigurationId2");
222 serviceInstance.getConfigurations().add(config);
224 Relationship relationship1 = new Relationship();
225 relationship1.setRelatedTo("vnfc");
226 RelationshipList relationshipList1 = new RelationshipList();
227 relationshipList1.getRelationship().add(relationship1);
229 Relationship relationship2 = new Relationship();
230 relationship2.setRelatedTo("vpn-binding");
231 RelationshipList relationshipList2 = new RelationshipList();
232 relationshipList2.getRelationship().add(relationship2);
234 org.onap.aai.domain.yang.Configuration aaiConfiguration1 = new org.onap.aai.domain.yang.Configuration();
235 aaiConfiguration1.setConfigurationId("testConfigurationId");
236 aaiConfiguration1.setRelationshipList(relationshipList1);
238 org.onap.aai.domain.yang.Configuration aaiConfiguration2 = new org.onap.aai.domain.yang.Configuration();
239 aaiConfiguration2.setConfigurationId("testConfigurationId2");
240 aaiConfiguration2.setRelationshipList(relationshipList1);
242 org.onap.aai.domain.yang.VfModule aaiVfModule = new org.onap.aai.domain.yang.VfModule();
243 aaiVfModule.setIsBaseVfModule(true);
246 doReturn(genericVnfAai).when(mockBbInputSetupUtils).getAAIGenericVnf(genericVnf.getVnfId());
247 doReturn(serviceInstanceAAI).when(mockBbInputSetupUtils).getAAIServiceInstanceById(resourceId);
248 doReturn(serviceInstance).when(mockBbInputSetup).getExistingServiceInstance(serviceInstanceAAI);
249 doReturn(Optional.of(aaiConfiguration1)).when(mockAaiConfigurationResources)
250 .getConfiguration("testConfigurationId");
251 doReturn(Optional.of(aaiConfiguration2)).when(mockAaiConfigurationResources)
252 .getConfiguration("testConfigurationId2");
253 doReturn(aaiVfModule).when(mockBbInputSetupUtils).getAAIVfModule(any(), any());
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.");
264 public void traverseVrfConfigurationTest() throws VrfBondingServiceException, JsonProcessingException {
265 List<Resource> resource = new ArrayList<>();
266 List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
268 Service service = new Service();
269 List<ConfigurationResourceCustomization> resourceCustomizations = new ArrayList<>();
271 ConfigurationResourceCustomization configuration = new ConfigurationResourceCustomization();
272 configuration.setModelCustomizationUUID("123");
273 resourceCustomizations.add(configuration);
274 service.setConfigurationCustomizations(resourceCustomizations);
276 Relationship relationship = new Relationship();
277 relationship.setRelatedTo("vpn-binding");
279 RelationshipList relationshipList = new RelationshipList();
280 relationshipList.getRelationship().add(relationship);
282 org.onap.aai.domain.yang.L3Network aaiLocalNetwork = new org.onap.aai.domain.yang.L3Network();
283 aaiLocalNetwork.setNetworkId("localNetworkId");
284 aaiLocalNetwork.setRelationshipList(relationshipList);
286 RelatedInstance relatedVpnBinding = new RelatedInstance();
287 relatedVpnBinding.setInstanceId("vpnBindingInstanceId");
288 RelatedInstance relatedLocalNetwork = new RelatedInstance();
289 relatedLocalNetwork.setInstanceId("localNetworkInstanceId");
292 doReturn(aaiLocalNetwork).when(mockBbInputSetupUtils).getAAIL3Network("localNetworkInstanceId");
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);
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"));
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);
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);
348 List<Resource> resource = new ArrayList<>();
349 List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
351 serviceEBBLoader.traverseCatalogDbService(execution, sIRequest, resource, aaiResourceIds);
352 assertEquals(resource.size(), 2);
355 private String readBpmnRequestFromFile(String fileName) throws IOException {
356 return new String(Files.readAllBytes(Paths.get("src/test/resources/__files/" + fileName)));
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));