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