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.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.onap.appc.dg.common.VnfExecutionFlow;
 
  35 import org.onap.appc.dg.common.impl.Constants;
 
  36 import org.onap.appc.dg.common.impl.VnfExecutionFlowImpl;
 
  37 import org.onap.appc.dg.dependencymanager.DependencyManager;
 
  38 import org.onap.appc.dg.dependencymanager.exception.DependencyModelNotFound;
 
  39 import org.onap.appc.dg.dependencymanager.impl.DependencyModelFactory;
 
  40 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModel;
 
  41 import org.onap.appc.dg.objects.DependencyTypes;
 
  42 import org.onap.appc.dg.objects.Node;
 
  43 import org.onap.appc.dg.objects.VnfcDependencyModel;
 
  44 import org.onap.appc.domainmodel.Vnfc;
 
  45 import org.onap.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;
 
  51 import java.util.HashMap;
 
  52 import java.util.HashSet;
 
  56 @RunWith(PowerMockRunner.class)
 
  57 @PrepareForTest({TestVnfExecutionFlowImpl.class, FrameworkUtil.class,DependencyManager.class,DependencyModelFactory.class})
 
  58 public class TestVnfExecutionFlowImpl {
 
  60     private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestVnfExecutionFlowImpl.class);
 
  64         logger.setLevel(EELFLogger.Level.DEBUG);
 
  68     public void testPositiveFlow() throws DependencyModelNotFound {
 
  69         Map<String, String> params = prepareParams();
 
  70         SvcLogicContext context = prepareContext();
 
  71         VnfcDependencyModel dependencyModel = readDependencyModel();
 
  73         PowerMockito.mockStatic(DependencyModelFactory.class);
 
  74         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
 
  76         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
  77         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
 
  78                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
 
  79                 .thenReturn(dependencyModel);
 
  81         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
  82         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
  86     public void testComplexFlow() throws DependencyModelNotFound {
 
  87         Map<String, String> params = prepareParams();
 
  88         SvcLogicContext context = prepareContextForComplexDependency();
 
  89         VnfcDependencyModel dependencyModel = readComplexDependencyModel();
 
  91         PowerMockito.mockStatic(DependencyModelFactory.class);
 
  92         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
 
  94         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
  95         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
 
  96                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
 
  97                 .thenReturn(dependencyModel);
 
  99         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 100         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
 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);
 
 111         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
 112         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
 
 113                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
 
 114                 .thenReturn(dependencyModel);
 
 116         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 117         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
 120     private VnfcDependencyModel readCyclicDependencyModel() {
 
 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);
 
 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);
 
 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);
 
 154         return new VnfcDependencyModel(dependencies);
 
 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");
 
 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");
 
 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");
 
 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");
 
 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");
 
 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");
 
 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");
 
 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");
 
 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);
 
 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);
 
 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);
 
 252         return new VnfcDependencyModel(dependencies);
 
 255     private VnfcDependencyModel readDependencyModel() {
 
 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);
 
 262         Node smpNode = new Node(smp);
 
 263         Node beNode = new Node(be);
 
 264         Node feNode = new Node(fe);
 
 266         beNode.addParent(smp);
 
 267         feNode.addParent(be);
 
 268 //        smpNode.addParent(fe);
 
 270         Set<Node<Vnfc>> dependencies = new HashSet<>();
 
 271         dependencies.add(smpNode);
 
 272         dependencies.add(feNode);
 
 273         dependencies.add(beNode);
 
 275         return new VnfcDependencyModel(dependencies);
 
 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");
 
 283         params.put(Constants.VNF_TYPE,"vSCP");
 
 284         params.put(Constants.VNF_VERION,"1.00");
 
 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");
 
 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");
 
 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");
 
 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");
 
 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");
 
 329         VnfcDependencyModel dependencyModel = readDependencyModel();
 
 331         PowerMockito.mockStatic(DependencyModelFactory.class);
 
 332         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
 
 334         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
 335         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
 
 336                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
 
 337                 .thenReturn(dependencyModel);
 
 339         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 340         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
 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();
 
 349         Vnfc xe = new Vnfc("XE","Active-Active",null, true);
 
 350         Node xeNode = new Node(xe);
 
 351         dependencyModel.getDependencies().add(xeNode);
 
 353         PowerMockito.mockStatic(DependencyModelFactory.class);
 
 354         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
 
 356         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
 
 357         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
 
 358                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
 
 359                 .thenReturn(dependencyModel);
 
 361         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 362         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
 366     public void testMissingOptionalVnfcTypeInInventoryModel() throws DependencyModelNotFound {
 
 367         Map<String, String> params = prepareParams();
 
 368         SvcLogicContext context = prepareContext();
 
 369         VnfcDependencyModel dependencyModel = readDependencyModel();
 
 371         Vnfc xe = new Vnfc("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((
 
 380                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
 
 381                 .thenReturn(dependencyModel);
 
 383         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 384         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
 
 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");
 
 398         VnfcDependencyModel dependencyModel = readDependencyModel();
 
 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);
 
 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((
 
 414                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
 
 415                 .thenReturn(dependencyModel);
 
 417         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
 
 418         vnfExecutionFlow.getVnfExecutionFlowData(params,context);