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.flowbuilder.impl;
 
  29 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
 
  30 import org.onap.appc.domainmodel.Vnfc;
 
  33 public class ReverseFlowStrategy extends AbstractFlowStrategy {
 
  36     protected List<List<Vnfc>> orderDependencies() throws InvalidDependencyModelException{
 
  37         ArrayList<List<Vnfc>> arrayList = new ArrayList<>();
 
  39         Queue<Vnfc> queue1 = new LinkedList();
 
  40         Set<Vnfc> queue2 = new LinkedHashSet<>();
 
  42         Set<Vnfc> uniqueElementSet = new HashSet<>();
 
  43         Set<Vnfc> duplicateElementSet = new HashSet<>();
 
  45         // identifying independent nodes in queue1
 
  46         for(int colIndex=0;colIndex<graph.getSize();colIndex++){
 
  48             for(int rowIndex=0;rowIndex<graph.getSize();rowIndex++){
 
  49                 sum+= graph.getDependencyMatrix()[rowIndex][colIndex];
 
  52                 Vnfc vnfc = graph.getVertexList().get(colIndex);
 
  57             throw new InvalidDependencyModelException("There seems to be no leaf node for Vnfc dependencies");
 
  59         arrayList.add((List<Vnfc>)queue1);
 
  60         queue1 = new LinkedList<>(queue1);
 
  65             // iterating over queue1 and for each node in it finding all dependent nodes and putting them on queue2
 
  66             while(!queue1.isEmpty()){
 
  67                 Vnfc listItem = queue1.remove();
 
  68                 Integer rowIndex = graph.getIndex(listItem);
 
  69                 for(Integer index =0;index<graph.getSize();index++){
 
  70                     Integer value = graph.getDependencyMatrix()[rowIndex][index];
 
  72                         Vnfc vnfc = graph.getVertexList().get(index);
 
  77             for(Vnfc vnfc:queue2){
 
  78                 if(!uniqueElementSet.add(vnfc)){
 
  79                     duplicateElementSet.add(vnfc);
 
  83                 flag= false; // empty queue2 indicates that all leaf nodes have been identified, i.e. stop the iteration
 
  86                 arrayList.add(new ArrayList<Vnfc>(queue2));
 
  87                 if(arrayList.size()>graph.getSize()){
 
  88                     // dependency list cannot be larger than total number of nodes
 
  89                     // if it happens indicates cycle in the dependency
 
  90                     throw new InvalidDependencyModelException("Cyclic dependency detected between the VNFCs");
 
  92                 queue1.addAll(queue2);
 
  93                 queue2 = new LinkedHashSet<>();
 
  96         // If any node depends on multiple nodes present in different execution sequence,
 
  97         // its execution should happen on the higher order, removing its presence on lower execution sequence
 
  98         if(!duplicateElementSet.isEmpty()){
 
  99             for(Vnfc vnfc:duplicateElementSet){
 
 100                 boolean firstOccurrence= true;
 
 101                 for(int i=0;i<arrayList.size();i++){
 
 102                     List<Vnfc> list = arrayList.get(i);
 
 103                     if(list.contains(vnfc)){
 
 105                             firstOccurrence =false;