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.
32 * Iterator instance is eagerly created, walking happens on initialization. Iteration is not ordered.
34 public class DataNodeIterator implements Iterator<DataSchemaNode> {
36 private final DataNodeContainer container;
37 private final List<ListSchemaNode> allLists;
38 private final List<ContainerSchemaNode> allContainers;
39 private final List<ChoiceSchemaNode> allChoices;
40 private final List<DataSchemaNode> allChilds;
41 private final List<GroupingDefinition> allGroupings;
42 private final List<TypeDefinition<?>> allTypedefs;
44 public DataNodeIterator(final DataNodeContainer container) {
45 if (container == null) {
46 throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");
49 this.allContainers = new ArrayList<>();
50 this.allLists = new ArrayList<>();
51 this.allChilds = new ArrayList<>();
52 this.allChoices = new ArrayList<>();
53 this.allGroupings = new ArrayList<>();
54 this.allTypedefs = new ArrayList<>();
56 this.container = container;
57 traverse(this.container);
61 * Returns list all containers present in subtree.
63 * @return Returns list all containers present in subtree.
65 public List<ContainerSchemaNode> allContainers() {
70 * Returns list all lists present in subtree.
72 * @return Returns list all containers present in subtree.
74 public List<ListSchemaNode> allLists() {
79 * Returns list all choices present in subtree.
81 * @return Returns list all containers present in subtree.
83 public List<ChoiceSchemaNode> allChoices() {
88 * Returns list all groupings present in subtree.
90 * @return Returns list all containers present in subtree.
92 public List<GroupingDefinition> allGroupings() {
97 * Returns list all typedefs present in subtree.
99 * @return Returns list all containers present in subtree.
101 public List<TypeDefinition<?>> allTypedefs() {
105 private void traverse(final DataNodeContainer dataNode) {
106 if (dataNode == null) {
110 final Iterable<DataSchemaNode> childNodes = dataNode.getChildNodes();
111 if (childNodes != null) {
112 for (DataSchemaNode childNode : childNodes) {
113 if (childNode.isAugmenting()) {
116 allChilds.add(childNode);
117 if (childNode instanceof ContainerSchemaNode) {
118 final ContainerSchemaNode containerNode = (ContainerSchemaNode) childNode;
119 allContainers.add(containerNode);
120 traverse(containerNode);
121 } else if (childNode instanceof ListSchemaNode) {
122 final ListSchemaNode list = (ListSchemaNode) childNode;
125 } else if (childNode instanceof ChoiceSchemaNode) {
126 final ChoiceSchemaNode choiceNode = (ChoiceSchemaNode) childNode;
127 allChoices.add(choiceNode);
128 for (final ChoiceCaseNode caseNode : choiceNode.getCases().values()) {
135 this.allTypedefs.addAll(dataNode.getTypeDefinitions());
136 traverseModule(dataNode);
137 traverseGroupings(dataNode);
141 private void traverseModule(final DataNodeContainer dataNode) {
143 if (dataNode instanceof Module) {
144 module = (Module) dataNode;
149 final Set<NotificationDefinition> notifications = module.getNotifications();
150 for (NotificationDefinition notificationDefinition : notifications) {
151 traverse(notificationDefinition);
154 final Set<RpcDefinition> rpcs = module.getRpcs();
155 for (RpcDefinition rpcDefinition : rpcs) {
156 this.allTypedefs.addAll(rpcDefinition.getTypeDefinitions());
157 ContainerSchemaNode input = rpcDefinition.getInput();
161 ContainerSchemaNode output = rpcDefinition.getOutput();
162 if (output != null) {
168 private void traverseGroupings(final DataNodeContainer dataNode) {
169 final Set<GroupingDefinition> groupings = dataNode.getGroupings();
170 if (groupings != null) {
171 for (GroupingDefinition grouping : groupings) {
172 allGroupings.add(grouping);
179 public boolean hasNext() {
180 if (container.getChildNodes() != null) {
181 final Collection<DataSchemaNode> childNodes = container.getChildNodes();
183 if (childNodes != null && !childNodes.isEmpty()) {
184 return childNodes.iterator().hasNext();
191 public DataSchemaNode next() {
192 return allChilds.iterator().next();
196 public void remove() {
197 throw new UnsupportedOperationException();