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