blob: beb8aa6fed4d9c8a905a49e37d95eee6d09411b1 [file] [log] [blame]
Vinod Kumar S8c4e6492016-02-05 20:21:19 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S8c4e6492016-02-05 20:21:19 +05303 *
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;
19
20/**
Bharat saraswald9822e92016-04-05 15:13:44 +053021 * Represents base class of a node in data model tree.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053022 */
Vinod Kumar S38046502016-03-23 15:30:27 +053023public abstract class YangNode {
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053024
Vinod Kumar S67e7be62016-02-11 20:13:28 +053025 /**
26 * Type of node.
27 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053028 private YangNodeType nodeType;
29
Vinod Kumar S67e7be62016-02-11 20:13:28 +053030 /**
31 * Parent reference.
32 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053033 private YangNode parent;
34
Vinod Kumar S67e7be62016-02-11 20:13:28 +053035 /**
36 * First child reference.
37 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053038 private YangNode child;
39
Vinod Kumar S67e7be62016-02-11 20:13:28 +053040 /**
41 * Next sibling reference.
42 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053043 private YangNode nextSibling;
44
Vinod Kumar S67e7be62016-02-11 20:13:28 +053045 /**
46 * Previous sibling reference.
47 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053048 private YangNode previousSibling;
49
50 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053051 * Returns the nodes name.
Vinod Kumar S38046502016-03-23 15:30:27 +053052 *
53 * @return nodes name
54 */
55 public abstract String getName();
56
57 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053058 * Sets the nodes name.
Vinod Kumar S38046502016-03-23 15:30:27 +053059 *
60 * @param name nodes name
61 */
62 public abstract void setName(String name);
63
64 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053065 * Creates a YANG node object.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053066 */
67 @SuppressWarnings("unused")
68 private YangNode() {
69
70 }
71
72 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053073 * Creates a specific type of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053074 *
75 * @param type of YANG node
76 */
77 protected YangNode(YangNodeType type) {
78 setNodeType(type);
79 }
80
81 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053082 * Returns the node type.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053083 *
84 * @return node type
85 */
86 public YangNodeType getNodeType() {
87 return nodeType;
88 }
89
90 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053091 * Sets the node type.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053092 *
93 * @param nodeType type of node
94 */
95 private void setNodeType(YangNodeType nodeType) {
96 this.nodeType = nodeType;
97 }
98
99 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530100 * Returns the parent of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530101 *
102 * @return parent of node
103 */
104 public YangNode getParent() {
105 return parent;
106 }
107
108 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 * Sets the parent of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530110 *
111 * @param parent node
112 */
113 public void setParent(YangNode parent) {
114 this.parent = parent;
115 }
116
117 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530118 * Returns the first child of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530119 *
120 * @return first child of node
121 */
122 public YangNode getChild() {
123 return child;
124 }
125
126 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530127 * Sets the first instance of a child node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530128 *
129 * @param child is only child to be set
130 */
131 public void setChild(YangNode child) {
132 this.child = child;
133 }
134
135 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530136 * Returns the next sibling of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530137 *
138 * @return next sibling of node
139 */
140 public YangNode getNextSibling() {
141 return nextSibling;
142 }
143
144 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530145 * Sets the next sibling of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530146 *
147 * @param sibling YANG node
148 */
149 public void setNextSibling(YangNode sibling) {
150 nextSibling = sibling;
151 }
152
153 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530154 * Returns the previous sibling.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530155 *
156 * @return previous sibling node
157 */
158 public YangNode getPreviousSibling() {
159 return previousSibling;
160 }
161
162 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530163 * Sets the previous sibling.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530164 *
165 * @param previousSibling points to predecessor sibling
166 */
167 public void setPreviousSibling(YangNode previousSibling) {
168 this.previousSibling = previousSibling;
169 }
170
171 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530172 * Adds a child node, the children sibling list will be sorted based on node
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530173 * type.
174 *
175 * @param newChild refers to a child to be added
176 * @throws DataModelException due to violation in data model rules
177 */
Vinod Kumar Scf044422016-02-09 19:53:45 +0530178 public void addChild(YangNode newChild) throws DataModelException {
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530179 if (newChild.getNodeType() == null) {
180 throw new DataModelException("Abstract node cannot be inserted into a tree");
181 }
182
183 if (newChild.getParent() == null) {
184 newChild.setParent(this);
185 } else if (newChild.getParent() != this) {
186 throw new DataModelException("Node is already part of a tree");
187 }
188
189 if (newChild.getChild() != null) {
190 throw new DataModelException("Child to be added is not atomic, it already has a child");
191 }
192
193 if (newChild.getNextSibling() != null) {
194 throw new DataModelException("Child to be added is not atomic, it already has a next sibling");
195 }
196
197 if (newChild.getPreviousSibling() != null) {
198 throw new DataModelException("Child to be added is not atomic, it already has a previous sibling");
199 }
200
201 /* First child to be added */
202 if (getChild() == null) {
203 setChild(newChild);
204 return;
205 }
206
207 YangNode curNode;
208 curNode = getChild();
209
Vinod Kumar S38046502016-03-23 15:30:27 +0530210 /*-
211 * If the new node needs to be the first child
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530212 if (newChild.getNodeType().ordinal() < curNode.getNodeType().ordinal()) {
213 newChild.setNextSibling(curNode);
214 curNode.setPreviousSibling(newChild);
215 setChild(newChild);
216 return;
217 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530218 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530219
220 /*
221 * Get the predecessor child of new child
222 */
223 while (curNode.getNextSibling() != null
Vinod Kumar S38046502016-03-23 15:30:27 +0530224 /*
225 * && newChild.getNodeType().ordinal() >=
226 * curNode.getNextSibling().getNodeType().ordinal()
227 */) {
228
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530229 curNode = curNode.getNextSibling();
230 }
231
232 /* If the new node needs to be the last child */
233 if (curNode.getNextSibling() == null) {
234 curNode.setNextSibling(newChild);
235 newChild.setPreviousSibling(curNode);
236 return;
237 }
238
Vinod Kumar S38046502016-03-23 15:30:27 +0530239 /*-
240 * Insert the new node in child node list sorted by type
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530241 newChild.setNextSibling(curNode.getNextSibling());
242 newChild.setPreviousSibling(curNode);
243 curNode.getNextSibling().setPreviousSibling(newChild);
244 curNode.setNextSibling(newChild);
245 return;
Vinod Kumar S38046502016-03-23 15:30:27 +0530246 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530247 }
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530248}