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