Mahesh Poojary Huawei | a330c58 | 2016-08-26 10:38:10 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Mahesh Poojary Huawei | a330c58 | 2016-08-26 10:38:10 +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 | |
| 17 | package org.onosproject.yms.ypm; |
| 18 | |
| 19 | /** |
| 20 | * Represents implementation of interfaces to build and obtain YANG protocol metadata tree node. |
| 21 | */ |
| 22 | public class DefaultYpmNode implements YpmContext { |
| 23 | |
| 24 | /** |
| 25 | * Name of the node. |
| 26 | */ |
| 27 | private String name; |
| 28 | |
| 29 | /** |
| 30 | * Parent reference. |
| 31 | */ |
| 32 | private DefaultYpmNode parent; |
| 33 | |
| 34 | /** |
| 35 | * First child reference. |
| 36 | */ |
| 37 | private DefaultYpmNode child; |
| 38 | |
| 39 | /** |
| 40 | * Next sibling reference. |
| 41 | */ |
| 42 | private DefaultYpmNode nextSibling; |
| 43 | |
| 44 | /** |
| 45 | * Previous sibling reference. |
| 46 | */ |
| 47 | private DefaultYpmNode previousSibling; |
| 48 | |
| 49 | /** |
| 50 | * Protocol metadata object. |
| 51 | */ |
| 52 | private Object metaData; |
| 53 | |
| 54 | /** |
| 55 | * Creates a specific type of node. |
| 56 | * |
| 57 | * @param name of ypm node |
| 58 | */ |
| 59 | public DefaultYpmNode(String name) { |
| 60 | setName(name); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public String getName() { |
| 65 | return name; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void setName(String name) { |
| 70 | this.name = name; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public DefaultYpmNode getParent() { |
| 75 | return parent; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void setParent(YpmContext parent) { |
| 80 | this.parent = (DefaultYpmNode) parent; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public YpmContext getFirstChild() { |
| 85 | return child; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public YpmContext getChild(String ypmName) { |
| 90 | if (ypmName == null) { |
| 91 | // Input is null. So, will not proceed. |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | if (child == null) { |
| 96 | // No children |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | // Check the first child |
| 101 | if (child.name.equals(ypmName)) { |
| 102 | return child; |
| 103 | } |
| 104 | |
| 105 | // Check its siblings |
| 106 | YpmContext currentChild = child; |
| 107 | while (currentChild.getNextSibling() != null) { |
| 108 | currentChild = currentChild.getNextSibling(); |
| 109 | if (currentChild.getName().equals(ypmName)) { |
| 110 | return currentChild; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void addChild(String ypmName) { |
| 119 | if (ypmName == null) { |
| 120 | // Input is null. So, will not proceed. |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | if (getChild(ypmName) != null) { |
| 125 | // Already available with the same name. So, no need to add |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // Create new ypm node and attach to its parent |
| 130 | DefaultYpmNode newNode = new DefaultYpmNode(ypmName); |
| 131 | newNode.setParent(this); |
| 132 | |
| 133 | // Check the first child |
| 134 | if (child == null) { |
| 135 | child = newNode; |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | // Add it to its siblings |
| 140 | YpmContext currentChild = child; |
| 141 | // Go to the last child |
| 142 | while (currentChild.getNextSibling() != null) { |
| 143 | currentChild = currentChild.getNextSibling(); |
| 144 | } |
| 145 | currentChild.setNextSibling(newNode); |
| 146 | newNode.setPreviousSibling((DefaultYpmNode) currentChild); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public YpmContext getSibling(String ypmName) { |
| 151 | if (ypmName == null) { |
| 152 | // Input is null. So, will not proceed. |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | YpmContext sibling = getNextSibling(); |
| 157 | while (sibling != null) { |
| 158 | if (sibling.getName().equals(ypmName)) { |
| 159 | return sibling; |
| 160 | } |
| 161 | sibling = sibling.getNextSibling(); |
| 162 | } |
| 163 | return null; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void addSibling(String ypmName) { |
| 168 | if (ypmName == null) { |
| 169 | // Input is null. So, will not proceed. |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if (getSibling(ypmName) != null) { |
| 174 | // Already available with the same name. So, no need to add |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // Create new ypm node and attach to its parent |
| 179 | DefaultYpmNode newSibling = new DefaultYpmNode(ypmName); |
| 180 | newSibling.setParent(this.getParent()); |
| 181 | |
| 182 | // Add it as its sibling |
| 183 | YpmContext sibling = getNextSibling(); |
| 184 | if (sibling == null) { |
| 185 | setNextSibling(newSibling); |
| 186 | newSibling.setPreviousSibling(this); |
| 187 | } else { |
| 188 | // Go to the last sibling |
| 189 | while (sibling.getNextSibling() != null) { |
| 190 | sibling = sibling.getNextSibling(); |
| 191 | } |
| 192 | sibling.setNextSibling(newSibling); |
| 193 | newSibling.setPreviousSibling((DefaultYpmNode) sibling); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public DefaultYpmNode getNextSibling() { |
| 199 | return nextSibling; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void setNextSibling(DefaultYpmNode sibling) { |
| 204 | nextSibling = sibling; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public DefaultYpmNode getPreviousSibling() { |
| 209 | return previousSibling; |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void setPreviousSibling(DefaultYpmNode sibling) { |
| 214 | this.previousSibling = sibling; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public Object getMetaData() { |
| 219 | return this.metaData; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public void setMetaData(Object data) { |
| 224 | this.metaData = data; |
| 225 | } |
| 226 | } |