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