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.ActionDefinition;
20 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
27 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
28 import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.UsesNode;
31 public final class GroupingDefinitionDependencySort {
32 private GroupingDefinitionDependencySort() {
37 * Sorts a set of {@code groupings} according to the mutual dependencies. Elements of {@code groupings} are first
38 * transformed to {@link Node} interfaces and then are sorted by {@link TopologicalSort#sort(Set) sort()} method of
39 * {@code TopologicalSort}.<br>
41 * <i>Definition of dependency relation:<br>
42 * The first {@code GroupingDefinition} object (in this context) depends on second {@code GroupingDefinition} object
43 * if the first one contains in its set of {@code UsesNode} (obtained through {@link DataNodeContainer#getUses()})
44 * a reference to the second one.
47 * @param groupings set of grouping definition which should be sorted according to mutual dependencies
48 * @return list of grouping definitions which are sorted by mutual dependencies
49 * @throws IllegalArgumentException if {@code groupingDefinitions}
52 public static List<GroupingDefinition> sort(final Collection<? extends GroupingDefinition> groupings) {
53 if (groupings == null) {
54 throw new IllegalArgumentException("Set of Type Definitions cannot be NULL!");
57 final List<Node> sortedNodes = TopologicalSort.sort(groupingDefinitionsToNodes(groupings));
58 final List<GroupingDefinition> resultGroupingDefinitions = new ArrayList<>(sortedNodes.size());
59 for (Node node : sortedNodes) {
60 NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
61 resultGroupingDefinitions.add((GroupingDefinition) nodeWrappedType.getWrappedType());
63 return resultGroupingDefinitions;
67 * Wraps every grouping definition to node type and adds to every node information about dependencies. The map
68 * with mapping from schema path (represents grouping definition) to node is created. For every created node
69 * (next <i>nodeFrom</i>) is for its wrapped grouping definition passed the set of its <i>uses nodes</i> through.
70 * For every uses node is found its wrapping node (next as <i>nodeTo</i>). This dependency relationship between
71 * nodeFrom and all found nodesTo is modeled with creating of one edge from nodeFrom to nodeTo.
73 * @param groupings set of grouping definitions which will be wrapped to nodes
74 * @return set of nodes where every one contains wrapped grouping definition
76 private static Set<Node> groupingDefinitionsToNodes(final Collection<? extends GroupingDefinition> groupings) {
77 final Map<GroupingDefinition, Node> nodeMap = new HashMap<>();
78 final Set<Node> resultNodes = new HashSet<>();
80 for (final GroupingDefinition grouping : groupings) {
81 final Node node = new NodeWrappedType(grouping);
82 nodeMap.put(grouping, node);
83 resultNodes.add(node);
86 for (final Node node : resultNodes) {
87 final NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
88 final GroupingDefinition grouping = (GroupingDefinition) nodeWrappedType.getWrappedType();
90 Set<UsesNode> usesNodes = getAllUsesNodes(grouping);
92 for (UsesNode usesNode : usesNodes) {
93 Node nodeTo = nodeMap.get(usesNode.getSourceGrouping());
95 nodeWrappedType.addEdge(nodeTo);
104 * Returns the set of the uses nodes which are get from uses in <code>container</code>, from uses in groupings
105 * inside <code>container</code> and from uses inside child nodes of the <code>container</code>.
107 * @param container data node container which can contain some uses of grouping
108 * @return set of uses nodes which were find in <code>container</code>.
110 private static Set<UsesNode> getAllUsesNodes(final DataNodeContainer container) {
111 Set<UsesNode> ret = new HashSet<>();
112 Collection<? extends UsesNode> usesNodes = container.getUses();
113 ret.addAll(usesNodes);
115 for (UsesNode usesNode : usesNodes) {
116 for (AugmentationSchemaNode augment : usesNode.getAugmentations()) {
117 ret.addAll(getAllUsesNodes(augment));
120 for (GroupingDefinition groupingDefinition : container.getGroupings()) {
121 ret.addAll(getAllUsesNodes(groupingDefinition));
123 for (DataSchemaNode childNode : container.getChildNodes()) {
124 if (childNode instanceof DataNodeContainer) {
125 ret.addAll(getAllUsesNodes((DataNodeContainer) childNode));
126 } else if (childNode instanceof ChoiceSchemaNode) {
127 for (CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childNode).getCases()) {
128 ret.addAll(getAllUsesNodes(choiceCaseNode));
132 if (container instanceof ActionNodeContainer) {
133 for (ActionDefinition action : ((ActionNodeContainer) container).getActions()) {
134 ret.addAll(getAllUsesNodes(action.getInput()));
135 ret.addAll(getAllUsesNodes(action.getOutput()));
138 if (container instanceof NotificationNodeContainer) {
139 for (NotificationDefinition notification : ((NotificationNodeContainer) container).getNotifications()) {
140 ret.addAll(getAllUsesNodes(notification));