blob: 20b16868e7a3058cf49031bf54db8b85a39b409b [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
Bharat saraswal96dfef02016-06-16 00:29:12 +053018import java.io.Serializable;
19
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053020import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
21
22/**
Bharat saraswald9822e92016-04-05 15:13:44 +053023 * Represents base class of a node in data model tree.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053024 */
Vinod Kumar S427d2932016-04-20 13:02:58 +053025public abstract class YangNode
Bharat saraswal96dfef02016-06-16 00:29:12 +053026 implements Cloneable, Serializable {
27
28 private static final long serialVersionUID = 806201601L;
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053029
Vinod Kumar S67e7be62016-02-11 20:13:28 +053030 /**
31 * Type of node.
32 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053033 private YangNodeType nodeType;
34
Vinod Kumar S67e7be62016-02-11 20:13:28 +053035 /**
36 * Parent reference.
37 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053038 private YangNode parent;
39
Vinod Kumar S67e7be62016-02-11 20:13:28 +053040 /**
41 * First child reference.
42 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053043 private YangNode child;
44
Vinod Kumar S67e7be62016-02-11 20:13:28 +053045 /**
46 * Next sibling reference.
47 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053048 private YangNode nextSibling;
49
Vinod Kumar S67e7be62016-02-11 20:13:28 +053050 /**
51 * Previous sibling reference.
52 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053053 private YangNode previousSibling;
54
55 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053056 * Returns the nodes name.
Vinod Kumar S38046502016-03-23 15:30:27 +053057 *
58 * @return nodes name
59 */
60 public abstract String getName();
61
62 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053063 * Sets the nodes name.
Vinod Kumar S38046502016-03-23 15:30:27 +053064 *
65 * @param name nodes name
66 */
67 public abstract void setName(String name);
68
69 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Creates a YANG node object.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053071 */
72 @SuppressWarnings("unused")
73 private YangNode() {
74
75 }
76
77 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053078 * Creates a specific type of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053079 *
80 * @param type of YANG node
81 */
82 protected YangNode(YangNodeType type) {
83 setNodeType(type);
84 }
85
86 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053087 * Returns the node type.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053088 *
89 * @return node type
90 */
91 public YangNodeType getNodeType() {
92 return nodeType;
93 }
94
95 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053096 * Sets the node type.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053097 *
98 * @param nodeType type of node
99 */
100 private void setNodeType(YangNodeType nodeType) {
101 this.nodeType = nodeType;
102 }
103
104 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530105 * Returns the parent of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530106 *
107 * @return parent of node
108 */
109 public YangNode getParent() {
110 return parent;
111 }
112
113 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530114 * Sets the parent of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530115 *
116 * @param parent node
117 */
118 public void setParent(YangNode parent) {
119 this.parent = parent;
120 }
121
122 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530123 * Returns the first child of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530124 *
125 * @return first child of node
126 */
127 public YangNode getChild() {
128 return child;
129 }
130
131 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530132 * Sets the first instance of a child node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530133 *
134 * @param child is only child to be set
135 */
136 public void setChild(YangNode child) {
137 this.child = child;
138 }
139
140 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530141 * Returns the next sibling of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530142 *
143 * @return next sibling of node
144 */
145 public YangNode getNextSibling() {
146 return nextSibling;
147 }
148
149 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530150 * Sets the next sibling of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530151 *
152 * @param sibling YANG node
153 */
Vinod Kumar S427d2932016-04-20 13:02:58 +0530154 private void setNextSibling(YangNode sibling) {
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530155 nextSibling = sibling;
156 }
157
158 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530159 * Returns the previous sibling.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530160 *
161 * @return previous sibling node
162 */
163 public YangNode getPreviousSibling() {
164 return previousSibling;
165 }
166
167 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530168 * Sets the previous sibling.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530169 *
170 * @param previousSibling points to predecessor sibling
171 */
Vinod Kumar S427d2932016-04-20 13:02:58 +0530172 private void setPreviousSibling(YangNode previousSibling) {
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530173 this.previousSibling = previousSibling;
174 }
175
176 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530177 * Adds a child node, the children sibling list will be sorted based on node
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530178 * type.
179 *
180 * @param newChild refers to a child to be added
181 * @throws DataModelException due to violation in data model rules
182 */
Vinod Kumar S427d2932016-04-20 13:02:58 +0530183 public void addChild(YangNode newChild)
184 throws DataModelException {
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530185 if (newChild.getNodeType() == null) {
186 throw new DataModelException("Abstract node cannot be inserted into a tree");
187 }
188
189 if (newChild.getParent() == null) {
190 newChild.setParent(this);
191 } else if (newChild.getParent() != this) {
192 throw new DataModelException("Node is already part of a tree");
193 }
194
195 if (newChild.getChild() != null) {
196 throw new DataModelException("Child to be added is not atomic, it already has a child");
197 }
198
199 if (newChild.getNextSibling() != null) {
200 throw new DataModelException("Child to be added is not atomic, it already has a next sibling");
201 }
202
203 if (newChild.getPreviousSibling() != null) {
204 throw new DataModelException("Child to be added is not atomic, it already has a previous sibling");
205 }
206
207 /* First child to be added */
208 if (getChild() == null) {
209 setChild(newChild);
210 return;
211 }
212
213 YangNode curNode;
214 curNode = getChild();
215
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530216 /*
217 * Get the predecessor child of new child
218 */
Vinod Kumar S427d2932016-04-20 13:02:58 +0530219 while (curNode.getNextSibling() != null) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530220
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530221 curNode = curNode.getNextSibling();
222 }
223
224 /* If the new node needs to be the last child */
225 if (curNode.getNextSibling() == null) {
226 curNode.setNextSibling(newChild);
227 newChild.setPreviousSibling(curNode);
Vinod Kumar S427d2932016-04-20 13:02:58 +0530228 }
229 }
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530230}