blob: 820b0b4a0302a7ae787aa47664361b69ee520474 [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;
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053019import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
20
21/**
Bharat saraswald9822e92016-04-05 15:13:44 +053022 * Represents base class of a node in data model tree.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053023 */
Vinod Kumar S427d2932016-04-20 13:02:58 +053024public abstract class YangNode
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053025 implements Cloneable, Serializable, YangDataNode {
Bharat saraswal96dfef02016-06-16 00:29:12 +053026
27 private static final long serialVersionUID = 806201601L;
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053028
Vinod Kumar S67e7be62016-02-11 20:13:28 +053029 /**
30 * Type of node.
31 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053032 private YangNodeType nodeType;
33
Vinod Kumar S67e7be62016-02-11 20:13:28 +053034 /**
35 * Parent reference.
36 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053037 private YangNode parent;
38
Vinod Kumar S67e7be62016-02-11 20:13:28 +053039 /**
40 * First child reference.
41 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053042 private YangNode child;
43
Vinod Kumar S67e7be62016-02-11 20:13:28 +053044 /**
45 * Next sibling reference.
46 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053047 private YangNode nextSibling;
48
Vinod Kumar S67e7be62016-02-11 20:13:28 +053049 /**
50 * Previous sibling reference.
51 */
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053052 private YangNode previousSibling;
53
54 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053055 * Returns the nodes name.
Vinod Kumar S38046502016-03-23 15:30:27 +053056 *
57 * @return nodes name
58 */
59 public abstract String getName();
60
61 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053062 * Sets the nodes name.
Vinod Kumar S38046502016-03-23 15:30:27 +053063 *
64 * @param name nodes name
65 */
66 public abstract void setName(String name);
67
68 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053069 * Creates a YANG node object.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053070 */
71 @SuppressWarnings("unused")
72 private YangNode() {
73
74 }
75
76 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053077 * Creates a specific type of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053078 *
79 * @param type of YANG node
80 */
81 protected YangNode(YangNodeType type) {
82 setNodeType(type);
83 }
84
85 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053086 * Returns the node type.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053087 *
88 * @return node type
89 */
90 public YangNodeType getNodeType() {
91 return nodeType;
92 }
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Sets the node type.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +053096 *
97 * @param nodeType type of node
98 */
99 private void setNodeType(YangNodeType nodeType) {
100 this.nodeType = nodeType;
101 }
102
103 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530104 * Returns the parent of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530105 *
106 * @return parent of node
107 */
108 public YangNode getParent() {
109 return parent;
110 }
111
112 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530113 * Sets the parent of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530114 *
115 * @param parent node
116 */
117 public void setParent(YangNode parent) {
118 this.parent = parent;
119 }
120
121 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530122 * Returns the first child of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530123 *
124 * @return first child of node
125 */
126 public YangNode getChild() {
127 return child;
128 }
129
130 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530131 * Sets the first instance of a child node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530132 *
133 * @param child is only child to be set
134 */
135 public void setChild(YangNode child) {
136 this.child = child;
137 }
138
139 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530140 * Returns the next sibling of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530141 *
142 * @return next sibling of node
143 */
144 public YangNode getNextSibling() {
145 return nextSibling;
146 }
147
148 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530149 * Sets the next sibling of node.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530150 *
151 * @param sibling YANG node
152 */
Vinod Kumar S427d2932016-04-20 13:02:58 +0530153 private void setNextSibling(YangNode sibling) {
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530154 nextSibling = sibling;
155 }
156
157 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530158 * Returns the previous sibling.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530159 *
160 * @return previous sibling node
161 */
162 public YangNode getPreviousSibling() {
163 return previousSibling;
164 }
165
166 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530167 * Sets the previous sibling.
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530168 *
169 * @param previousSibling points to predecessor sibling
170 */
Vinod Kumar S427d2932016-04-20 13:02:58 +0530171 private void setPreviousSibling(YangNode previousSibling) {
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530172 this.previousSibling = previousSibling;
173 }
174
175 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530176 * Adds a child node, the children sibling list will be sorted based on node
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530177 * type.
178 *
179 * @param newChild refers to a child to be added
180 * @throws DataModelException due to violation in data model rules
181 */
Vinod Kumar S427d2932016-04-20 13:02:58 +0530182 public void addChild(YangNode newChild)
183 throws DataModelException {
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530184 if (newChild.getNodeType() == null) {
185 throw new DataModelException("Abstract node cannot be inserted into a tree");
186 }
187
188 if (newChild.getParent() == null) {
189 newChild.setParent(this);
190 } else if (newChild.getParent() != this) {
191 throw new DataModelException("Node is already part of a tree");
192 }
193
194 if (newChild.getChild() != null) {
195 throw new DataModelException("Child to be added is not atomic, it already has a child");
196 }
197
198 if (newChild.getNextSibling() != null) {
199 throw new DataModelException("Child to be added is not atomic, it already has a next sibling");
200 }
201
202 if (newChild.getPreviousSibling() != null) {
203 throw new DataModelException("Child to be added is not atomic, it already has a previous sibling");
204 }
205
206 /* First child to be added */
207 if (getChild() == null) {
208 setChild(newChild);
209 return;
210 }
211
212 YangNode curNode;
213 curNode = getChild();
214
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530215 /*
216 * Get the predecessor child of new child
217 */
Vinod Kumar S427d2932016-04-20 13:02:58 +0530218 while (curNode.getNextSibling() != null) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530219
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530220 curNode = curNode.getNextSibling();
221 }
222
223 /* If the new node needs to be the last child */
224 if (curNode.getNextSibling() == null) {
225 curNode.setNextSibling(newChild);
226 newChild.setPreviousSibling(curNode);
Vinod Kumar S427d2932016-04-20 13:02:58 +0530227 }
228 }
Vinod Kumar S8c4e6492016-02-05 20:21:19 +0530229}