Fix minor bug in FRM proactive flow code path
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / 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.controller.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.controller.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 container = (ContainerSchemaNode) childNode;
65                     allContainers.add(container);
66                     traverse(container);
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         final Set<GroupingDefinition> groupings = dataNode.getGroupings();
85         if (groupings != null) {
86             for (GroupingDefinition grouping : groupings) {
87                 traverse(grouping);
88             }
89         }
90     }
91
92     @Override
93     public boolean hasNext() {
94         if (container.getChildNodes() != null) {
95             final Set<DataSchemaNode> childNodes = container.getChildNodes();
96
97             if ((childNodes != null) && !childNodes.isEmpty()) {
98                 return childNodes.iterator().hasNext();
99             }
100         }
101         return false;
102     }
103
104     @Override
105     public DataSchemaNode next() {
106         return allChilds.iterator().next();
107     }
108
109     @Override
110     public void remove() {
111         throw new UnsupportedOperationException();
112     }
113 }