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.Iterator;
12 import java.util.List;
15 import org.opendaylight.yangtools.yang.model.api.*;
17 public class DataNodeIterator implements Iterator<DataSchemaNode> {
19 private final DataNodeContainer container;
20 private final List<ListSchemaNode> allLists;
21 private final List<ContainerSchemaNode> allContainers;
22 private final List<ChoiceNode> allChoices;
23 private final List<DataSchemaNode> allChilds;
25 public DataNodeIterator(final DataNodeContainer container) {
26 if (container == null) {
27 throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");
30 this.allContainers = new ArrayList<>();
31 this.allLists = new ArrayList<>();
32 this.allChilds = new ArrayList<>();
33 this.allChoices = new ArrayList<>();
35 this.container = container;
36 traverse(this.container);
39 public List<ContainerSchemaNode> allContainers() {
43 public List<ListSchemaNode> allLists() {
47 public List<ChoiceNode> allChoices() {
51 private void traverse(final DataNodeContainer dataNode) {
52 if (dataNode == null) {
56 final Set<DataSchemaNode> childNodes = dataNode.getChildNodes();
57 if (childNodes != null) {
58 for (DataSchemaNode childNode : childNodes) {
59 if (childNode.isAugmenting()) {
62 allChilds.add(childNode);
63 if (childNode instanceof ContainerSchemaNode) {
64 final ContainerSchemaNode containerNode = (ContainerSchemaNode) childNode;
65 allContainers.add(containerNode);
66 traverse(containerNode);
67 } else if (childNode instanceof ListSchemaNode) {
68 final ListSchemaNode list = (ListSchemaNode) childNode;
71 } else if (childNode instanceof ChoiceNode) {
72 final ChoiceNode choiceNode = (ChoiceNode) childNode;
73 allChoices.add(choiceNode);
74 final Set<ChoiceCaseNode> cases = choiceNode.getCases();
76 for (final ChoiceCaseNode caseNode : cases) {
84 traverseModule(dataNode);
85 traverseGroupings(dataNode);
89 private void traverseModule(DataNodeContainer dataNode) {
91 if (dataNode instanceof Module) {
92 module = (Module) dataNode;
96 final Set<NotificationDefinition> notifications = module.getNotifications();
97 for (NotificationDefinition notificationDefinition : notifications) {
98 traverse(notificationDefinition);
100 final Set<RpcDefinition> rpcs = module.getRpcs();
101 for (RpcDefinition rpcDefinition : rpcs) {
102 ContainerSchemaNode input = rpcDefinition.getInput();
106 ContainerSchemaNode output = rpcDefinition.getInput();
113 private void traverseGroupings(DataNodeContainer dataNode) {
114 final Set<GroupingDefinition> groupings = dataNode.getGroupings();
115 if (groupings != null) {
116 for (GroupingDefinition grouping : groupings) {
123 public boolean hasNext() {
124 if (container.getChildNodes() != null) {
125 final Set<DataSchemaNode> childNodes = container.getChildNodes();
127 if ((childNodes != null) && !childNodes.isEmpty()) {
128 return childNodes.iterator().hasNext();
135 public DataSchemaNode next() {
136 return allChilds.iterator().next();
140 public void remove() {
141 throw new UnsupportedOperationException();