Fixed bug in DataNodeIterator 18/818/2
authorTony Tkacik <ttkacik@cisco.com>
Wed, 7 Aug 2013 14:49:53 +0000 (16:49 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 8 Aug 2013 08:51:50 +0000 (08:51 +0000)
- Added support to DataNodeIterator to scan notifications and rpcs
  for common types.

This fix is part of the fix of
https://bugs.opendaylight.org/show_bug.cgi?id=34

Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/DataNodeIterator.java

index ed77d35d9245911b4dfc231aec4f7e5d300b4bcd..24e7f221e0d9eebd963e6afcaf58b073aad79da5 100644 (file)
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.model.util;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-
-import org.opendaylight.yangtools.yang.model.api.*;
-
-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<DataSchemaNode> allChilds;
-
-    public DataNodeIterator(final DataNodeContainer container) {
-        if (container == null) {
-            throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");
-        }
-
-        this.allContainers = new ArrayList<>();
-        this.allLists = new ArrayList<>();
-        this.allChilds = new ArrayList<>();
-        this.allChoices = new ArrayList<>();
-
-        this.container = container;
-        traverse(this.container);
-    }
-
-    public List<ContainerSchemaNode> allContainers() {
-        return allContainers;
-    }
-
-    public List<ListSchemaNode> allLists() {
-        return allLists;
-    }
-
-    public List<ChoiceNode> allChoices() {
-        return allChoices;
-    }
-
-    private void traverse(final DataNodeContainer dataNode) {
-        if (dataNode == null) {
-            return;
-        }
-
-        final Set<DataSchemaNode> childNodes = dataNode.getChildNodes();
-        if (childNodes != null) {
-            for (DataSchemaNode childNode : childNodes) {
-                if (childNode.isAugmenting()) {
-                    continue;
-                }
-                allChilds.add(childNode);
-                if (childNode instanceof ContainerSchemaNode) {
-                    final ContainerSchemaNode container = (ContainerSchemaNode) childNode;
-                    allContainers.add(container);
-                    traverse(container);
-                } 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;
-                    allChoices.add(choiceNode);
-                    final Set<ChoiceCaseNode> cases = choiceNode.getCases();
-                    if (cases != null) {
-                        for (final ChoiceCaseNode caseNode : cases) {
-                            traverse(caseNode);
-                        }
-                    }
-                }
-            }
-        }
-
-        final Set<GroupingDefinition> groupings = dataNode.getGroupings();
-        if (groupings != null) {
-            for (GroupingDefinition grouping : groupings) {
-                traverse(grouping);
-            }
-        }
-    }
-
-    @Override
-    public boolean hasNext() {
-        if (container.getChildNodes() != null) {
-            final Set<DataSchemaNode> childNodes = container.getChildNodes();
-
-            if ((childNodes != null) && !childNodes.isEmpty()) {
-                return childNodes.iterator().hasNext();
-            }
-        }
-        return false;
-    }
-
-    @Override
-    public DataSchemaNode next() {
-        return allChilds.iterator().next();
-    }
-
-    @Override
-    public void remove() {
-        throw new UnsupportedOperationException();
-    }
-}
+/*\r
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+package org.opendaylight.yangtools.yang.model.util;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Set;\r
+\r
+import org.opendaylight.yangtools.yang.model.api.*;\r
+\r
+public class DataNodeIterator implements Iterator<DataSchemaNode> {\r
+\r
+    private final DataNodeContainer container;\r
+    private final List<ListSchemaNode> allLists;\r
+    private final List<ContainerSchemaNode> allContainers;\r
+    private final List<ChoiceNode> allChoices;\r
+    private final List<DataSchemaNode> allChilds;\r
+    \r
+    public DataNodeIterator(final DataNodeContainer container) {\r
+        if (container == null) {\r
+            throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");\r
+        }\r
+\r
+        this.allContainers = new ArrayList<>();\r
+        this.allLists = new ArrayList<>();\r
+        this.allChilds = new ArrayList<>();\r
+        this.allChoices = new ArrayList<>();\r
+\r
+        this.container = container;\r
+        traverse(this.container);\r
+    }\r
+\r
+    public List<ContainerSchemaNode> allContainers() {\r
+        return allContainers;\r
+    }\r
+\r
+    public List<ListSchemaNode> allLists() {\r
+        return allLists;\r
+    }\r
+\r
+    public List<ChoiceNode> allChoices() {\r
+        return allChoices;\r
+    }\r
+\r
+    private void traverse(final DataNodeContainer dataNode) {\r
+        if (dataNode == null) {\r
+            return;\r
+        }\r
+\r
+        final Set<DataSchemaNode> childNodes = dataNode.getChildNodes();\r
+        if (childNodes != null) {\r
+            for (DataSchemaNode childNode : childNodes) {\r
+                if (childNode.isAugmenting()) {\r
+                    continue;\r
+                }\r
+                allChilds.add(childNode);\r
+                if (childNode instanceof ContainerSchemaNode) {\r
+                    final ContainerSchemaNode container = (ContainerSchemaNode) childNode;\r
+                    allContainers.add(container);\r
+                    traverse(container);\r
+                } else if (childNode instanceof ListSchemaNode) {\r
+                    final ListSchemaNode list = (ListSchemaNode) childNode;\r
+                    allLists.add(list);\r
+                    traverse(list);\r
+                } else if (childNode instanceof ChoiceNode) {\r
+                    final ChoiceNode choiceNode = (ChoiceNode) childNode;\r
+                    allChoices.add(choiceNode);\r
+                    final Set<ChoiceCaseNode> cases = choiceNode.getCases();\r
+                    if (cases != null) {\r
+                        for (final ChoiceCaseNode caseNode : cases) {\r
+                            traverse(caseNode);\r
+                        }\r
+                    }\r
+                }\r
+            }\r
+        }\r
+\r
+        if(dataNode instanceof Module) {\r
+            traverseModule((Module)dataNode);\r
+        }\r
+        \r
+        final Set<GroupingDefinition> groupings = dataNode.getGroupings();\r
+        if (groupings != null) {\r
+            for (GroupingDefinition grouping : groupings) {\r
+                traverse(grouping);\r
+            }\r
+        }\r
+    }\r
+    \r
+\r
+    private void traverseModule(Module module) {\r
+        final Set<NotificationDefinition> notifications = module.getNotifications();\r
+        for (NotificationDefinition notificationDefinition : notifications) {\r
+            traverse(notificationDefinition);\r
+        }\r
+        final Set<RpcDefinition> rpcs = module.getRpcs();\r
+        for (RpcDefinition rpcDefinition : rpcs) {\r
+            ContainerSchemaNode input = rpcDefinition.getInput();\r
+            if(input != null) {\r
+                traverse(input);\r
+            }\r
+            ContainerSchemaNode output = rpcDefinition.getInput();\r
+            if(input != null) {\r
+                traverse(output);\r
+            }\r
+        }\r
+    }\r
+    \r
+    @Override\r
+    public boolean hasNext() {\r
+        if (container.getChildNodes() != null) {\r
+            final Set<DataSchemaNode> childNodes = container.getChildNodes();\r
+\r
+            if ((childNodes != null) && !childNodes.isEmpty()) {\r
+                return childNodes.iterator().hasNext();\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    public DataSchemaNode next() {\r
+        return allChilds.iterator().next();\r
+    }\r
+\r
+    @Override\r
+    public void remove() {\r
+        throw new UnsupportedOperationException();\r
+    }\r
+}\r