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 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 17 | package org.onosproject.yms.app.ypm; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
| 20 | import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier; |
| 21 | import org.onosproject.yms.app.ydt.AppType; |
| 22 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 23 | import org.onosproject.yms.app.ydt.YdtNode; |
| 24 | import org.onosproject.yms.ydt.YdtContext; |
| 25 | import org.onosproject.yms.ydt.YdtContextOperationType; |
| 26 | import org.onosproject.yms.ydt.YdtExtendedInfoType; |
| 27 | import org.onosproject.yms.ydt.YdtType; |
Mahesh Poojary Huawei | a330c58 | 2016-08-26 10:38:10 +0530 | [diff] [blame] | 28 | |
| 29 | import java.util.HashMap; |
| 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
Mahesh Poojary Huawei | a330c58 | 2016-08-26 10:38:10 +0530 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Represents implementation of interfaces to build and obtain YANG data tree. |
| 36 | */ |
| 37 | public class YdtNodeAdapter<T> implements YdtExtendedContext { |
| 38 | |
| 39 | /** |
| 40 | * Parent reference. |
| 41 | */ |
| 42 | private YdtNodeAdapter parent; |
| 43 | |
| 44 | /** |
| 45 | * First child reference. |
| 46 | */ |
| 47 | private YdtNodeAdapter child; |
| 48 | |
| 49 | /** |
| 50 | * Next sibling reference. |
| 51 | */ |
| 52 | private YdtNodeAdapter nextSibling; |
| 53 | |
| 54 | /** |
| 55 | * Previous sibling reference. |
| 56 | */ |
| 57 | private YdtNodeAdapter previousSibling; |
| 58 | |
| 59 | /** |
| 60 | * Last child reference. |
| 61 | */ |
| 62 | private YdtNode lastChild; |
| 63 | |
| 64 | /** |
| 65 | * Type of node. |
| 66 | */ |
| 67 | private YdtType ydtType; |
| 68 | |
| 69 | /** |
| 70 | * Flag to keep the track of context switch. |
| 71 | */ |
| 72 | private boolean isContextSwitch; |
| 73 | |
| 74 | private T ydtExtendedInfo; |
| 75 | |
| 76 | /** |
| 77 | * YDT extended information type. |
| 78 | */ |
| 79 | YdtExtendedInfoType ydtExtendedInfoType; |
| 80 | |
| 81 | /** |
| 82 | * Ydt map to keep the track of node added in YDT. |
| 83 | */ |
| 84 | private Map<YangSchemaNodeIdentifier, List<YdtNode<T>>> ydtNodeMap = new HashMap<>(); |
| 85 | |
| 86 | /** |
| 87 | * Reference for data-model schema node. |
| 88 | */ |
| 89 | private YangSchemaNode yangSchemaNode; |
| 90 | |
| 91 | /** |
| 92 | * Reference for ydt node operation type. |
| 93 | */ |
| 94 | private YdtContextOperationType ydtContextOperationType; |
| 95 | |
| 96 | /** |
| 97 | * Key object for ydtNodeMap. |
| 98 | */ |
| 99 | protected YangSchemaNodeIdentifier nodeIdentifier; |
| 100 | |
| 101 | /** |
| 102 | * Ydt map to keep the track of application information object with respective type. |
| 103 | */ |
| 104 | Map<AppType, Object> ydtAppInfoMap = new HashMap<>(); |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Creation of YANG node object. |
| 109 | */ |
| 110 | public YdtNodeAdapter() { |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Creates a specific type of node. |
| 115 | * |
| 116 | * @param type of YDT node |
| 117 | * @param name name of the YDT node |
| 118 | */ |
| 119 | public YdtNodeAdapter(YdtType type, String name) { |
| 120 | setYdtType(type); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public String getName() { |
| 125 | return this.nodeIdentifier.getName(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String getNamespace() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 130 | return yangSchemaNode.getNameSpace().getModuleNamespace(); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public String getModuleNameAsNameSpace() { |
| 135 | return null; |
Mahesh Poojary Huawei | a330c58 | 2016-08-26 10:38:10 +0530 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public <T> T getYdtContextExtendedInfo() { |
| 140 | return (T) ydtExtendedInfo; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public YdtExtendedInfoType getYdtExtendedInfoType() { |
| 145 | return ydtExtendedInfoType; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public YdtType getYdtType() { |
| 150 | return ydtType; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Sets the node type. |
| 155 | * |
| 156 | * @param ydtType type of YDT attribute |
| 157 | */ |
| 158 | public void setYdtType(YdtType ydtType) { |
| 159 | this.ydtType = ydtType; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public YdtNodeAdapter getParent() { |
| 164 | return parent; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Sets the parent of node. |
| 169 | * |
| 170 | * @param parent node |
| 171 | */ |
| 172 | public void setParent(YdtNodeAdapter parent) { |
| 173 | this.parent = parent; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public YdtNodeAdapter getFirstChild() { |
| 178 | return child; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public YdtNodeAdapter getNextSibling() { |
| 183 | return nextSibling; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Sets the next sibling of node. |
| 188 | * |
| 189 | * @param sibling YANG node |
| 190 | */ |
| 191 | public void setNextSibling(YdtNodeAdapter sibling) { |
| 192 | nextSibling = sibling; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public YdtNodeAdapter getPreviousSibling() { |
| 197 | return previousSibling; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Sets the previous sibling. |
| 202 | * |
| 203 | * @param previousSibling points to predecessor sibling |
| 204 | */ |
| 205 | public void setPreviousSibling(YdtNodeAdapter previousSibling) { |
| 206 | this.previousSibling = previousSibling; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns data-model node reference for of a given node. |
| 211 | * |
| 212 | * @return yang schema data node of a data-model. |
| 213 | */ |
| 214 | public YangSchemaNode getYangSchemaNode() { |
| 215 | return yangSchemaNode; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Sets the data-model node reference for of a given node.. |
| 220 | * |
| 221 | * @param yangSchemaNode YANG data node. |
| 222 | */ |
| 223 | public void setYangSchemaNode(YangSchemaNode yangSchemaNode) { |
| 224 | this.yangSchemaNode = yangSchemaNode; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public YdtNode getLastChild() { |
| 229 | return lastChild; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Sets the last instance of a child node. |
| 234 | * |
| 235 | * @param child is last child to be set |
| 236 | */ |
| 237 | public void setLastChild(YdtNode child) { |
| 238 | this.lastChild = child; |
| 239 | } |
| 240 | |
| 241 | public void setNodeIdentifier(YangSchemaNodeIdentifier nodeIdentifier) { |
| 242 | this.nodeIdentifier = nodeIdentifier; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Adds a child node. |
| 247 | * |
| 248 | * @param newChild refers to a child to be added |
| 249 | */ |
| 250 | public void addChild(YdtContext newChild) { |
| 251 | |
| 252 | ((YdtNodeAdapter) newChild).setParent(this); |
| 253 | |
| 254 | if (this.child == null) { |
| 255 | this.child = (YdtNodeAdapter) newChild; |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | YdtNodeAdapter currNode = this.child; |
| 260 | while (currNode.getNextSibling() != null) { |
| 261 | currNode = currNode.getNextSibling(); |
| 262 | } |
| 263 | currNode.setNextSibling((YdtNodeAdapter) newChild); |
| 264 | ((YdtNodeAdapter) newChild).setPreviousSibling(currNode); |
| 265 | } |
| 266 | |
Mahesh Poojary Huawei | a330c58 | 2016-08-26 10:38:10 +0530 | [diff] [blame] | 267 | /** |
| 268 | * Adds a sibling to YANG data tree. |
| 269 | * |
| 270 | * @param newSibling context of sibling to be added |
| 271 | */ |
| 272 | public void addSibling(YdtContext newSibling) { |
| 273 | |
| 274 | ((YdtNodeAdapter) newSibling).setParent(this.getParent()); |
| 275 | |
| 276 | YdtNodeAdapter currNode = this; |
| 277 | |
| 278 | while (currNode.getNextSibling() != null) { |
| 279 | currNode = currNode.getNextSibling(); |
| 280 | } |
| 281 | currNode.setNextSibling((YdtNodeAdapter) newSibling); |
| 282 | ((YdtNodeAdapter) newSibling).setPreviousSibling(currNode); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Gets the flag for node if context switch. |
| 287 | * |
| 288 | * @return isContextSwitch flag of a node. |
| 289 | */ |
| 290 | public boolean getContextSwitch() { |
| 291 | return isContextSwitch; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Sets the flag to keep the track of context switch. |
| 296 | * |
| 297 | * @param contextSwitch boolean flag. |
| 298 | */ |
| 299 | public void setContextSwitch(boolean contextSwitch) { |
| 300 | isContextSwitch = contextSwitch; |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public String getValue() { |
| 305 | return null; |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public Set<String> getValueSet() { |
| 310 | return null; |
| 311 | } |
| 312 | |
| 313 | @Override |
Mahesh Poojary Huawei | a330c58 | 2016-08-26 10:38:10 +0530 | [diff] [blame] | 314 | public Object getAppInfo(AppType appType) { |
| 315 | return null; |
| 316 | } |
| 317 | |
| 318 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 319 | public void addAppInfo(AppType appType, Object object) { |
| 320 | |
Mahesh Poojary Huawei | a330c58 | 2016-08-26 10:38:10 +0530 | [diff] [blame] | 321 | } |
Ray Milkey | b2fdb74 | 2016-11-23 10:12:42 -0800 | [diff] [blame] | 322 | |
| 323 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 324 | public YdtContextOperationType getYdtContextOperationType() { |
| 325 | return ydtContextOperationType; |
Ray Milkey | b2fdb74 | 2016-11-23 10:12:42 -0800 | [diff] [blame] | 326 | } |
Mahesh Poojary Huawei | a330c58 | 2016-08-26 10:38:10 +0530 | [diff] [blame] | 327 | } |