Multiple PNFs with same ModelInfo but different instance name not able to be instantiated
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / ebb / loader / UserParamsServiceTraversalTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Modifications Copyright (c) 2020 Nokia
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.workflow.tasks.ebb.loader;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.so.bpmn.BaseTaskTest;
30 import org.onap.so.bpmn.common.BBConstants;
31 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
32 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
33 import org.onap.so.client.exception.ExceptionBuilder;
34 import org.onap.so.db.catalog.beans.ConfigurationResource;
35 import org.onap.so.db.catalog.beans.CvnfcCustomization;
36 import org.onap.so.db.catalog.beans.HeatTemplate;
37 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
38 import org.onap.so.db.catalog.beans.VfModuleCustomization;
39 import org.onap.so.db.catalog.beans.Service;
40 import org.onap.so.db.catalog.beans.VfModule;
41 import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
42 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
43 import org.onap.so.db.catalog.beans.HeatEnvironment;
44 import org.onap.so.db.catalog.client.CatalogDbClient;
45 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
46 import java.io.IOException;
47 import java.nio.file.Files;
48 import java.nio.file.Paths;
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.stream.Collectors;
53 import static org.hamcrest.MatcherAssert.assertThat;
54 import static org.hamcrest.Matchers.is;
55 import static org.junit.Assert.assertEquals;
56 import static org.junit.Assert.assertTrue;
57 import static org.mockito.ArgumentMatchers.anyString;
58 import static org.mockito.Mockito.mock;
59
60 public class UserParamsServiceTraversalTest extends BaseTaskTest {
61
62     private static final String MACRO_ASSIGN_JSON = "Macro/ServiceMacroAssign.json";
63     private static final String MACRO_CREATE_JSON = "Macro/ServiceMacroAssignVnfAndPnf.json";
64     private static final String MACRO_ASSIGN_PNF_JSON = "Macro/ServiceMacroAssignPnf.json";
65     private static final String NETWORK_COLLECTION_JSON = "Macro/CreateNetworkCollection.json";
66     private static final String MACRO_CREATE_WITHOUT_RESOURCES_JSON = "Macro/ServiceMacroCreateWithoutResources.json";
67     private static final String MACRO_CREATE_SVC_SAME_MODEL_VNF_VFMODULE =
68             "Macro/ServiceMacroCreateMultipleSameModelVnfsAndVfModules.json";
69     private static final String MACRO_CREATE_SVC_SAME_MODEL_PNF = "Macro/ServiceMacroCreateMultipleSameModelPnfs.json";
70     private static final String serviceInstanceId = "123";
71     private DelegateExecution execution;
72     private CatalogDbClient mockCatalogDbClient;
73     private UserParamsServiceTraversal userParamsServiceTraversal;
74     private String requestAction;
75
76     @Before
77     public void before() throws Exception {
78         execution = new DelegateExecutionFake();
79         mockCatalogDbClient = mock(CatalogDbClient.class);
80         userParamsServiceTraversal = new UserParamsServiceTraversal(mockCatalogDbClient, mock(ExceptionBuilder.class));
81         requestAction = "assignInstance";
82     }
83
84     @Test
85     public void getResourceListFromUserParams() throws Exception {
86         initExecution(requestAction, readBpmnRequestFromFile(MACRO_CREATE_WITHOUT_RESOURCES_JSON), false);
87         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
88                 anyString(), anyString());
89
90         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
91                 getUserParams(), serviceInstanceId, requestAction);
92         List<WorkflowType> expected = List.of(WorkflowType.SERVICE);
93         List<WorkflowType> result =
94                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
95
96         assertEquals(1, resourceListFromUserParams.size());
97         assertThat(expected, is(result));
98     }
99
100     @Test
101     public void getResourceListFromUserParamsMultipleSameModelVnfVfModule() throws Exception {
102         initExecution("createInstance", readBpmnRequestFromFile(MACRO_CREATE_SVC_SAME_MODEL_VNF_VFMODULE), false);
103         Mockito.doReturn(getVfModuleCustomization("3bd19000-6d21-49f1-9eb3-ea76a6eac5e0", false))
104                 .when(mockCatalogDbClient)
105                 .getVfModuleCustomizationByModelCuztomizationUUID("3bd19000-6d21-49f1-9eb3-ea76a6eac5e0");
106         Mockito.doReturn(getVfModuleCustomization("83677d89-428a-407b-b4ec-738e68275d84", false))
107                 .when(mockCatalogDbClient)
108                 .getVfModuleCustomizationByModelCuztomizationUUID("83677d89-428a-407b-b4ec-738e68275d84");
109
110         List<Resource> resources = userParamsServiceTraversal.getResourceListFromUserParams(execution, getUserParams(),
111                 serviceInstanceId, requestAction);
112
113         assertEquals(7, resources.size());
114
115         Resource service = resources.get(0);
116         assertTrue(service.getResourceType() == WorkflowType.SERVICE);
117         assertEquals(2, service.getChildren().size());
118
119         Resource vnf1 = service.getChildren().get(0);
120         assertEquals(service, vnf1.getParent());
121         assertEquals("vnf-instanceName-1", vnf1.getInstanceName());
122         assertEquals("0d0ba1ee-6b7f-47fe-8266-2967993b2c08", vnf1.getResourceId());
123         assertEquals(2, vnf1.getChildren().size());
124
125         Resource vnf2 = service.getChildren().get(1);
126         assertEquals(service, vnf2.getParent());
127         assertEquals("vnf-instanceName-2", vnf2.getInstanceName());
128         assertEquals("0d0ba1ee-6b7f-47fe-8266-2967993b2c08", vnf2.getResourceId());
129         assertEquals(2, vnf2.getChildren().size());
130
131         Resource vfmodule1 = vnf1.getChildren().get(0);
132         assertEquals(vnf1, vfmodule1.getParent());
133         assertEquals("demo-network-1", vfmodule1.getInstanceName());
134         assertEquals("3bd19000-6d21-49f1-9eb3-ea76a6eac5e0", vfmodule1.getResourceId());
135
136         Resource vfmodule2 = vnf1.getChildren().get(1);
137         assertEquals(vnf1, vfmodule2.getParent());
138         assertEquals("demo-1", vfmodule2.getInstanceName());
139         assertEquals("83677d89-428a-407b-b4ec-738e68275d84", vfmodule2.getResourceId());
140
141         Resource vfmodule3 = vnf2.getChildren().get(0);
142         assertEquals(vnf2, vfmodule3.getParent());
143         assertEquals("demo-2", vfmodule3.getInstanceName());
144         assertEquals("83677d89-428a-407b-b4ec-738e68275d84", vfmodule3.getResourceId());
145
146         Resource vfmodule4 = vnf2.getChildren().get(1);
147         assertEquals(vnf2, vfmodule4.getParent());
148         assertEquals("demo-3", vfmodule4.getInstanceName());
149         assertEquals("83677d89-428a-407b-b4ec-738e68275d84", vfmodule4.getResourceId());
150     }
151
152     @Test
153     public void getResourceListFromUserParamsMultiplePnfs() throws Exception {
154         initExecution("createInstance", readBpmnRequestFromFile(MACRO_CREATE_SVC_SAME_MODEL_PNF), false);
155
156         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
157                 getUserParams(), serviceInstanceId, requestAction);
158
159         assertEquals(3, resourceListFromUserParams.size());
160
161         Resource service = resourceListFromUserParams.get(0);
162         assertTrue(service.getResourceType() == WorkflowType.SERVICE);
163         assertEquals(2, service.getChildren().size());
164
165         Resource pnf1 = service.getChildren().get(0);
166         assertEquals(service, pnf1.getParent());
167         assertEquals("ORAN_SIM1_2106_pnf_01", pnf1.getInstanceName());
168         assertEquals("88a3096a-af87-4853-99f6-7256a9ab6c3e", pnf1.getResourceId());
169
170         Resource pnf2 = service.getChildren().get(1);
171         assertEquals(service, pnf2.getParent());
172         assertEquals("ORAN_SIM1_2106_pnf_02", pnf2.getInstanceName());
173         assertEquals("88a3096a-af87-4853-99f6-7256a9ab6c3e", pnf2.getResourceId());
174     }
175
176     @Test
177     public void getResourceListFromUserParamsForVnfs() throws Exception {
178         initExecution(requestAction, readBpmnRequestFromFile(MACRO_ASSIGN_JSON), false);
179         Mockito.doReturn(getVfModuleCustomization("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f", true))
180                 .when(mockCatalogDbClient)
181                 .getVfModuleCustomizationByModelCuztomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
182         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
183                 anyString(), anyString());
184
185         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
186                 getUserParams(), serviceInstanceId, requestAction);
187         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.VNF, WorkflowType.VOLUMEGROUP,
188                 WorkflowType.VFMODULE, WorkflowType.CONFIGURATION);
189         List<WorkflowType> result =
190                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
191
192         assertEquals(5, resourceListFromUserParams.size());
193         assertThat(expected, is(result));
194     }
195
196     @Test
197     public void getResourceListFromUserParamsForVnfsWithPriorities() throws Exception {
198         initExecution(requestAction, readBpmnRequestFromFile(MACRO_CREATE_JSON), false);
199         Mockito.doReturn(getVfModuleCustomization("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f", true))
200                 .when(mockCatalogDbClient)
201                 .getVfModuleCustomizationByModelCuztomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
202         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
203                 anyString(), anyString());
204
205         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
206                 getUserParams(), serviceInstanceId, requestAction);
207         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.VNF, WorkflowType.VOLUMEGROUP,
208                 WorkflowType.VFMODULE, WorkflowType.CONFIGURATION, WorkflowType.PNF);
209         List<WorkflowType> result =
210                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
211
212         assertEquals(6, resourceListFromUserParams.size());
213         assertThat(expected, is(result));
214         assertEquals(2, resourceListFromUserParams.get(1).getChildren().get(1).getProcessingPriority());
215     }
216
217     @Test
218     public void getResourceListFromUserParamsForPnfs() throws Exception {
219         initExecution(requestAction, readBpmnRequestFromFile(MACRO_ASSIGN_PNF_JSON), false);
220         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
221                 anyString(), anyString());
222
223         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
224                 getUserParams(), serviceInstanceId, requestAction);
225         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.PNF);
226         List<WorkflowType> result =
227                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
228
229         assertEquals(2, resourceListFromUserParams.size());
230         assertThat(expected, is(result));
231     }
232
233     @Test
234     public void getResourceListFromUserParamsForNetworks() throws Exception {
235         requestAction = "createInstance";
236         initExecution(requestAction, readBpmnRequestFromFile(NETWORK_COLLECTION_JSON), false);
237         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
238                 anyString(), anyString());
239         Mockito.doReturn(getService()).when(mockCatalogDbClient).getServiceByID(anyString());
240         Mockito.doReturn(new NetworkCollectionResourceCustomization()).when(mockCatalogDbClient)
241                 .getNetworkCollectionResourceCustomizationByID(anyString());
242
243         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
244                 getUserParams(), serviceInstanceId, requestAction);
245         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.NETWORK, WorkflowType.NETWORK,
246                 WorkflowType.NETWORKCOLLECTION);
247         List<WorkflowType> result =
248                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
249
250         assertEquals(4, resourceListFromUserParams.size());
251         assertThat(expected, is(result));
252     }
253
254     @Test
255     public void getResourceListFromUserParamsBuildAndThrowExceptionWhenVfModuleAreEmpty() throws Exception {
256         initExecution(requestAction, readBpmnRequestFromFile(MACRO_ASSIGN_JSON), false);
257         VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
258         vfModuleCustomization.setVfModule(null);
259         Mockito.doReturn(vfModuleCustomization).when(mockCatalogDbClient)
260                 .getVfModuleCustomizationByModelCuztomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
261
262         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
263                 getUserParams(), serviceInstanceId, requestAction);
264         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.VNF);
265         List<WorkflowType> result =
266                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
267
268         assertEquals(2, resourceListFromUserParams.size());
269         assertThat(expected, is(result));
270     }
271
272     private List<Map<String, Object>> getUserParams() throws IOException {
273         String bpmnRequest = (String) execution.getVariable(BBConstants.G_BPMN_REQUEST);
274         ServiceInstancesRequest sIRequest = new ObjectMapper().readValue(bpmnRequest, ServiceInstancesRequest.class);
275         return sIRequest.getRequestDetails().getRequestParameters().getUserParams();
276     }
277
278     @Test
279     public void getResourceListFromUserParamsWhenUserParamsAreNull() throws Exception {
280         List<Resource> expectedResourceList = new ArrayList<>();
281         List<Resource> resultResourceList = userParamsServiceTraversal.getResourceListFromUserParams(execution, null,
282                 serviceInstanceId, requestAction);
283
284         assertEquals(expectedResourceList, resultResourceList);
285     }
286
287     private String readBpmnRequestFromFile(String fileName) throws IOException {
288         return new String(Files.readAllBytes(Paths.get("src/test/resources/__files/" + fileName)));
289     }
290
291     private void initExecution(String gAction, String bpmnRequest, boolean isAlaCarte) {
292         execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
293         execution.setVariable("requestAction", gAction);
294         execution.setVariable("bpmnRequest", bpmnRequest);
295         execution.setVariable("aLaCarte", isAlaCarte);
296         execution.setVariable("apiVersion", "7");
297     }
298
299     private Service getService() {
300         Service service = new Service();
301         List<CollectionResourceCustomization> collectionResourceCustomizations = new ArrayList<>();
302         CollectionResourceCustomization collectionResourceCustomization = new CollectionResourceCustomization();
303         collectionResourceCustomization.setModelCustomizationUUID("123");
304         collectionResourceCustomizations.add(collectionResourceCustomization);
305         service.setCollectionResourceCustomizations(collectionResourceCustomizations);
306         return service;
307     }
308
309     private VfModuleCustomization getVfModuleCustomization(String modelCustomizationUUID, boolean includeVolumeGroup) {
310         VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
311         vfModuleCustomization.setModelCustomizationUUID(modelCustomizationUUID);
312         VfModule vfModule = new VfModule();
313         if (includeVolumeGroup) {
314             vfModuleCustomization.setVolumeHeatEnv(new HeatEnvironment());
315             vfModule.setVolumeHeatTemplate(new HeatTemplate());
316             vfModule.setModelName("helm");
317         } else {
318             vfModuleCustomization.setHeatEnvironment(new HeatEnvironment());
319         }
320         vfModule.setModuleHeatTemplate(new HeatTemplate());
321         vfModuleCustomization.setVfModule(vfModule);
322         return vfModuleCustomization;
323     }
324
325     private List<CvnfcCustomization> getCvnfcCustomizations() {
326         ConfigurationResource configurationResource = new ConfigurationResource();
327         configurationResource.setToscaNodeType("FabricConfiguration");
328
329         CvnfcConfigurationCustomization cvnfcConfigurationCustomization = new CvnfcConfigurationCustomization();
330         cvnfcConfigurationCustomization.setConfigurationResource(configurationResource);
331         CvnfcCustomization cvnfcCustomization = new CvnfcCustomization();
332
333         List<CvnfcConfigurationCustomization> cvnfcConfigurationCustomizations = new ArrayList<>();
334         cvnfcConfigurationCustomizations.add(cvnfcConfigurationCustomization);
335         cvnfcCustomization.setCvnfcConfigurationCustomization(cvnfcConfigurationCustomizations);
336
337         List<CvnfcCustomization> cvnfcCustomizations = new ArrayList<>();
338         cvnfcCustomizations.add(cvnfcCustomization);
339         return cvnfcCustomizations;
340     }
341 }