Updated SDC listener and dependent bundles
[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 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
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  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.common.impl;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Matchers;
33 import org.onap.appc.dg.common.VnfExecutionFlow;
34 import org.onap.appc.dg.dependencymanager.DependencyManager;
35 import org.onap.appc.dg.dependencymanager.exception.DependencyModelNotFound;
36 import org.onap.appc.dg.dependencymanager.impl.DependencyModelFactory;
37 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
38 import org.onap.appc.dg.objects.Node;
39 import org.onap.appc.dg.objects.VnfcDependencyModel;
40 import org.onap.appc.domainmodel.Vnfc;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import org.osgi.framework.FrameworkUtil;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.Map;
50 import java.util.Set;
51
52 @RunWith(PowerMockRunner.class)
53 @PrepareForTest({VnfExecutionFlowImplTest.class, 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         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
79     }
80
81     @Test
82     public void testComplexFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
83         Map<String, String> params = prepareParams();
84         SvcLogicContext context = prepareContextForComplexDependency();
85         VnfcDependencyModel dependencyModel = readComplexDependencyModel();
86
87         PowerMockito.mockStatic(DependencyModelFactory.class);
88         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
89
90         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
91         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
92                 .thenReturn(dependencyModel);
93
94         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
95         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
96     }
97
98     @Test(expected = RuntimeException.class)
99     public void testCycleFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
100         Map<String, String> params = prepareParams();
101         SvcLogicContext context = prepareContextForComplexDependency();
102         VnfcDependencyModel dependencyModel = readCyclicDependencyModel();
103         PowerMockito.mockStatic(DependencyModelFactory.class);
104         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
105
106         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
107         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
108                 .thenReturn(dependencyModel);
109
110         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
111         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
112     }
113
114     private VnfcDependencyModel readCyclicDependencyModel() {
115
116         Vnfc a = createVnfc("A","Active-Passive",null,false);
117         Vnfc b = createVnfc("B","Active-Active",null,false);
118         Vnfc c = createVnfc("C","Active-Active",null,false);
119         Vnfc d = createVnfc("D","Active-Active",null,false);
120         Vnfc e = createVnfc("E","Active-Active",null,false);
121         Vnfc f = createVnfc("F","Active-Active",null,false);
122         Vnfc g = createVnfc("G","Active-Active",null,false);
123
124         Node aNode = new Node(a);
125         Node bNode = new Node(b);
126         Node cNode = new Node(c);
127         Node dNode = new Node(d);
128         Node eNode = new Node(e);
129         Node fNode = new Node(f);
130         Node gNode = new Node(g);
131
132         bNode.addParent(a);
133         cNode.addParent(a);
134         cNode.addParent(b);
135
136         bNode.addParent(d);
137         dNode.addParent(c);
138
139         Set<Node<Vnfc>> dependencies = new HashSet<>();
140         dependencies.add(aNode);
141         dependencies.add(bNode);
142         dependencies.add(cNode);
143         dependencies.add(dNode);
144         dependencies.add(eNode);
145         dependencies.add(fNode);
146         dependencies.add(gNode);
147
148         return new VnfcDependencyModel(dependencies);
149
150     }
151
152     private Vnfc createVnfc(String vnfcType,String resilienceType,String vnfcName,boolean mandatory) {
153         Vnfc vnfc = new Vnfc();
154         vnfc.setVnfcType(vnfcType);
155         vnfc.setResilienceType(resilienceType);
156         vnfc.setVnfcName(vnfcName);
157         vnfc.setMandatory(mandatory);
158         return vnfc;
159     }
160
161     private SvcLogicContext prepareContextForComplexDependency() {
162         SvcLogicContext context = new SvcLogicContext();
163         context.setAttribute("input.action-identifiers.vnf-id","1");
164         context.setAttribute("vnf.type","vSCP");
165         context.setAttribute("vnf.vnfcCount","7");
166
167         context.setAttribute("vnf.vnfc[0].name","A");
168         context.setAttribute("vnf.vnfc[0].type","A");
169         context.setAttribute("vnf.vnfc[0].vm_count","2");
170         context.setAttribute("vnf.vnfc[0].vm[0].url","A1");
171         context.setAttribute("vnf.vnfc[0].vm[1].url","A2");
172
173         context.setAttribute("vnf.vnfc[1].name","B");
174         context.setAttribute("vnf.vnfc[1].type","B");
175         context.setAttribute("vnf.vnfc[1].vm_count","5");
176         context.setAttribute("vnf.vnfc[1].vm[0].url","B1");
177         context.setAttribute("vnf.vnfc[1].vm[1].url","B2");
178         context.setAttribute("vnf.vnfc[1].vm[2].url","B3");
179         context.setAttribute("vnf.vnfc[1].vm[3].url","B4");
180         context.setAttribute("vnf.vnfc[1].vm[4].url","B5");
181
182         context.setAttribute("vnf.vnfc[2].name","C");
183         context.setAttribute("vnf.vnfc[2].type","C");
184         context.setAttribute("vnf.vnfc[2].vm_count","4");
185         context.setAttribute("vnf.vnfc[2].vm[0].url","C1");
186         context.setAttribute("vnf.vnfc[2].vm[1].url","C2");
187         context.setAttribute("vnf.vnfc[2].vm[2].url","C3");
188         context.setAttribute("vnf.vnfc[2].vm[3].url","C4");
189
190         context.setAttribute("vnf.vnfc[3].name","D");
191         context.setAttribute("vnf.vnfc[3].type","D");
192         context.setAttribute("vnf.vnfc[3].vm_count","3");
193         context.setAttribute("vnf.vnfc[3].vm[0].url","D1");
194         context.setAttribute("vnf.vnfc[3].vm[1].url","D2");
195         context.setAttribute("vnf.vnfc[3].vm[2].url","D3");
196
197         context.setAttribute("vnf.vnfc[4].name","E");
198         context.setAttribute("vnf.vnfc[4].type","E");
199         context.setAttribute("vnf.vnfc[4].vm_count","2");
200         context.setAttribute("vnf.vnfc[4].vm[0].url","E1");
201         context.setAttribute("vnf.vnfc[4].vm[1].url","E2");
202
203         context.setAttribute("vnf.vnfc[5].name","F");
204         context.setAttribute("vnf.vnfc[5].type","F");
205         context.setAttribute("vnf.vnfc[5].vm_count","1");
206         context.setAttribute("vnf.vnfc[5].vm[0].url","F1");
207
208         context.setAttribute("vnf.vnfc[6].name","G");
209         context.setAttribute("vnf.vnfc[6].type","G");
210         context.setAttribute("vnf.vnfc[6].vm_count","1");
211         context.setAttribute("vnf.vnfc[6].vm[0].url","G1");
212
213
214         return context;
215     }
216
217     private VnfcDependencyModel readComplexDependencyModel() {
218         Vnfc a = createVnfc("A","Active-Passive",null,false);
219         Vnfc b = createVnfc("B","Active-Active",null,false);
220         Vnfc c = createVnfc("C","Active-Active",null,false);
221         Vnfc d = createVnfc("D","Active-Active",null,false);
222         Vnfc e = createVnfc("E","Active-Active",null,false);
223         Vnfc f = createVnfc("F","Active-Active",null,false);
224         Vnfc g = createVnfc("G","Active-Active",null,false);
225
226
227         Node aNode = new Node(a);
228         Node bNode = new Node(b);
229         Node cNode = new Node(c);
230         Node dNode = new Node(d);
231         Node eNode = new Node(e);
232         Node fNode = new Node(f);
233         Node gNode = new Node(g);
234
235         bNode.addParent(a);
236         cNode.addParent(a);
237
238         dNode.addParent(b);
239         eNode.addParent(b);
240         gNode.addParent(b);
241
242         fNode.addParent(c);
243
244         gNode.addParent(f);
245
246         Set<Node<Vnfc>> dependencies = new HashSet<>();
247         dependencies.add(aNode);
248         dependencies.add(bNode);
249         dependencies.add(cNode);
250         dependencies.add(dNode);
251         dependencies.add(eNode);
252         dependencies.add(fNode);
253         dependencies.add(gNode);
254
255         return new VnfcDependencyModel(dependencies);
256     }
257
258     private VnfcDependencyModel readDependencyModel() {
259
260         Vnfc smp = createVnfc("SMP","Active-Passive",null,false);
261         Vnfc be = createVnfc("BE","Active-Active",null,false);
262         Vnfc fe = createVnfc("FE","Active-Active",null,false);
263
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() throws DependencyModelNotFound, InvalidDependencyModelException {
347         Map<String, String> params = prepareParams();
348         SvcLogicContext context = prepareContext();
349         VnfcDependencyModel dependencyModel = readDependencyModel();
350
351         Vnfc xe = createVnfc("XE","Active-Active",null, true);
352         Node xeNode = new Node(xe);
353         dependencyModel.getDependencies().add(xeNode);
354
355         PowerMockito.mockStatic(DependencyModelFactory.class);
356         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
357
358         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
359         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
360                 .thenReturn(dependencyModel);
361
362         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
363         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
364     }
365
366     @Test
367     public void testMissingOptionalVnfcTypeInInventoryModel() throws DependencyModelNotFound, InvalidDependencyModelException {
368         Map<String, String> params = prepareParams();
369         SvcLogicContext context = prepareContext();
370         VnfcDependencyModel dependencyModel = readDependencyModel();
371
372         Vnfc xe = createVnfc("XE","Active-Active",null, false);
373         Node xeNode = new Node(xe);
374         dependencyModel.getDependencies().add(xeNode);
375
376         PowerMockito.mockStatic(DependencyModelFactory.class);
377         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
378
379         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
380         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
381                 .thenReturn(dependencyModel);
382
383         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
384         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
385     }
386
387     @Test
388     public void testMissingOptionalVnfcTypeInInventoryModelWithDependentChild() throws DependencyModelNotFound, InvalidDependencyModelException {
389         Map<String, String> params = prepareParams();
390         SvcLogicContext context = prepareContext();
391         context.setAttribute("vnf.vnfc[3].name","YEname");
392         context.setAttribute("vnf.vnfc[3].type","YE");
393         context.setAttribute("vnf.vnfc[3].vm_count","2");
394         context.setAttribute("vnf.vnfc[3].vm[0].url","YE_URL1");
395         context.setAttribute("vnf.vnfc[3].vm[1].url","YE_URL2");
396         context.setAttribute("vnf.vnfcCount","4");
397
398         VnfcDependencyModel dependencyModel = readDependencyModel();
399
400         Vnfc xe = createVnfc("XE","Active-Active",null, false);
401         Vnfc ye = createVnfc("YE","Active-Active",null, true);
402         Node xeNode = new Node(xe);
403         Node yeNode = new Node(ye);
404         yeNode.addParent(xe);
405
406         dependencyModel.getDependencies().add(yeNode);
407         dependencyModel.getDependencies().add(xeNode);
408
409         PowerMockito.mockStatic(DependencyModelFactory.class);
410         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
411
412         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
413         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
414                 .thenReturn(dependencyModel);
415
416         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
417         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
418     }
419
420
421 }