Committed by
Thomas Vachuska
[ONOS-3884] Base Data model node and parser interface
Change-Id: I94caf7fbf26125126d0779c283076c05fc7cd8cf
Showing
3 changed files
with
402 additions
and
0 deletions
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.yangutils.datamodel; | ||
17 | + | ||
18 | +import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ||
19 | + | ||
20 | +/** | ||
21 | + * Base class of a node in data model tree. | ||
22 | + */ | ||
23 | +public abstract class YangNode { | ||
24 | + | ||
25 | + /* Type of information maintained in node */ | ||
26 | + private YangNodeType nodeType; | ||
27 | + | ||
28 | + /* Parent reference */ | ||
29 | + private YangNode parent; | ||
30 | + | ||
31 | + /* First child reference */ | ||
32 | + private YangNode child; | ||
33 | + | ||
34 | + /* Next sibling reference */ | ||
35 | + private YangNode nextSibling; | ||
36 | + | ||
37 | + /* Previous sibling reference */ | ||
38 | + private YangNode previousSibling; | ||
39 | + | ||
40 | + /** | ||
41 | + * Default constructor is made private to ensure node type is always set. | ||
42 | + */ | ||
43 | + @SuppressWarnings("unused") | ||
44 | + private YangNode() { | ||
45 | + | ||
46 | + } | ||
47 | + | ||
48 | + /** | ||
49 | + * Create a specific type of node. | ||
50 | + * | ||
51 | + * @param type of YANG node | ||
52 | + */ | ||
53 | + protected YangNode(YangNodeType type) { | ||
54 | + setNodeType(type); | ||
55 | + } | ||
56 | + | ||
57 | + /** | ||
58 | + * Get the node type. | ||
59 | + * | ||
60 | + * @return node type | ||
61 | + */ | ||
62 | + public YangNodeType getNodeType() { | ||
63 | + return nodeType; | ||
64 | + } | ||
65 | + | ||
66 | + /** | ||
67 | + * Set the node type. | ||
68 | + * | ||
69 | + * @param nodeType type of node | ||
70 | + */ | ||
71 | + private void setNodeType(YangNodeType nodeType) { | ||
72 | + this.nodeType = nodeType; | ||
73 | + } | ||
74 | + | ||
75 | + /** | ||
76 | + * Get the parent of node. | ||
77 | + * | ||
78 | + * @return parent of node | ||
79 | + */ | ||
80 | + public YangNode getParent() { | ||
81 | + return parent; | ||
82 | + } | ||
83 | + | ||
84 | + /** | ||
85 | + * Set the parent of node. | ||
86 | + * | ||
87 | + * @param parent node | ||
88 | + */ | ||
89 | + public void setParent(YangNode parent) { | ||
90 | + this.parent = parent; | ||
91 | + } | ||
92 | + | ||
93 | + /** | ||
94 | + * Get the first child of node. | ||
95 | + * | ||
96 | + * @return first child of node | ||
97 | + */ | ||
98 | + public YangNode getChild() { | ||
99 | + return child; | ||
100 | + } | ||
101 | + | ||
102 | + /** | ||
103 | + * Set the first instance of a child node. | ||
104 | + * | ||
105 | + * @param child is only child to be set | ||
106 | + */ | ||
107 | + public void setChild(YangNode child) { | ||
108 | + this.child = child; | ||
109 | + } | ||
110 | + | ||
111 | + /** | ||
112 | + * Get the next sibling of node. | ||
113 | + * | ||
114 | + * @return next sibling of node | ||
115 | + */ | ||
116 | + public YangNode getNextSibling() { | ||
117 | + return nextSibling; | ||
118 | + } | ||
119 | + | ||
120 | + /** | ||
121 | + * Set the next sibling of node. | ||
122 | + * | ||
123 | + * @param sibling YANG node | ||
124 | + */ | ||
125 | + public void setNextSibling(YangNode sibling) { | ||
126 | + nextSibling = sibling; | ||
127 | + } | ||
128 | + | ||
129 | + /** | ||
130 | + * Get the previous sibling. | ||
131 | + * | ||
132 | + * @return previous sibling node | ||
133 | + */ | ||
134 | + public YangNode getPreviousSibling() { | ||
135 | + return previousSibling; | ||
136 | + } | ||
137 | + | ||
138 | + /** | ||
139 | + * Set the previous sibling. | ||
140 | + * | ||
141 | + * @param previousSibling points to predecessor sibling | ||
142 | + */ | ||
143 | + public void setPreviousSibling(YangNode previousSibling) { | ||
144 | + this.previousSibling = previousSibling; | ||
145 | + } | ||
146 | + | ||
147 | + /** | ||
148 | + * Add a child node, the children sibling list will be sorted based on node | ||
149 | + * type. | ||
150 | + * | ||
151 | + * @param newChild refers to a child to be added | ||
152 | + * @throws DataModelException due to violation in data model rules | ||
153 | + */ | ||
154 | + void addChild(YangNode newChild) throws DataModelException { | ||
155 | + if (newChild.getNodeType() == null) { | ||
156 | + throw new DataModelException("Abstract node cannot be inserted into a tree"); | ||
157 | + } | ||
158 | + | ||
159 | + if (newChild.getParent() == null) { | ||
160 | + newChild.setParent(this); | ||
161 | + } else if (newChild.getParent() != this) { | ||
162 | + throw new DataModelException("Node is already part of a tree"); | ||
163 | + } | ||
164 | + | ||
165 | + if (newChild.getChild() != null) { | ||
166 | + throw new DataModelException("Child to be added is not atomic, it already has a child"); | ||
167 | + } | ||
168 | + | ||
169 | + if (newChild.getNextSibling() != null) { | ||
170 | + throw new DataModelException("Child to be added is not atomic, it already has a next sibling"); | ||
171 | + } | ||
172 | + | ||
173 | + if (newChild.getPreviousSibling() != null) { | ||
174 | + throw new DataModelException("Child to be added is not atomic, it already has a previous sibling"); | ||
175 | + } | ||
176 | + | ||
177 | + /* First child to be added */ | ||
178 | + if (getChild() == null) { | ||
179 | + setChild(newChild); | ||
180 | + return; | ||
181 | + } | ||
182 | + | ||
183 | + YangNode curNode; | ||
184 | + curNode = getChild(); | ||
185 | + | ||
186 | + /* If the new node needs to be the first child */ | ||
187 | + if (newChild.getNodeType().ordinal() < curNode.getNodeType().ordinal()) { | ||
188 | + newChild.setNextSibling(curNode); | ||
189 | + curNode.setPreviousSibling(newChild); | ||
190 | + setChild(newChild); | ||
191 | + return; | ||
192 | + } | ||
193 | + | ||
194 | + /* | ||
195 | + * Get the predecessor child of new child | ||
196 | + */ | ||
197 | + while (curNode.getNextSibling() != null | ||
198 | + && newChild.getNodeType().ordinal() >= curNode.getNextSibling().getNodeType().ordinal()) { | ||
199 | + curNode = curNode.getNextSibling(); | ||
200 | + } | ||
201 | + | ||
202 | + /* If the new node needs to be the last child */ | ||
203 | + if (curNode.getNextSibling() == null) { | ||
204 | + curNode.setNextSibling(newChild); | ||
205 | + newChild.setPreviousSibling(curNode); | ||
206 | + return; | ||
207 | + } | ||
208 | + | ||
209 | + /* Insert the new node in child node list sorted by type */ | ||
210 | + newChild.setNextSibling(curNode.getNextSibling()); | ||
211 | + newChild.setPreviousSibling(curNode); | ||
212 | + curNode.getNextSibling().setPreviousSibling(newChild); | ||
213 | + curNode.setNextSibling(newChild); | ||
214 | + return; | ||
215 | + } | ||
216 | +} |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +package org.onosproject.yangutils.parser; | ||
18 | + | ||
19 | +import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ||
20 | + | ||
21 | +/** | ||
22 | + * Abstraction of an entity which process the data of lexer's parse tree. | ||
23 | + */ | ||
24 | +public interface Parsable { | ||
25 | + | ||
26 | + /** | ||
27 | + * Get the type of parsable data. | ||
28 | + * | ||
29 | + * @return the type of parsable data | ||
30 | + */ | ||
31 | + ParsableDataType getParsableDataType(); | ||
32 | + | ||
33 | + /** | ||
34 | + * Check if the node is valid as per YANG grammar's syntax and semantics. | ||
35 | + * This validation will be performed on entering the node in traversal | ||
36 | + * | ||
37 | + * @throws DataModelException if there is any violation of the YANG rules | ||
38 | + * in parsed data, corresponding exception should be thrown | ||
39 | + */ | ||
40 | + void validateDataOnEntry() throws DataModelException; | ||
41 | + | ||
42 | + /** | ||
43 | + * Check if the node is valid as per YANG grammar's syntax and semantics. | ||
44 | + * This validation will be performed on exiting the node in traversal | ||
45 | + * | ||
46 | + * @throws DataModelException if there is any violation of the YANG rules | ||
47 | + * in parsed data, corresponding exception should be thrown | ||
48 | + */ | ||
49 | + void validateDataOnExit() throws DataModelException; | ||
50 | +} |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.yangutils.parser; | ||
17 | + | ||
18 | +/** | ||
19 | + * ENUM to represent the type of data in parse tree. | ||
20 | + */ | ||
21 | +public enum ParsableDataType { | ||
22 | + /** | ||
23 | + * Identifies the module parsed data. | ||
24 | + */ | ||
25 | + MODULE_DATA, | ||
26 | + | ||
27 | + /** | ||
28 | + * Identifies the sub module parsed data. | ||
29 | + */ | ||
30 | + SUB_MODULE_DATA, | ||
31 | + | ||
32 | + /** | ||
33 | + * Identifies the typedef parsed data. | ||
34 | + */ | ||
35 | + TYPEDEF_DATA, | ||
36 | + | ||
37 | + /** | ||
38 | + * Identifies the type parsed data. | ||
39 | + */ | ||
40 | + TYPE_DATA, | ||
41 | + | ||
42 | + /** | ||
43 | + * Identifies the choice parsed data. | ||
44 | + */ | ||
45 | + CHOICE_DATA, | ||
46 | + | ||
47 | + /** | ||
48 | + * Identifies the case parsed data. | ||
49 | + */ | ||
50 | + CASE_DATA, | ||
51 | + | ||
52 | + /** | ||
53 | + * Identifies the YANG enumeration parsed data. | ||
54 | + */ | ||
55 | + ENUMERATION_DATA, | ||
56 | + | ||
57 | + /** | ||
58 | + * Identifies the grouping parsed data. | ||
59 | + */ | ||
60 | + GROUPING_DATA, | ||
61 | + | ||
62 | + /** | ||
63 | + * Identifies the uses parsed data. | ||
64 | + */ | ||
65 | + USES_DATA, | ||
66 | + | ||
67 | + /** | ||
68 | + * Identifies the augment parsed data. | ||
69 | + */ | ||
70 | + AUGMENT_DATA, | ||
71 | + | ||
72 | + /** | ||
73 | + * Identifies the container parsed data. | ||
74 | + */ | ||
75 | + CONTAINER_DATA, | ||
76 | + | ||
77 | + /** | ||
78 | + * Identifies the YANG list parsed data. | ||
79 | + */ | ||
80 | + LIST_DATA, | ||
81 | + | ||
82 | + /** | ||
83 | + * Identifies the YANG belongs-to parsed data. | ||
84 | + */ | ||
85 | + BELONGS_TO_DATA, | ||
86 | + | ||
87 | + /** | ||
88 | + * Identifies the YANG bit parsed data. | ||
89 | + */ | ||
90 | + BIT_DATA, | ||
91 | + | ||
92 | + /** | ||
93 | + * Identifies the YANG bits parsed data. | ||
94 | + */ | ||
95 | + BITS_DATA, | ||
96 | + | ||
97 | + /** | ||
98 | + * Identifies the YANG enum parsed data. | ||
99 | + */ | ||
100 | + ENUM_DATA, | ||
101 | + | ||
102 | + /** | ||
103 | + * Identifies the YANG import parsed data. | ||
104 | + */ | ||
105 | + IMPORT_DATA, | ||
106 | + | ||
107 | + /** | ||
108 | + * Identifies the YANG include parsed data. | ||
109 | + */ | ||
110 | + INCLUDE_DATA, | ||
111 | + | ||
112 | + /** | ||
113 | + * Identifies the YANG leaf parsed data. | ||
114 | + */ | ||
115 | + LEAF_DATA, | ||
116 | + | ||
117 | + /** | ||
118 | + * Identifies the YANG leaf list parsed data. | ||
119 | + */ | ||
120 | + LEAF_LIST_DATA, | ||
121 | + | ||
122 | + /** | ||
123 | + * Identifies the YANG must parsed data. | ||
124 | + */ | ||
125 | + MUST_DATA, | ||
126 | + | ||
127 | + /** | ||
128 | + * Identifies the YANG revision parsed data. | ||
129 | + */ | ||
130 | + REVISION_DATA, | ||
131 | + | ||
132 | + /** | ||
133 | + * Identifies the YANG namespace parsed data. | ||
134 | + */ | ||
135 | + NAMESPACE_DATA | ||
136 | +} |
-
Please register or login to post a comment