2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.sal.binding.yang.types;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.List;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.UsesNode;
24 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort;
25 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Node;
27 import com.google.common.collect.Maps;
28 import com.google.common.collect.Sets;
30 public class GroupingDefinitionDependencySort {
33 * Sorts set <code>groupingDefinitions</code> according to the mutual
36 * Elements of <code>groupingDefinitions</code> are firstly transformed to
37 * {@link org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Node
38 * Node} interfaces and then are sorted by
39 * {@link org.opendaylight.yangtools.yang.parser.util.TopologicalSort#sort(Set)
40 * sort()} method of <code>TopologicalSort</code>.<br />
44 * <i>Definition of dependency relation:<br />
45 * The first <code>GroupingDefinition</code> object (in this context)
46 * depends on second <code>GroupingDefinition</code> object if the first one
47 * contains in its set of <code>UsesNode</code> (obtained through
48 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer#getUses()
49 * getUses} method) reference to the second one.</i>
51 * @param groupingDefinitions
52 * set of grouping definition which should be sorted according to
54 * @return list of grouping definitiond which are sorted by mutual
56 * @throws IllegalArgumentException
57 * if <code>groupingDefinitions</code>
60 public List<GroupingDefinition> sort(final Collection<GroupingDefinition> groupingDefinitions) {
61 if (groupingDefinitions == null) {
62 throw new IllegalArgumentException("Set of Type Definitions " + "cannot be NULL!");
65 final List<GroupingDefinition> resultGroupingDefinitions = new ArrayList<GroupingDefinition>();
66 final Set<Node> unsorted = groupingDefinitionsToNodes(groupingDefinitions);
67 final List<Node> sortedNodes = TopologicalSort.sort(unsorted);
68 for (Node node : sortedNodes) {
69 NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
70 resultGroupingDefinitions.add((GroupingDefinition) (nodeWrappedType.getWrappedType()));
72 return resultGroupingDefinitions;
77 * Wraps every grouping definition to node type and adds to every node
78 * information about dependencies.
80 * The map with mapping from schema path (represents grouping definition) to
81 * node is created. For every created node (next <i>nodeFrom</i>) is for its
82 * wrapped grouping definition passed the set of its <i>uses nodes</i>
83 * through. For every uses node is found its wrapping node (next as
84 * <i>nodeTo</i>). This dependency relationship between nodeFrom and all
85 * found nodesTo is modeled with creating of one edge from nodeFrom to
89 * @param groupingDefinitions
90 * set of goruping definition which will be wrapped to nodes
92 * @return set of nodes where every one contains wrapped grouping definition
94 private Set<Node> groupingDefinitionsToNodes(final Collection<GroupingDefinition> groupingDefinitions) {
95 final Map<SchemaPath, Node> nodeMap = Maps.newHashMap();
96 final Set<Node> resultNodes = Sets.newHashSet();
98 for (final GroupingDefinition groupingDefinition : groupingDefinitions) {
99 final Node node = new NodeWrappedType(groupingDefinition);
100 nodeMap.put(groupingDefinition.getPath(), node);
101 resultNodes.add(node);
104 for (final Node node : resultNodes) {
105 final NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
106 final GroupingDefinition groupingDefinition = (GroupingDefinition) nodeWrappedType.getWrappedType();
108 Set<UsesNode> usesNodes = getAllUsesNodes(groupingDefinition);
110 for (UsesNode usesNode : usesNodes) {
111 SchemaPath schemaPath = usesNode.getGroupingPath();
112 if (schemaPath != null) {
113 Node nodeTo = nodeMap.get(schemaPath);
114 if (nodeTo != null) {
115 nodeWrappedType.addEdge(nodeTo);
125 * Returns the set of the uses nodes which are get from uses in
126 * <code>container</code>, from uses in groupings inside
127 * <code>container</code> and from uses inside child nodes of the
128 * <code>container</code>.
131 * data node container which can contain some uses of grouping
132 * @return set of uses nodes which were find in <code>container</code>.
134 private Set<UsesNode> getAllUsesNodes(DataNodeContainer container) {
135 Set<UsesNode> ret = new HashSet<>();
136 ret.addAll(container.getUses());
138 Set<GroupingDefinition> groupings = container.getGroupings();
139 for (GroupingDefinition groupingDefinition : groupings) {
140 ret.addAll(getAllUsesNodes(groupingDefinition));
142 Set<DataSchemaNode> childNodes = container.getChildNodes();
143 for (DataSchemaNode childNode : childNodes) {
144 if (childNode instanceof DataNodeContainer) {
145 ret.addAll(getAllUsesNodes((DataNodeContainer) childNode));
146 } else if (childNode instanceof ChoiceNode) {
147 Set<ChoiceCaseNode> cases = ((ChoiceNode) childNode).getCases();
148 for (ChoiceCaseNode choiceCaseNode : cases) {
149 ret.addAll(getAllUsesNodes(choiceCaseNode));