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;
16 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
25 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 * DataNodeIterator is iterator, which walks down whole YANG DataNodeContainer
30 * and walks all instances of {@link DataSchemaNode} present in subtree.
32 * Iterator instance is eagerly created, walking happens on initialization.
34 * Iteration is not ordered.
37 public class DataNodeIterator implements Iterator<DataSchemaNode> {
39 private final DataNodeContainer container;
40 private final List<ListSchemaNode> allLists;
41 private final List<ContainerSchemaNode> allContainers;
42 private final List<ChoiceNode> allChoices;
43 private final List<DataSchemaNode> allChilds;
44 private final List<GroupingDefinition> allGroupings;
45 private final List<TypeDefinition<?>> allTypedefs;
47 public DataNodeIterator(final DataNodeContainer container) {
48 if (container == null) {
49 throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");
52 this.allContainers = new ArrayList<>();
53 this.allLists = new ArrayList<>();
54 this.allChilds = new ArrayList<>();
55 this.allChoices = new ArrayList<>();
56 this.allGroupings = new ArrayList<>();
57 this.allTypedefs = new ArrayList<>();
59 this.container = container;
60 traverse(this.container);
64 * Returns list all containers present in subtree.
66 * @return Returns list all containers present in subtree.
68 public List<ContainerSchemaNode> allContainers() {
73 * Returns list all lists present in subtree.
75 * @return Returns list all containers present in subtree.
77 public List<ListSchemaNode> allLists() {
82 * Returns list all choices present in subtree.
84 * @return Returns list all containers present in subtree.
86 public List<ChoiceNode> allChoices() {
91 * Returns list all groupings present in subtree.
93 * @return Returns list all containers present in subtree.
95 public List<GroupingDefinition> allGroupings() {
100 * Returns list all typedefs present in subtree.
102 * @return Returns list all containers present in subtree.
104 public List<TypeDefinition<?>> allTypedefs() {
108 private void traverse(final DataNodeContainer dataNode) {
109 if (dataNode == null) {
113 final Iterable<DataSchemaNode> childNodes = dataNode.getChildNodes();
114 if (childNodes != null) {
115 for (DataSchemaNode childNode : childNodes) {
116 if (childNode.isAugmenting()) {
119 allChilds.add(childNode);
120 if (childNode instanceof ContainerSchemaNode) {
121 final ContainerSchemaNode containerNode = (ContainerSchemaNode) childNode;
122 allContainers.add(containerNode);
123 traverse(containerNode);
124 } else if (childNode instanceof ListSchemaNode) {
125 final ListSchemaNode list = (ListSchemaNode) childNode;
128 } else if (childNode instanceof ChoiceNode) {
129 final ChoiceNode choiceNode = (ChoiceNode) childNode;
130 allChoices.add(choiceNode);
131 final Set<ChoiceCaseNode> cases = choiceNode.getCases();
133 for (final ChoiceCaseNode caseNode : cases) {
141 this.allTypedefs.addAll(dataNode.getTypeDefinitions());
142 traverseModule(dataNode);
143 traverseGroupings(dataNode);
147 private void traverseModule(final DataNodeContainer dataNode) {
149 if (dataNode instanceof Module) {
150 module = (Module) dataNode;
155 final Set<NotificationDefinition> notifications = module.getNotifications();
156 for (NotificationDefinition notificationDefinition : notifications) {
157 traverse(notificationDefinition);
160 final Set<RpcDefinition> rpcs = module.getRpcs();
161 for (RpcDefinition rpcDefinition : rpcs) {
162 this.allTypedefs.addAll(rpcDefinition.getTypeDefinitions());
163 ContainerSchemaNode input = rpcDefinition.getInput();
167 ContainerSchemaNode output = rpcDefinition.getInput();
174 private void traverseGroupings(final DataNodeContainer dataNode) {
175 final Set<GroupingDefinition> groupings = dataNode.getGroupings();
176 if (groupings != null) {
177 for (GroupingDefinition grouping : groupings) {
178 allGroupings.add(grouping);
185 public boolean hasNext() {
186 if (container.getChildNodes() != null) {
187 final Collection<DataSchemaNode> childNodes = container.getChildNodes();
189 if ((childNodes != null) && !childNodes.isEmpty()) {
190 return childNodes.iterator().hasNext();
197 public DataSchemaNode next() {
198 return allChilds.iterator().next();
202 public void remove() {
203 throw new UnsupportedOperationException();