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