Bug 6414: Fixed DataNodeIterator's traverseModule method
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / DataNodeIterator.java
index ed77d35d9245911b4dfc231aec4f7e5d300b4bcd..02b81df296394f04a7dbdd73cdba81340ca5db75 100644 (file)
@@ -8,19 +8,40 @@
 package org.opendaylight.yangtools.yang.model.util;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
-
-import org.opendaylight.yangtools.yang.model.api.*;
-
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+
+/**
+ * DataNodeIterator is iterator, which walks down whole YANG DataNodeContainer
+ * and walks all instances of {@link DataSchemaNode} present in subtree.
+ *
+ * Iterator instance is eagerly created, walking happens on initialization.
+ *
+ * Iteration is not ordered.
+ *
+ */
 public class DataNodeIterator implements Iterator<DataSchemaNode> {
 
     private final DataNodeContainer container;
     private final List<ListSchemaNode> allLists;
     private final List<ContainerSchemaNode> allContainers;
-    private final List<ChoiceNode> allChoices;
+    private final List<ChoiceSchemaNode> allChoices;
     private final List<DataSchemaNode> allChilds;
+    private final List<GroupingDefinition> allGroupings;
+    private final List<TypeDefinition<?>> allTypedefs;
 
     public DataNodeIterator(final DataNodeContainer container) {
         if (container == null) {
@@ -31,29 +52,64 @@ public class DataNodeIterator implements Iterator<DataSchemaNode> {
         this.allLists = new ArrayList<>();
         this.allChilds = new ArrayList<>();
         this.allChoices = new ArrayList<>();
+        this.allGroupings = new ArrayList<>();
+        this.allTypedefs = new ArrayList<>();
 
         this.container = container;
         traverse(this.container);
     }
 
+    /**
+     * Returns list all containers present in subtree.
+     *
+     * @return Returns list all containers present in subtree.
+     */
     public List<ContainerSchemaNode> allContainers() {
         return allContainers;
     }
 
+    /**
+     * Returns list all lists present in subtree.
+     *
+     * @return Returns list all containers present in subtree.
+     */
     public List<ListSchemaNode> allLists() {
         return allLists;
     }
 
-    public List<ChoiceNode> allChoices() {
+    /**
+     * Returns list all choices present in subtree.
+     *
+     * @return Returns list all containers present in subtree.
+     */
+    public List<ChoiceSchemaNode> allChoices() {
         return allChoices;
     }
 
+    /**
+     * Returns list all groupings present in subtree.
+     *
+     * @return Returns list all containers present in subtree.
+     */
+    public List<GroupingDefinition> allGroupings() {
+        return allGroupings;
+    }
+
+    /**
+     * Returns list all typedefs present in subtree.
+     *
+     * @return Returns list all containers present in subtree.
+     */
+    public List<TypeDefinition<?>> allTypedefs() {
+        return allTypedefs;
+    }
+
     private void traverse(final DataNodeContainer dataNode) {
         if (dataNode == null) {
             return;
         }
 
-        final Set<DataSchemaNode> childNodes = dataNode.getChildNodes();
+        final Iterable<DataSchemaNode> childNodes = dataNode.getChildNodes();
         if (childNodes != null) {
             for (DataSchemaNode childNode : childNodes) {
                 if (childNode.isAugmenting()) {
@@ -61,15 +117,15 @@ public class DataNodeIterator implements Iterator<DataSchemaNode> {
                 }
                 allChilds.add(childNode);
                 if (childNode instanceof ContainerSchemaNode) {
-                    final ContainerSchemaNode container = (ContainerSchemaNode) childNode;
-                    allContainers.add(container);
-                    traverse(container);
+                    final ContainerSchemaNode containerNode = (ContainerSchemaNode) childNode;
+                    allContainers.add(containerNode);
+                    traverse(containerNode);
                 } else if (childNode instanceof ListSchemaNode) {
                     final ListSchemaNode list = (ListSchemaNode) childNode;
                     allLists.add(list);
                     traverse(list);
-                } else if (childNode instanceof ChoiceNode) {
-                    final ChoiceNode choiceNode = (ChoiceNode) childNode;
+                } else if (childNode instanceof ChoiceSchemaNode) {
+                    final ChoiceSchemaNode choiceNode = (ChoiceSchemaNode) childNode;
                     allChoices.add(choiceNode);
                     final Set<ChoiceCaseNode> cases = choiceNode.getCases();
                     if (cases != null) {
@@ -81,9 +137,44 @@ public class DataNodeIterator implements Iterator<DataSchemaNode> {
             }
         }
 
+        this.allTypedefs.addAll(dataNode.getTypeDefinitions());
+        traverseModule(dataNode);
+        traverseGroupings(dataNode);
+
+    }
+
+    private void traverseModule(final DataNodeContainer dataNode) {
+        final Module module;
+        if (dataNode instanceof Module) {
+            module = (Module) dataNode;
+        } else {
+            return;
+        }
+
+        final Set<NotificationDefinition> notifications = module.getNotifications();
+        for (NotificationDefinition notificationDefinition : notifications) {
+            traverse(notificationDefinition);
+        }
+
+        final Set<RpcDefinition> rpcs = module.getRpcs();
+        for (RpcDefinition rpcDefinition : rpcs) {
+            this.allTypedefs.addAll(rpcDefinition.getTypeDefinitions());
+            ContainerSchemaNode input = rpcDefinition.getInput();
+            if (input != null) {
+                traverse(input);
+            }
+            ContainerSchemaNode output = rpcDefinition.getOutput();
+            if (output != null) {
+                traverse(output);
+            }
+        }
+    }
+
+    private void traverseGroupings(final DataNodeContainer dataNode) {
         final Set<GroupingDefinition> groupings = dataNode.getGroupings();
         if (groupings != null) {
             for (GroupingDefinition grouping : groupings) {
+                allGroupings.add(grouping);
                 traverse(grouping);
             }
         }
@@ -92,7 +183,7 @@ public class DataNodeIterator implements Iterator<DataSchemaNode> {
     @Override
     public boolean hasNext() {
         if (container.getChildNodes() != null) {
-            final Set<DataSchemaNode> childNodes = container.getChildNodes();
+            final Collection<DataSchemaNode> childNodes = container.getChildNodes();
 
             if ((childNodes != null) && !childNodes.isEmpty()) {
                 return childNodes.iterator().hasNext();