Update ChoiceSchemaNode design
[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                     for (final ChoiceCaseNode caseNode : choiceNode.getCases().values()) {
129                         traverse(caseNode);
130                     }
131                 }
132             }
133         }
134
135         this.allTypedefs.addAll(dataNode.getTypeDefinitions());
136         traverseModule(dataNode);
137         traverseGroupings(dataNode);
138
139     }
140
141     private void traverseModule(final DataNodeContainer dataNode) {
142         final Module module;
143         if (dataNode instanceof Module) {
144             module = (Module) dataNode;
145         } else {
146             return;
147         }
148
149         final Set<NotificationDefinition> notifications = module.getNotifications();
150         for (NotificationDefinition notificationDefinition : notifications) {
151             traverse(notificationDefinition);
152         }
153
154         final Set<RpcDefinition> rpcs = module.getRpcs();
155         for (RpcDefinition rpcDefinition : rpcs) {
156             this.allTypedefs.addAll(rpcDefinition.getTypeDefinitions());
157             ContainerSchemaNode input = rpcDefinition.getInput();
158             if (input != null) {
159                 traverse(input);
160             }
161             ContainerSchemaNode output = rpcDefinition.getOutput();
162             if (output != null) {
163                 traverse(output);
164             }
165         }
166     }
167
168     private void traverseGroupings(final DataNodeContainer dataNode) {
169         final Set<GroupingDefinition> groupings = dataNode.getGroupings();
170         if (groupings != null) {
171             for (GroupingDefinition grouping : groupings) {
172                 allGroupings.add(grouping);
173                 traverse(grouping);
174             }
175         }
176     }
177
178     @Override
179     public boolean hasNext() {
180         if (container.getChildNodes() != null) {
181             final Collection<DataSchemaNode> childNodes = container.getChildNodes();
182
183             if (childNodes != null && !childNodes.isEmpty()) {
184                 return childNodes.iterator().hasNext();
185             }
186         }
187         return false;
188     }
189
190     @Override
191     public DataSchemaNode next() {
192         return allChilds.iterator().next();
193     }
194
195     @Override
196     public void remove() {
197         throw new UnsupportedOperationException();
198     }
199 }