2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.dg.common.impl;
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;
46 import java.util.HashMap;
47 import java.util.HashSet;
51 @RunWith(PowerMockRunner.class)
52 @PrepareForTest({VnfExecutionFlowImplTest.class, FrameworkUtil.class,DependencyManager.class,DependencyModelFactory.class})
53 @SuppressWarnings("unchecked")
54 public class VnfExecutionFlowImplTest {
56 private final EELFLogger logger = EELFManager.getInstance().getLogger(VnfExecutionFlowImplTest.class);
60 logger.setLevel(EELFLogger.Level.DEBUG);
64 public void testPositiveFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
65 Map<String, String> params = prepareParams();
66 SvcLogicContext context = prepareContext();
67 VnfcDependencyModel dependencyModel = readDependencyModel();
69 PowerMockito.mockStatic(DependencyModelFactory.class);
70 DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
72 PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
73 PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
74 .thenReturn(dependencyModel);
76 VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
77 vnfExecutionFlow.getVnfExecutionFlowData(params,context);
81 public void testComplexFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
82 Map<String, String> params = prepareParams();
83 SvcLogicContext context = prepareContextForComplexDependency();
84 VnfcDependencyModel dependencyModel = readComplexDependencyModel();
86 PowerMockito.mockStatic(DependencyModelFactory.class);
87 DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
89 PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
90 PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
91 .thenReturn(dependencyModel);
93 VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
94 vnfExecutionFlow.getVnfExecutionFlowData(params,context);
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);
105 PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
106 PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
107 .thenReturn(dependencyModel);
109 VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
110 vnfExecutionFlow.getVnfExecutionFlowData(params,context);
113 private VnfcDependencyModel readCyclicDependencyModel() {
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);
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);
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);
147 return new VnfcDependencyModel(dependencies);
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);
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");
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");
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");
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");
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");
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");
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");
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");
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);
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);
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);
254 return new VnfcDependencyModel(dependencies);
257 private VnfcDependencyModel readDependencyModel() {
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);
264 Node smpNode = new Node(smp);
265 Node beNode = new Node(be);
266 Node feNode = new Node(fe);
268 beNode.addParent(smp);
269 feNode.addParent(be);
270 // smpNode.addParent(fe);
272 Set<Node<Vnfc>> dependencies = new HashSet<>();
273 dependencies.add(smpNode);
274 dependencies.add(feNode);
275 dependencies.add(beNode);
277 return new VnfcDependencyModel(dependencies);
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");
285 params.put(Constants.VNF_TYPE,"vSCP");
286 params.put(Constants.VNF_VERION,"1.00");
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");
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");
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");
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");
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");
331 VnfcDependencyModel dependencyModel = readDependencyModel();
333 PowerMockito.mockStatic(DependencyModelFactory.class);
334 DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
336 PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
337 PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
338 .thenReturn(dependencyModel);
340 VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
341 vnfExecutionFlow.getVnfExecutionFlowData(params,context);
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();
350 Vnfc xe = createVnfc("XE","Active-Active",null, true);
351 Node xeNode = new Node(xe);
352 dependencyModel.getDependencies().add(xeNode);
354 PowerMockito.mockStatic(DependencyModelFactory.class);
355 DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
357 PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
358 PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
359 .thenReturn(dependencyModel);
361 VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
362 vnfExecutionFlow.getVnfExecutionFlowData(params,context);
366 public void testMissingOptionalVnfcTypeInInventoryModel() throws DependencyModelNotFound, InvalidDependencyModelException {
367 Map<String, String> params = prepareParams();
368 SvcLogicContext context = prepareContext();
369 VnfcDependencyModel dependencyModel = readDependencyModel();
371 Vnfc xe = createVnfc("XE","Active-Active",null, false);
372 Node xeNode = new Node(xe);
373 dependencyModel.getDependencies().add(xeNode);
375 PowerMockito.mockStatic(DependencyModelFactory.class);
376 DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
378 PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
379 PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
380 .thenReturn(dependencyModel);
382 VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
383 vnfExecutionFlow.getVnfExecutionFlowData(params,context);
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");
397 VnfcDependencyModel dependencyModel = readDependencyModel();
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);
405 dependencyModel.getDependencies().add(yeNode);
406 dependencyModel.getDependencies().add(xeNode);
408 PowerMockito.mockStatic(DependencyModelFactory.class);
409 DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
411 PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
412 PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
413 .thenReturn(dependencyModel);
415 VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
416 vnfExecutionFlow.getVnfExecutionFlowData(params,context);