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 final Set<ChoiceCaseNode> cases = choiceNode.getCases();
130 for (final ChoiceCaseNode caseNode : cases) {
138 this.allTypedefs.addAll(dataNode.getTypeDefinitions());
139 traverseModule(dataNode);
140 traverseGroupings(dataNode);
144 private void traverseModule(final DataNodeContainer dataNode) {
146 if (dataNode instanceof Module) {
147 module = (Module) dataNode;
152 final Set<NotificationDefinition> notifications = module.getNotifications();
153 for (NotificationDefinition notificationDefinition : notifications) {
154 traverse(notificationDefinition);
157 final Set<RpcDefinition> rpcs = module.getRpcs();
158 for (RpcDefinition rpcDefinition : rpcs) {
159 this.allTypedefs.addAll(rpcDefinition.getTypeDefinitions());
160 ContainerSchemaNode input = rpcDefinition.getInput();
164 ContainerSchemaNode output = rpcDefinition.getOutput();
165 if (output != null) {
171 private void traverseGroupings(final DataNodeContainer dataNode) {
172 final Set<GroupingDefinition> groupings = dataNode.getGroupings();
173 if (groupings != null) {
174 for (GroupingDefinition grouping : groupings) {
175 allGroupings.add(grouping);
182 public boolean hasNext() {
183 if (container.getChildNodes() != null) {
184 final Collection<DataSchemaNode> childNodes = container.getChildNodes();
186 if ((childNodes != null) && !childNodes.isEmpty()) {
187 return childNodes.iterator().hasNext();
194 public DataSchemaNode next() {
195 return allChilds.iterator().next();
199 public void remove() {
200 throw new UnsupportedOperationException();