Fix for Bug 125.
[mdsal.git] / code-generator / binding-type-provider / src / main / java / org / opendaylight / yangtools / sal / binding / yang / types / GroupingDefinitionDependencySort.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.sal.binding.yang.types;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
20 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.UsesNode;
25 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort;
26 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Node;
27
28 import com.google.common.collect.Maps;
29 import com.google.common.collect.Sets;
30
31 public class GroupingDefinitionDependencySort {
32
33     /**
34      * Sorts set <code>groupingDefinitions</code> according to the mutual
35      * dependencies.<br />
36      *
37      * Elements of <code>groupingDefinitions</code> are firstly transformed to
38      * {@link org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Node
39      * Node} interfaces and then are sorted by
40      * {@link org.opendaylight.yangtools.yang.parser.util.TopologicalSort#sort(Set)
41      * sort()} method of <code>TopologicalSort</code>.<br />
42      * <br />
43      *
44      *
45      * <i>Definition of dependency relation:<br />
46      * The first <code>GroupingDefinition</code> object (in this context)
47      * depends on second <code>GroupingDefinition</code> object if the first one
48      * contains in its set of <code>UsesNode</code> (obtained through
49      * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer#getUses()
50      * getUses} method) reference to the second one.</i>
51      *
52      * @param groupingDefinitions
53      *            set of grouping definition which should be sorted according to
54      *            mutual dependencies
55      * @return list of grouping definitiond which are sorted by mutual
56      *         dependencies
57      * @throws IllegalArgumentException
58      *             if <code>groupingDefinitions</code>
59      *
60      */
61     public List<GroupingDefinition> sort(final Collection<GroupingDefinition> groupingDefinitions) {
62         if (groupingDefinitions == null) {
63             throw new IllegalArgumentException("Set of Type Definitions " + "cannot be NULL!");
64         }
65
66         final List<GroupingDefinition> resultGroupingDefinitions = new ArrayList<GroupingDefinition>();
67         final Set<Node> unsorted = groupingDefinitionsToNodes(groupingDefinitions);
68         final List<Node> sortedNodes = TopologicalSort.sort(unsorted);
69         for (Node node : sortedNodes) {
70             NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
71             resultGroupingDefinitions.add((GroupingDefinition) (nodeWrappedType.getWrappedType()));
72         }
73         return resultGroupingDefinitions;
74
75     }
76
77     /**
78      * Wraps every grouping definition to node type and adds to every node
79      * information about dependencies.
80      *
81      * The map with mapping from schema path (represents grouping definition) to
82      * node is created. For every created node (next <i>nodeFrom</i>) is for its
83      * wrapped grouping definition passed the set of its <i>uses nodes</i>
84      * through. For every uses node is found its wrapping node (next as
85      * <i>nodeTo</i>). This dependency relationship between nodeFrom and all
86      * found nodesTo is modeled with creating of one edge from nodeFrom to
87      * nodeTo.
88      *
89      *
90      * @param groupingDefinitions
91      *            set of goruping definition which will be wrapped to nodes
92      *
93      * @return set of nodes where every one contains wrapped grouping definition
94      */
95     private Set<Node> groupingDefinitionsToNodes(final Collection<GroupingDefinition> groupingDefinitions) {
96         final Map<SchemaPath, Node> nodeMap = Maps.newHashMap();
97         final Set<Node> resultNodes = Sets.newHashSet();
98
99         for (final GroupingDefinition groupingDefinition : groupingDefinitions) {
100             final Node node = new NodeWrappedType(groupingDefinition);
101             nodeMap.put(groupingDefinition.getPath(), node);
102             resultNodes.add(node);
103         }
104
105         for (final Node node : resultNodes) {
106             final NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
107             final GroupingDefinition groupingDefinition = (GroupingDefinition) nodeWrappedType.getWrappedType();
108
109             Set<UsesNode> usesNodes = getAllUsesNodes(groupingDefinition);
110
111             for (UsesNode usesNode : usesNodes) {
112                 SchemaPath schemaPath = usesNode.getGroupingPath();
113                 if (schemaPath != null) {
114                     Node nodeTo = nodeMap.get(schemaPath);
115                     if (nodeTo != null) {
116                         nodeWrappedType.addEdge(nodeTo);
117                     }
118                 }
119             }
120         }
121
122         return resultNodes;
123     }
124
125     /**
126      * Returns the set of the uses nodes which are get from uses in
127      * <code>container</code>, from uses in groupings inside
128      * <code>container</code> and from uses inside child nodes of the
129      * <code>container</code>.
130      *
131      * @param container
132      *            data node container which can contain some uses of grouping
133      * @return set of uses nodes which were find in <code>container</code>.
134      */
135     private Set<UsesNode> getAllUsesNodes(DataNodeContainer container) {
136         Set<UsesNode> ret = new HashSet<>();
137         Set<UsesNode> usesNodes = container.getUses();
138         ret.addAll(usesNodes);
139
140         for (UsesNode usesNode : usesNodes) {
141             for (AugmentationSchema augment : usesNode.getAugmentations()) {
142                 ret.addAll(getAllUsesNodes(augment));
143             }
144         }
145         Set<GroupingDefinition> groupings = container.getGroupings();
146         for (GroupingDefinition groupingDefinition : groupings) {
147             ret.addAll(getAllUsesNodes(groupingDefinition));
148         }
149         Set<DataSchemaNode> childNodes = container.getChildNodes();
150         for (DataSchemaNode childNode : childNodes) {
151             if (childNode instanceof DataNodeContainer) {
152                 ret.addAll(getAllUsesNodes((DataNodeContainer) childNode));
153             } else if (childNode instanceof ChoiceNode) {
154                 Set<ChoiceCaseNode> cases = ((ChoiceNode) childNode).getCases();
155                 for (ChoiceCaseNode choiceCaseNode : cases) {
156                     ret.addAll(getAllUsesNodes(choiceCaseNode));
157                 }
158             }
159         }
160         return ret;
161     }
162
163 }