2  * ============LICENSE_START=======================================================
 
   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
 
  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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.appc.dg.common.impl;
 
  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;
 
  47 import java.util.HashMap;
 
  48 import java.util.HashSet;
 
  52 @RunWith(PowerMockRunner.class)
 
  53 @PrepareForTest({VnfExecutionFlowImplTest.class, FrameworkUtil.class,DependencyManager.class,DependencyModelFactory.class})
 
  54 @SuppressWarnings("unchecked")
 
  55 public class VnfExecutionFlowImplTest {
 
  57     private final EELFLogger logger = EELFManager.getInstance().getLogger(VnfExecutionFlowImplTest.class);
 
  61         logger.setLevel(EELFLogger.Level.DEBUG);
 
  65     public void testPositiveFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
 
  66         Map<String, String> params = prepareParams();
 
  67         SvcLogicContext context = prepareContext();
 
  68         VnfcDependencyModel dependencyModel = readDependencyModel();
 
  70         PowerMockito.mockStatic(DependencyModelFactory.class);
 
  71         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
 
  73         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
  74         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
 
  75                 .thenReturn(dependencyModel);
 
  77         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
  78         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
  82     public void testComplexFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
 
  83         Map<String, String> params = prepareParams();
 
  84         SvcLogicContext context = prepareContextForComplexDependency();
 
  85         VnfcDependencyModel dependencyModel = readComplexDependencyModel();
 
  87         PowerMockito.mockStatic(DependencyModelFactory.class);
 
  88         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
 
  90         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
  91         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
 
  92                 .thenReturn(dependencyModel);
 
  94         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
  95         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
  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);
 
 106         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
 107         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
 
 108                 .thenReturn(dependencyModel);
 
 110         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 111         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
 114     private VnfcDependencyModel readCyclicDependencyModel() {
 
 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);
 
 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);
 
 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);
 
 148         return new VnfcDependencyModel(dependencies);
 
 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);
 
 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");
 
 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");
 
 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");
 
 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");
 
 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");
 
 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");
 
 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");
 
 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");
 
 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);
 
 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);
 
 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);
 
 255         return new VnfcDependencyModel(dependencies);
 
 258     private VnfcDependencyModel readDependencyModel() {
 
 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);
 
 265         Node smpNode = new Node(smp);
 
 266         Node beNode = new Node(be);
 
 267         Node feNode = new Node(fe);
 
 269         beNode.addParent(smp);
 
 270         feNode.addParent(be);
 
 271 //        smpNode.addParent(fe);
 
 273         Set<Node<Vnfc>> dependencies = new HashSet<>();
 
 274         dependencies.add(smpNode);
 
 275         dependencies.add(feNode);
 
 276         dependencies.add(beNode);
 
 278         return new VnfcDependencyModel(dependencies);
 
 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");
 
 286         params.put(Constants.VNF_TYPE,"vSCP");
 
 287         params.put(Constants.VNF_VERION,"1.00");
 
 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");
 
 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");
 
 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");
 
 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");
 
 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");
 
 332         VnfcDependencyModel dependencyModel = readDependencyModel();
 
 334         PowerMockito.mockStatic(DependencyModelFactory.class);
 
 335         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
 
 337         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
 338         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
 
 339                 .thenReturn(dependencyModel);
 
 341         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 342         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
 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();
 
 351         Vnfc xe = createVnfc("XE","Active-Active",null, true);
 
 352         Node xeNode = new Node(xe);
 
 353         dependencyModel.getDependencies().add(xeNode);
 
 355         PowerMockito.mockStatic(DependencyModelFactory.class);
 
 356         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
 
 358         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
 359         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
 
 360                 .thenReturn(dependencyModel);
 
 362         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 363         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
 367     public void testMissingOptionalVnfcTypeInInventoryModel() throws DependencyModelNotFound, InvalidDependencyModelException {
 
 368         Map<String, String> params = prepareParams();
 
 369         SvcLogicContext context = prepareContext();
 
 370         VnfcDependencyModel dependencyModel = readDependencyModel();
 
 372         Vnfc xe = createVnfc("XE","Active-Active",null, false);
 
 373         Node xeNode = new Node(xe);
 
 374         dependencyModel.getDependencies().add(xeNode);
 
 376         PowerMockito.mockStatic(DependencyModelFactory.class);
 
 377         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
 
 379         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
 380         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
 
 381                 .thenReturn(dependencyModel);
 
 383         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 384         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
 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");
 
 398         VnfcDependencyModel dependencyModel = readDependencyModel();
 
 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);
 
 406         dependencyModel.getDependencies().add(yeNode);
 
 407         dependencyModel.getDependencies().add(xeNode);
 
 409         PowerMockito.mockStatic(DependencyModelFactory.class);
 
 410         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
 
 412         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
 413         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
 
 414                 .thenReturn(dependencyModel);
 
 416         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 417         vnfExecutionFlow.getVnfExecutionFlowData(params,context);