Prevent stub and integrationtest plugins to go into the distribution
[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 final List<ListSchemaNode> allLists;
19     private final List<ContainerSchemaNode> allContainers;
20     private final List<LeafSchemaNode> allLeafs;
21     private final List<LeafListSchemaNode> allLeafLists;
22     private final List<DataSchemaNode> allChilds;
23     
24     public DataNodeIterator(final DataNodeContainer container) {
25         if (container == null) {
26             throw new IllegalArgumentException("Data Node Container MUST be specified!");
27         }
28         
29         this.allContainers = new ArrayList<ContainerSchemaNode>();
30         this.allLists = new ArrayList<ListSchemaNode>();
31         this.allLeafs = new ArrayList<LeafSchemaNode>();
32         this.allLeafLists = new ArrayList<LeafListSchemaNode>();
33         this.allChilds = new ArrayList<DataSchemaNode>();
34         
35         this.container = container;
36         
37         traverse(this.container);
38     }
39     
40     public List<ContainerSchemaNode> allContainers() {
41         return allContainers;
42     }
43     
44     public List<ListSchemaNode> allLists() {
45         return allLists;
46     }
47     
48     public List<LeafSchemaNode> allLeafs() {
49         return allLeafs;
50     }
51     
52     public List<LeafListSchemaNode> allLeafLists() {
53         return allLeafLists;
54     }
55     
56     private void traverse(final DataNodeContainer dataNode) {
57         if (!containChildDataNodeContainer(dataNode)) {
58             return;
59         }
60
61         final Set<DataSchemaNode> childs = dataNode.getChildNodes();
62         if (childs != null) {
63             for (DataSchemaNode childNode : childs) {
64                 allChilds.add(childNode);
65                 if (childNode instanceof ContainerSchemaNode) {
66                     final ContainerSchemaNode container = (ContainerSchemaNode) childNode;
67                     allContainers.add(container);
68                     traverse(container);
69                 } else if (childNode instanceof ListSchemaNode) {
70                     final ListSchemaNode list = (ListSchemaNode) childNode;
71                     allLists.add(list);
72                     traverse(list);
73                 } else if (childNode instanceof LeafSchemaNode) {
74                     final LeafSchemaNode leaf = (LeafSchemaNode) childNode;
75                     allLeafs.add(leaf);
76                 } else if (childNode instanceof LeafListSchemaNode) {
77                     final LeafListSchemaNode leafList = (LeafListSchemaNode) childNode;
78                     allLeafLists.add(leafList);
79                 }
80             }
81         }
82     }
83
84     /**
85      * Returns <code>true</code> if and only if the child node contain at least
86      * one child container schema node or child list schema node, otherwise will
87      * always returns <code>false</code>
88      * 
89      * @param container
90      * @return <code>true</code> if and only if the child node contain at least
91      *         one child container schema node or child list schema node,
92      *         otherwise will always returns <code>false</code>
93      */
94     private boolean containChildDataNodeContainer(
95             final DataNodeContainer container) {
96         if (container != null) {
97             final Set<DataSchemaNode> childs = container.getChildNodes();
98             if ((childs != null) && (childs.size() > 0)) {
99                 for (final DataSchemaNode childNode : childs) {
100                     if (childNode instanceof DataNodeContainer) {
101                         return true;
102                     }
103                 }
104             }
105         }
106         return false;
107     }
108     
109     @Override
110     public boolean hasNext() {
111         if (container.getChildNodes() != null) {
112             Set<DataSchemaNode> childs = container.getChildNodes();
113             
114             if ((childs != null) && !childs.isEmpty()) {
115                 return childs.iterator().hasNext();
116             }
117         }
118         return false;
119     }
120
121     @Override
122     public DataSchemaNode next() {
123         return allChilds.iterator().next();
124     }
125
126     @Override
127     public void remove() {
128         throw new UnsupportedOperationException();
129     }
130 }