b3fcbed4f0f426530542e0914bd4fe5b5408204e
[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.Collection;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Set;
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;
26
27 /**
28  * DataNodeIterator is iterator, which walks down whole YANG DataNodeContainer
29  * and walks all instances of {@link DataSchemaNode} present in subtree.
30  *
31  * Iterator instance is eagerly created, walking happens on initialization.
32  *
33  * Iteration is not ordered.
34  *
35  */
36 public class DataNodeIterator implements Iterator<DataSchemaNode> {
37
38     private final DataNodeContainer container;
39     private final List<ListSchemaNode> allLists;
40     private final List<ContainerSchemaNode> allContainers;
41     private final List<ChoiceSchemaNode> allChoices;
42     private final List<DataSchemaNode> allChilds;
43     private final List<GroupingDefinition> allGroupings;
44     private final List<TypeDefinition<?>> allTypedefs;
45
46     public DataNodeIterator(final DataNodeContainer container) {
47         if (container == null) {
48             throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");
49         }
50
51         this.allContainers = new ArrayList<>();
52         this.allLists = new ArrayList<>();
53         this.allChilds = new ArrayList<>();
54         this.allChoices = new ArrayList<>();
55         this.allGroupings = new ArrayList<>();
56         this.allTypedefs = new ArrayList<>();
57
58         this.container = container;
59         traverse(this.container);
60     }
61
62     /**
63      * Returns list all containers present in subtree.
64      *
65      * @return Returns list all containers present in subtree.
66      */
67     public List<ContainerSchemaNode> allContainers() {
68         return allContainers;
69     }
70
71     /**
72      * Returns list all lists present in subtree.
73      *
74      * @return Returns list all containers present in subtree.
75      */
76     public List<ListSchemaNode> allLists() {
77         return allLists;
78     }
79
80     /**
81      * Returns list all choices present in subtree.
82      *
83      * @return Returns list all containers present in subtree.
84      */
85     public List<ChoiceSchemaNode> allChoices() {
86         return allChoices;
87     }
88
89     /**
90      * Returns list all groupings present in subtree.
91      *
92      * @return Returns list all containers present in subtree.
93      */
94     public List<GroupingDefinition> allGroupings() {
95         return allGroupings;
96     }
97
98     /**
99      * Returns list all typedefs present in subtree.
100      *
101      * @return Returns list all containers present in subtree.
102      */
103     public List<TypeDefinition<?>> allTypedefs() {
104         return allTypedefs;
105     }
106
107     private void traverse(final DataNodeContainer dataNode) {
108         if (dataNode == null) {
109             return;
110         }
111
112         final Iterable<DataSchemaNode> childNodes = dataNode.getChildNodes();
113         if (childNodes != null) {
114             for (DataSchemaNode childNode : childNodes) {
115                 if (childNode.isAugmenting()) {
116                     continue;
117                 }
118                 allChilds.add(childNode);
119                 if (childNode instanceof ContainerSchemaNode) {
120                     final ContainerSchemaNode containerNode = (ContainerSchemaNode) childNode;
121                     allContainers.add(containerNode);
122                     traverse(containerNode);
123                 } else if (childNode instanceof ListSchemaNode) {
124                     final ListSchemaNode list = (ListSchemaNode) childNode;
125                     allLists.add(list);
126                     traverse(list);
127                 } else if (childNode instanceof ChoiceSchemaNode) {
128                     final ChoiceSchemaNode choiceNode = (ChoiceSchemaNode) childNode;
129                     allChoices.add(choiceNode);
130                     final Set<ChoiceCaseNode> cases = choiceNode.getCases();
131                     if (cases != null) {
132                         for (final ChoiceCaseNode caseNode : cases) {
133                             traverse(caseNode);
134                         }
135                     }
136                 }
137             }
138         }
139
140         this.allTypedefs.addAll(dataNode.getTypeDefinitions());
141         traverseModule(dataNode);
142         traverseGroupings(dataNode);
143
144     }
145
146     private void traverseModule(final DataNodeContainer dataNode) {
147         final Module module;
148         if (dataNode instanceof Module) {
149             module = (Module) dataNode;
150         } else {
151             return;
152         }
153
154         final Set<NotificationDefinition> notifications = module.getNotifications();
155         for (NotificationDefinition notificationDefinition : notifications) {
156             traverse(notificationDefinition);
157         }
158
159         final Set<RpcDefinition> rpcs = module.getRpcs();
160         for (RpcDefinition rpcDefinition : rpcs) {
161             this.allTypedefs.addAll(rpcDefinition.getTypeDefinitions());
162             ContainerSchemaNode input = rpcDefinition.getInput();
163             if (input != null) {
164                 traverse(input);
165             }
166             ContainerSchemaNode output = rpcDefinition.getInput();
167             if (input != null) {
168                 traverse(output);
169             }
170         }
171     }
172
173     private void traverseGroupings(final DataNodeContainer dataNode) {
174         final Set<GroupingDefinition> groupings = dataNode.getGroupings();
175         if (groupings != null) {
176             for (GroupingDefinition grouping : groupings) {
177                 allGroupings.add(grouping);
178                 traverse(grouping);
179             }
180         }
181     }
182
183     @Override
184     public boolean hasNext() {
185         if (container.getChildNodes() != null) {
186             final Collection<DataSchemaNode> childNodes = container.getChildNodes();
187
188             if ((childNodes != null) && !childNodes.isEmpty()) {
189                 return childNodes.iterator().hasNext();
190             }
191         }
192         return false;
193     }
194
195     @Override
196     public DataSchemaNode next() {
197         return allChilds.iterator().next();
198     }
199
200     @Override
201     public void remove() {
202         throw new UnsupportedOperationException();
203     }
204 }