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 | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 23 | public abstract class YangNode |
| 24 | implements Cloneable { |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 25 | |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 26 | /** |
| 27 | * Type of node. |
| 28 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 29 | private YangNodeType nodeType; |
| 30 | |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 31 | /** |
| 32 | * Parent reference. |
| 33 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 34 | private YangNode parent; |
| 35 | |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 36 | /** |
| 37 | * First child reference. |
| 38 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 39 | private YangNode child; |
| 40 | |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 41 | /** |
| 42 | * Next sibling reference. |
| 43 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 44 | private YangNode nextSibling; |
| 45 | |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 46 | /** |
| 47 | * Previous sibling reference. |
| 48 | */ |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 49 | private YangNode previousSibling; |
| 50 | |
| 51 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 52 | * Returns the nodes name. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 53 | * |
| 54 | * @return nodes name |
| 55 | */ |
| 56 | public abstract String getName(); |
| 57 | |
| 58 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 59 | * Sets the nodes name. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 60 | * |
| 61 | * @param name nodes name |
| 62 | */ |
| 63 | public abstract void setName(String name); |
| 64 | |
| 65 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 66 | * Creates a YANG node object. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 67 | */ |
| 68 | @SuppressWarnings("unused") |
| 69 | private YangNode() { |
| 70 | |
| 71 | } |
| 72 | |
| 73 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 74 | * Creates a specific type of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 75 | * |
| 76 | * @param type of YANG node |
| 77 | */ |
| 78 | protected YangNode(YangNodeType type) { |
| 79 | setNodeType(type); |
| 80 | } |
| 81 | |
| 82 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 83 | * Returns the node type. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 84 | * |
| 85 | * @return node type |
| 86 | */ |
| 87 | public YangNodeType getNodeType() { |
| 88 | return nodeType; |
| 89 | } |
| 90 | |
| 91 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 92 | * Sets the node type. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 93 | * |
| 94 | * @param nodeType type of node |
| 95 | */ |
| 96 | private void setNodeType(YangNodeType nodeType) { |
| 97 | this.nodeType = nodeType; |
| 98 | } |
| 99 | |
| 100 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 101 | * Returns the parent of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 102 | * |
| 103 | * @return parent of node |
| 104 | */ |
| 105 | public YangNode getParent() { |
| 106 | return parent; |
| 107 | } |
| 108 | |
| 109 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 110 | * Sets the parent of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 111 | * |
| 112 | * @param parent node |
| 113 | */ |
| 114 | public void setParent(YangNode parent) { |
| 115 | this.parent = parent; |
| 116 | } |
| 117 | |
| 118 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 119 | * Returns the first child of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 120 | * |
| 121 | * @return first child of node |
| 122 | */ |
| 123 | public YangNode getChild() { |
| 124 | return child; |
| 125 | } |
| 126 | |
| 127 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 128 | * Sets the first instance of a child node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 129 | * |
| 130 | * @param child is only child to be set |
| 131 | */ |
| 132 | public void setChild(YangNode child) { |
| 133 | this.child = child; |
| 134 | } |
| 135 | |
| 136 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 137 | * Returns the next sibling of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 138 | * |
| 139 | * @return next sibling of node |
| 140 | */ |
| 141 | public YangNode getNextSibling() { |
| 142 | return nextSibling; |
| 143 | } |
| 144 | |
| 145 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 146 | * Sets the next sibling of node. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 147 | * |
| 148 | * @param sibling YANG node |
| 149 | */ |
Vinod Kumar S | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 150 | private void setNextSibling(YangNode sibling) { |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 151 | nextSibling = sibling; |
| 152 | } |
| 153 | |
| 154 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 155 | * Returns the previous sibling. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 156 | * |
| 157 | * @return previous sibling node |
| 158 | */ |
| 159 | public YangNode getPreviousSibling() { |
| 160 | return previousSibling; |
| 161 | } |
| 162 | |
| 163 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 164 | * Sets the previous sibling. |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 165 | * |
| 166 | * @param previousSibling points to predecessor sibling |
| 167 | */ |
Vinod Kumar S | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 168 | private void setPreviousSibling(YangNode previousSibling) { |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 169 | this.previousSibling = previousSibling; |
| 170 | } |
| 171 | |
| 172 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 173 | * 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] | 174 | * 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 S | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 179 | public void addChild(YangNode newChild) |
| 180 | throws DataModelException { |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 181 | 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 S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 212 | /* |
| 213 | * Get the predecessor child of new child |
| 214 | */ |
Vinod Kumar S | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 215 | while (curNode.getNextSibling() != null) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 216 | |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 217 | 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 S | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 224 | } |
| 225 | } |
Vinod Kumar S | 13139e9 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 226 | } |