Switch over BBRollback to use DB lookups.
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionBBTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2020 Nokia
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;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.anyObject;
31 import static org.mockito.ArgumentMatchers.anyString;
32 import static org.mockito.ArgumentMatchers.isA;
33 import static org.mockito.Mockito.doNothing;
34 import static org.mockito.Mockito.doReturn;
35 import static org.mockito.Mockito.times;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.when;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Optional;
43 import org.camunda.bpm.engine.delegate.BpmnError;
44 import org.camunda.bpm.engine.delegate.DelegateExecution;
45 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
46 import org.junit.Before;
47 import org.junit.Rule;
48 import org.junit.Test;
49 import org.junit.rules.ExpectedException;
50 import org.mockito.ArgumentCaptor;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.Spy;
55 import org.onap.aai.domain.yang.Configuration;
56 import org.onap.aai.domain.yang.GenericVnf;
57 import org.onap.aai.domain.yang.InstanceGroup;
58 import org.onap.aai.domain.yang.L3Network;
59 import org.onap.aai.domain.yang.ServiceInstance;
60 import org.onap.aai.domain.yang.VfModule;
61 import org.onap.aai.domain.yang.VolumeGroup;
62 import org.onap.so.bpmn.BaseTaskTest;
63 import org.onap.so.bpmn.common.listener.flowmanipulator.FlowManipulatorListenerRunner;
64 import org.onap.so.bpmn.core.WorkflowException;
65 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
66 import org.onap.so.bpmn.servicedecomposition.entities.ConfigurationResourceKeys;
67 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
68 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
69 import org.onap.so.db.catalog.beans.BuildingBlockRollback;
70 import org.onap.so.db.catalog.beans.ConfigurationResource;
71 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
72 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
73 import org.onap.so.db.request.beans.InfraActiveRequests;
74 import org.onap.so.serviceinstancebeans.ModelInfo;
75 import org.onap.so.serviceinstancebeans.ModelType;
76 import org.onap.so.serviceinstancebeans.RelatedInstance;
77 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
78 import org.onap.so.serviceinstancebeans.RequestDetails;
79 import org.springframework.core.env.Environment;
80
81 public class WorkflowActionBBTasksTest extends BaseTaskTest {
82
83     private static final String SAMPLE_MSO_REQUEST_ID = "00f704ca-c5e5-4f95-a72c-6889db7b0688";
84     private static final String SAMPLE_REQUEST_ACTION = "Delete-Network-Collection";
85     private static final int SAMPLE_SEQUENCE = 0;
86     private static final String EMPTY_STRING = "";
87     @Mock
88     protected WorkflowAction workflowAction;
89
90     @Mock
91     protected WorkflowActionBBFailure workflowActionBBFailure;
92
93     @InjectMocks
94     @Spy
95     protected WorkflowActionBBTasks workflowActionBBTasks;
96
97     @Mock
98     InfraActiveRequests reqMock;
99
100     private DelegateExecution execution;
101
102     @Mock
103     protected Environment environment;
104
105     @Mock
106     private FlowManipulatorListenerRunner flowManipulatorListenerRunner;
107
108     @Rule
109     public ExpectedException thrown = ExpectedException.none();
110
111     @Mock
112     private DelegateExecution mockExecution;
113
114     @Before
115     public void initCatalogDBRollbackTable() {
116         when(catalogDbClient.getBuildingBlockRollbackEntries()).thenReturn(getRollbackBuildingBlockList());
117     }
118
119     @Before
120     public void before() throws Exception {
121         execution = new DelegateExecutionFake();
122         ServiceInstance servInstance = new ServiceInstance();
123         servInstance.setServiceInstanceId("TEST");
124         when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), anyObject())).thenReturn(servInstance);
125         workflowAction.setBbInputSetupUtils(bbSetupUtils);
126         workflowAction.setBbInputSetup(bbInputSetup);
127     }
128
129     @Test
130     public void selectBBTest() {
131         String vnfCustomizationUUID = "1234567";
132         String modelUuid = "1234567";
133         prepareDelegateExecution();
134         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
135
136         BuildingBlock buildingBlock =
137                 new BuildingBlock().setBpmnFlowName("ConfigAssignVnfBB").setKey(vnfCustomizationUUID);
138         RequestDetails rd = new RequestDetails();
139         ModelInfo mi = new ModelInfo();
140         mi.setModelUuid(modelUuid);
141         rd.setModelInfo(mi);
142         ExecuteBuildingBlock ebb = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock).setRequestDetails(rd);
143         flowsToExecute.add(ebb);
144
145         List<VnfResourceCustomization> vnfResourceCustomizations = new ArrayList<>();
146         VnfResourceCustomization vrc = new VnfResourceCustomization();
147         vrc.setSkipPostInstConf(false);
148         vrc.setModelCustomizationUUID(vnfCustomizationUUID);
149         vnfResourceCustomizations.add(vrc);
150         GenericVnf genericVnf = new GenericVnf();
151         genericVnf.setModelCustomizationId(vnfCustomizationUUID);
152         doReturn(vnfResourceCustomizations).when(catalogDbClient).getVnfResourceCustomizationByModelUuid(modelUuid);
153         doReturn(vrc).when(catalogDbClient).findVnfResourceCustomizationInList(vnfCustomizationUUID,
154                 vnfResourceCustomizations);
155
156         execution.setVariable("flowsToExecute", flowsToExecute);
157         workflowActionBBTasks.selectBB(execution);
158         boolean success = (boolean) execution.getVariable("completed");
159         int currentSequence = (int) execution.getVariable("gCurrentSequence");
160         assertFalse(success);
161         assertEquals(1, currentSequence);
162     }
163
164     @Test
165     public void select2BBTest() {
166         String vnfCustomizationUUID = "1234567";
167         String modelUuid = "1234567";
168
169         prepareDelegateExecution();
170         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
171         BuildingBlock buildingBlock =
172                 new BuildingBlock().setBpmnFlowName("ConfigDeployVnfBB").setKey(vnfCustomizationUUID);
173         RequestDetails rd = new RequestDetails();
174         ModelInfo mi = new ModelInfo();
175         mi.setModelUuid(modelUuid);
176         rd.setModelInfo(mi);
177         ExecuteBuildingBlock ebb = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock).setRequestDetails(rd);
178         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
179
180         flowsToExecute.add(ebb);
181
182         List<VnfResourceCustomization> vnfResourceCustomizations = new ArrayList<>();
183         VnfResourceCustomization vrc = new VnfResourceCustomization();
184
185         vrc.setSkipPostInstConf(false);
186         vrc.setModelCustomizationUUID(vnfCustomizationUUID);
187         vnfResourceCustomizations.add(vrc);
188         GenericVnf genericVnf = new GenericVnf();
189         genericVnf.setModelCustomizationId(vnfCustomizationUUID);
190
191         doReturn(vnfResourceCustomizations).when(catalogDbClient).getVnfResourceCustomizationByModelUuid(modelUuid);
192         doReturn(vrc).when(catalogDbClient).findVnfResourceCustomizationInList(vnfCustomizationUUID,
193                 vnfResourceCustomizations);
194
195         flowsToExecute.add(ebb2);
196         execution.setVariable("flowsToExecute", flowsToExecute);
197         workflowActionBBTasks.selectBB(execution);
198         boolean success = (boolean) execution.getVariable("completed");
199         int currentSequence = (int) execution.getVariable("gCurrentSequence");
200         assertFalse(success);
201         assertEquals(1, currentSequence);
202     }
203
204     @Test
205     public void updateRequestStatusToCompleteTest() {
206         String reqId = "reqId123";
207         execution.setVariable("mso-request-id", reqId);
208         execution.setVariable("requestAction", "createInstance");
209         execution.setVariable("resourceName", "Service");
210         execution.setVariable("aLaCarte", true);
211         InfraActiveRequests req = new InfraActiveRequests();
212         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
213         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
214         workflowActionBBTasks.updateRequestStatusToComplete(execution);
215         assertEquals("ALaCarte-Service-createInstance request was executed correctly.",
216                 execution.getVariable("finalStatusMessage"));
217     }
218
219     @Test
220     public void rollbackExecutionPathTest() {
221         execution.setVariable("handlingCode", "Rollback");
222         execution.setVariable("isRollback", false);
223         execution.setVariable("requestAction", EMPTY_STRING);
224         execution.setVariable("resourceName", EMPTY_STRING);
225         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
226         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("AssignVfModuleBB");
227         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
228         flowsToExecute.add(ebb1);
229
230         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("CreateVfModuleBB");
231         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
232         flowsToExecute.add(ebb2);
233
234         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
235         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
236         flowsToExecute.add(ebb3);
237
238         execution.setVariable("flowsToExecute", flowsToExecute);
239         execution.setVariable("gCurrentSequence", 3);
240         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
241
242         workflowActionBBTasks.rollbackExecutionPath(execution);
243         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
244         assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(), "DeactivateVfModuleBB");
245         assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(), "DeleteVfModuleBB");
246         assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(), "UnassignVfModuleBB");
247         assertEquals(0, execution.getVariable("gCurrentSequence"));
248     }
249
250     @Test
251     public void rollbackExecutionPathUnfinishedFlowTest() {
252         execution.setVariable("handlingCode", "Rollback");
253         execution.setVariable("isRollback", false);
254         execution.setVariable("requestAction", EMPTY_STRING);
255         execution.setVariable("resourceName", EMPTY_STRING);
256         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
257         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("AssignVfModuleBB");
258         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
259         flowsToExecute.add(ebb1);
260
261         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("CreateVfModuleBB");
262         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
263         flowsToExecute.add(ebb2);
264
265         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
266         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
267         flowsToExecute.add(ebb3);
268
269         execution.setVariable("flowsToExecute", flowsToExecute);
270         execution.setVariable("gCurrentSequence", 2);
271         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
272
273         workflowActionBBTasks.rollbackExecutionPath(execution);
274         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
275         assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(), "DeleteVfModuleBB");
276         assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(), "UnassignVfModuleBB");
277         assertEquals(0, execution.getVariable("gCurrentSequence"));
278         assertEquals(0, execution.getVariable("retryCount"));
279     }
280
281     @Test
282     public void rollbackExecutionTest() {
283         execution.setVariable("handlingCode", "Rollback");
284         execution.setVariable("isRollback", false);
285         execution.setVariable("requestAction", EMPTY_STRING);
286         execution.setVariable("resourceName", EMPTY_STRING);
287         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
288         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("AssignServiceInstanceBB");
289         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
290         flowsToExecute.add(ebb1);
291
292         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("CreateNetworkCollectionBB");
293         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
294         flowsToExecute.add(ebb2);
295
296         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("AssignNetworkBB");
297         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
298         flowsToExecute.add(ebb3);
299
300         BuildingBlock buildingBlock4 = new BuildingBlock().setBpmnFlowName("CreateNetworkBB");
301         ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock4);
302         flowsToExecute.add(ebb4);
303
304         execution.setVariable("flowsToExecute", flowsToExecute);
305         execution.setVariable("gCurrentSequence", 3);
306         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
307
308         workflowActionBBTasks.rollbackExecutionPath(execution);
309         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
310         assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(), "UnassignNetworkBB");
311         assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(), "DeleteNetworkCollectionBB");
312         assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(), "UnassignServiceInstanceBB");
313         assertEquals(0, execution.getVariable("gCurrentSequence"));
314     }
315
316     @Test
317     public void rollbackExecutionRollbackToAssignedTest() {
318         execution.setVariable("isRollback", false);
319         execution.setVariable("handlingCode", "RollbackToAssigned");
320         execution.setVariable("requestAction", EMPTY_STRING);
321         execution.setVariable("resourceName", EMPTY_STRING);
322         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
323
324         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("AssignVfModuleBB");
325         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
326         flowsToExecute.add(ebb1);
327
328         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("CreateVfModuleBB");
329         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
330         flowsToExecute.add(ebb2);
331
332         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
333         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
334         flowsToExecute.add(ebb3);
335
336         execution.setVariable("flowsToExecute", flowsToExecute);
337         execution.setVariable("gCurrentSequence", 2);
338
339         workflowActionBBTasks.rollbackExecutionPath(execution);
340         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
341         assertEquals("DeleteVfModuleBB", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
342         assertEquals(0, execution.getVariable("gCurrentSequence"));
343         assertEquals(1, ebbs.size());
344     }
345
346     @Test
347     public void rollbackExecutionPathChangeBBForReplaceVFModuleTest() {
348         execution.setVariable("handlingCode", "Rollback");
349         execution.setVariable("isRollback", false);
350         execution.setVariable("requestAction", "replaceInstance");
351         execution.setVariable("resourceName", "VfModule");
352         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
353         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("AssignVfModuleBB");
354         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
355         flowsToExecute.add(ebb1);
356
357         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("CreateVfModuleBB");
358         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
359         flowsToExecute.add(ebb2);
360
361         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
362         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
363         flowsToExecute.add(ebb3);
364
365
366         BuildingBlock buildingBlock4 = new BuildingBlock().setBpmnFlowName("ChangeModelVnfBB");
367         ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock4);
368         flowsToExecute.add(ebb4);
369
370         BuildingBlock buildingBlock5 = new BuildingBlock().setBpmnFlowName("ChangeModelServiceInstanceBB");
371         ExecuteBuildingBlock ebb5 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock5);
372         flowsToExecute.add(ebb5);
373
374         execution.setVariable("flowsToExecute", flowsToExecute);
375         execution.setVariable("gCurrentSequence", 5);
376         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
377
378         workflowActionBBTasks.rollbackExecutionPath(execution);
379         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
380         assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(), "ChangeModelVnfBB");
381         assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(), "ChangeModelServiceInstanceBB");
382         assertEquals(0, execution.getVariable("gCurrentSequence"));
383     }
384
385     @Test
386     public void rollbackExecutionRollbackToAssignedWithFabricTest() {
387         execution.setVariable("isRollback", false);
388         execution.setVariable("handlingCode", "RollbackToAssigned");
389         execution.setVariable("requestAction", EMPTY_STRING);
390         execution.setVariable("resourceName", EMPTY_STRING);
391         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
392
393         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("AssignVfModuleBB");
394         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
395         flowsToExecute.add(ebb1);
396
397         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("CreateVfModuleBB");
398         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
399         flowsToExecute.add(ebb2);
400
401         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
402         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
403         flowsToExecute.add(ebb3);
404
405         BuildingBlock buildingBlock4 = new BuildingBlock().setBpmnFlowName("AddFabricConfigurationBB");
406         ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock4);
407         flowsToExecute.add(ebb4);
408
409         execution.setVariable("flowsToExecute", flowsToExecute);
410         execution.setVariable("gCurrentSequence", 4);
411
412         workflowActionBBTasks.rollbackExecutionPath(execution);
413         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
414         assertEquals(0, execution.getVariable("gCurrentSequence"));
415         assertEquals(3, ebbs.size());
416         assertEquals("DeleteFabricConfigurationBB", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
417         assertEquals("DeactivateVfModuleBB", ebbs.get(1).getBuildingBlock().getBpmnFlowName());
418         assertEquals("DeleteVfModuleBB", ebbs.get(2).getBuildingBlock().getBpmnFlowName());
419
420     }
421
422     @Test
423     public void rollbackExecutionRollbackToCreatedWithFabricTest() {
424         execution.setVariable("isRollback", false);
425         execution.setVariable("handlingCode", "RollbackToCreated");
426         execution.setVariable("requestAction", EMPTY_STRING);
427         execution.setVariable("resourceName", EMPTY_STRING);
428         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
429
430         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("AssignVfModuleBB");
431         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
432         flowsToExecute.add(ebb1);
433
434         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("CreateVfModuleBB");
435         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
436         flowsToExecute.add(ebb2);
437
438         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
439         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
440         flowsToExecute.add(ebb3);
441
442         BuildingBlock buildingBlock4 = new BuildingBlock().setBpmnFlowName("AddFabricConfigurationBB");
443         ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock4);
444         flowsToExecute.add(ebb4);
445
446         execution.setVariable("flowsToExecute", flowsToExecute);
447         execution.setVariable("gCurrentSequence", 4);
448
449         workflowActionBBTasks.rollbackExecutionPath(execution);
450         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
451         assertEquals(0, execution.getVariable("gCurrentSequence"));
452         assertEquals(2, ebbs.size());
453         assertEquals("DeleteFabricConfigurationBB", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
454         assertEquals("DeactivateVfModuleBB", ebbs.get(1).getBuildingBlock().getBpmnFlowName());
455
456     }
457
458     @Test
459     public void rollbackExecutionRollbackToCreatedNoConfigurationWithFabricTest() {
460         execution.setVariable("isRollback", false);
461         execution.setVariable("handlingCode", "RollbackToCreatedNoConfiguration");
462         execution.setVariable("requestAction", EMPTY_STRING);
463         execution.setVariable("resourceName", EMPTY_STRING);
464         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
465
466         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("AssignVfModuleBB");
467         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
468         flowsToExecute.add(ebb1);
469
470         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("CreateVfModuleBB");
471         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
472         flowsToExecute.add(ebb2);
473
474         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
475         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
476         flowsToExecute.add(ebb3);
477
478         BuildingBlock buildingBlock4 = new BuildingBlock().setBpmnFlowName("AddFabricConfigurationBB");
479         ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock4);
480         flowsToExecute.add(ebb4);
481
482         execution.setVariable("flowsToExecute", flowsToExecute);
483         execution.setVariable("gCurrentSequence", 4);
484
485         workflowActionBBTasks.rollbackExecutionPath(execution);
486         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
487         assertEquals(0, execution.getVariable("gCurrentSequence"));
488         assertEquals(1, ebbs.size());
489         assertEquals("DeactivateVfModuleBB", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
490     }
491
492     @Test
493     public void rollbackExecutionRollbackToCreatedTest() {
494         execution.setVariable("isRollback", false);
495         execution.setVariable("handlingCode", "RollbackToCreated");
496         execution.setVariable("requestAction", EMPTY_STRING);
497         execution.setVariable("resourceName", EMPTY_STRING);
498         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
499         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("AssignVfModuleBB");
500         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
501         flowsToExecute.add(ebb1);
502
503         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("CreateVfModuleBB");
504         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
505         flowsToExecute.add(ebb2);
506
507         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
508         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
509         flowsToExecute.add(ebb3);
510
511         execution.setVariable("flowsToExecute", flowsToExecute);
512         execution.setVariable("gCurrentSequence", 3);
513
514         workflowActionBBTasks.rollbackExecutionPath(execution);
515         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
516         assertEquals("DeactivateVfModuleBB", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
517         assertEquals(0, execution.getVariable("gCurrentSequence"));
518         assertEquals(1, ebbs.size());
519     }
520
521     @Test
522     public void rollbackExecutionRollbackInPlaceSoftwareUpdateTest() {
523         execution.setVariable("isRollback", false);
524         execution.setVariable("handlingCode", "Rollback");
525         execution.setVariable("requestAction", EMPTY_STRING);
526         execution.setVariable("resourceName", EMPTY_STRING);
527         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
528         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("VNFCheckPserversLockedFlagActivity");
529         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
530         flowsToExecute.add(ebb1);
531
532         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("VNFCheckInMaintFlagActivity");
533         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
534         flowsToExecute.add(ebb2);
535
536         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("VNFSetInMaintFlagActivity");
537         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
538         flowsToExecute.add(ebb3);
539
540         BuildingBlock buildingBlock4 = new BuildingBlock().setBpmnFlowName("VNFCheckClosedLoopDisabledFlagActivity");
541         ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock4);
542         flowsToExecute.add(ebb4);
543
544         BuildingBlock buildingBlock5 = new BuildingBlock().setBpmnFlowName("VNFSetClosedLoopDisabledFlagActivity");
545         ExecuteBuildingBlock ebb5 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock5);
546         flowsToExecute.add(ebb5);
547
548         BuildingBlock buildingBlock6 = new BuildingBlock().setBpmnFlowName("VNFLockActivity");
549         ExecuteBuildingBlock ebb6 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock6);
550         flowsToExecute.add(ebb6);
551
552         BuildingBlock buildingBlock7 = new BuildingBlock().setBpmnFlowName("VNFUpgradePreCheckActivity");
553         ExecuteBuildingBlock ebb7 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock7);
554         flowsToExecute.add(ebb7);
555
556         BuildingBlock buildingBlock8 = new BuildingBlock().setBpmnFlowName("VNFQuiesceTrafficActivity");
557         ExecuteBuildingBlock ebb8 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock8);
558         flowsToExecute.add(ebb8);
559
560         BuildingBlock buildingBlock9 = new BuildingBlock().setBpmnFlowName("VNFStopActivity");
561         ExecuteBuildingBlock ebb9 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock9);
562         flowsToExecute.add(ebb9);
563
564         BuildingBlock buildingBlock10 = new BuildingBlock().setBpmnFlowName("VNFSnapShotActivity");
565         ExecuteBuildingBlock ebb10 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock10);
566         flowsToExecute.add(ebb10);
567
568         execution.setVariable("flowsToExecute", flowsToExecute);
569         execution.setVariable("gCurrentSequence", 10);
570
571         workflowActionBBTasks.rollbackExecutionPath(execution);
572         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
573         assertEquals("VNFStartActivity", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
574         assertEquals("VNFResumeTrafficActivity", ebbs.get(1).getBuildingBlock().getBpmnFlowName());
575         assertEquals("VNFUnlockActivity", ebbs.get(2).getBuildingBlock().getBpmnFlowName());
576         assertEquals("VNFUnsetClosedLoopDisabledFlagActivity", ebbs.get(3).getBuildingBlock().getBpmnFlowName());
577         assertEquals("VNFUnsetInMaintFlagActivity", ebbs.get(4).getBuildingBlock().getBpmnFlowName());
578         assertEquals(0, execution.getVariable("gCurrentSequence"));
579         assertEquals(5, ebbs.size());
580     }
581
582     @Test
583     public void rollbackExecutionRollbackConfigModifyTest() {
584         execution.setVariable("isRollback", false);
585         execution.setVariable("handlingCode", "Rollback");
586         execution.setVariable("requestAction", EMPTY_STRING);
587         execution.setVariable("resourceName", EMPTY_STRING);
588         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
589         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("VNFCheckPserversLockedFlagActivity");
590         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
591         flowsToExecute.add(ebb1);
592
593         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("VNFCheckInMaintFlagActivity");
594         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
595         flowsToExecute.add(ebb2);
596
597         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("VNFSetInMaintFlagActivity");
598         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
599         flowsToExecute.add(ebb3);
600
601         BuildingBlock buildingBlock4 = new BuildingBlock().setBpmnFlowName("VNFCheckClosedLoopDisabledFlagActivity");
602         ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock4);
603         flowsToExecute.add(ebb4);
604
605         BuildingBlock buildingBlock5 = new BuildingBlock().setBpmnFlowName("VNFSetClosedLoopDisabledFlagActivity");
606         ExecuteBuildingBlock ebb5 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock5);
607         flowsToExecute.add(ebb5);
608
609         BuildingBlock buildingBlock6 = new BuildingBlock().setBpmnFlowName("VNFHealthCheckActivity");
610         ExecuteBuildingBlock ebb6 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock6);
611         flowsToExecute.add(ebb6);
612
613         execution.setVariable("flowsToExecute", flowsToExecute);
614         execution.setVariable("gCurrentSequence", 6);
615
616         workflowActionBBTasks.rollbackExecutionPath(execution);
617         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
618         assertEquals("VNFUnsetClosedLoopDisabledFlagActivity", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
619         assertEquals("VNFUnsetInMaintFlagActivity", ebbs.get(1).getBuildingBlock().getBpmnFlowName());
620         assertEquals(0, execution.getVariable("gCurrentSequence"));
621         assertEquals(2, ebbs.size());
622     }
623
624     @Test
625     public void rollbackExecutionRollbackControllerExecutionBBTest() {
626         execution.setVariable("isRollback", false);
627         execution.setVariable("handlingCode", "Rollback");
628         execution.setVariable("requestAction", EMPTY_STRING);
629         execution.setVariable("resourceName", EMPTY_STRING);
630         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
631         BuildingBlock buildingBlock1 = new BuildingBlock().setBpmnFlowName("AssignServiceInstanceBB");
632         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock1);
633         flowsToExecute.add(ebb1);
634
635         BuildingBlock buildingBlock2 = new BuildingBlock().setBpmnFlowName("AssignNetworkBB");
636         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock2);
637         flowsToExecute.add(ebb2);
638
639         BuildingBlock buildingBlock3 = new BuildingBlock().setBpmnFlowName("AssignVnfBB");
640         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock3);
641         flowsToExecute.add(ebb3);
642
643         BuildingBlock buildingBlock4 = new BuildingBlock().setBpmnFlowName("AssignVfModuleBB");
644         ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock4);
645         flowsToExecute.add(ebb4);
646
647         BuildingBlock buildingBlock5 = new BuildingBlock().setBpmnFlowName("ControllerExecutionBB");
648         buildingBlock5.setBpmnScope("vnf");
649         buildingBlock5.setBpmnAction("config-assign");
650         ExecuteBuildingBlock ebb5 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock5);
651         flowsToExecute.add(ebb5);
652
653         BuildingBlock buildingBlock6 = new BuildingBlock().setBpmnFlowName("CreateVfModuleBB");
654         ExecuteBuildingBlock ebb6 = new ExecuteBuildingBlock().setBuildingBlock(buildingBlock6);
655         flowsToExecute.add(ebb6);
656
657         execution.setVariable("flowsToExecute", flowsToExecute);
658         execution.setVariable("gCurrentSequence", 5);
659
660         workflowActionBBTasks.rollbackExecutionPath(execution);
661         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
662         BuildingBlock bb = ebbs.get(0).getBuildingBlock();
663         assertEquals("ControllerExecutionBB", bb.getBpmnFlowName());
664         assertEquals("vnf", bb.getBpmnScope());
665         assertEquals("config-unassign", bb.getBpmnAction());
666         assertEquals(0, execution.getVariable("gCurrentSequence"));
667         assertEquals(5, ebbs.size());
668     }
669
670     @Test
671     public void postProcessingExecuteBBActivateVfModuleNotReplaceInstanceTest() throws CloneNotSupportedException {
672         WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
673         workflowResourceIds.setServiceInstanceId("1");
674         workflowResourceIds.setVnfId("1");
675
676         BuildingBlock bbActivateVfModule = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
677         ExecuteBuildingBlock ebbActivateVfModule = new ExecuteBuildingBlock().setBuildingBlock(bbActivateVfModule);
678         ebbActivateVfModule.setWorkflowResourceIds(workflowResourceIds);
679         ebbActivateVfModule.setResourceId("1");
680
681         ServiceInstance service = new ServiceInstance();
682         service.setServiceInstanceName("name");
683         service.setModelVersionId("1");
684         doReturn(service).when(bbSetupUtils).getAAIServiceInstanceById("1");
685
686         GenericVnf vnf = new GenericVnf();
687         vnf.setVnfName("name");
688         vnf.setModelCustomizationId("1");
689         doReturn(vnf).when(bbSetupUtils).getAAIGenericVnf("1");
690
691         VfModule vfModule = new VfModule();
692         vfModule.setVfModuleName("name");
693         vfModule.setModelCustomizationId("1");
694         doReturn(vfModule).when(bbSetupUtils).getAAIVfModule("1", "1");
695
696         List<org.onap.aai.domain.yang.Vnfc> vnfcs = new ArrayList<org.onap.aai.domain.yang.Vnfc>();
697         org.onap.aai.domain.yang.Vnfc vnfc = new org.onap.aai.domain.yang.Vnfc();
698         vnfc.setModelInvariantId("1");
699         vnfc.setVnfcName("name");
700         vnfc.setModelCustomizationId("2");
701         vnfcs.add(vnfc);
702         doReturn(vnfcs).when(workflowAction).getRelatedResourcesInVfModule(any(), any(), any(), any());
703
704         CvnfcConfigurationCustomization vfModuleCustomization = new CvnfcConfigurationCustomization();
705         ConfigurationResource configuration = new ConfigurationResource();
706         configuration.setToscaNodeType("FabricConfiguration");
707         configuration.setModelUUID("1");
708         vfModuleCustomization.setConfigurationResource(configuration);
709
710         doReturn(vfModuleCustomization).when(catalogDbClient).getCvnfcCustomization("1", "1", "1", "2");
711
712         prepareDelegateExecution();
713         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
714         flowsToExecute.add(ebbActivateVfModule);
715
716         execution.setVariable("requestAction", "createInstance");
717         execution.setVariable("completed", true);
718
719         ArgumentCaptor<DelegateExecution> executionCaptor = ArgumentCaptor.forClass(DelegateExecution.class);
720         ArgumentCaptor<ExecuteBuildingBlock> bbCaptor = ArgumentCaptor.forClass(ExecuteBuildingBlock.class);
721         ArgumentCaptor<List> listCaptor = ArgumentCaptor.forClass(List.class);
722         workflowActionBBTasks.postProcessingExecuteBBActivateVfModule(execution, ebbActivateVfModule, flowsToExecute);
723         verify(workflowActionBBTasks, times(1)).postProcessingExecuteBBActivateVfModule(executionCaptor.capture(),
724                 bbCaptor.capture(), listCaptor.capture());
725         assertEquals(false, executionCaptor.getAllValues().get(0).getVariable("completed"));
726         assertEquals(2, ((ArrayList) executionCaptor.getAllValues().get(0).getVariable("flowsToExecute")).size());
727         assertEquals("2",
728                 ((ExecuteBuildingBlock) ((ArrayList) executionCaptor.getAllValues().get(0)
729                         .getVariable("flowsToExecute")).get(1)).getConfigurationResourceKeys()
730                                 .getCvnfcCustomizationUUID());
731         assertEquals("AddFabricConfigurationBB", ((ExecuteBuildingBlock) ((ArrayList) executionCaptor.getAllValues()
732                 .get(0).getVariable("flowsToExecute")).get(1)).getBuildingBlock().getBpmnFlowName());
733     }
734
735     @Test
736     public void postProcessingExecuteBBActivateVfModuleReplaceInstanceHasConfigurationTest()
737             throws CloneNotSupportedException {
738         RequestDetails reqDetails = new RequestDetails();
739         RelatedInstanceList[] list = new RelatedInstanceList[2];
740         RelatedInstanceList vnfList = new RelatedInstanceList();
741         RelatedInstanceList serviceList = new RelatedInstanceList();
742         list[0] = vnfList;
743         list[1] = serviceList;
744         RelatedInstance vnfInstance = new RelatedInstance();
745         RelatedInstance serviceInstance = new RelatedInstance();
746         ModelInfo vnfModelInfo = new ModelInfo();
747         vnfModelInfo.setModelType(ModelType.vnf);
748         vnfModelInfo.setModelCustomizationId("1");
749         ModelInfo serviceModelInfo = new ModelInfo();
750         serviceModelInfo.setModelType(ModelType.service);
751         serviceModelInfo.setModelVersionId("1");
752         vnfInstance.setModelInfo(vnfModelInfo);
753         serviceInstance.setModelInfo(serviceModelInfo);
754         reqDetails.setRelatedInstanceList(list);
755         vnfList.setRelatedInstance(vnfInstance);
756         serviceList.setRelatedInstance(serviceInstance);
757         ModelInfo vfModuleInfo = new ModelInfo();
758         vfModuleInfo.setModelCustomizationId("1");
759         reqDetails.setModelInfo(vfModuleInfo);
760         BuildingBlock bbAddFabric = new BuildingBlock().setBpmnFlowName("AddFabricConfigurationBB");
761         ExecuteBuildingBlock ebbAddFabric = new ExecuteBuildingBlock().setBuildingBlock(bbAddFabric);
762         WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
763         workflowResourceIds.setServiceInstanceId("1");
764         workflowResourceIds.setVnfId("1");
765         ebbAddFabric.setWorkflowResourceIds(workflowResourceIds);
766         ebbAddFabric.setResourceId("1");
767
768         BuildingBlock bbActivateVfModule = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
769         ExecuteBuildingBlock ebbActivateVfModule = new ExecuteBuildingBlock().setBuildingBlock(bbActivateVfModule);
770         ebbActivateVfModule.setWorkflowResourceIds(workflowResourceIds);
771         ebbActivateVfModule.setResourceId("1");
772         ConfigurationResourceKeys configurationResourceKeys = new ConfigurationResourceKeys();
773         ebbAddFabric.setConfigurationResourceKeys(configurationResourceKeys);
774         ebbActivateVfModule.setRequestDetails(reqDetails);
775
776         ServiceInstance service = new ServiceInstance();
777         service.setServiceInstanceName("name");
778         service.setModelVersionId("1");
779         doReturn(service).when(bbSetupUtils).getAAIServiceInstanceById("1");
780
781         GenericVnf vnf = new GenericVnf();
782         vnf.setVnfName("name");
783         vnf.setModelCustomizationId("1");
784         doReturn(vnf).when(bbSetupUtils).getAAIGenericVnf("1");
785
786         VfModule vfModule = new VfModule();
787         vfModule.setVfModuleName("name");
788         vfModule.setModelCustomizationId("1");
789         doReturn(vfModule).when(bbSetupUtils).getAAIVfModule("1", "1");
790
791         List<org.onap.aai.domain.yang.Vnfc> vnfcs = new ArrayList<org.onap.aai.domain.yang.Vnfc>();
792         org.onap.aai.domain.yang.Vnfc vnfc = new org.onap.aai.domain.yang.Vnfc();
793         vnfc.setModelInvariantId("1");
794         vnfc.setVnfcName("name");
795         vnfc.setModelCustomizationId("2");
796         vnfcs.add(vnfc);
797         doReturn(vnfcs).when(workflowAction).getRelatedResourcesInVfModule(any(), any(), any(), any());
798
799         CvnfcConfigurationCustomization vfModuleCustomization = new CvnfcConfigurationCustomization();
800         ConfigurationResource configuration = new ConfigurationResource();
801         configuration.setToscaNodeType("FabricConfiguration");
802         configuration.setModelUUID("1");
803         vfModuleCustomization.setConfigurationResource(configuration);
804
805         doReturn(vfModuleCustomization).when(catalogDbClient).getCvnfcCustomization("1", "1", "1", "2");
806
807         prepareDelegateExecution();
808         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
809         flowsToExecute.add(ebbActivateVfModule);
810
811         ArgumentCaptor<DelegateExecution> executionCaptor = ArgumentCaptor.forClass(DelegateExecution.class);
812         ArgumentCaptor<ExecuteBuildingBlock> bbCaptor = ArgumentCaptor.forClass(ExecuteBuildingBlock.class);
813         ArgumentCaptor<List> listCaptor = ArgumentCaptor.forClass(List.class);
814
815         execution.setVariable("requestAction", "replaceInstance");
816         execution.setVariable("completed", true);
817         workflowActionBBTasks.postProcessingExecuteBBActivateVfModule(execution, ebbActivateVfModule, flowsToExecute);
818         verify(workflowActionBBTasks, times(1)).postProcessingExecuteBBActivateVfModule(executionCaptor.capture(),
819                 bbCaptor.capture(), listCaptor.capture());
820         assertEquals(false, executionCaptor.getAllValues().get(0).getVariable("completed"));
821         assertEquals(2, ((ArrayList) executionCaptor.getAllValues().get(0).getVariable("flowsToExecute")).size());
822         assertEquals("2",
823                 ((ExecuteBuildingBlock) ((ArrayList) executionCaptor.getAllValues().get(0)
824                         .getVariable("flowsToExecute")).get(1)).getConfigurationResourceKeys()
825                                 .getCvnfcCustomizationUUID());
826     }
827
828     @Test
829     public void postProcessingExecuteBBActivateVfModuleReplaceInstanceHasNoConfigurationTest()
830             throws CloneNotSupportedException {
831
832         WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
833         workflowResourceIds.setServiceInstanceId("1");
834         workflowResourceIds.setVnfId("1");
835
836         BuildingBlock bbActivateVfModule = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
837         ExecuteBuildingBlock ebbActivateVfModule = new ExecuteBuildingBlock().setBuildingBlock(bbActivateVfModule);
838         ebbActivateVfModule.setWorkflowResourceIds(workflowResourceIds);
839         ebbActivateVfModule.setResourceId("1");
840
841         ServiceInstance service = new ServiceInstance();
842         service.setServiceInstanceName("name");
843         service.setModelVersionId("1");
844         doReturn(service).when(bbSetupUtils).getAAIServiceInstanceById("1");
845
846         GenericVnf vnf = new GenericVnf();
847         vnf.setVnfName("name");
848         vnf.setModelCustomizationId("1");
849         doReturn(vnf).when(bbSetupUtils).getAAIGenericVnf("1");
850
851         VfModule vfModule = new VfModule();
852         vfModule.setVfModuleName("name");
853         vfModule.setModelCustomizationId("1");
854         doReturn(vfModule).when(bbSetupUtils).getAAIVfModule("1", "1");
855
856         List<org.onap.aai.domain.yang.Vnfc> vnfcs = new ArrayList<org.onap.aai.domain.yang.Vnfc>();
857         org.onap.aai.domain.yang.Vnfc vnfc = new org.onap.aai.domain.yang.Vnfc();
858         vnfc.setModelInvariantId("1");
859         vnfc.setVnfcName("name");
860         vnfc.setModelCustomizationId("2");
861         vnfcs.add(vnfc);
862         doReturn(vnfcs).when(workflowAction).getRelatedResourcesInVfModule(any(), any(), any(), any());
863
864         CvnfcConfigurationCustomization vfModuleCustomization = new CvnfcConfigurationCustomization();
865         ConfigurationResource configuration = new ConfigurationResource();
866         configuration.setToscaNodeType("FabricConfiguration");
867         configuration.setModelUUID("1");
868         vfModuleCustomization.setConfigurationResource(configuration);
869
870         doReturn(vfModuleCustomization).when(catalogDbClient).getCvnfcCustomization("1", "1", "1", "2");
871
872         prepareDelegateExecution();
873         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
874         flowsToExecute.add(ebbActivateVfModule);
875
876         ArgumentCaptor<DelegateExecution> executionCaptor = ArgumentCaptor.forClass(DelegateExecution.class);
877         ArgumentCaptor<ExecuteBuildingBlock> bbCaptor = ArgumentCaptor.forClass(ExecuteBuildingBlock.class);
878         ArgumentCaptor<List> listCaptor = ArgumentCaptor.forClass(List.class);
879
880         execution.setVariable("requestAction", "replaceInstance");
881         execution.setVariable("completed", true);
882
883         workflowActionBBTasks.postProcessingExecuteBBActivateVfModule(execution, ebbActivateVfModule, flowsToExecute);
884         verify(workflowActionBBTasks, times(1)).postProcessingExecuteBBActivateVfModule(executionCaptor.capture(),
885                 bbCaptor.capture(), listCaptor.capture());
886         assertEquals(true, executionCaptor.getAllValues().get(0).getVariable("completed"));
887     }
888
889
890
891     @Test
892     public void getExecuteBBForConfigTest() throws CloneNotSupportedException {
893         BuildingBlock bbActivateVfModule = new BuildingBlock().setBpmnFlowName("ActivateVfModuleBB");
894         ExecuteBuildingBlock ebbActivateVfModule = new ExecuteBuildingBlock().setBuildingBlock(bbActivateVfModule);
895
896         WorkflowResourceIds resourceIdsActivateVfModule = new WorkflowResourceIds();
897         resourceIdsActivateVfModule.setServiceInstanceId("test-service-inbstance-id");
898         resourceIdsActivateVfModule.setVnfId("test-vnf-id");
899         resourceIdsActivateVfModule.setVfModuleId("test-vf-module-id");
900         resourceIdsActivateVfModule.setConfigurationId("");
901
902         RequestDetails requestDetails = new RequestDetails();
903
904         ebbActivateVfModule.setApiVersion("7");
905         ebbActivateVfModule.setaLaCarte(true);
906         ebbActivateVfModule.setRequestAction("createInstance");
907         ebbActivateVfModule.setVnfType("test-vnf-type");
908         ebbActivateVfModule.setRequestId("f6c00ae2-a205-4cbd-b055-02e553efde12");
909         ebbActivateVfModule.setRequestDetails(requestDetails);
910         ebbActivateVfModule.setWorkflowResourceIds(resourceIdsActivateVfModule);
911
912         ConfigurationResourceKeys configurationResourceKeys = new ConfigurationResourceKeys();
913         configurationResourceKeys.setCvnfcCustomizationUUID("07d64cd2-4427-4156-b11d-d14b96b3e4cb");
914         configurationResourceKeys.setVfModuleCustomizationUUID("50b61075-6ebb-4aab-a9fc-bedad9a2aa76");
915         configurationResourceKeys.setVnfResourceCustomizationUUID("a1d0e36e-34a9-431b-b5ba-4bbb72f63c7c");
916         configurationResourceKeys.setVnfcName("rdm54bvbgw5001vm018pim001");
917
918         ExecuteBuildingBlock ebbAddFabricConfig =
919                 workflowActionBBTasks.getExecuteBBForConfig("AddFabricConfigurationBB", ebbActivateVfModule,
920                         "cc7e12f9-967c-4362-8d14-e5b2bf0608a4", configurationResourceKeys);
921
922         assertEquals("7", ebbAddFabricConfig.getApiVersion());
923         assertTrue(ebbAddFabricConfig.isaLaCarte());
924         assertEquals("createInstance", ebbAddFabricConfig.getRequestAction());
925         assertEquals("test-vnf-type", ebbAddFabricConfig.getVnfType());
926         assertEquals("f6c00ae2-a205-4cbd-b055-02e553efde12", ebbAddFabricConfig.getRequestId());
927         assertEquals(requestDetails, ebbAddFabricConfig.getRequestDetails());
928         assertEquals("cc7e12f9-967c-4362-8d14-e5b2bf0608a4",
929                 ebbAddFabricConfig.getWorkflowResourceIds().getConfigurationId());
930         assertEquals("test-service-inbstance-id", ebbAddFabricConfig.getWorkflowResourceIds().getServiceInstanceId());
931         assertEquals("test-vnf-id", ebbAddFabricConfig.getWorkflowResourceIds().getVnfId());
932         assertEquals("test-vf-module-id", ebbAddFabricConfig.getWorkflowResourceIds().getVfModuleId());
933
934         assertThat(ebbAddFabricConfig.getConfigurationResourceKeys()).isEqualTo(configurationResourceKeys);
935         assertThat(ebbAddFabricConfig.getWorkflowResourceIds())
936                 .isNotEqualTo(ebbActivateVfModule.getWorkflowResourceIds());
937         assertThat(ebbAddFabricConfig.getWorkflowResourceIds().getConfigurationId())
938                 .isNotEqualTo(ebbActivateVfModule.getWorkflowResourceIds().getConfigurationId());
939     }
940
941     @Test
942     public void checkRetryStatusTest() {
943         String reqId = "reqId123";
944         execution.setVariable("mso-request-id", reqId);
945         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
946         doReturn("6").when(environment).getProperty("mso.rainyDay.maxRetries");
947         execution.setVariable("handlingCode", "Retry");
948         execution.setVariable("retryCount", 1);
949         execution.setVariable("gCurrentSequence", 1);
950         InfraActiveRequests req = new InfraActiveRequests();
951         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
952         workflowActionBBTasks.checkRetryStatus(execution);
953         assertEquals(0, execution.getVariable("gCurrentSequence"));
954     }
955
956     @Test
957     public void checkRetryStatusTestExceededMaxRetries() {
958         String reqId = "reqId123";
959         execution.setVariable("mso-request-id", reqId);
960         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
961         doReturn("6").when(environment).getProperty("mso.rainyDay.maxRetries");
962         execution.setVariable("handlingCode", "Retry");
963         execution.setVariable("retryCount", 6);
964         execution.setVariable("gCurrentSequence", 1);
965         InfraActiveRequests req = new InfraActiveRequests();
966         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
967         try {
968             workflowActionBBTasks.checkRetryStatus(execution);
969         } catch (BpmnError e) {
970             WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
971             assertEquals("Exceeded maximum retries. Ending flow with status Abort", exception.getErrorMessage());
972         }
973     }
974
975     @Test
976     public void checkRetryStatusNoRetryTest() {
977         String reqId = "reqId123";
978         execution.setVariable("mso-request-id", reqId);
979         execution.setVariable("retryCount", 3);
980         execution.setVariable("handlingCode", "Success");
981         execution.setVariable("gCurrentSequence", 1);
982         InfraActiveRequests req = new InfraActiveRequests();
983         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
984         workflowActionBBTasks.checkRetryStatus(execution);
985         assertEquals(0, execution.getVariable("retryCount"));
986     }
987
988     @Test
989     public void updateInstanceId() {
990         String reqId = "req123";
991         String instanceId = "123123123";
992         execution.setVariable("mso-request-id", reqId);
993         execution.setVariable("resourceId", instanceId);
994         execution.setVariable("resourceType", WorkflowType.SERVICE);
995         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
996         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
997         workflowActionBBTasks.updateInstanceId(execution);
998         Mockito.verify(reqMock, Mockito.times(1)).setServiceInstanceId(instanceId);
999     }
1000
1001     @Test
1002     public void getConfigurationId() throws Exception {
1003         org.onap.aai.domain.yang.Vnfc vnfc = new org.onap.aai.domain.yang.Vnfc();
1004         vnfc.setModelInvariantId("modelInvariantId");
1005         vnfc.setVnfcName("testVnfcName");
1006         List<Configuration> configurations = new ArrayList<>();
1007         org.onap.aai.domain.yang.Configuration configuration = new org.onap.aai.domain.yang.Configuration();
1008         configuration.setConfigurationId("configurationId");
1009         configuration.setModelCustomizationId("modelCustimizationId");
1010         configuration.setConfigurationName("testConfigurationName");
1011         configurations.add(configuration);
1012         doReturn(configurations.get(0).getConfigurationId()).when(workflowActionBBTasks).getConfigurationId(vnfc);
1013         assertEquals(workflowActionBBTasks.getConfigurationId(vnfc), "configurationId");
1014     }
1015
1016     @Test
1017     public void setServiceInstanceNameTest() {
1018         String resourceId = "40bc4ebd-11df-4610-8055-059f7441ec1c";
1019         WorkflowType resourceType = WorkflowType.SERVICE;
1020         InfraActiveRequests request = new InfraActiveRequests();
1021         ServiceInstance service = new ServiceInstance();
1022         service.setServiceInstanceName("serviceInstanceName");
1023         doReturn(service).when(bbSetupUtils).getAAIServiceInstanceById(resourceId);
1024
1025         workflowActionBBTasks.setInstanceName(resourceId, resourceType, request);
1026         assertEquals("serviceInstanceName", request.getServiceInstanceName());
1027     }
1028
1029     @Test
1030     public void setVnfNameTest() {
1031         String resourceId = "40bc4ebd-11df-4610-8055-059f7441ec1c";
1032         WorkflowType resourceType = WorkflowType.VNF;
1033         InfraActiveRequests request = new InfraActiveRequests();
1034         GenericVnf vnf = new GenericVnf();
1035         vnf.setVnfName("vnfName");
1036         doReturn(vnf).when(bbSetupUtils).getAAIGenericVnf(resourceId);
1037
1038         workflowActionBBTasks.setInstanceName(resourceId, resourceType, request);
1039         assertEquals("vnfName", request.getVnfName());
1040     }
1041
1042     @Test
1043     public void setVfModuleNameTest() {
1044         String resourceId = "40bc4ebd-11df-4610-8055-059f7441ec1c";
1045         WorkflowType resourceType = WorkflowType.VFMODULE;
1046         InfraActiveRequests request = new InfraActiveRequests();
1047         request.setVnfId("ae5cc3e8-c13c-4d88-aaf6-694ab4977b0e");
1048         VfModule vfModule = new VfModule();
1049         vfModule.setVfModuleName("vfModuleName");
1050         doReturn(vfModule).when(bbSetupUtils).getAAIVfModule("ae5cc3e8-c13c-4d88-aaf6-694ab4977b0e", resourceId);
1051
1052         workflowActionBBTasks.setInstanceName(resourceId, resourceType, request);
1053         assertEquals("vfModuleName", request.getVfModuleName());
1054     }
1055
1056     @Test
1057     public void setNetworkNameTest() {
1058         String resourceId = "40bc4ebd-11df-4610-8055-059f7441ec1c";
1059         WorkflowType resourceType = WorkflowType.NETWORK;
1060         InfraActiveRequests request = new InfraActiveRequests();
1061         L3Network network = new L3Network();
1062         network.setNetworkName("networkName");
1063         doReturn(network).when(bbSetupUtils).getAAIL3Network(resourceId);
1064
1065         workflowActionBBTasks.setInstanceName(resourceId, resourceType, request);
1066         assertEquals("networkName", request.getNetworkName());
1067     }
1068
1069     @Test
1070     public void setConfigurationNameTest() {
1071         String resourceId = "40bc4ebd-11df-4610-8055-059f7441ec1c";
1072         WorkflowType resourceType = WorkflowType.CONFIGURATION;
1073         InfraActiveRequests request = new InfraActiveRequests();
1074         Configuration configuration = new Configuration();
1075         configuration.setConfigurationName("configurationName");
1076         doReturn(configuration).when(bbSetupUtils).getAAIConfiguration(resourceId);
1077
1078         workflowActionBBTasks.setInstanceName(resourceId, resourceType, request);
1079         assertEquals("configurationName", request.getConfigurationName());
1080     }
1081
1082     @Test
1083     public void setInstanceGroupNameTest() {
1084         String resourceId = "40bc4ebd-11df-4610-8055-059f7441ec1c";
1085         WorkflowType resourceType = WorkflowType.INSTANCE_GROUP;
1086         InfraActiveRequests request = new InfraActiveRequests();
1087         InstanceGroup instanceGroup = new InstanceGroup();
1088         instanceGroup.setInstanceGroupName("instanceGroupName");
1089         doReturn(instanceGroup).when(bbSetupUtils).getAAIInstanceGroup(resourceId);
1090
1091         workflowActionBBTasks.setInstanceName(resourceId, resourceType, request);
1092         assertEquals("instanceGroupName", request.getInstanceGroupName());
1093     }
1094
1095     @Test
1096     public void setVolumeGroupNameTest() {
1097         String resourceId = "40bc4ebd-11df-4610-8055-059f7441ec1c";
1098         WorkflowType resourceType = WorkflowType.VOLUMEGROUP;
1099         InfraActiveRequests request = new InfraActiveRequests();
1100         request.setVnfId("4aa72c90-21eb-4465-8847-997e27af6c3e");
1101         VolumeGroup volumeGroup = new VolumeGroup();
1102         volumeGroup.setVolumeGroupName("volumeGroupName");
1103         Optional<VolumeGroup> returnVolumeGroup = Optional.of(volumeGroup);
1104
1105         doReturn(returnVolumeGroup).when(bbSetupUtils).getRelatedVolumeGroupByIdFromVnf(request.getVnfId(), resourceId);
1106         workflowActionBBTasks.setInstanceName(resourceId, resourceType, request);
1107
1108         assertEquals("volumeGroupName", request.getVolumeGroupName());
1109     }
1110
1111     private void prepareDelegateExecution() {
1112         execution.setVariable("mso-request-id", SAMPLE_MSO_REQUEST_ID);
1113         execution.setVariable("requestAction", SAMPLE_REQUEST_ACTION);
1114         execution.setVariable("gCurrentSequence", SAMPLE_SEQUENCE);
1115         execution.setVariable("homing", false);
1116         execution.setVariable("calledHoming", false);
1117     }
1118
1119     private List<BuildingBlockRollback> getRollbackBuildingBlockList() {
1120         List<BuildingBlockRollback> rollbackBBList = Collections.unmodifiableList(Arrays.asList(
1121                 new BuildingBlockRollback(1, "ActivateNetworkBB", null, "DeactivateNetworkBB", null),
1122                 new BuildingBlockRollback(2, "ActivatePnfBB", null, "DeactivatePnfBB", null),
1123                 new BuildingBlockRollback(3, "ActivateServiceInstanceBB", null, "DeactivateServiceInstanceBB", null),
1124                 new BuildingBlockRollback(4, "ActivateVfModuleBB", null, "DeactivateVfModuleBB", null),
1125                 new BuildingBlockRollback(5, "ActivateVnfBB", null, "DeactivateVnfBB", null),
1126                 new BuildingBlockRollback(6, "ActivateVolumeGroupBB", null, "DeactivateVolumeGroupBB", null),
1127                 new BuildingBlockRollback(7, "AssignNetworkBB", null, "UnassignNetworkBB", null),
1128                 new BuildingBlockRollback(8, "AssignServiceInstanceBB", null, "UnassignServiceInstanceBB", null),
1129                 new BuildingBlockRollback(9, "AssignVfModuleBB", null, "UnassignVfModuleBB", null),
1130                 new BuildingBlockRollback(10, "AssignVnfBB", null, "UnassignVnfBB", null),
1131                 new BuildingBlockRollback(11, "AssignVolumeGroupBB", null, "UnassignVolumeGroupBB", null),
1132                 new BuildingBlockRollback(12, "ControllerExecutionBB", "config-assign", "ControllerExecutionBB",
1133                         "config-unassign"),
1134                 new BuildingBlockRollback(13, "ControllerExecutionBB", "config-deploy", "ControllerExecutionBB",
1135                         "config-undeploy"),
1136                 new BuildingBlockRollback(14, "ControllerExecutionBB", "service-config-deploy", "ControllerExecutionBB",
1137                         "service-config-undeploy"),
1138                 new BuildingBlockRollback(15, "CreateNetworkBB", null, "DeleteNetworkBB", null),
1139                 new BuildingBlockRollback(16, "CreateNetworkCollectionBB", null, "DeleteNetworkCollectionBB", null),
1140                 new BuildingBlockRollback(17, "CreateVfModuleBB", null, "DeleteVfModuleBB", null),
1141                 new BuildingBlockRollback(18, "CreateVolumeGroupBB", null, "DeleteVolumeGroupBB", null),
1142                 new BuildingBlockRollback(19, "VNFSetInMaintFlagActivity", null, "VNFUnsetInMaintFlagActivity", null),
1143                 new BuildingBlockRollback(20, "VNFSetClosedLoopDisabledFlagActivity", null,
1144                         "VNFUnsetClosedLoopDisabledFlagActivity", null),
1145                 new BuildingBlockRollback(21, "VNFLockActivity", null, "VNFUnlockActivity", null),
1146                 new BuildingBlockRollback(22, "VNFStopActivity", null, "VNFStartActivity", null),
1147                 new BuildingBlockRollback(23, "VNFQuiesceTrafficActivity", null, "VNFResumeTrafficActivity", null),
1148                 new BuildingBlockRollback(24, "EtsiVnfInstantiateBB", null, "EtsiVnfDeleteBB", null),
1149                 // AddFabricConfigurationBB this does not seem to be present as a bpmn in Guilin
1150                 new BuildingBlockRollback(25, "AddFabricConfigurationBB", null, "DeleteFabricConfigurationBB", null)));
1151         return rollbackBBList;
1152     }
1153
1154 }