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