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.mdsal.binding.yang.types;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
17 import org.opendaylight.yangtools.util.TopologicalSort;
18 import org.opendaylight.yangtools.util.TopologicalSort.Node;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.UsesNode;
28 public class GroupingDefinitionDependencySort {
30 * Sorts set {@code groupingDefinitions} according to the mutual dependencies.<br>
31 * Elements of {@code groupingDefinitions} are firstly transformed to {@link Node} interfaces and then are
32 * sorted by {@link TopologicalSort#sort(Set) sort()} method of {@code TopologicalSort}.<br>
34 * <i>Definition of dependency relation:<br>
35 * The first {@code GroupingDefinition} object (in this context) depends on second {@code GroupingDefinition} object
36 * if the first one contains in its set of {@code UsesNode} (obtained through {@link DataNodeContainer#getUses()})
37 * a reference to the second one.
40 * @param groupingDefinitions set of grouping definition which should be sorted according to mutual dependencies
41 * @return list of grouping definitions which are sorted by mutual dependencies
42 * @throws IllegalArgumentException if {@code groupingDefinitions}
45 public List<GroupingDefinition> sort(final Collection<GroupingDefinition> groupingDefinitions) {
46 if (groupingDefinitions == null) {
47 throw new IllegalArgumentException("Set of Type Definitions cannot be NULL!");
50 final List<GroupingDefinition> resultGroupingDefinitions = new ArrayList<>();
51 final Set<Node> unsorted = groupingDefinitionsToNodes(groupingDefinitions);
52 final List<Node> sortedNodes = TopologicalSort.sort(unsorted);
53 for (Node node : sortedNodes) {
54 NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
55 resultGroupingDefinitions.add((GroupingDefinition) nodeWrappedType.getWrappedType());
57 return resultGroupingDefinitions;
61 * Wraps every grouping definition to node type and adds to every node information about dependencies. The map
62 * with mapping from schema path (represents grouping definition) to node is created. For every created node
63 * (next <i>nodeFrom</i>) is for its wrapped grouping definition passed the set of its <i>uses nodes</i> through.
64 * For every uses node is found its wrapping node (next as <i>nodeTo</i>). This dependency relationship between
65 * nodeFrom and all found nodesTo is modeled with creating of one edge from nodeFrom to nodeTo.
67 * @param groupingDefinitions set of goruping definition which will be wrapped to nodes
68 * @return set of nodes where every one contains wrapped grouping definition
70 private Set<Node> groupingDefinitionsToNodes(final Collection<GroupingDefinition> groupingDefinitions) {
71 final Map<SchemaPath, Node> nodeMap = new HashMap<>();
72 final Set<Node> resultNodes = new HashSet<>();
74 for (final GroupingDefinition groupingDefinition : groupingDefinitions) {
75 final Node node = new NodeWrappedType(groupingDefinition);
76 nodeMap.put(groupingDefinition.getPath(), node);
77 resultNodes.add(node);
80 for (final Node node : resultNodes) {
81 final NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
82 final GroupingDefinition groupingDefinition = (GroupingDefinition) nodeWrappedType.getWrappedType();
84 Set<UsesNode> usesNodes = getAllUsesNodes(groupingDefinition);
86 for (UsesNode usesNode : usesNodes) {
87 SchemaPath schemaPath = usesNode.getGroupingPath();
88 if (schemaPath != null) {
89 Node nodeTo = nodeMap.get(schemaPath);
91 nodeWrappedType.addEdge(nodeTo);
101 * Returns the set of the uses nodes which are get from uses in <code>container</code>, from uses in groupings
102 * inside <code>container</code> and from uses inside child nodes of the <code>container</code>.
104 * @param container data node container which can contain some uses of grouping
105 * @return set of uses nodes which were find in <code>container</code>.
107 private Set<UsesNode> getAllUsesNodes(final DataNodeContainer container) {
108 Set<UsesNode> ret = new HashSet<>();
109 Set<UsesNode> usesNodes = container.getUses();
110 ret.addAll(usesNodes);
112 for (UsesNode usesNode : usesNodes) {
113 for (AugmentationSchemaNode augment : usesNode.getAugmentations()) {
114 ret.addAll(getAllUsesNodes(augment));
117 Set<GroupingDefinition> groupings = container.getGroupings();
118 for (GroupingDefinition groupingDefinition : groupings) {
119 ret.addAll(getAllUsesNodes(groupingDefinition));
121 for (DataSchemaNode childNode : container.getChildNodes()) {
122 if (childNode instanceof DataNodeContainer) {
123 ret.addAll(getAllUsesNodes((DataNodeContainer) childNode));
124 } else if (childNode instanceof ChoiceSchemaNode) {
125 for (CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childNode).getCases().values()) {
126 ret.addAll(getAllUsesNodes(choiceCaseNode));