blob: f9d5b2680dd0239e7ea336ad1297e6b6c9bf9aab [file] [log] [blame]
Vinod Kumar S13139e92016-02-05 20:21:19 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S13139e92016-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 saraswal63f26fb2016-04-05 15:13:44 +053021 * Represents base class of a node in data model tree.
Vinod Kumar S13139e92016-02-05 20:21:19 +053022 */
Vinod Kumar S9aacac52016-04-20 13:02:58 +053023public abstract class YangNode
24 implements Cloneable {
Vinod Kumar S13139e92016-02-05 20:21:19 +053025
Vinod Kumar S6a6ce4c2016-02-11 20:13:28 +053026 /**
27 * Type of node.
28 */
Vinod Kumar S13139e92016-02-05 20:21:19 +053029 private YangNodeType nodeType;
30
Vinod Kumar S6a6ce4c2016-02-11 20:13:28 +053031 /**
32 * Parent reference.
33 */
Vinod Kumar S13139e92016-02-05 20:21:19 +053034 private YangNode parent;
35
Vinod Kumar S6a6ce4c2016-02-11 20:13:28 +053036 /**
37 * First child reference.
38 */
Vinod Kumar S13139e92016-02-05 20:21:19 +053039 private YangNode child;
40
Vinod Kumar S6a6ce4c2016-02-11 20:13:28 +053041 /**
42 * Next sibling reference.
43 */
Vinod Kumar S13139e92016-02-05 20:21:19 +053044 private YangNode nextSibling;
45
Vinod Kumar S6a6ce4c2016-02-11 20:13:28 +053046 /**
47 * Previous sibling reference.
48 */
Vinod Kumar S13139e92016-02-05 20:21:19 +053049 private YangNode previousSibling;
50
51 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053052 * Returns the nodes name.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053053 *
54 * @return nodes name
55 */
56 public abstract String getName();
57
58 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053059 * Sets the nodes name.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053060 *
61 * @param name nodes name
62 */
63 public abstract void setName(String name);
64
65 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053066 * Creates a YANG node object.
Vinod Kumar S13139e92016-02-05 20:21:19 +053067 */
68 @SuppressWarnings("unused")
69 private YangNode() {
70
71 }
72
73 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053074 * Creates a specific type of node.
Vinod Kumar S13139e92016-02-05 20:21:19 +053075 *
76 * @param type of YANG node
77 */
78 protected YangNode(YangNodeType type) {
79 setNodeType(type);
80 }
81
82 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053083 * Returns the node type.
Vinod Kumar S13139e92016-02-05 20:21:19 +053084 *
85 * @return node type
86 */
87 public YangNodeType getNodeType() {
88 return nodeType;
89 }
90
91 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053092 * Sets the node type.
Vinod Kumar S13139e92016-02-05 20:21:19 +053093 *
94 * @param nodeType type of node
95 */
96 private void setNodeType(YangNodeType nodeType) {
97 this.nodeType = nodeType;
98 }
99
100 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530101 * Returns the parent of node.
Vinod Kumar S13139e92016-02-05 20:21:19 +0530102 *
103 * @return parent of node
104 */
105 public YangNode getParent() {
106 return parent;
107 }
108
109 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530110 * Sets the parent of node.
Vinod Kumar S13139e92016-02-05 20:21:19 +0530111 *
112 * @param parent node
113 */
114 public void setParent(YangNode parent) {
115 this.parent = parent;
116 }
117
118 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530119 * Returns the first child of node.
Vinod Kumar S13139e92016-02-05 20:21:19 +0530120 *
121 * @return first child of node
122 */
123 public YangNode getChild() {
124 return child;
125 }
126
127 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530128 * Sets the first instance of a child node.
Vinod Kumar S13139e92016-02-05 20:21:19 +0530129 *
130 * @param child is only child to be set
131 */
132 public void setChild(YangNode child) {
133 this.child = child;
134 }
135
136 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530137 * Returns the next sibling of node.
Vinod Kumar S13139e92016-02-05 20:21:19 +0530138 *
139 * @return next sibling of node
140 */
141 public YangNode getNextSibling() {
142 return nextSibling;
143 }
144
145 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530146 * Sets the next sibling of node.
Vinod Kumar S13139e92016-02-05 20:21:19 +0530147 *
148 * @param sibling YANG node
149 */
Vinod Kumar S9aacac52016-04-20 13:02:58 +0530150 private void setNextSibling(YangNode sibling) {
Vinod Kumar S13139e92016-02-05 20:21:19 +0530151 nextSibling = sibling;
152 }
153
154 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530155 * Returns the previous sibling.
Vinod Kumar S13139e92016-02-05 20:21:19 +0530156 *
157 * @return previous sibling node
158 */
159 public YangNode getPreviousSibling() {
160 return previousSibling;
161 }
162
163 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530164 * Sets the previous sibling.
Vinod Kumar S13139e92016-02-05 20:21:19 +0530165 *
166 * @param previousSibling points to predecessor sibling
167 */
Vinod Kumar S9aacac52016-04-20 13:02:58 +0530168 private void setPreviousSibling(YangNode previousSibling) {
Vinod Kumar S13139e92016-02-05 20:21:19 +0530169 this.previousSibling = previousSibling;
170 }
171
172 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530173 * Adds a child node, the children sibling list will be sorted based on node
Vinod Kumar S13139e92016-02-05 20:21:19 +0530174 * type.
175 *
176 * @param newChild refers to a child to be added
177 * @throws DataModelException due to violation in data model rules
178 */
Vinod Kumar S9aacac52016-04-20 13:02:58 +0530179 public void addChild(YangNode newChild)
180 throws DataModelException {
Vinod Kumar S13139e92016-02-05 20:21:19 +0530181 if (newChild.getNodeType() == null) {
182 throw new DataModelException("Abstract node cannot be inserted into a tree");
183 }
184
185 if (newChild.getParent() == null) {
186 newChild.setParent(this);
187 } else if (newChild.getParent() != this) {
188 throw new DataModelException("Node is already part of a tree");
189 }
190
191 if (newChild.getChild() != null) {
192 throw new DataModelException("Child to be added is not atomic, it already has a child");
193 }
194
195 if (newChild.getNextSibling() != null) {
196 throw new DataModelException("Child to be added is not atomic, it already has a next sibling");
197 }
198
199 if (newChild.getPreviousSibling() != null) {
200 throw new DataModelException("Child to be added is not atomic, it already has a previous sibling");
201 }
202
203 /* First child to be added */
204 if (getChild() == null) {
205 setChild(newChild);
206 return;
207 }
208
209 YangNode curNode;
210 curNode = getChild();
211
Vinod Kumar S13139e92016-02-05 20:21:19 +0530212 /*
213 * Get the predecessor child of new child
214 */
Vinod Kumar S9aacac52016-04-20 13:02:58 +0530215 while (curNode.getNextSibling() != null) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530216
Vinod Kumar S13139e92016-02-05 20:21:19 +0530217 curNode = curNode.getNextSibling();
218 }
219
220 /* If the new node needs to be the last child */
221 if (curNode.getNextSibling() == null) {
222 curNode.setNextSibling(newChild);
223 newChild.setPreviousSibling(curNode);
Vinod Kumar S9aacac52016-04-20 13:02:58 +0530224 }
225 }
Vinod Kumar S13139e92016-02-05 20:21:19 +0530226}