Updated SDC listener and dependent bundles
[appc.git] / appc-dg / appc-dg-shared / appc-dg-dependency-model / src / main / java / org / onap / appc / dg / flowbuilder / impl / AbstractFlowStrategy.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.onap.appc.dg.flowbuilder.impl;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Set;
33
34 import org.onap.appc.dg.flowbuilder.FlowStrategy;
35 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
36 import org.onap.appc.dg.flowbuilder.helper.Graph;
37 import org.onap.appc.dg.objects.*;
38 import org.onap.appc.domainmodel.Vnfc;
39
40
41 public abstract class AbstractFlowStrategy implements FlowStrategy {
42
43     private final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractFlowStrategy.class);
44
45     Graph<Vnfc> graph;
46
47     public VnfcFlowModel buildFlowModel(VnfcDependencyModel dependencyModel, InventoryModel inventoryModel) throws InvalidDependencyModelException {
48         if(logger.isTraceEnabled()){
49             logger.trace("Entering into buildFlowModel with dependency model = " + dependencyModel
50                     + "inventory model = " +inventoryModel);
51         }
52
53         if(dependencyModel == null
54                 || dependencyModel.getDependencies() ==null
55                 || dependencyModel.getDependencies().size() ==0){
56             logger.debug("Dependency model not available, building flow model with sequence");
57             throw new InvalidDependencyModelException("Dependency model either null or does not contain any dependency");
58         }
59
60         VnfcFlowModel flowModel = buildFlowModel(dependencyModel);
61         if(logger.isDebugEnabled()){
62             logger.debug("Flow Model without instance data: \n" + flowModel);
63         }
64
65         logger.info("Populating flow model with A&AI data");
66         populateFlowModel(flowModel,inventoryModel);
67         if(logger.isDebugEnabled()){
68             logger.debug("Flow Model with instance data: \n" + flowModel);
69         }
70
71         return flowModel;
72     }
73
74     private void populateFlowModel(VnfcFlowModel flowModel, InventoryModel inventoryModel) {
75         Iterator<List<Vnfc>> flowIterator;
76
77         for(Vnfc vnfcFromInventory:inventoryModel.getVnf().getVnfcs()){
78             flowIterator = flowModel.getModelIterator();
79             String vnfcType = vnfcFromInventory.getVnfcType();
80             while (flowIterator.hasNext()){
81                 for(Vnfc vnfcFromFlowModel:flowIterator.next() ){
82                     if(vnfcType.equalsIgnoreCase(vnfcFromFlowModel.getVnfcType())){
83                         vnfcFromFlowModel.setVnfcName(vnfcFromInventory.getVnfcName());
84                         vnfcFromFlowModel.setVserverList(vnfcFromInventory.getVserverList());
85                     }
86                 }
87             }
88
89         }
90
91     }
92
93     @SuppressWarnings("unchecked")
94     private VnfcFlowModel buildFlowModel(VnfcDependencyModel dependencyModel) throws InvalidDependencyModelException {
95         Set<Node<Vnfc>> dependencies = dependencyModel.getDependencies();
96         graph = new Graph(dependencies.size());
97
98         for(Node<Vnfc> node:dependencies){
99             graph.addVertex(node.getChild());
100         }
101
102         for(Node node:dependencies){
103             Vnfc child = (Vnfc)node.getChild();
104             List<Vnfc> parents = node.getParents();
105             for(Vnfc parent:parents){
106                 graph.addEdge(child,parent);
107             }
108         }
109         List<List<Vnfc>> dependencyList = orderDependencies();
110
111         VnfcFlowModel.VnfcFlowModelBuilder builder = new VnfcFlowModel.VnfcFlowModelBuilder();
112         int count=0;
113         int flowModelSize = 0;
114         for(List<Vnfc> vnfcList:dependencyList){
115             builder.addMetadata(count,vnfcList);
116             flowModelSize += vnfcList.size();
117             count++;
118         }
119         if(flowModelSize != dependencies.size()){
120             throw new InvalidDependencyModelException("Cyclic dependency detected between the VNFC's");
121         }
122
123         return builder.build();
124     }
125
126     protected abstract List<List<Vnfc>> orderDependencies() throws InvalidDependencyModelException;
127 }