Merge "Prevent stub and integrationtest plugins to go into the distribution of openda...
[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                 if (childNode.isAugmenting()) {
65                     continue;
66                 }
67                 allChilds.add(childNode);
68                 if (childNode instanceof ContainerSchemaNode) {
69                     final ContainerSchemaNode container = (ContainerSchemaNode) childNode;
70                     allContainers.add(container);
71                     traverse(container);
72                 } else if (childNode instanceof ListSchemaNode) {
73                     final ListSchemaNode list = (ListSchemaNode) childNode;
74                     allLists.add(list);
75                     traverse(list);
76                 } else if (childNode instanceof LeafSchemaNode) {
77                     final LeafSchemaNode leaf = (LeafSchemaNode) childNode;
78                     allLeafs.add(leaf);
79                 } else if (childNode instanceof LeafListSchemaNode) {
80                     final LeafListSchemaNode leafList = (LeafListSchemaNode) childNode;
81                     allLeafLists.add(leafList);
82                 }
83             }
84         }
85     }
86
87     /**
88      * Returns <code>true</code> if and only if the child node contain at least
89      * one child container schema node or child list schema node, otherwise will
90      * always returns <code>false</code>
91      * 
92      * @param container
93      * @return <code>true</code> if and only if the child node contain at least
94      *         one child container schema node or child list schema node,
95      *         otherwise will always returns <code>false</code>
96      */
97     private boolean containChildDataNodeContainer(
98             final DataNodeContainer container) {
99         if (container != null) {
100             final Set<DataSchemaNode> childs = container.getChildNodes();
101             if ((childs != null) && (childs.size() > 0)) {
102                 for (final DataSchemaNode childNode : childs) {
103                     if (childNode instanceof DataNodeContainer) {
104                         return true;
105                     }
106                 }
107             }
108         }
109         return false;
110     }
111     
112     @Override
113     public boolean hasNext() {
114         if (container.getChildNodes() != null) {
115             Set<DataSchemaNode> childs = container.getChildNodes();
116             
117             if ((childs != null) && !childs.isEmpty()) {
118                 return childs.iterator().hasNext();
119             }
120         }
121         return false;
122     }
123
124     @Override
125     public DataSchemaNode next() {
126         return allChilds.iterator().next();
127     }
128
129     @Override
130     public void remove() {
131         throw new UnsupportedOperationException();
132     }
133 }