Merge "Add IfNewHostNotify to DeviceManager"
[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.GroupingDefinition;
12 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
13 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
14 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
15
16 public class DataNodeIterator implements Iterator<DataSchemaNode> {
17
18     private final DataNodeContainer container;
19     private List<ListSchemaNode> allLists;
20     private List<ContainerSchemaNode> allContainers;
21     private List<LeafSchemaNode> allLeafs;
22     private List<LeafListSchemaNode> allLeafLists;
23     private 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         init();
31         this.container = container;
32         traverse(this.container);
33     }
34
35     private void init() {
36         this.allContainers = new ArrayList<ContainerSchemaNode>();
37         this.allLists = new ArrayList<ListSchemaNode>();
38         this.allLeafs = new ArrayList<LeafSchemaNode>();
39         this.allLeafLists = new ArrayList<LeafListSchemaNode>();
40         this.allChilds = new ArrayList<DataSchemaNode>();
41     }
42
43     public List<ContainerSchemaNode> allContainers() {
44         return allContainers;
45     }
46
47     public List<ListSchemaNode> allLists() {
48         return allLists;
49     }
50
51     public List<LeafSchemaNode> allLeafs() {
52         return allLeafs;
53     }
54
55     public List<LeafListSchemaNode> allLeafLists() {
56         return allLeafLists;
57     }
58
59     private void traverse(final DataNodeContainer dataNode) {
60         if (dataNode == null) {
61             return;
62         }
63
64         final Set<DataSchemaNode> childs = dataNode.getChildNodes();
65         if (childs != null) {
66             for (DataSchemaNode childNode : childs) {
67                 if (childNode.isAugmenting()) {
68                     continue;
69                 }
70                 allChilds.add(childNode);
71                 if (childNode instanceof ContainerSchemaNode) {
72                     final ContainerSchemaNode container = (ContainerSchemaNode) childNode;
73                     allContainers.add(container);
74                     traverse(container);
75                 } else if (childNode instanceof ListSchemaNode) {
76                     final ListSchemaNode list = (ListSchemaNode) childNode;
77                     allLists.add(list);
78                     traverse(list);
79                 } else if (childNode instanceof LeafSchemaNode) {
80                     final LeafSchemaNode leaf = (LeafSchemaNode) childNode;
81                     allLeafs.add(leaf);
82                 } else if (childNode instanceof LeafListSchemaNode) {
83                     final LeafListSchemaNode leafList = (LeafListSchemaNode) childNode;
84                     allLeafLists.add(leafList);
85                 }
86             }
87         }
88
89         final Set<GroupingDefinition> groupings = dataNode.getGroupings();
90         if(groupings != null) {
91             for(GroupingDefinition grouping : groupings) {
92                 traverse(grouping);
93             }
94         }
95     }
96
97     @Override
98     public boolean hasNext() {
99         if (container.getChildNodes() != null) {
100             Set<DataSchemaNode> childs = container.getChildNodes();
101
102             if ((childs != null) && !childs.isEmpty()) {
103                 return childs.iterator().hasNext();
104             }
105         }
106         return false;
107     }
108
109     @Override
110     public DataSchemaNode next() {
111         return allChilds.iterator().next();
112     }
113
114     @Override
115     public void remove() {
116         throw new UnsupportedOperationException();
117     }
118 }