sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +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.app.ydt; |
| 18 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 20 | import org.onosproject.yms.ydt.YdtContext; |
| 21 | import org.onosproject.yms.ydt.YdtContextOperationType; |
| 22 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 23 | import java.util.List; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 24 | |
| 25 | import static org.onosproject.yms.app.ydt.YdtAppNodeOperationType.BOTH; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 26 | import static org.onosproject.yms.app.ydt.YdtUtils.getAppOpTypeFromYdtOpType; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 27 | |
| 28 | /** |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 29 | * Abstraction of an entity which represents YDT application context |
| 30 | * information. This context information will be used by protocol to obtain |
| 31 | * the information associated with YDT application tree node. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 32 | */ |
| 33 | public final class DefaultYdtAppContext<T extends AppData> |
| 34 | implements YdtAppContext { |
| 35 | |
| 36 | /* |
| 37 | * Parent reference. |
| 38 | */ |
| 39 | private YdtAppContext parent; |
| 40 | |
| 41 | /* |
| 42 | * First child reference. |
| 43 | */ |
| 44 | private YdtAppContext child; |
| 45 | |
| 46 | /* |
| 47 | * Next sibling reference. |
| 48 | */ |
| 49 | private YdtAppContext nextSibling; |
| 50 | |
| 51 | /* |
| 52 | * Previous sibling reference. |
| 53 | */ |
| 54 | private YdtAppContext previousSibling; |
| 55 | |
| 56 | /* |
| 57 | * Last child reference. |
| 58 | */ |
| 59 | private YdtAppContext lastChild; |
| 60 | |
| 61 | /* |
| 62 | * YDT application tree extended information. |
| 63 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 64 | private final T appData; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Reference for operation type for application root node. |
| 68 | */ |
| 69 | private YdtAppNodeOperationType operationType; |
| 70 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 71 | /** |
| 72 | * Creates an instance of YANG application tree which is used by all node |
| 73 | * needs delete list. |
Bharat saraswal | 1f37198 | 2016-11-23 15:32:57 +0530 | [diff] [blame] | 74 | * |
| 75 | * @param data application data |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 76 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 77 | DefaultYdtAppContext(T data) { |
| 78 | appData = data; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void updateAppOperationType(YdtContextOperationType ydtOpType) { |
| 83 | if (parent == null) { |
| 84 | return; |
| 85 | } |
| 86 | YdtAppNodeOperationType opType = getAppOpTypeFromYdtOpType(ydtOpType); |
| 87 | YdtAppContext curNode = this; |
| 88 | YdtAppNodeOperationType parentOpType = operationType; |
Sonu Gupta | 1f418aa | 2017-02-23 20:48:46 +0530 | [diff] [blame] | 89 | if (opType != parentOpType) { |
| 90 | if (parentOpType != null) { |
| 91 | while (curNode.getOperationType() != BOTH && |
| 92 | curNode.getParent() != null) { |
| 93 | curNode.setOperationType(BOTH); |
| 94 | curNode = curNode.getParent(); |
| 95 | } |
| 96 | } else { |
| 97 | // If operation type for ydt node is "NONE" then in that |
| 98 | // case operation type for module node in app tree set as null. |
| 99 | // Once the target node operation type received by ydt then |
| 100 | // operation type for module node will be updated with the |
| 101 | // same target node operation type in app tree. |
| 102 | while (curNode.getParent() != null && curNode |
| 103 | .getOperationType() == null) { |
| 104 | curNode.setOperationType(opType); |
| 105 | curNode = curNode.getParent(); |
| 106 | } |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void setAppData(YdtNode moduleNode, YangSchemaNode augmentNode) { |
| 113 | if (augmentNode != null) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 114 | ((AugmentAppData) appData).setAugmentingSchemaNode(augmentNode); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 115 | } else { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 116 | ((ModuleAppData) appData).setModuleContext(moduleNode); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public AppData getAppData() { |
| 122 | return appData; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public YdtAppContext getParent() { |
| 127 | return parent; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void setParent(YdtAppContext parent) { |
| 132 | this.parent = parent; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public YdtAppContext getFirstChild() { |
| 137 | return child; |
| 138 | } |
| 139 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 140 | /** |
| 141 | * Sets the context of first child. |
| 142 | * |
| 143 | * @param child node |
| 144 | */ |
| 145 | private void setChild(YdtAppContext child) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 146 | this.child = child; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public YdtAppContext getNextSibling() { |
| 151 | return nextSibling; |
| 152 | } |
| 153 | |
| 154 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 155 | public void setNextSibling(YdtAppContext context) { |
| 156 | this.nextSibling = context; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public YdtAppContext getPreviousSibling() { |
| 161 | return previousSibling; |
| 162 | } |
| 163 | |
| 164 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 165 | public void setPreviousSibling(YdtAppContext context) { |
| 166 | this.previousSibling = context; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public YdtAppNodeOperationType getOperationType() { |
| 171 | return operationType; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void setOperationType(YdtAppNodeOperationType opType) { |
| 176 | operationType = opType; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public List<YdtContext> getDeleteNodes() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 181 | return ((ModuleAppData) appData).getDeleteNodes(); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 182 | } |
| 183 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 184 | @Override |
| 185 | public void addDeleteNode(YdtNode node) { |
| 186 | DefaultYdtAppContext<?> curNode = this; |
| 187 | while (curNode.getParent().getParent() != null) { |
| 188 | curNode = (DefaultYdtAppContext<?>) curNode.getParent(); |
| 189 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 190 | ((ModuleAppData) curNode.appData).addDeleteNodes(node); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public YdtContext getModuleContext() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 195 | return ((ModuleAppData) appData).getModuleContext(); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public YangSchemaNode getAugmentingSchemaNode() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 200 | return ((AugmentAppData) appData).getAugmentingSchemaNode(); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void setAugmentingSchemaNode(YangSchemaNode schemaNode) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 205 | ((AugmentAppData) appData).setAugmentingSchemaNode(schemaNode); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public YdtAppContext getLastChild() { |
| 210 | return lastChild; |
| 211 | } |
| 212 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 213 | /** |
| 214 | * Sets the context of last child. |
| 215 | * |
| 216 | * @param child node |
| 217 | */ |
| 218 | private void setLastChild(YdtAppContext child) { |
| 219 | lastChild = child; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public void addChild(YdtAppContext newChild) { |
| 224 | |
| 225 | if (newChild.getParent() == null) { |
| 226 | newChild.setParent(this); |
| 227 | } |
| 228 | |
| 229 | // First child to be added. |
| 230 | if (getFirstChild() == null) { |
| 231 | setChild(newChild); |
| 232 | // Update last child. |
| 233 | setLastChild(newChild); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | // If the new node needs to be add as last child. |
| 238 | YdtAppContext curNode = getLastChild(); |
| 239 | curNode.setNextSibling(newChild); |
| 240 | newChild.setPreviousSibling(curNode); |
| 241 | setLastChild(newChild); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public YangSchemaNode getYangSchemaNode() { |
| 246 | return appData.getSchemaNode(); |
| 247 | } |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 248 | } |