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