blob: 0ac44d0565eb77c3b5408503992ce661abb92198 [file] [log] [blame]
Vinod Kumar S8c4e6492016-02-05 20:21:19 +05301/*
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 */
16package org.onosproject.yangutils.datamodel;
17
18import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal594bc6d2016-02-22 22:15:21 +053019import org.onosproject.yangutils.translator.CachedFileHandle;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053020import org.onosproject.yangutils.translator.CodeGenerator;
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053021
22/**
23 * Base class of a node in data model tree.
24 */
Vinod Kumar S67e7be62016-02-11 20:13:28 +053025public abstract class YangNode implements CodeGenerator {
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053026
Vinod Kumar S67e7be62016-02-11 20:13:28 +053027 /**
28 * Type of node.
29 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053030 private YangNodeType nodeType;
31
Vinod Kumar S67e7be62016-02-11 20:13:28 +053032 /**
33 * Parent reference.
34 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053035 private YangNode parent;
36
Vinod Kumar S67e7be62016-02-11 20:13:28 +053037 /**
38 * First child reference.
39 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053040 private YangNode child;
41
Vinod Kumar S67e7be62016-02-11 20:13:28 +053042 /**
43 * Next sibling reference.
44 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053045 private YangNode nextSibling;
46
Vinod Kumar S67e7be62016-02-11 20:13:28 +053047 /**
48 * Previous sibling reference.
49 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053050 private YangNode previousSibling;
51
52 /**
53 * Default constructor is made private to ensure node type is always set.
54 */
55 @SuppressWarnings("unused")
56 private YangNode() {
57
58 }
59
60 /**
61 * Create a specific type of node.
62 *
63 * @param type of YANG node
64 */
65 protected YangNode(YangNodeType type) {
66 setNodeType(type);
67 }
68
69 /**
70 * Get the node type.
71 *
72 * @return node type
73 */
74 public YangNodeType getNodeType() {
75 return nodeType;
76 }
77
78 /**
79 * Set the node type.
80 *
81 * @param nodeType type of node
82 */
83 private void setNodeType(YangNodeType nodeType) {
84 this.nodeType = nodeType;
85 }
86
87 /**
88 * Get the parent of node.
89 *
90 * @return parent of node
91 */
92 public YangNode getParent() {
93 return parent;
94 }
95
96 /**
97 * Set the parent of node.
98 *
99 * @param parent node
100 */
101 public void setParent(YangNode parent) {
102 this.parent = parent;
103 }
104
105 /**
106 * Get the first child of node.
107 *
108 * @return first child of node
109 */
110 public YangNode getChild() {
111 return child;
112 }
113
114 /**
115 * Set the first instance of a child node.
116 *
117 * @param child is only child to be set
118 */
119 public void setChild(YangNode child) {
120 this.child = child;
121 }
122
123 /**
124 * Get the next sibling of node.
125 *
126 * @return next sibling of node
127 */
128 public YangNode getNextSibling() {
129 return nextSibling;
130 }
131
132 /**
133 * Set the next sibling of node.
134 *
135 * @param sibling YANG node
136 */
137 public void setNextSibling(YangNode sibling) {
138 nextSibling = sibling;
139 }
140
141 /**
142 * Get the previous sibling.
143 *
144 * @return previous sibling node
145 */
146 public YangNode getPreviousSibling() {
147 return previousSibling;
148 }
149
150 /**
151 * Set the previous sibling.
152 *
153 * @param previousSibling points to predecessor sibling
154 */
155 public void setPreviousSibling(YangNode previousSibling) {
156 this.previousSibling = previousSibling;
157 }
158
159 /**
160 * Add a child node, the children sibling list will be sorted based on node
161 * type.
162 *
163 * @param newChild refers to a child to be added
164 * @throws DataModelException due to violation in data model rules
165 */
Vinod Kumar Scf044422016-02-09 19:53:45 +0530166 public void addChild(YangNode newChild) throws DataModelException {
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530167 if (newChild.getNodeType() == null) {
168 throw new DataModelException("Abstract node cannot be inserted into a tree");
169 }
170
171 if (newChild.getParent() == null) {
172 newChild.setParent(this);
173 } else if (newChild.getParent() != this) {
174 throw new DataModelException("Node is already part of a tree");
175 }
176
177 if (newChild.getChild() != null) {
178 throw new DataModelException("Child to be added is not atomic, it already has a child");
179 }
180
181 if (newChild.getNextSibling() != null) {
182 throw new DataModelException("Child to be added is not atomic, it already has a next sibling");
183 }
184
185 if (newChild.getPreviousSibling() != null) {
186 throw new DataModelException("Child to be added is not atomic, it already has a previous sibling");
187 }
188
189 /* First child to be added */
190 if (getChild() == null) {
191 setChild(newChild);
192 return;
193 }
194
195 YangNode curNode;
196 curNode = getChild();
197
198 /* If the new node needs to be the first child */
199 if (newChild.getNodeType().ordinal() < curNode.getNodeType().ordinal()) {
200 newChild.setNextSibling(curNode);
201 curNode.setPreviousSibling(newChild);
202 setChild(newChild);
203 return;
204 }
205
206 /*
207 * Get the predecessor child of new child
208 */
209 while (curNode.getNextSibling() != null
210 && newChild.getNodeType().ordinal() >= curNode.getNextSibling().getNodeType().ordinal()) {
211 curNode = curNode.getNextSibling();
212 }
213
214 /* If the new node needs to be the last child */
215 if (curNode.getNextSibling() == null) {
216 curNode.setNextSibling(newChild);
217 newChild.setPreviousSibling(curNode);
218 return;
219 }
220
221 /* Insert the new node in child node list sorted by type */
222 newChild.setNextSibling(curNode.getNextSibling());
223 newChild.setPreviousSibling(curNode);
224 curNode.getNextSibling().setPreviousSibling(newChild);
225 curNode.setNextSibling(newChild);
226 return;
227 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530228
229 /**
230 * Get the YANG name of the node.
231 *
232 * @return the name of node as defined in YANG file.
233 */
234 public abstract String getName();
235
236 /**
237 * Set the YANG name of the node.
238 *
239 * @param name the name of node as defined in YANG file.
240 */
241 public abstract void setName(String name);
242
243 /**
244 * Get the mapped java package.
245 *
246 * @return the java package
247 */
248 public abstract String getPackage();
249
250 /**
251 * Set the mapped java package.
252 *
253 * @param pkg the package to set
254 */
255 public abstract void setPackage(String pkg);
256
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530257 /**
258 * Get the mapped java file handler.
259 *
260 * @return the file handle.
261 */
262 public abstract CachedFileHandle getFileHandle();
263
264 /**
265 * Set the mapped java file handle.
266 *
267 * @param fileHandle the file handle to set of current node.
268 */
269 public abstract void setFileHandle(CachedFileHandle fileHandle);
270
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530271}