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 | /** |
| 21 | * Base class of a node in data model tree. |
| 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 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 51 | * Get the nodes name. |
| 52 | * |
| 53 | * @return nodes name |
| 54 | */ |
| 55 | public abstract String getName(); |
| 56 | |
| 57 | /** |
| 58 | * Set the nodes name. |
| 59 | * |
| 60 | * @param name nodes name |
| 61 | */ |
| 62 | public abstract void setName(String name); |
| 63 | |
| 64 | /** |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 65 | * Default constructor is made private to ensure node type is always set. |
| 66 | */ |
| 67 | @SuppressWarnings("unused") |
| 68 | private YangNode() { |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Create a specific type of node. |
| 74 | * |
| 75 | * @param type of YANG node |
| 76 | */ |
| 77 | protected YangNode(YangNodeType type) { |
| 78 | setNodeType(type); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the node type. |
| 83 | * |
| 84 | * @return node type |
| 85 | */ |
| 86 | public YangNodeType getNodeType() { |
| 87 | return nodeType; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set the node type. |
| 92 | * |
| 93 | * @param nodeType type of node |
| 94 | */ |
| 95 | private void setNodeType(YangNodeType nodeType) { |
| 96 | this.nodeType = nodeType; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the parent of node. |
| 101 | * |
| 102 | * @return parent of node |
| 103 | */ |
| 104 | public YangNode getParent() { |
| 105 | return parent; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Set the parent of node. |
| 110 | * |
| 111 | * @param parent node |
| 112 | */ |
| 113 | public void setParent(YangNode parent) { |
| 114 | this.parent = parent; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get the first child of node. |
| 119 | * |
| 120 | * @return first child of node |
| 121 | */ |
| 122 | public YangNode getChild() { |
| 123 | return child; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Set the first instance of a child node. |
| 128 | * |
| 129 | * @param child is only child to be set |
| 130 | */ |
| 131 | public void setChild(YangNode child) { |
| 132 | this.child = child; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get the next sibling of node. |
| 137 | * |
| 138 | * @return next sibling of node |
| 139 | */ |
| 140 | public YangNode getNextSibling() { |
| 141 | return nextSibling; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Set the next sibling of node. |
| 146 | * |
| 147 | * @param sibling YANG node |
| 148 | */ |
| 149 | public void setNextSibling(YangNode sibling) { |
| 150 | nextSibling = sibling; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get the previous sibling. |
| 155 | * |
| 156 | * @return previous sibling node |
| 157 | */ |
| 158 | public YangNode getPreviousSibling() { |
| 159 | return previousSibling; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Set the previous sibling. |
| 164 | * |
| 165 | * @param previousSibling points to predecessor sibling |
| 166 | */ |
| 167 | public void setPreviousSibling(YangNode previousSibling) { |
| 168 | this.previousSibling = previousSibling; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Add a child node, the children sibling list will be sorted based on node |
| 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 | } |