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 | |
| 19 | import com.google.common.collect.ImmutableMap; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangAugment; |
| 21 | import org.onosproject.yangutils.datamodel.YangLeaf; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangList; |
| 23 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
| 24 | import org.onosproject.yangutils.datamodel.YangSchemaNodeContextInfo; |
| 25 | import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 26 | import org.onosproject.yms.app.ydt.exceptions.YdtException; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 27 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 28 | import org.onosproject.yms.ydt.YdtContext; |
| 29 | import org.onosproject.yms.ydt.YdtContextOperationType; |
| 30 | import org.onosproject.yms.ydt.YdtType; |
| 31 | import org.onosproject.yms.ydt.YmsOperationType; |
| 32 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 33 | import java.util.HashSet; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 34 | import java.util.Iterator; |
| 35 | import java.util.List; |
| 36 | import java.util.Map; |
| 37 | import java.util.Set; |
| 38 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 39 | import static org.onosproject.yms.app.ydt.AppNodeFactory.getAppContext; |
| 40 | import static org.onosproject.yms.app.ydt.RequestedCallType.LEAF; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 41 | import static org.onosproject.yms.app.ydt.RequestedCallType.NON_LEAF; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 42 | import static org.onosproject.yms.app.ydt.RequestedCardinality.MULTI_INSTANCE; |
| 43 | import static org.onosproject.yms.app.ydt.RequestedCardinality.MULTI_INSTANCE_LEAF; |
| 44 | import static org.onosproject.yms.app.ydt.RequestedCardinality.SINGLE_INSTANCE; |
| 45 | import static org.onosproject.yms.app.ydt.RequestedCardinality.UNKNOWN; |
| 46 | import static org.onosproject.yms.app.ydt.YdtConstants.errorMsg; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 47 | import static org.onosproject.yms.app.ydt.YdtNodeFactory.getNode; |
| 48 | import static org.onosproject.yms.app.ydt.YdtNodeFactory.getYangSchemaNodeTypeSpecificContext; |
| 49 | import static org.onosproject.yms.app.ydt.YdtUtils.checkElementCount; |
| 50 | import static org.onosproject.yms.app.ydt.YdtUtils.freeRestResources; |
| 51 | import static org.onosproject.yms.app.ydt.YdtUtils.getAppOpTypeFromYdtOpType; |
| 52 | import static org.onosproject.yms.app.ydt.YdtUtils.getAugmentingSchemaNode; |
| 53 | import static org.onosproject.yms.app.ydt.YdtUtils.getNodeIdentifier; |
| 54 | import static org.onosproject.yms.app.ydt.YdtUtils.getValidOpType; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 55 | import static org.onosproject.yms.ydt.YdtContextOperationType.DELETE; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 56 | import static org.onosproject.yms.ydt.YdtContextOperationType.REMOVE; |
| 57 | import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_LEAF_VALUE_NODE; |
| 58 | import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_NODE; |
| 59 | |
| 60 | /** |
| 61 | * Represents YANG request work bench which contains all parameters for |
| 62 | * request handling and methods to build and obtain YANG data tree |
| 63 | * which is data (sub)instance representation, abstract of protocol. |
| 64 | */ |
| 65 | public class YangRequestWorkBench implements YdtExtendedBuilder { |
| 66 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 67 | // Ydt formatted error string |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 68 | private static final String FMT_NOT_EXIST = |
| 69 | "Application with name \"%s\" doesn't exist."; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 70 | |
| 71 | // Ydt error strings. |
| 72 | private static final String E_USE_ADD_LEAF = |
| 73 | "Requested Node should be created using addLeaf interface."; |
| 74 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 75 | private static final String E_INVOKE_PARENT = |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 76 | "Can't invoke get parent at logical root node."; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 77 | |
| 78 | /* |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 79 | * Reference for the current context node in YANG data tree. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 80 | */ |
| 81 | private YdtNode curNode; |
| 82 | |
| 83 | /* |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 84 | * Reference for the logical root node in YANG data tree. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 85 | */ |
| 86 | private YdtNode rootNode; |
| 87 | |
| 88 | /* |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 89 | * Reference for the current context in ydt application tree. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 90 | */ |
| 91 | private YdtAppContext appCurNode; |
| 92 | |
| 93 | /* |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 94 | * Reference for the logical root node context in ydt application tree. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 95 | */ |
| 96 | private YdtAppContext appRootNode; |
| 97 | |
| 98 | /** |
| 99 | * Root Node Tag attribute in YANG data tree, kept to maintain the root |
| 100 | * tag attributes in YDT. |
| 101 | * <p> |
| 102 | * First key param of map represent tagName name of tag attribute. |
| 103 | * Second param of map represent tagValue value of tag attribute |
| 104 | */ |
| 105 | private Map<String, String> rootTagAttributeMap; |
| 106 | |
| 107 | /* |
| 108 | * YANG schema registry reference. |
| 109 | */ |
| 110 | private YangSchemaRegistry registry = null; |
| 111 | |
| 112 | /* |
| 113 | * YMS operation type. |
| 114 | */ |
| 115 | private final YmsOperationType ymsOperationType; |
| 116 | |
| 117 | /* |
| 118 | * YDT default operation type. |
| 119 | */ |
| 120 | private YdtContextOperationType ydtDefaultOpType; |
| 121 | |
| 122 | /* |
| 123 | * Flag to identify data validation need to be done by YDT or not. |
| 124 | */ |
Vidyashree Rama | 76faccc | 2016-10-17 22:06:52 +0530 | [diff] [blame] | 125 | private boolean validate = false; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 126 | // TODO validate need to be handle later with interaction type basis in |
| 127 | // future when it will be supported |
| 128 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 129 | /* |
| 130 | * Reference for application tree node set. |
| 131 | * This set contains the method name's generated for an augmented |
| 132 | * target node to avoid the duplicate entries in YDT application tree for |
| 133 | * multiple augmented nodes under a single XPATH. |
| 134 | */ |
| 135 | private Set<String> augGenMethodSet; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Creates an instance of YANG request work bench which is use to initialize |
| 139 | * logical rootNode and and schema registry. |
| 140 | * |
| 141 | * @param name name of logical container of a protocol |
| 142 | * which is a holder of the complete tree |
| 143 | * @param namespace namespace of logical container |
| 144 | * @param opType type of operation done by using YANG |
| 145 | * interface |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 146 | * @param reg Yang schema registry |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 147 | * @param isValidate Flag to identify data validation need to be |
| 148 | * done by YDT or not |
| 149 | */ |
| 150 | public YangRequestWorkBench(String name, String namespace, |
| 151 | YmsOperationType opType, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 152 | YangSchemaRegistry reg, |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 153 | boolean isValidate) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 154 | |
| 155 | setRootNode(new YdtLogicalNode(name, namespace)); |
| 156 | registry = reg; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 157 | ymsOperationType = opType; |
| 158 | validate = isValidate; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 159 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 160 | setAppRootNode(getAppContext(true)); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 164 | * Sets the logical root node for ydt. |
Vidyashree Rama | 76faccc | 2016-10-17 22:06:52 +0530 | [diff] [blame] | 165 | * |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 166 | * @param node ydt logical root node |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 167 | */ |
| 168 | private void setRootNode(YdtNode node) { |
| 169 | rootNode = node; |
| 170 | curNode = node; |
| 171 | } |
| 172 | |
| 173 | /** |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 174 | * Sets the logical root node for ydt application tree. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 175 | * |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 176 | * @param node ydt application context logical root node |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 177 | */ |
| 178 | private void setAppRootNode(YdtAppContext node) { |
| 179 | appRootNode = node; |
| 180 | appCurNode = node; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns the YANG schema registry of Ydt. |
| 185 | * This method will be used by ytb. |
| 186 | * |
| 187 | * @return YANG schema registry |
| 188 | */ |
| 189 | public YangSchemaRegistry getYangSchemaRegistry() { |
| 190 | return registry; |
| 191 | } |
| 192 | |
| 193 | /** |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 194 | * Returns the ydt app context tree logical root node. |
| 195 | * This method will be used by yab and ytb. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 196 | * |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 197 | * @return YdtAppContext app tree logical root node |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 198 | */ |
| 199 | public YdtAppContext getAppRootNode() { |
| 200 | return appRootNode; |
| 201 | } |
| 202 | |
| 203 | /** |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 204 | * Returns the ydt module node with requested node identifier. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 205 | * |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 206 | * @param id module/application node identifier |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 207 | * @return YANG data tree node |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 208 | * @throws YdtException when user requested node schema doesn't exist or |
| 209 | * requested node is already part of the tree |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 210 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 211 | private YdtNode moduleHandler(YangSchemaNodeIdentifier id) |
| 212 | throws YdtException { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 213 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 214 | YangSchemaNode node = |
| 215 | registry.getYangSchemaNodeUsingSchemaName(id.getName()); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 216 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 217 | String namespace = id.getNameSpace().getModuleNamespace(); |
| 218 | |
| 219 | /* |
| 220 | * Checking received schema node is having same namespace as |
| 221 | * requested by user or not. |
| 222 | */ |
| 223 | if (node == null || namespace != null && |
| 224 | !namespace.equals(node.getYangSchemaNodeIdentifier() |
| 225 | .getNameSpace() |
| 226 | .getModuleNamespace())) { |
| 227 | throw new YdtException(errorMsg(FMT_NOT_EXIST, id.getName())); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 228 | } |
| 229 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 230 | /* |
| 231 | * If yms operation is for query then no validation need to be |
| 232 | * performed. |
| 233 | */ |
| 234 | if (ymsOperationType != YmsOperationType.QUERY_REQUEST) { |
| 235 | // Checking whether module node is already exits in YDT or not. |
| 236 | try { |
| 237 | curNode.getCollidingChild(id); |
| 238 | } catch (YdtException e) { |
| 239 | throw new YdtException(e.getLocalizedMessage()); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | YdtNode newNode = new YdtSingleInstanceNode(node); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 244 | newNode.setYangSchemaNode(node); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 245 | return newNode; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void setRootTagAttributeMap(Map<String, String> attributeTag) { |
| 250 | rootTagAttributeMap = attributeTag; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public Map<String, String> getRootTagAttributeMap() { |
| 255 | if (rootTagAttributeMap != null) { |
| 256 | return ImmutableMap.copyOf(rootTagAttributeMap); |
| 257 | } |
| 258 | return null; |
| 259 | } |
| 260 | |
| 261 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 262 | public void addChild(String name, String namespace) |
| 263 | throws IllegalArgumentException { |
| 264 | addChild(name, namespace, UNKNOWN, null, NON_LEAF); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 268 | public void addChild(String name, String namespace, YdtType ydtType) |
| 269 | throws IllegalArgumentException { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 270 | addChild(name, namespace, ydtType, null); |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public void addChild(String name, String namespace, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 275 | YdtContextOperationType opType) |
| 276 | throws IllegalArgumentException { |
| 277 | addChild(name, namespace, UNKNOWN, opType, NON_LEAF); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public void addChild(String name, String namespace, YdtType ydtType, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 282 | YdtContextOperationType opType) |
| 283 | throws IllegalArgumentException { |
| 284 | RequestedCardinality cardinality; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 285 | switch (ydtType) { |
| 286 | case MULTI_INSTANCE_NODE: |
| 287 | cardinality = MULTI_INSTANCE; |
| 288 | break; |
| 289 | case SINGLE_INSTANCE_NODE: |
| 290 | cardinality = SINGLE_INSTANCE; |
| 291 | break; |
| 292 | default: |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 293 | throw new IllegalArgumentException(E_USE_ADD_LEAF); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 294 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 295 | addChild(name, namespace, cardinality, opType, NON_LEAF); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Adds a last child to YANG data tree; this method is to be used by all |
| 300 | * protocols internally which are aware or unaware of the nature |
| 301 | * (single/multiple) of node. |
| 302 | * |
| 303 | * @param name name of child to be added |
| 304 | * @param namespace namespace of child to be added |
| 305 | * @param cardinality type of YANG data tree node operation |
| 306 | * @param opType type of requested operation over a node |
| 307 | * @param callType to identify the whether its a leaf or other node |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 308 | * @throws IllegalArgumentException when method has been passed an illegal |
| 309 | * or inappropriate argument. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 310 | */ |
| 311 | private void addChild(String name, String namespace, |
| 312 | RequestedCardinality cardinality, |
| 313 | YdtContextOperationType opType, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 314 | RequestedCallType callType) |
| 315 | throws IllegalArgumentException { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 316 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 317 | YdtNode newNode; |
| 318 | boolean contextSwitch = false; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 319 | YangSchemaNode augmentingSchema = null; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 320 | YangSchemaNodeIdentifier id = getNodeIdentifier(name, namespace); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 321 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 322 | try { |
| 323 | // Module/sub-module node handler. |
| 324 | if (curNode.equals(rootNode)) { |
| 325 | newNode = moduleHandler(id); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 326 | } else { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 327 | |
| 328 | YangSchemaNode schemaNode; |
| 329 | YangSchemaNodeContextInfo contextInfo; |
| 330 | |
| 331 | // If namespace given by user null, then take namespace from parent. |
| 332 | if (namespace == null) { |
| 333 | id.setNameSpace(curNode.getYangSchemaNode().getNameSpace()); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 334 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 335 | |
| 336 | /* |
| 337 | * Get the already exiting YDT node in YDT tree with same |
| 338 | * nodeIdentifier |
| 339 | */ |
| 340 | newNode = curNode.getCollidingChild(id); |
| 341 | |
| 342 | /* |
| 343 | * If colliding child doesn't exist , |
| 344 | * then query yang data model for schema of given node. |
| 345 | */ |
| 346 | if (newNode == null) { |
| 347 | /* |
| 348 | * Get Yang Schema node context info which is having |
| 349 | * YangSchemaNode and ContextSwitchedNode. |
| 350 | */ |
| 351 | contextInfo = curNode.getSchemaNodeContextInfo(id); |
| 352 | |
| 353 | if (contextInfo.getContextSwitchedNode() != null) { |
| 354 | augmentingSchema = getAugmentingSchemaNode( |
| 355 | id, contextInfo); |
| 356 | if (augmentingSchema != null) { |
| 357 | /* |
| 358 | * As two tree(YDT and YDT Application Tree) are getting |
| 359 | * prepared in parallel, So setting context switch |
| 360 | * flag it will help ydt to keep the track whether |
| 361 | * ydtApp tree also need to be traversed back to parent |
| 362 | * or not with YDT tree traverse to parent call. |
| 363 | */ |
| 364 | contextSwitch = true; |
| 365 | } |
| 366 | } |
| 367 | schemaNode = contextInfo.getSchemaNode(); |
| 368 | } else { |
| 369 | /* |
| 370 | * If colliding child exist , then it will be leaf-list or list. |
| 371 | * If its leaf-list then return and add new requested |
| 372 | * value/valueSet in same node else take yang data model |
| 373 | * information from colliding child. |
| 374 | */ |
| 375 | if (newNode.getYdtType() == MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 376 | curNode = newNode; |
| 377 | return; |
| 378 | } |
| 379 | schemaNode = newNode.getYangSchemaNode(); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * For yms query request node specific validation are not |
| 384 | * required as rest-conf can call addChild api for leaf/leaf-list |
| 385 | * node addition also in ydt. |
| 386 | */ |
| 387 | if (ymsOperationType == YmsOperationType.QUERY_REQUEST) { |
| 388 | newNode = getYangSchemaNodeTypeSpecificContext(schemaNode); |
| 389 | } else { |
| 390 | newNode = getNode(schemaNode, cardinality, callType); |
| 391 | } |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 392 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 393 | |
| 394 | opType = getValidOpType(opType, ydtDefaultOpType, newNode, curNode); |
| 395 | |
| 396 | newNode.setYdtContextOperationType(opType); |
| 397 | |
| 398 | curNode.addChild(newNode, true); |
| 399 | } catch (YdtException e) { |
| 400 | freeRestResources(rootNode); |
| 401 | throw new IllegalArgumentException(e.getLocalizedMessage()); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 402 | } |
| 403 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 404 | // Update parent ydt node map. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 405 | curNode.updateYdtMap(newNode); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 406 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 407 | processAppTree(opType, newNode, augmentingSchema, contextSwitch); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 408 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 409 | curNode = newNode; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Processes application tree on the bases of requested ydt node. |
| 414 | * |
| 415 | * @param opType user requested operation type |
| 416 | * @param childNode requested ydt node |
| 417 | * @param augmentingSchema schema of last augmenting node |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 418 | * @param contextSwitch true, for module node call; false for modules |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 419 | * sub-node calls |
| 420 | */ |
| 421 | private void processAppTree( |
| 422 | YdtContextOperationType opType, YdtNode childNode, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 423 | YangSchemaNode augmentingSchema, boolean contextSwitch) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 424 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 425 | if (curNode == rootNode) { |
| 426 | augGenMethodSet = new HashSet<>(); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 427 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 428 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 429 | if (opType == null) { |
| 430 | opType = curNode.getYdtContextOperationType(); |
| 431 | } else { |
| 432 | // Updating operation type for parent nodes |
| 433 | appCurNode.updateAppOperationType(opType); |
| 434 | } |
| 435 | |
| 436 | /* |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 437 | * This is to avoid multiple entries of single augmented target. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 438 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 439 | if (augmentingSchema != null) { |
| 440 | if (!augGenMethodSet.add(((YangAugment) augmentingSchema) |
| 441 | .getSetterMethodName())) { |
| 442 | return; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Create entry of module node in ydt app tree. |
| 448 | * Or if context switch happened then also add entry for same |
| 449 | * augmented ydt node in the ydt application tree. |
| 450 | */ |
| 451 | if (curNode.equals(rootNode) || contextSwitch) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 452 | addChildInAppTree(childNode, augmentingSchema, opType, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 453 | contextSwitch); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 454 | |
| 455 | // Setting app tree node operation. |
| 456 | appCurNode.setOperationType(getAppOpTypeFromYdtOpType(opType)); |
| 457 | } |
| 458 | |
| 459 | // Updating the delete operation list in app tree. |
| 460 | if (opType == DELETE || opType == REMOVE) { |
| 461 | appCurNode.addDeleteNode(childNode); |
| 462 | } |
| 463 | } |
| 464 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 465 | |
| 466 | /** |
| 467 | * Adds a last child to YANG app data tree.this method is to be used |
| 468 | * internally by other ydt interfaces. |
| 469 | * |
| 470 | * @param childNode node to be added in tree |
| 471 | * @param schemaNode last augmenting module node |
| 472 | * @param childOpType operation type of node |
| 473 | * @param isContextSwitch true, for module node call; false for modules |
| 474 | * sub-node calls |
| 475 | */ |
| 476 | private void addChildInAppTree(YdtNode childNode, |
| 477 | YangSchemaNode schemaNode, |
| 478 | YdtContextOperationType childOpType, |
| 479 | boolean isContextSwitch) { |
| 480 | |
| 481 | YdtAppNodeOperationType opType; |
| 482 | |
| 483 | DefaultYdtAppContext appContext = getAppContext(isContextSwitch); |
| 484 | |
| 485 | // Add context switched child in ydt App tree. |
| 486 | appCurNode.addChild(appContext); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 487 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 488 | appCurNode = appContext; |
| 489 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 490 | opType = getAppOpTypeFromYdtOpType(childOpType); |
| 491 | |
| 492 | appCurNode.setAppData(childNode, schemaNode); |
| 493 | |
| 494 | appCurNode.setOperationType(opType); |
| 495 | |
| 496 | childNode.setAppContextSwitch(); |
| 497 | } |
| 498 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 499 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 500 | public void addLeaf(String name, String namespace, String value) |
| 501 | throws IllegalArgumentException { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 502 | addLeaf(name, namespace, value, null, UNKNOWN); |
| 503 | } |
| 504 | |
| 505 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 506 | public void addLeaf(String name, String namespace, Set<String> valueSet) |
| 507 | throws IllegalArgumentException { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 508 | addLeaf(name, namespace, null, valueSet, MULTI_INSTANCE_LEAF); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Adds a last leaf with list of values/single value to YANG data tree. |
| 513 | * This method is used by all protocols which knows the nature |
| 514 | * (single/multiple) or not. |
| 515 | * Value of leaf can be null which indicates selection node in get |
| 516 | * operation. |
| 517 | * |
| 518 | * @param name name of child to be added |
| 519 | * @param namespace namespace of child to be added, if it's |
| 520 | * null, parent's |
| 521 | * namespace will be applied to child |
| 522 | * @param value value of the child |
| 523 | * @param valueSet list of value of the child |
| 524 | * @param cardinality type of YANG data tree node operation |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 525 | * @throws IllegalArgumentException when method has been passed an illegal |
| 526 | * or inappropriate argument. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 527 | */ |
| 528 | private void addLeaf(String name, String namespace, String value, |
| 529 | Set<String> valueSet, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 530 | RequestedCardinality cardinality) |
| 531 | throws IllegalArgumentException { |
| 532 | try { |
| 533 | addChild(name, namespace, cardinality, null, LEAF); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 534 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 535 | // After successful addition of child node updating the values in same. |
| 536 | if (value != null) { |
| 537 | curNode.addValue(value); |
| 538 | } else if (valueSet != null) { |
| 539 | curNode.addValueSet(valueSet); |
| 540 | } |
| 541 | } catch (YdtException e) { |
| 542 | freeRestResources(rootNode); |
| 543 | throw new IllegalArgumentException(e.getLocalizedMessage()); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 548 | public void traverseToParent() throws IllegalStateException { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 549 | // If traverse back to parent for logical root node comes |
| 550 | if (curNode.equals(rootNode)) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 551 | freeRestResources(rootNode); |
| 552 | throw new IllegalStateException(E_INVOKE_PARENT); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 553 | } |
| 554 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 555 | try { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 556 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 557 | // If node is of multiInstanceNode type then check key uniqueness. |
| 558 | if (curNode.getYdtType() == MULTI_INSTANCE_NODE) { |
| 559 | curNode.createKeyNodeList(); |
| 560 | } |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 561 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 562 | /* |
| 563 | * Check application switch for curNode if set, |
| 564 | * then traverseToParent in YDT application tree. |
| 565 | */ |
| 566 | if (curNode.getParent().equals(rootNode) || |
| 567 | curNode.getAppContextSwitch()) { |
| 568 | traverseToAppTreeParent(); |
| 569 | } |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 570 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 571 | /* |
| 572 | * Validate all multi Instance inside current context, |
| 573 | * This is not valid for leaf and leaf-list node. |
| 574 | */ |
| 575 | if (curNode instanceof YdtMultiInstanceNode || |
| 576 | curNode instanceof YdtSingleInstanceNode) { |
| 577 | curNode.validateMultiInstanceNode(); |
| 578 | } |
| 579 | |
| 580 | curNode = curNode.getParent(); |
| 581 | } catch (YdtException e) { |
| 582 | freeRestResources(rootNode); |
| 583 | throw new IllegalStateException(e.getLocalizedMessage()); |
| 584 | } |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Traverses up in YANG application tree to the parent node, |
| 589 | * This will be used when Ydt current context switch flag is set. |
| 590 | */ |
| 591 | private void traverseToAppTreeParent() { |
| 592 | appCurNode = appCurNode.getParent(); |
| 593 | } |
| 594 | |
| 595 | @Override |
| 596 | public YdtContext getCurNode() { |
| 597 | return curNode; |
| 598 | } |
| 599 | |
| 600 | @Override |
| 601 | public void setDefaultEditOperationType( |
| 602 | YdtContextOperationType opType) { |
| 603 | ydtDefaultOpType = opType; |
| 604 | } |
| 605 | |
| 606 | @Override |
| 607 | public YdtExtendedContext getRootNode() { |
| 608 | return rootNode; |
| 609 | } |
| 610 | |
| 611 | @Override |
| 612 | public YmsOperationType getYmsOperationType() { |
| 613 | return ymsOperationType; |
| 614 | } |
| 615 | |
| 616 | @Override |
| 617 | public void addMultiInstanceChild(String name, String namespace, |
| 618 | List<String> keysValueList, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 619 | YdtContextOperationType opType) |
| 620 | throws IllegalArgumentException { |
| 621 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 622 | addChild(name, namespace, UNKNOWN, opType, |
| 623 | RequestedCallType.MULTI_INSTANCE); |
| 624 | int inputCount = keysValueList.size(); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 625 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 626 | try { |
| 627 | if (curNode.getYdtType() == MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 628 | |
| 629 | /* |
| 630 | * Calculating the current leaf-list node array size by adding |
| 631 | * existing elements count and new supplied elements by user for |
| 632 | * the same. |
| 633 | */ |
| 634 | // TODO instance count for leaf list need to be handled. |
| 635 | // if (curNode.getValueSet().size() + inputCount > expectedCount) { |
| 636 | // curNode.errorHandler( |
| 637 | // errorMsg(FMT_MANY_INS, name, expectedCount), rootNode); |
| 638 | // } |
| 639 | |
| 640 | /* |
| 641 | * After successful addition of child node updating |
| 642 | * the values in same. |
| 643 | */ |
| 644 | for (String value : keysValueList) { |
| 645 | curNode.addValue(value); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 646 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 647 | } else if (curNode.getYdtType() == MULTI_INSTANCE_NODE) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 648 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 649 | YangList yangListHolder = (YangList) curNode.getYangSchemaNode(); |
| 650 | List<String> schemaKeyList = yangListHolder.getKeyList(); |
| 651 | int expectedCount = schemaKeyList.size(); |
| 652 | checkElementCount(name, expectedCount, inputCount); |
| 653 | |
| 654 | //After validation adding the key nodes under the list node. |
| 655 | Iterator<String> sklIter = schemaKeyList.iterator(); |
| 656 | Iterator<String> kvlIter = keysValueList.iterator(); |
| 657 | String keyEleName; |
| 658 | |
| 659 | while (kvlIter.hasNext()) { |
| 660 | String value = kvlIter.next(); |
| 661 | keyEleName = sklIter.next(); |
| 662 | addLeaf(keyEleName, namespace, value); |
| 663 | if (kvlIter.hasNext()) { |
| 664 | traverseToParentWithoutValidation(); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | curNode = curNode.getParent(); |
| 669 | } |
| 670 | } catch (YdtException e) { |
| 671 | freeRestResources(rootNode); |
| 672 | throw new IllegalArgumentException(e.getLocalizedMessage()); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Adds a last child to YANG data tree, this method is to be used by |
| 678 | * YANG object builder sub-calls internally. |
| 679 | * |
| 680 | * @param opType type of requested operation over a node |
| 681 | * @return returns added ydt node in YDT tree |
| 682 | */ |
| 683 | private YdtNode addExtendedChildNode(YdtContextOperationType opType, |
| 684 | YangSchemaNode schemaNode) { |
| 685 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 686 | YdtNode childNode = getYangSchemaNodeTypeSpecificContext(schemaNode); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 687 | |
| 688 | childNode.setYangSchemaNode(schemaNode); |
| 689 | |
| 690 | childNode.setYdtContextOperationType(opType); |
| 691 | |
| 692 | curNode.addChild(childNode, true); |
| 693 | |
| 694 | curNode = childNode; |
| 695 | |
| 696 | return childNode; |
| 697 | } |
| 698 | |
| 699 | @Override |
| 700 | public YdtExtendedContext addChild(YdtContextOperationType opType, |
| 701 | YangSchemaNode schemaNode) { |
| 702 | return addExtendedChildNode(opType, schemaNode); |
| 703 | } |
| 704 | |
| 705 | @Override |
| 706 | public YdtExtendedContext addLeafList(Set<String> valueSet, |
| 707 | YangSchemaNode schemaNode) { |
| 708 | YdtNode childNode = addExtendedChildNode(null, schemaNode); |
| 709 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 710 | /* |
| 711 | * After successful addition of child node updating the values in |
| 712 | * valueSet. |
| 713 | */ |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 714 | childNode.addValueSetWithoutValidation(valueSet); |
| 715 | return childNode; |
| 716 | } |
| 717 | |
| 718 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 719 | public YdtExtendedContext addLeaf(String value, YangSchemaNode schemaNode) { |
| 720 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 721 | YdtNode childNode = addExtendedChildNode(null, schemaNode); |
| 722 | |
| 723 | // After successful addition of child node updating the values in same. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 724 | childNode.addValueWithoutValidation(value, ((YangLeaf) schemaNode) |
| 725 | .isKeyLeaf()); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 726 | return childNode; |
| 727 | } |
| 728 | |
| 729 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 730 | public void traverseToParentWithoutValidation() |
| 731 | throws IllegalStateException { |
| 732 | // If traverse back to parent for logical root node comes. |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 733 | if (curNode.equals(rootNode)) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 734 | freeRestResources(rootNode); |
| 735 | throw new IllegalStateException(E_INVOKE_PARENT); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 736 | } |
| 737 | curNode = curNode.getParent(); |
| 738 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 739 | |
| 740 | /** |
| 741 | * Returns the method name's set for an augmented target node in an |
| 742 | * application tree. |
| 743 | * |
| 744 | * @return augGenMethodSet set of method name's |
| 745 | */ |
| 746 | public Set<String> getAugGenMethodSet() { |
| 747 | return augGenMethodSet; |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Sets the method name's set for an augmented target node in an |
| 752 | * application tree. |
| 753 | * |
| 754 | * @param augGenMethodSet set of method name's |
| 755 | */ |
| 756 | public void setAugGenMethodSet(Set<String> augGenMethodSet) { |
| 757 | this.augGenMethodSet = augGenMethodSet; |
| 758 | } |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 759 | } |