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