sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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. |
| 74 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 75 | DefaultYdtAppContext(T data) { |
| 76 | appData = data; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void updateAppOperationType(YdtContextOperationType ydtOpType) { |
| 81 | if (parent == null) { |
| 82 | return; |
| 83 | } |
| 84 | YdtAppNodeOperationType opType = getAppOpTypeFromYdtOpType(ydtOpType); |
| 85 | YdtAppContext curNode = this; |
| 86 | YdtAppNodeOperationType parentOpType = operationType; |
| 87 | if (parentOpType != null && opType != parentOpType) { |
| 88 | while (curNode.getOperationType() != BOTH && |
| 89 | curNode.getParent() != null) { |
| 90 | curNode.setOperationType(BOTH); |
| 91 | curNode = curNode.getParent(); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void setAppData(YdtNode moduleNode, YangSchemaNode augmentNode) { |
| 98 | if (augmentNode != null) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 99 | ((AugmentAppData) appData).setAugmentingSchemaNode(augmentNode); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 100 | } else { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 101 | ((ModuleAppData) appData).setModuleContext(moduleNode); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public AppData getAppData() { |
| 107 | return appData; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public YdtAppContext getParent() { |
| 112 | return parent; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void setParent(YdtAppContext parent) { |
| 117 | this.parent = parent; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public YdtAppContext getFirstChild() { |
| 122 | return child; |
| 123 | } |
| 124 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 125 | /** |
| 126 | * Sets the context of first child. |
| 127 | * |
| 128 | * @param child node |
| 129 | */ |
| 130 | private void setChild(YdtAppContext child) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 131 | this.child = child; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public YdtAppContext getNextSibling() { |
| 136 | return nextSibling; |
| 137 | } |
| 138 | |
| 139 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 140 | public void setNextSibling(YdtAppContext context) { |
| 141 | this.nextSibling = context; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public YdtAppContext getPreviousSibling() { |
| 146 | return previousSibling; |
| 147 | } |
| 148 | |
| 149 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 150 | public void setPreviousSibling(YdtAppContext context) { |
| 151 | this.previousSibling = context; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public YdtAppNodeOperationType getOperationType() { |
| 156 | return operationType; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void setOperationType(YdtAppNodeOperationType opType) { |
| 161 | operationType = opType; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public List<YdtContext> getDeleteNodes() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 166 | return ((ModuleAppData) appData).getDeleteNodes(); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 167 | } |
| 168 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 169 | @Override |
| 170 | public void addDeleteNode(YdtNode node) { |
| 171 | DefaultYdtAppContext<?> curNode = this; |
| 172 | while (curNode.getParent().getParent() != null) { |
| 173 | curNode = (DefaultYdtAppContext<?>) curNode.getParent(); |
| 174 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 175 | ((ModuleAppData) curNode.appData).addDeleteNodes(node); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public YdtContext getModuleContext() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 180 | return ((ModuleAppData) appData).getModuleContext(); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public YangSchemaNode getAugmentingSchemaNode() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 185 | return ((AugmentAppData) appData).getAugmentingSchemaNode(); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void setAugmentingSchemaNode(YangSchemaNode schemaNode) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 190 | ((AugmentAppData) appData).setAugmentingSchemaNode(schemaNode); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public YdtAppContext getLastChild() { |
| 195 | return lastChild; |
| 196 | } |
| 197 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 198 | /** |
| 199 | * Sets the context of last child. |
| 200 | * |
| 201 | * @param child node |
| 202 | */ |
| 203 | private void setLastChild(YdtAppContext child) { |
| 204 | lastChild = child; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void addChild(YdtAppContext newChild) { |
| 209 | |
| 210 | if (newChild.getParent() == null) { |
| 211 | newChild.setParent(this); |
| 212 | } |
| 213 | |
| 214 | // First child to be added. |
| 215 | if (getFirstChild() == null) { |
| 216 | setChild(newChild); |
| 217 | // Update last child. |
| 218 | setLastChild(newChild); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | // If the new node needs to be add as last child. |
| 223 | YdtAppContext curNode = getLastChild(); |
| 224 | curNode.setNextSibling(newChild); |
| 225 | newChild.setPreviousSibling(curNode); |
| 226 | setLastChild(newChild); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public YangSchemaNode getYangSchemaNode() { |
| 231 | return appData.getSchemaNode(); |
| 232 | } |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 233 | } |