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