2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 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 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 * ECOMP and OpenECOMP are trademarks
21 * and service marks of AT&T Intellectual Property.
23 package org.openecomp.modelloader.entity.model;
25 import jline.internal.Log;
27 import org.openecomp.modelloader.entity.Artifact;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.List;
37 * Utility class to sort the given Models according to their dependencies.
38 * Example: Given a list of Models [A, B, C] where B depends on A, and A depends
39 * on C, the sorted result will be [C, A, B]
41 public class ModelSorter {
44 * Wraps a Model object to form dependencies other Models using Edges.
47 private final AbstractModelArtifact model;
48 private final HashSet<Edge> inEdges;
49 private final HashSet<Edge> outEdges;
51 public Node(AbstractModelArtifact model) {
53 inEdges = new HashSet<Edge>();
54 outEdges = new HashSet<Edge>();
57 public Node addEdge(Node node) {
58 Edge edge = new Edge(this, node);
60 node.inEdges.add(edge);
65 public String toString() {
66 return model.getUniqueIdentifier();
70 public boolean equals(Object other) {
71 AbstractModelArtifact otherModel = ((Node) other).model;
72 return this.model.getUniqueIdentifier().equals(otherModel.getUniqueIdentifier());
76 public int hashCode() {
77 return this.model.getUniqueIdentifier().hashCode();
82 * Represents a dependency between two Nodes.
85 public final Node from;
88 public Edge(Node from, Node to) {
94 public boolean equals(Object obj) {
95 Edge edge = (Edge) obj;
96 return edge.from == from && edge.to == to;
101 * Returns the list of models sorted by order of dependency.
103 * @param originalList
104 * the list that needs to be sorted
105 * @return a list of sorted models
107 public List<Artifact> sort(List<Artifact> originalList) {
109 if (originalList.size() <= 1) {
113 Collection<Node> nodes = createNodes(originalList);
114 Collection<Node> sortedNodes = sortNodes(nodes);
116 List<Artifact> sortedModelsList = new ArrayList<Artifact>(sortedNodes.size());
117 for (Node node : sortedNodes) {
118 sortedModelsList.add(node.model);
121 return sortedModelsList;
125 * Create nodes from the list of models and their dependencies.
128 * what the nodes creation is based upon
129 * @return Collection of Node objects
131 private Collection<Node> createNodes(Collection<Artifact> models) {
133 // load list of models into a map, so we can later replace referenceIds with
135 HashMap<String, AbstractModelArtifact> versionIdToModelMap = new HashMap<String, AbstractModelArtifact>();
136 for (Artifact art : models) {
137 AbstractModelArtifact ma = (AbstractModelArtifact) art;
138 versionIdToModelMap.put(ma.getUniqueIdentifier(), ma);
141 HashMap<String, Node> nodes = new HashMap<String, Node>();
142 // create a node for each model and its referenced models
143 for (Artifact art : models) {
145 AbstractModelArtifact model = (AbstractModelArtifact) art;
147 // node might have been created by another model referencing it
148 Node node = nodes.get(model.getUniqueIdentifier());
151 node = new Node(model);
152 nodes.put(model.getUniqueIdentifier(), node);
155 for (String referencedModelId : model.getDependentModelIds()) {
156 // node might have been created by another model referencing it
157 Node referencedNode = nodes.get(referencedModelId);
159 if (null == referencedNode) {
161 AbstractModelArtifact referencedModel = versionIdToModelMap.get(referencedModelId);
162 if (referencedModel == null) {
163 Log.debug("ignoring " + referencedModelId);
164 continue; // referenced model not supplied, no need to sort it
166 referencedNode = new Node(referencedModel);
167 nodes.put(referencedModelId, referencedNode);
169 referencedNode.addEdge(node);
173 return nodes.values();
177 * Sorts the given Nodes by order of dependency.
179 * @param originalList
180 * the collection of nodes to be sorted
181 * @return a sorted collection of the given nodes
183 private Collection<Node> sortNodes(Collection<Node> unsortedNodes) {
184 // L <- Empty list that will contain the sorted elements
185 ArrayList<Node> nodeList = new ArrayList<Node>();
187 // S <- Set of all nodes with no incoming edges
188 HashSet<Node> nodeSet = new HashSet<Node>();
189 for (Node unsortedNode : unsortedNodes) {
190 if (unsortedNode.inEdges.size() == 0) {
191 nodeSet.add(unsortedNode);
195 // while S is non-empty do
196 while (!nodeSet.isEmpty()) {
197 // remove a node n from S
198 Node node = nodeSet.iterator().next();
199 nodeSet.remove(node);
204 // for each node m with an edge e from n to m do
205 for (Iterator<Edge> it = node.outEdges.iterator(); it.hasNext();) {
206 // remove edge e from the graph
207 Edge edge = it.next();
209 it.remove();// Remove edge from n
210 to.inEdges.remove(edge);// Remove edge from m
212 // if m has no other incoming edges then insert m into S
213 if (to.inEdges.isEmpty()) {
218 // Check to see if all edges are removed
219 boolean cycle = false;
220 for (Node node : unsortedNodes) {
221 if (!node.inEdges.isEmpty()) {
227 throw new RuntimeException(
228 "Circular dependency present between models, topological sort not possible");