Improve grouping error reporting
[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 java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
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.SchemaPath;
30 import org.opendaylight.yangtools.yang.model.api.UsesNode;
31
32 public class GroupingDefinitionDependencySort {
33     /**
34      * Sorts set {@code groupingDefinitions} according to the mutual dependencies.<br>
35      * Elements of {@code groupingDefinitions} are firstly transformed to {@link Node} interfaces and then are
36      * sorted by {@link TopologicalSort#sort(Set) sort()} method of {@code TopologicalSort}.<br>
37      *
38      * <i>Definition of dependency relation:<br>
39      * The first {@code GroupingDefinition} object (in this context) depends on second {@code GroupingDefinition} object
40      * if the first one contains in its set of {@code UsesNode} (obtained through {@link DataNodeContainer#getUses()})
41      * a reference to the second one.
42      * </i>
43      *
44      * @param groupingDefinitions set of grouping definition which should be sorted according to mutual dependencies
45      * @return list of grouping definitions which are sorted by mutual dependencies
46      * @throws IllegalArgumentException if {@code groupingDefinitions}
47      *
48      */
49     public List<GroupingDefinition> sort(final Collection<GroupingDefinition> groupingDefinitions) {
50         if (groupingDefinitions == null) {
51             throw new IllegalArgumentException("Set of Type Definitions cannot be NULL!");
52         }
53
54         final List<GroupingDefinition> resultGroupingDefinitions = new ArrayList<>();
55         final Set<Node> unsorted = groupingDefinitionsToNodes(groupingDefinitions);
56         final List<Node> sortedNodes = TopologicalSort.sort(unsorted);
57         for (Node node : sortedNodes) {
58             NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
59             resultGroupingDefinitions.add((GroupingDefinition) nodeWrappedType.getWrappedType());
60         }
61         return resultGroupingDefinitions;
62     }
63
64     /**
65      * Wraps every grouping definition to node type and adds to every node information about dependencies. The map
66      * with mapping from schema path (represents grouping definition) to node is created. For every created node
67      * (next <i>nodeFrom</i>) is for its wrapped grouping definition passed the set of its <i>uses nodes</i> through.
68      * For every uses node is found its wrapping node (next as <i>nodeTo</i>). This dependency relationship between
69      * nodeFrom and all found nodesTo is modeled with creating of one edge from nodeFrom to nodeTo.
70      *
71      * @param groupingDefinitions set of goruping definition which will be wrapped to nodes
72      * @return set of nodes where every one contains wrapped grouping definition
73      */
74     private Set<Node> groupingDefinitionsToNodes(final Collection<GroupingDefinition> groupingDefinitions) {
75         final Map<SchemaPath, Node> nodeMap = new HashMap<>();
76         final Set<Node> resultNodes = new HashSet<>();
77
78         for (final GroupingDefinition groupingDefinition : groupingDefinitions) {
79             final Node node = new NodeWrappedType(groupingDefinition);
80             nodeMap.put(groupingDefinition.getPath(), node);
81             resultNodes.add(node);
82         }
83
84         for (final Node node : resultNodes) {
85             final NodeWrappedType nodeWrappedType = (NodeWrappedType) node;
86             final GroupingDefinition groupingDefinition = (GroupingDefinition) nodeWrappedType.getWrappedType();
87
88             Set<UsesNode> usesNodes = getAllUsesNodes(groupingDefinition);
89
90             for (UsesNode usesNode : usesNodes) {
91                 SchemaPath schemaPath = usesNode.getGroupingPath();
92                 if (schemaPath != null) {
93                     Node nodeTo = nodeMap.get(schemaPath);
94                     if (nodeTo != null) {
95                         nodeWrappedType.addEdge(nodeTo);
96                     }
97                 }
98             }
99         }
100
101         return resultNodes;
102     }
103
104     /**
105      * Returns the set of the uses nodes which are get from uses in <code>container</code>, from uses in groupings
106      * inside <code>container</code> and from uses inside child nodes of the <code>container</code>.
107      *
108      * @param container data node container which can contain some uses of grouping
109      * @return set of uses nodes which were find in <code>container</code>.
110      */
111     private Set<UsesNode> getAllUsesNodes(final DataNodeContainer container) {
112         Set<UsesNode> ret = new HashSet<>();
113         Set<UsesNode> usesNodes = container.getUses();
114         ret.addAll(usesNodes);
115
116         for (UsesNode usesNode : usesNodes) {
117             for (AugmentationSchemaNode augment : usesNode.getAugmentations()) {
118                 ret.addAll(getAllUsesNodes(augment));
119             }
120         }
121         for (GroupingDefinition groupingDefinition : container.getGroupings()) {
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         if (container instanceof ActionNodeContainer) {
134             for (ActionDefinition action : ((ActionNodeContainer) container).getActions()) {
135                 ret.addAll(getAllUsesNodes(action.getInput()));
136                 ret.addAll(getAllUsesNodes(action.getOutput()));
137             }
138         }
139         if (container instanceof NotificationNodeContainer) {
140             for (NotificationDefinition notification : ((NotificationNodeContainer) container).getNotifications()) {
141                 ret.addAll(getAllUsesNodes(notification));
142             }
143         }
144
145         return ret;
146     }
147 }