6fb90d96cb3b1e60160447102706171105922b1f
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / DataNodeIterator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.util;
9
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Set;
14
15 import org.opendaylight.yangtools.yang.model.api.*;
16
17 public class DataNodeIterator implements Iterator<DataSchemaNode> {
18
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;
24
25     public DataNodeIterator(final DataNodeContainer container) {
26         if (container == null) {
27             throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");
28         }
29
30         this.allContainers = new ArrayList<>();
31         this.allLists = new ArrayList<>();
32         this.allChilds = new ArrayList<>();
33         this.allChoices = new ArrayList<>();
34
35         this.container = container;
36         traverse(this.container);
37     }
38
39     public List<ContainerSchemaNode> allContainers() {
40         return allContainers;
41     }
42
43     public List<ListSchemaNode> allLists() {
44         return allLists;
45     }
46
47     public List<ChoiceNode> allChoices() {
48         return allChoices;
49     }
50
51     private void traverse(final DataNodeContainer dataNode) {
52         if (dataNode == null) {
53             return;
54         }
55
56         final Set<DataSchemaNode> childNodes = dataNode.getChildNodes();
57         if (childNodes != null) {
58             for (DataSchemaNode childNode : childNodes) {
59                 if (childNode.isAugmenting()) {
60                     continue;
61                 }
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;
69                     allLists.add(list);
70                     traverse(list);
71                 } else if (childNode instanceof ChoiceNode) {
72                     final ChoiceNode choiceNode = (ChoiceNode) childNode;
73                     allChoices.add(choiceNode);
74                     final Set<ChoiceCaseNode> cases = choiceNode.getCases();
75                     if (cases != null) {
76                         for (final ChoiceCaseNode caseNode : cases) {
77                             traverse(caseNode);
78                         }
79                     }
80                 }
81             }
82         }
83
84         traverseModule(dataNode);
85         traverseGroupings(dataNode);
86
87     }
88
89     private void traverseModule(DataNodeContainer dataNode) {
90         final Module module;
91         if (dataNode instanceof Module) {
92             module = (Module) dataNode;
93         } else {
94             return;
95         }
96         final Set<NotificationDefinition> notifications = module.getNotifications();
97         for (NotificationDefinition notificationDefinition : notifications) {
98             traverse(notificationDefinition);
99         }
100         final Set<RpcDefinition> rpcs = module.getRpcs();
101         for (RpcDefinition rpcDefinition : rpcs) {
102             ContainerSchemaNode input = rpcDefinition.getInput();
103             if (input != null) {
104                 traverse(input);
105             }
106             ContainerSchemaNode output = rpcDefinition.getInput();
107             if (input != null) {
108                 traverse(output);
109             }
110         }
111     }
112
113     private void traverseGroupings(DataNodeContainer dataNode) {
114         final Set<GroupingDefinition> groupings = dataNode.getGroupings();
115         if (groupings != null) {
116             for (GroupingDefinition grouping : groupings) {
117                 traverse(grouping);
118             }
119         }
120     }
121
122     @Override
123     public boolean hasNext() {
124         if (container.getChildNodes() != null) {
125             final Set<DataSchemaNode> childNodes = container.getChildNodes();
126
127             if ((childNodes != null) && !childNodes.isEmpty()) {
128                 return childNodes.iterator().hasNext();
129             }
130         }
131         return false;
132     }
133
134     @Override
135     public DataSchemaNode next() {
136         return allChilds.iterator().next();
137     }
138
139     @Override
140     public void remove() {
141         throw new UnsupportedOperationException();
142     }
143 }