Added support for resolving augmentations.
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / yang / model / util / DataNodeIterator.java
1 package org.opendaylight.controller.yang.model.util;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
9 import org.opendaylight.controller.yang.model.api.DataNodeContainer;
10 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
11 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
12 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
13 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
14
15 public class DataNodeIterator implements Iterator<DataSchemaNode> {
16     
17     private final DataNodeContainer container;
18     private List<ListSchemaNode> allLists;
19     private List<ContainerSchemaNode> allContainers;
20     private List<LeafSchemaNode> allLeafs;
21     private List<LeafListSchemaNode> allLeafLists;
22     private List<DataSchemaNode> allChilds;
23     
24     public DataNodeIterator(final DataNodeContainer container) {
25         if (container == null) {
26             throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");
27         }
28         
29         init();
30         this.container = container;
31         traverse(this.container);
32     }
33     
34     private void init() {
35         this.allContainers = new ArrayList<ContainerSchemaNode>();
36         this.allLists = new ArrayList<ListSchemaNode>();
37         this.allLeafs = new ArrayList<LeafSchemaNode>();
38         this.allLeafLists = new ArrayList<LeafListSchemaNode>();
39         this.allChilds = new ArrayList<DataSchemaNode>();
40     }
41     
42     public List<ContainerSchemaNode> allContainers() {
43         return allContainers;
44     }
45     
46     public List<ListSchemaNode> allLists() {
47         return allLists;
48     }
49     
50     public List<LeafSchemaNode> allLeafs() {
51         return allLeafs;
52     }
53     
54     public List<LeafListSchemaNode> allLeafLists() {
55         return allLeafLists;
56     }
57     
58     private void traverse(final DataNodeContainer dataNode) {
59         if (dataNode == null) {
60             return;
61         }
62         
63         final Set<DataSchemaNode> childs = dataNode.getChildNodes();
64         if (childs != null) {
65             for (DataSchemaNode childNode : childs) {
66                 if (childNode.isAugmenting()) {
67                     continue;
68                 }
69                 allChilds.add(childNode);
70                 if (childNode instanceof ContainerSchemaNode) {
71                     final ContainerSchemaNode container = (ContainerSchemaNode) childNode;
72                     allContainers.add(container);
73                     traverse(container);
74                 } else if (childNode instanceof ListSchemaNode) {
75                     final ListSchemaNode list = (ListSchemaNode) childNode;
76                     allLists.add(list);
77                     traverse(list);
78                 } else if (childNode instanceof LeafSchemaNode) {
79                     final LeafSchemaNode leaf = (LeafSchemaNode) childNode;
80                     allLeafs.add(leaf);
81                 } else if (childNode instanceof LeafListSchemaNode) {
82                     final LeafListSchemaNode leafList = (LeafListSchemaNode) childNode;
83                     allLeafLists.add(leafList);
84                 }
85             }
86             return;
87         }
88     }
89     
90     @Override
91     public boolean hasNext() {
92         if (container.getChildNodes() != null) {
93             Set<DataSchemaNode> childs = container.getChildNodes();
94             
95             if ((childs != null) && !childs.isEmpty()) {
96                 return childs.iterator().hasNext();
97             }
98         }
99         return false;
100     }
101
102     @Override
103     public DataSchemaNode next() {
104         return allChilds.iterator().next();
105     }
106
107     @Override
108     public void remove() {
109         throw new UnsupportedOperationException();
110     }
111 }