2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Iterator;
13 import java.util.List;
15 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
16 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
24 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 * DataNodeIterator is iterator, which walks down whole YANG DataNodeContainer
29 * and walks all instances of {@link DataSchemaNode} present in subtree.
31 * Iterator instance is eagerly created, walking happens on initialization.
33 * Iteration is not ordered.
36 public class DataNodeIterator implements Iterator<DataSchemaNode> {
38 private final DataNodeContainer container;
39 private final List<ListSchemaNode> allLists;
40 private final List<ContainerSchemaNode> allContainers;
41 private final List<ChoiceSchemaNode> allChoices;
42 private final List<DataSchemaNode> allChilds;
43 private final List<GroupingDefinition> allGroupings;
44 private final List<TypeDefinition<?>> allTypedefs;
46 public DataNodeIterator(final DataNodeContainer container) {
47 if (container == null) {
48 throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");
51 this.allContainers = new ArrayList<>();
52 this.allLists = new ArrayList<>();
53 this.allChilds = new ArrayList<>();
54 this.allChoices = new ArrayList<>();
55 this.allGroupings = new ArrayList<>();
56 this.allTypedefs = new ArrayList<>();
58 this.container = container;
59 traverse(this.container);
63 * Returns list all containers present in subtree.
65 * @return Returns list all containers present in subtree.
67 public List<ContainerSchemaNode> allContainers() {
72 * Returns list all lists present in subtree.
74 * @return Returns list all containers present in subtree.
76 public List<ListSchemaNode> allLists() {
81 * Returns list all choices present in subtree.
83 * @return Returns list all containers present in subtree.
85 public List<ChoiceSchemaNode> allChoices() {
90 * Returns list all groupings present in subtree.
92 * @return Returns list all containers present in subtree.
94 public List<GroupingDefinition> allGroupings() {
99 * Returns list all typedefs present in subtree.
101 * @return Returns list all containers present in subtree.
103 public List<TypeDefinition<?>> allTypedefs() {
107 private void traverse(final DataNodeContainer dataNode) {
108 if (dataNode == null) {
112 final Iterable<DataSchemaNode> childNodes = dataNode.getChildNodes();
113 if (childNodes != null) {
114 for (DataSchemaNode childNode : childNodes) {
115 if (childNode.isAugmenting()) {
118 allChilds.add(childNode);
119 if (childNode instanceof ContainerSchemaNode) {
120 final ContainerSchemaNode containerNode = (ContainerSchemaNode) childNode;
121 allContainers.add(containerNode);
122 traverse(containerNode);
123 } else if (childNode instanceof ListSchemaNode) {
124 final ListSchemaNode list = (ListSchemaNode) childNode;
127 } else if (childNode instanceof ChoiceSchemaNode) {
128 final ChoiceSchemaNode choiceNode = (ChoiceSchemaNode) childNode;
129 allChoices.add(choiceNode);
130 final Set<ChoiceCaseNode> cases = choiceNode.getCases();
132 for (final ChoiceCaseNode caseNode : cases) {
140 this.allTypedefs.addAll(dataNode.getTypeDefinitions());
141 traverseModule(dataNode);
142 traverseGroupings(dataNode);
146 private void traverseModule(final DataNodeContainer dataNode) {
148 if (dataNode instanceof Module) {
149 module = (Module) dataNode;
154 final Set<NotificationDefinition> notifications = module.getNotifications();
155 for (NotificationDefinition notificationDefinition : notifications) {
156 traverse(notificationDefinition);
159 final Set<RpcDefinition> rpcs = module.getRpcs();
160 for (RpcDefinition rpcDefinition : rpcs) {
161 this.allTypedefs.addAll(rpcDefinition.getTypeDefinitions());
162 ContainerSchemaNode input = rpcDefinition.getInput();
166 ContainerSchemaNode output = rpcDefinition.getOutput();
167 if (output != null) {
173 private void traverseGroupings(final DataNodeContainer dataNode) {
174 final Set<GroupingDefinition> groupings = dataNode.getGroupings();
175 if (groupings != null) {
176 for (GroupingDefinition grouping : groupings) {
177 allGroupings.add(grouping);
184 public boolean hasNext() {
185 if (container.getChildNodes() != null) {
186 final Collection<DataSchemaNode> childNodes = container.getChildNodes();
188 if ((childNodes != null) && !childNodes.isEmpty()) {
189 return childNodes.iterator().hasNext();
196 public DataSchemaNode next() {
197 return allChilds.iterator().next();
201 public void remove() {
202 throw new UnsupportedOperationException();