Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / test / java / org / onap / appc / dg / common / impl / VnfExecutionFlowImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (C) 2018 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.common.impl;
26
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Matchers;
35 import org.onap.appc.dg.common.VnfExecutionFlow;
36 import org.onap.appc.dg.dependencymanager.DependencyManager;
37 import org.onap.appc.dg.dependencymanager.exception.DependencyModelNotFound;
38 import org.onap.appc.dg.dependencymanager.impl.DependencyModelFactory;
39 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
40 import org.onap.appc.dg.objects.Node;
41 import org.onap.appc.dg.objects.VnfcDependencyModel;
42 import org.onap.appc.domainmodel.Vnfc;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.osgi.framework.FrameworkUtil;
45 import org.powermock.api.mockito.PowerMockito;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
48 import org.powermock.reflect.Whitebox;
49 import com.att.eelf.configuration.EELFLogger;
50 import com.att.eelf.configuration.EELFManager;
51
52 @RunWith(PowerMockRunner.class)
53 @PrepareForTest({FrameworkUtil.class, DependencyManager.class, DependencyModelFactory.class})
54 @SuppressWarnings("unchecked")
55 public class VnfExecutionFlowImplTest {
56
57     private final EELFLogger logger = EELFManager.getInstance().getLogger(VnfExecutionFlowImplTest.class);
58
59     @Before
60     public void setUp() {
61         logger.setLevel(EELFLogger.Level.DEBUG);
62     }
63
64     @Test
65     public void testPositiveFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
66         Map<String, String> params = prepareParams();
67         SvcLogicContext context = prepareContext();
68         VnfcDependencyModel dependencyModel = readDependencyModel();
69
70         PowerMockito.mockStatic(DependencyModelFactory.class);
71         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
72
73         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
74         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
75                 .thenReturn(dependencyModel);
76
77         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
78         Whitebox.setInternalState(vnfExecutionFlow, "logger", logger);
79         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
80     }
81
82     @Test
83     public void testComplexFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
84         Map<String, String> params = prepareParams();
85         SvcLogicContext context = prepareContextForComplexDependency();
86         VnfcDependencyModel dependencyModel = readComplexDependencyModel();
87
88         PowerMockito.mockStatic(DependencyModelFactory.class);
89         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
90
91         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
92         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
93                 .thenReturn(dependencyModel);
94
95         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
96         Whitebox.setInternalState(vnfExecutionFlow, "logger", logger);
97         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
98     }
99
100     @Test(expected = RuntimeException.class)
101     public void testCycleFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
102         Map<String, String> params = prepareParams();
103         SvcLogicContext context = prepareContextForComplexDependency();
104         VnfcDependencyModel dependencyModel = readCyclicDependencyModel();
105         PowerMockito.mockStatic(DependencyModelFactory.class);
106         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
107
108         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
109         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
110                 .thenReturn(dependencyModel);
111
112         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
113         Whitebox.setInternalState(vnfExecutionFlow, "logger", logger);
114         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
115     }
116
117     private VnfcDependencyModel readCyclicDependencyModel() {
118
119         Vnfc a = createVnfc("A", "Active-Passive", null, false);
120         Vnfc b = createVnfc("B", "Active-Active", null, false);
121         Vnfc c = createVnfc("C", "Active-Active", null, false);
122         Vnfc d = createVnfc("D", "Active-Active", null, false);
123         Vnfc e = createVnfc("E", "Active-Active", null, false);
124         Vnfc f = createVnfc("F", "Active-Active", null, false);
125         Vnfc g = createVnfc("G", "Active-Active", null, false);
126
127         Node aNode = new Node(a);
128         Node bNode = new Node(b);
129         Node cNode = new Node(c);
130         Node dNode = new Node(d);
131         Node eNode = new Node(e);
132         Node fNode = new Node(f);
133         Node gNode = new Node(g);
134
135         bNode.addParent(a);
136         cNode.addParent(a);
137         cNode.addParent(b);
138
139         bNode.addParent(d);
140         dNode.addParent(c);
141
142         Set<Node<Vnfc>> dependencies = new HashSet<>();
143         dependencies.add(aNode);
144         dependencies.add(bNode);
145         dependencies.add(cNode);
146         dependencies.add(dNode);
147         dependencies.add(eNode);
148         dependencies.add(fNode);
149         dependencies.add(gNode);
150
151         return new VnfcDependencyModel(dependencies);
152
153     }
154
155     private Vnfc createVnfc(String vnfcType, String resilienceType, String vnfcName, boolean mandatory) {
156         Vnfc vnfc = new Vnfc();
157         vnfc.setVnfcType(vnfcType);
158         vnfc.setResilienceType(resilienceType);
159         vnfc.setVnfcName(vnfcName);
160         vnfc.setMandatory(mandatory);
161         return vnfc;
162     }
163
164     private SvcLogicContext prepareContextForComplexDependency() {
165         SvcLogicContext context = new SvcLogicContext();
166         context.setAttribute("input.action-identifiers.vnf-id", "1");
167         context.setAttribute("vnf.type", "vSCP");
168         context.setAttribute("vnf.vnfcCount", "7");
169
170         context.setAttribute("vnf.vnfc[0].name", "A");
171         context.setAttribute("vnf.vnfc[0].type", "A");
172         context.setAttribute("vnf.vnfc[0].vm_count", "2");
173         context.setAttribute("vnf.vnfc[0].vm[0].url", "A1");
174         context.setAttribute("vnf.vnfc[0].vm[1].url", "A2");
175
176         context.setAttribute("vnf.vnfc[1].name", "B");
177         context.setAttribute("vnf.vnfc[1].type", "B");
178         context.setAttribute("vnf.vnfc[1].vm_count", "5");
179         context.setAttribute("vnf.vnfc[1].vm[0].url", "B1");
180         context.setAttribute("vnf.vnfc[1].vm[1].url", "B2");
181         context.setAttribute("vnf.vnfc[1].vm[2].url", "B3");
182         context.setAttribute("vnf.vnfc[1].vm[3].url", "B4");
183         context.setAttribute("vnf.vnfc[1].vm[4].url", "B5");
184
185         context.setAttribute("vnf.vnfc[2].name", "C");
186         context.setAttribute("vnf.vnfc[2].type", "C");
187         context.setAttribute("vnf.vnfc[2].vm_count", "4");
188         context.setAttribute("vnf.vnfc[2].vm[0].url", "C1");
189         context.setAttribute("vnf.vnfc[2].vm[1].url", "C2");
190         context.setAttribute("vnf.vnfc[2].vm[2].url", "C3");
191         context.setAttribute("vnf.vnfc[2].vm[3].url", "C4");
192
193         context.setAttribute("vnf.vnfc[3].name", "D");
194         context.setAttribute("vnf.vnfc[3].type", "D");
195         context.setAttribute("vnf.vnfc[3].vm_count", "3");
196         context.setAttribute("vnf.vnfc[3].vm[0].url", "D1");
197         context.setAttribute("vnf.vnfc[3].vm[1].url", "D2");
198         context.setAttribute("vnf.vnfc[3].vm[2].url", "D3");
199
200         context.setAttribute("vnf.vnfc[4].name", "E");
201         context.setAttribute("vnf.vnfc[4].type", "E");
202         context.setAttribute("vnf.vnfc[4].vm_count", "2");
203         context.setAttribute("vnf.vnfc[4].vm[0].url", "E1");
204         context.setAttribute("vnf.vnfc[4].vm[1].url", "E2");
205
206         context.setAttribute("vnf.vnfc[5].name", "F");
207         context.setAttribute("vnf.vnfc[5].type", "F");
208         context.setAttribute("vnf.vnfc[5].vm_count", "1");
209         context.setAttribute("vnf.vnfc[5].vm[0].url", "F1");
210
211         context.setAttribute("vnf.vnfc[6].name", "G");
212         context.setAttribute("vnf.vnfc[6].type", "G");
213         context.setAttribute("vnf.vnfc[6].vm_count", "1");
214         context.setAttribute("vnf.vnfc[6].vm[0].url", "G1");
215
216         return context;
217     }
218
219     private VnfcDependencyModel readComplexDependencyModel() {
220         Vnfc a = createVnfc("A", "Active-Passive", null, false);
221         Vnfc b = createVnfc("B", "Active-Active", null, false);
222         Vnfc c = createVnfc("C", "Active-Active", null, false);
223         Vnfc d = createVnfc("D", "Active-Active", null, false);
224         Vnfc e = createVnfc("E", "Active-Active", null, false);
225         Vnfc f = createVnfc("F", "Active-Active", null, false);
226         Vnfc g = createVnfc("G", "Active-Active", null, false);
227
228         Node aNode = new Node(a);
229         Node bNode = new Node(b);
230         Node cNode = new Node(c);
231         Node dNode = new Node(d);
232         Node eNode = new Node(e);
233         Node fNode = new Node(f);
234         Node gNode = new Node(g);
235
236         bNode.addParent(a);
237         cNode.addParent(a);
238
239         dNode.addParent(b);
240         eNode.addParent(b);
241         gNode.addParent(b);
242
243         fNode.addParent(c);
244
245         gNode.addParent(f);
246
247         Set<Node<Vnfc>> dependencies = new HashSet<>();
248         dependencies.add(aNode);
249         dependencies.add(bNode);
250         dependencies.add(cNode);
251         dependencies.add(dNode);
252         dependencies.add(eNode);
253         dependencies.add(fNode);
254         dependencies.add(gNode);
255
256         return new VnfcDependencyModel(dependencies);
257     }
258
259     private VnfcDependencyModel readDependencyModel() {
260
261         Vnfc smp = createVnfc("SMP", "Active-Passive", null, false);
262         Vnfc be = createVnfc("BE", "Active-Active", null, false);
263         Vnfc fe = createVnfc("FE", "Active-Active", null, false);
264
265         Node smpNode = new Node(smp);
266         Node beNode = new Node(be);
267         Node feNode = new Node(fe);
268
269         beNode.addParent(smp);
270         feNode.addParent(be);
271 //        smpNode.addParent(fe);
272
273         Set<Node<Vnfc>> dependencies = new HashSet<>();
274         dependencies.add(smpNode);
275         dependencies.add(feNode);
276         dependencies.add(beNode);
277
278         return new VnfcDependencyModel(dependencies);
279     }
280
281     private Map<String, String> prepareParams() {
282         Map<String, String> params = new HashMap<>();
283         params.put(Constants.DEPENDENCY_TYPE, "RESOURCE");
284         params.put(Constants.FLOW_STRATEGY, "FORWARD");
285
286         params.put(Constants.VNF_TYPE, "vSCP");
287         params.put(Constants.VNF_VERION, "1.00");
288         return params;
289     }
290
291     private SvcLogicContext prepareContext() {
292         SvcLogicContext context = new SvcLogicContext();
293         context.setAttribute("input.action-identifiers.vnf-id", "1");
294         context.setAttribute("vnf.type", "vSCP");
295         context.setAttribute("vnf.vnfcCount", "3");
296
297         context.setAttribute("vnf.vnfc[0].name", "SMPname");
298         context.setAttribute("vnf.vnfc[0].type", "SMP");
299         context.setAttribute("vnf.vnfc[0].vm_count", "2");
300         context.setAttribute("vnf.vnfc[0].vm[0].url", "SMP_URL1");
301         context.setAttribute("vnf.vnfc[0].vm[1].url", "SMP_URL2");
302
303         context.setAttribute("vnf.vnfc[1].name", "BEname");
304         context.setAttribute("vnf.vnfc[1].type", "BE");
305         context.setAttribute("vnf.vnfc[1].vm_count", "5");
306         context.setAttribute("vnf.vnfc[1].vm[0].url", "BE_URL1");
307         context.setAttribute("vnf.vnfc[1].vm[1].url", "BE_URL2");
308         context.setAttribute("vnf.vnfc[1].vm[2].url", "BE_URL3");
309         context.setAttribute("vnf.vnfc[1].vm[3].url", "BE_URL4");
310         context.setAttribute("vnf.vnfc[1].vm[4].url", "BE_URL5");
311
312         context.setAttribute("vnf.vnfc[2].name", "FEname");
313         context.setAttribute("vnf.vnfc[2].type", "FE");
314         context.setAttribute("vnf.vnfc[2].vm_count", "2");
315         context.setAttribute("vnf.vnfc[2].vm[0].url", "FE_URL1");
316         context.setAttribute("vnf.vnfc[2].vm[1].url", "FE_URL2");
317
318         return context;
319     }
320
321     @Test(expected = RuntimeException.class)
322     public void testMissingVnfcTypeInDependencyModel() throws DependencyModelNotFound, InvalidDependencyModelException {
323         Map<String, String> params = prepareParams();
324         SvcLogicContext context = prepareContext();
325         context.setAttribute("vnf.vnfc[3].name", "XEname");
326         context.setAttribute("vnf.vnfc[3].type", "XE");
327         context.setAttribute("vnf.vnfc[3].vm_count", "2");
328         context.setAttribute("vnf.vnfc[3].vm[0].url", "XE_URL1");
329         context.setAttribute("vnf.vnfc[3].vm[1].url", "XE_URL2");
330         context.setAttribute("vnf.vnfcCount", "4");
331
332         VnfcDependencyModel dependencyModel = readDependencyModel();
333
334         PowerMockito.mockStatic(DependencyModelFactory.class);
335         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
336
337         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
338         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
339                 .thenReturn(dependencyModel);
340
341         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
342         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
343     }
344
345     @Test(expected = RuntimeException.class)
346     public void testMissingMandatoryVnfcTypeInInventoryModel()
347             throws DependencyModelNotFound, InvalidDependencyModelException {
348         Map<String, String> params = prepareParams();
349         SvcLogicContext context = prepareContext();
350         VnfcDependencyModel dependencyModel = readDependencyModel();
351
352         Vnfc xe = createVnfc("XE", "Active-Active", null, true);
353         Node xeNode = new Node(xe);
354         dependencyModel.getDependencies().add(xeNode);
355
356         PowerMockito.mockStatic(DependencyModelFactory.class);
357         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
358
359         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
360         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
361                 .thenReturn(dependencyModel);
362
363         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
364         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
365     }
366
367     @Test
368     public void testMissingOptionalVnfcTypeInInventoryModel()
369             throws DependencyModelNotFound, InvalidDependencyModelException {
370         Map<String, String> params = prepareParams();
371         SvcLogicContext context = prepareContext();
372         VnfcDependencyModel dependencyModel = readDependencyModel();
373
374         Vnfc xe = createVnfc("XE", "Active-Active", null, false);
375         Node xeNode = new Node(xe);
376         dependencyModel.getDependencies().add(xeNode);
377
378         PowerMockito.mockStatic(DependencyModelFactory.class);
379         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
380
381         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
382         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
383                 .thenReturn(dependencyModel);
384
385         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
386         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
387     }
388
389     @Test
390     public void testMissingOptionalVnfcTypeInInventoryModelWithDependentChild()
391             throws DependencyModelNotFound, InvalidDependencyModelException {
392         Map<String, String> params = prepareParams();
393         SvcLogicContext context = prepareContext();
394         context.setAttribute("vnf.vnfc[3].name", "YEname");
395         context.setAttribute("vnf.vnfc[3].type", "YE");
396         context.setAttribute("vnf.vnfc[3].vm_count", "2");
397         context.setAttribute("vnf.vnfc[3].vm[0].url", "YE_URL1");
398         context.setAttribute("vnf.vnfc[3].vm[1].url", "YE_URL2");
399         context.setAttribute("vnf.vnfcCount", "4");
400
401         VnfcDependencyModel dependencyModel = readDependencyModel();
402
403         Vnfc xe = createVnfc("XE", "Active-Active", null, false);
404         Vnfc ye = createVnfc("YE", "Active-Active", null, true);
405         Node xeNode = new Node(xe);
406         Node yeNode = new Node(ye);
407         yeNode.addParent(xe);
408
409         dependencyModel.getDependencies().add(yeNode);
410         dependencyModel.getDependencies().add(xeNode);
411
412         PowerMockito.mockStatic(DependencyModelFactory.class);
413         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
414
415         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
416         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
417                 .thenReturn(dependencyModel);
418
419         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
420         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
421     }
422 }