Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.yangutils.datamodel; |
| 17 | |
| 18 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 19 | |
| 20 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 21 | * Represents base class of a node in data model tree. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 22 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 23 | public abstract class YangNode { |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 24 | |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 25 | /** |
| 26 | * Type of node. |
| 27 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 28 | private YangNodeType nodeType; |
| 29 | |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 30 | /** |
| 31 | * Parent reference. |
| 32 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 33 | private YangNode parent; |
| 34 | |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 35 | /** |
| 36 | * First child reference. |
| 37 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 38 | private YangNode child; |
| 39 | |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 40 | /** |
| 41 | * Next sibling reference. |
| 42 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 43 | private YangNode nextSibling; |
| 44 | |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 45 | /** |
| 46 | * Previous sibling reference. |
| 47 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 48 | private YangNode previousSibling; |
| 49 | |
| 50 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 51 | * Returns the nodes name. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 52 | * |
| 53 | * @return nodes name |
| 54 | */ |
| 55 | public abstract String getName(); |
| 56 | |
| 57 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 58 | * Sets the nodes name. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 59 | * |
| 60 | * @param name nodes name |
| 61 | */ |
| 62 | public abstract void setName(String name); |
| 63 | |
| 64 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 65 | * Creates a YANG node object. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 66 | */ |
| 67 | @SuppressWarnings("unused") |
| 68 | private YangNode() { |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 73 | * Creates a specific type of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 74 | * |
| 75 | * @param type of YANG node |
| 76 | */ |
| 77 | protected YangNode(YangNodeType type) { |
| 78 | setNodeType(type); |
| 79 | } |
| 80 | |
| 81 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 82 | * Returns the node type. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 83 | * |
| 84 | * @return node type |
| 85 | */ |
| 86 | public YangNodeType getNodeType() { |
| 87 | return nodeType; |
| 88 | } |
| 89 | |
| 90 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 91 | * Sets the node type. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 92 | * |
| 93 | * @param nodeType type of node |
| 94 | */ |
| 95 | private void setNodeType(YangNodeType nodeType) { |
| 96 | this.nodeType = nodeType; |
| 97 | } |
| 98 | |
| 99 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 100 | * Returns the parent of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 101 | * |
| 102 | * @return parent of node |
| 103 | */ |
| 104 | public YangNode getParent() { |
| 105 | return parent; |
| 106 | } |
| 107 | |
| 108 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 109 | * Sets the parent of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 110 | * |
| 111 | * @param parent node |
| 112 | */ |
| 113 | public void setParent(YangNode parent) { |
| 114 | this.parent = parent; |
| 115 | } |
| 116 | |
| 117 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 118 | * Returns the first child of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 119 | * |
| 120 | * @return first child of node |
| 121 | */ |
| 122 | public YangNode getChild() { |
| 123 | return child; |
| 124 | } |
| 125 | |
| 126 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 127 | * Sets the first instance of a child node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 128 | * |
| 129 | * @param child is only child to be set |
| 130 | */ |
| 131 | public void setChild(YangNode child) { |
| 132 | this.child = child; |
| 133 | } |
| 134 | |
| 135 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 136 | * Returns the next sibling of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 137 | * |
| 138 | * @return next sibling of node |
| 139 | */ |
| 140 | public YangNode getNextSibling() { |
| 141 | return nextSibling; |
| 142 | } |
| 143 | |
| 144 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 145 | * Sets the next sibling of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 146 | * |
| 147 | * @param sibling YANG node |
| 148 | */ |
| 149 | public void setNextSibling(YangNode sibling) { |
| 150 | nextSibling = sibling; |
| 151 | } |
| 152 | |
| 153 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 154 | * Returns the previous sibling. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 155 | * |
| 156 | * @return previous sibling node |
| 157 | */ |
| 158 | public YangNode getPreviousSibling() { |
| 159 | return previousSibling; |
| 160 | } |
| 161 | |
| 162 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 163 | * Sets the previous sibling. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 164 | * |
| 165 | * @param previousSibling points to predecessor sibling |
| 166 | */ |
| 167 | public void setPreviousSibling(YangNode previousSibling) { |
| 168 | this.previousSibling = previousSibling; |
| 169 | } |
| 170 | |
| 171 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 172 | * Adds a child node, the children sibling list will be sorted based on node |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 173 | * 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 S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 178 | public void addChild(YangNode newChild) throws DataModelException { |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 179 | 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 210 | /*- |
| 211 | * If the new node needs to be the first child |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 212 | if (newChild.getNodeType().ordinal() < curNode.getNodeType().ordinal()) { |
| 213 | newChild.setNextSibling(curNode); |
| 214 | curNode.setPreviousSibling(newChild); |
| 215 | setChild(newChild); |
| 216 | return; |
| 217 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 218 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * Get the predecessor child of new child |
| 222 | */ |
| 223 | while (curNode.getNextSibling() != null |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 224 | /* |
| 225 | * && newChild.getNodeType().ordinal() >= |
| 226 | * curNode.getNextSibling().getNodeType().ordinal() |
| 227 | */) { |
| 228 | |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 229 | 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 239 | /*- |
| 240 | * Insert the new node in child node list sorted by type |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 241 | newChild.setNextSibling(curNode.getNextSibling()); |
| 242 | newChild.setPreviousSibling(curNode); |
| 243 | curNode.getNextSibling().setPreviousSibling(newChild); |
| 244 | curNode.setNextSibling(newChild); |
| 245 | return; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 246 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 247 | } |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 248 | } |