Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 8c4e649 | 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 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 18 | import java.io.Serializable; |
| 19 | |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 21 | |
| 22 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 23 | * Represents base class of a node in data model tree. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 24 | */ |
Vinod Kumar S | 427d293 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 25 | public abstract class YangNode |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 26 | implements Cloneable, Serializable { |
| 27 | |
| 28 | private static final long serialVersionUID = 806201601L; |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 29 | |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 30 | /** |
| 31 | * Type of node. |
| 32 | */ |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 33 | private YangNodeType nodeType; |
| 34 | |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 35 | /** |
| 36 | * Parent reference. |
| 37 | */ |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 38 | private YangNode parent; |
| 39 | |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 40 | /** |
| 41 | * First child reference. |
| 42 | */ |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 43 | private YangNode child; |
| 44 | |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 45 | /** |
| 46 | * Next sibling reference. |
| 47 | */ |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 48 | private YangNode nextSibling; |
| 49 | |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 50 | /** |
| 51 | * Previous sibling reference. |
| 52 | */ |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 53 | private YangNode previousSibling; |
| 54 | |
| 55 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 56 | * Returns the nodes name. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 57 | * |
| 58 | * @return nodes name |
| 59 | */ |
| 60 | public abstract String getName(); |
| 61 | |
| 62 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 63 | * Sets the nodes name. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 64 | * |
| 65 | * @param name nodes name |
| 66 | */ |
| 67 | public abstract void setName(String name); |
| 68 | |
| 69 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 70 | * Creates a YANG node object. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 71 | */ |
| 72 | @SuppressWarnings("unused") |
| 73 | private YangNode() { |
| 74 | |
| 75 | } |
| 76 | |
| 77 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 78 | * Creates a specific type of node. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 79 | * |
| 80 | * @param type of YANG node |
| 81 | */ |
| 82 | protected YangNode(YangNodeType type) { |
| 83 | setNodeType(type); |
| 84 | } |
| 85 | |
| 86 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 87 | * Returns the node type. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 88 | * |
| 89 | * @return node type |
| 90 | */ |
| 91 | public YangNodeType getNodeType() { |
| 92 | return nodeType; |
| 93 | } |
| 94 | |
| 95 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 96 | * Sets the node type. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 97 | * |
| 98 | * @param nodeType type of node |
| 99 | */ |
| 100 | private void setNodeType(YangNodeType nodeType) { |
| 101 | this.nodeType = nodeType; |
| 102 | } |
| 103 | |
| 104 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 105 | * Returns the parent of node. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 106 | * |
| 107 | * @return parent of node |
| 108 | */ |
| 109 | public YangNode getParent() { |
| 110 | return parent; |
| 111 | } |
| 112 | |
| 113 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 114 | * Sets the parent of node. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 115 | * |
| 116 | * @param parent node |
| 117 | */ |
| 118 | public void setParent(YangNode parent) { |
| 119 | this.parent = parent; |
| 120 | } |
| 121 | |
| 122 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 123 | * Returns the first child of node. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 124 | * |
| 125 | * @return first child of node |
| 126 | */ |
| 127 | public YangNode getChild() { |
| 128 | return child; |
| 129 | } |
| 130 | |
| 131 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 132 | * Sets the first instance of a child node. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 133 | * |
| 134 | * @param child is only child to be set |
| 135 | */ |
| 136 | public void setChild(YangNode child) { |
| 137 | this.child = child; |
| 138 | } |
| 139 | |
| 140 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 141 | * Returns the next sibling of node. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 142 | * |
| 143 | * @return next sibling of node |
| 144 | */ |
| 145 | public YangNode getNextSibling() { |
| 146 | return nextSibling; |
| 147 | } |
| 148 | |
| 149 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 150 | * Sets the next sibling of node. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 151 | * |
| 152 | * @param sibling YANG node |
| 153 | */ |
Vinod Kumar S | 427d293 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 154 | private void setNextSibling(YangNode sibling) { |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 155 | nextSibling = sibling; |
| 156 | } |
| 157 | |
| 158 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 159 | * Returns the previous sibling. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 160 | * |
| 161 | * @return previous sibling node |
| 162 | */ |
| 163 | public YangNode getPreviousSibling() { |
| 164 | return previousSibling; |
| 165 | } |
| 166 | |
| 167 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 168 | * Sets the previous sibling. |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 169 | * |
| 170 | * @param previousSibling points to predecessor sibling |
| 171 | */ |
Vinod Kumar S | 427d293 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 172 | private void setPreviousSibling(YangNode previousSibling) { |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 173 | this.previousSibling = previousSibling; |
| 174 | } |
| 175 | |
| 176 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 177 | * Adds a child node, the children sibling list will be sorted based on node |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 178 | * type. |
| 179 | * |
| 180 | * @param newChild refers to a child to be added |
| 181 | * @throws DataModelException due to violation in data model rules |
| 182 | */ |
Vinod Kumar S | 427d293 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 183 | public void addChild(YangNode newChild) |
| 184 | throws DataModelException { |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 185 | if (newChild.getNodeType() == null) { |
| 186 | throw new DataModelException("Abstract node cannot be inserted into a tree"); |
| 187 | } |
| 188 | |
| 189 | if (newChild.getParent() == null) { |
| 190 | newChild.setParent(this); |
| 191 | } else if (newChild.getParent() != this) { |
| 192 | throw new DataModelException("Node is already part of a tree"); |
| 193 | } |
| 194 | |
| 195 | if (newChild.getChild() != null) { |
| 196 | throw new DataModelException("Child to be added is not atomic, it already has a child"); |
| 197 | } |
| 198 | |
| 199 | if (newChild.getNextSibling() != null) { |
| 200 | throw new DataModelException("Child to be added is not atomic, it already has a next sibling"); |
| 201 | } |
| 202 | |
| 203 | if (newChild.getPreviousSibling() != null) { |
| 204 | throw new DataModelException("Child to be added is not atomic, it already has a previous sibling"); |
| 205 | } |
| 206 | |
| 207 | /* First child to be added */ |
| 208 | if (getChild() == null) { |
| 209 | setChild(newChild); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | YangNode curNode; |
| 214 | curNode = getChild(); |
| 215 | |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 216 | /* |
| 217 | * Get the predecessor child of new child |
| 218 | */ |
Vinod Kumar S | 427d293 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 219 | while (curNode.getNextSibling() != null) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 220 | |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 221 | curNode = curNode.getNextSibling(); |
| 222 | } |
| 223 | |
| 224 | /* If the new node needs to be the last child */ |
| 225 | if (curNode.getNextSibling() == null) { |
| 226 | curNode.setNextSibling(newChild); |
| 227 | newChild.setPreviousSibling(curNode); |
Vinod Kumar S | 427d293 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 228 | } |
| 229 | } |
Vinod Kumar S | 8c4e649 | 2016-02-05 20:21:19 +0530 | [diff] [blame] | 230 | } |