Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +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.yangutils.datamodel; |
| 18 | |
| 19 | import java.util.Stack; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 20 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 22 | |
| 23 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 24 | * Represents resolution object which will be resolved by linker. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 25 | */ |
| 26 | public class YangResolutionInfo<T> { |
| 27 | |
| 28 | // Prefix associated with the linking. |
| 29 | private String prefix; |
| 30 | |
| 31 | // Parsable node for which resolution is to be performed. |
| 32 | private T entityToResolve; |
| 33 | |
| 34 | // Holder of the YANG construct for which resolution has to be carried out. |
| 35 | private YangNode holderOfEntityToResolve; |
| 36 | |
| 37 | // Error Line number. |
| 38 | private int lineNumber; |
| 39 | |
| 40 | // Error character position. |
| 41 | private int charPosition; |
| 42 | |
| 43 | // Status of resolution. |
| 44 | private boolean isResolved; |
| 45 | |
| 46 | /* |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 47 | * Stack for type/uses is maintained for hierarchical references, this is |
| 48 | * used during resolution. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 49 | */ |
| 50 | private Stack<T> partialResolvedStack; |
| 51 | |
| 52 | // Flag to indicate whether more references are detected. |
| 53 | private boolean isMoreReferenceDetected; |
| 54 | |
| 55 | // Module/Sub-module prefix. |
| 56 | private String resolutionInfoRootNodePrefix; |
| 57 | |
| 58 | /** |
| 59 | * Create a resolution information object. |
| 60 | */ |
| 61 | private YangResolutionInfo() { |
| 62 | |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Creates a resolution information object with all the inputs. |
| 67 | * |
| 68 | * @param dataNode current parsable data node |
| 69 | * @param resolutionType type of resolution whether grouping/typedef |
| 70 | * @param holderNode parent YANG node |
| 71 | * @param prefix imported module prefix |
| 72 | * @param lineNumber error line number |
| 73 | * @param charPositionInLine error character position in line |
| 74 | */ |
| 75 | public YangResolutionInfo(T dataNode, ResolutionType resolutionType, |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 76 | YangNode holderNode, String prefix, int lineNumber, |
| 77 | int charPositionInLine) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 78 | this.setHolderOfEntityToResolve(holderNode); |
| 79 | this.setEntityToResolve(dataNode); |
| 80 | this.setPrefix(prefix); |
| 81 | this.setLineNumber(lineNumber); |
| 82 | this.setCharPosition(charPositionInLine); |
| 83 | setPartialResolvedStack(new Stack<T>()); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Creates a resolution information object with all the inputs except prefix. |
| 88 | * |
| 89 | * @param dataNode current parsable data node |
| 90 | * @param resolutionType type of resolution whether grouping/typedef |
| 91 | * @param holderNode parent YANG node |
| 92 | * @param lineNumber error line number |
| 93 | * @param charPositionInLine error character position in line |
| 94 | */ |
| 95 | public YangResolutionInfo(T dataNode, ResolutionType resolutionType, |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 96 | YangNode holderNode, int lineNumber, |
| 97 | int charPositionInLine) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 98 | this.setHolderOfEntityToResolve(holderNode); |
| 99 | this.setEntityToResolve(dataNode); |
| 100 | this.setLineNumber(lineNumber); |
| 101 | this.setCharPosition(charPositionInLine); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Resolve linking with all the ancestors node for a resolution info. |
| 106 | * |
| 107 | * @param resolutionInfoNodePrefix module/sub-module prefix |
| 108 | * @throws DataModelException DataModelException a violation of data model rules |
| 109 | */ |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 110 | public void resolveLinkingForResolutionInfo(String resolutionInfoNodePrefix) throws DataModelException { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 111 | |
| 112 | this.resolutionInfoRootNodePrefix = resolutionInfoNodePrefix; |
| 113 | |
| 114 | // Current node to resolve, it can be a YANG type or YANG uses. |
| 115 | T entityToResolve = getEntityToResolve(); |
| 116 | |
| 117 | // Check if linking is already done |
| 118 | if (entityToResolve instanceof Resolvable) { |
| 119 | Resolvable resolvable = (Resolvable) entityToResolve; |
| 120 | if (resolvable.getResolvableStatus() == ResolvableStatus.RESOLVED || |
| 121 | resolvable.getResolvableStatus() == ResolvableStatus.PARTIALLY_RESOLVED) { |
| 122 | return; |
| 123 | } |
| 124 | } else { |
| 125 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 126 | } |
| 127 | |
| 128 | // Push the initial YANG type to the stack. |
| 129 | getPartialResolvedStack().push(entityToResolve); |
| 130 | |
| 131 | // Get holder of entity to resolve |
| 132 | YangNode curNode = getHolderOfEntityToResolve(); |
| 133 | |
| 134 | resolveLinkingWithAncestors(curNode); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Resolves linking with ancestors. |
| 139 | * |
| 140 | * @param curNode current node for which ancestors to be checked |
| 141 | * @throws DataModelException a violation of data model rules |
| 142 | */ |
| 143 | private void resolveLinkingWithAncestors(YangNode curNode) throws DataModelException { |
| 144 | |
| 145 | while (curNode != null) { |
| 146 | YangNode node = curNode.getChild(); |
| 147 | if (resolveLinkingForNodesChildAndSibling(node, curNode)) { |
| 148 | return; |
| 149 | } |
| 150 | curNode = curNode.getParent(); |
| 151 | } |
| 152 | |
| 153 | // If curNode is null, it indicates an error condition in YANG file. |
| 154 | DataModelException dataModelException = new DataModelException("YANG file error: Unable to find base " + |
| 155 | "typedef/grouping for given type/uses"); |
| 156 | dataModelException.setLine(getLineNumber()); |
| 157 | dataModelException.setCharPosition(getCharPosition()); |
| 158 | throw dataModelException; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Resolves linking for a node child and siblings. |
| 163 | * |
| 164 | * @param node current node |
| 165 | * @param parentNode parent node of current node |
| 166 | * @return flag to indicate whether resolution is done |
| 167 | * @throws DataModelException |
| 168 | */ |
| 169 | private boolean resolveLinkingForNodesChildAndSibling(YangNode node, YangNode parentNode) |
| 170 | throws DataModelException { |
| 171 | while ((node != null)) { |
| 172 | isMoreReferenceDetected = false; |
| 173 | // Check if node is of type, typedef or grouping |
| 174 | if (isNodeOfResolveType(node)) { |
| 175 | if (resolveLinkingForNode(node, parentNode)) { |
| 176 | return true; |
| 177 | } |
| 178 | } |
| 179 | if (isMoreReferenceDetected) { |
| 180 | /* |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 181 | * If more reference are present, tree traversal must start from |
| 182 | * first child again, to check the availability of |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 183 | * typedef/grouping. |
| 184 | */ |
| 185 | node = parentNode.getChild(); |
| 186 | } else { |
| 187 | node = node.getNextSibling(); |
| 188 | } |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Resolves linking for a node. |
| 195 | * |
| 196 | * @param node current node |
| 197 | * @param parentNode parent node of current node |
| 198 | * @return flag to indicate whether resolution is done |
| 199 | * @throws DataModelException a violation of data model rules |
| 200 | */ |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 201 | private boolean resolveLinkingForNode(YangNode node, YangNode parentNode) throws DataModelException { |
| 202 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 203 | /* |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 204 | * Check if name of node name matches with the entity name under |
| 205 | * resolution. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 206 | */ |
| 207 | if (isNodeNameSameAsResolutionInfoName(node)) { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 208 | // Adds reference of entity to the node under resolution. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 209 | addReferredEntityLink(node); |
| 210 | // Check if referred entity has further reference to uses/type. |
| 211 | if (!(isMoreReferencePresent(node))) { |
| 212 | // Resolve all the entities in stack. |
| 213 | resolveStackAndAddToStack(node); |
| 214 | return true; |
| 215 | } else { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 216 | // Adds referred type/uses to the stack. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 217 | addToPartialResolvedStack(node); |
| 218 | /* |
| 219 | * Check whether referred type is resolved, partially resolved |
| 220 | * or unresolved. |
| 221 | */ |
| 222 | if (isReferenceFullyResolved()) { |
| 223 | // Resolve the stack which is complete. |
| 224 | resolveCompleteStack(); |
| 225 | return true; |
| 226 | } else if (isReferencePartiallyResolved()) { |
| 227 | /* |
| 228 | * Update the resolution type to partially resolved for all |
| 229 | * type/uses in stack |
| 230 | */ |
| 231 | updateResolutionTypeToPartial(); |
| 232 | return true; |
| 233 | } else { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 234 | /* |
| 235 | * Check if prefix is present to find that the derived |
| 236 | * reference is for intra file or inter file, if it's |
| 237 | * inter-file return and stop further processing. |
| 238 | */ |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 239 | if (isExternalPrefixPresent(node)) { |
| 240 | /* |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 241 | * Update the resolution type to partially resolved for |
| 242 | * all type/uses in stack |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 243 | */ |
| 244 | updateResolutionTypeToPartial(); |
| 245 | return true; |
| 246 | } else { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 247 | /* |
| 248 | * If prefix is not present it indicates intra-file |
| 249 | * dependency in this case set the node back to first |
| 250 | * child, as referred entity may appear in any order and |
| 251 | * continue with the resolution. |
| 252 | */ |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 253 | isMoreReferenceDetected = true; |
| 254 | return false; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Update resolution type to partial for all type/uses in stack. |
| 264 | * |
| 265 | * @throws DataModelException a violation of data model rules |
| 266 | */ |
| 267 | private void updateResolutionTypeToPartial() throws DataModelException { |
| 268 | // For all entries in stack calls for the resolution in type/uses. |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 269 | for (T entity : getPartialResolvedStack()) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 270 | if (!(entity instanceof Resolvable)) { |
| 271 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 272 | } |
| 273 | if (((Resolvable) entity).getResolvableStatus() == ResolvableStatus.UNRESOLVED) { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 274 | // Sets the resolution status in inside the type/uses. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 275 | ((Resolvable) entity).setResolvableStatus(ResolvableStatus.PARTIALLY_RESOLVED); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 281 | * Adds referred type/uses to the stack and resolve the stack. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 282 | * |
| 283 | * @param node typedef/grouping node |
| 284 | * @throws DataModelException a violation of data model rules |
| 285 | */ |
| 286 | private void resolveStackAndAddToStack(YangNode node) throws DataModelException { |
| 287 | if (getEntityToResolve() instanceof YangType) { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 288 | // Adds to the stack only for YANG typedef. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 289 | getPartialResolvedStack().push((T) ((YangTypeDef) node).getDataType()); |
| 290 | } |
| 291 | // Don't add to stack in case of YANG grouping. |
| 292 | |
| 293 | // Resolve the complete stack. |
| 294 | resolveCompleteStack(); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Check if the referred type/uses is partially resolved. |
| 299 | * |
| 300 | * @return true if reference is partially resolved, otherwise false |
| 301 | */ |
| 302 | private boolean isReferencePartiallyResolved() { |
| 303 | if (getPartialResolvedStack().peek() instanceof YangType) { |
| 304 | /* |
| 305 | * Checks if type is partially resolved. |
| 306 | */ |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 307 | if (((YangType) getPartialResolvedStack().peek()) |
| 308 | .getResolvableStatus() == ResolvableStatus.PARTIALLY_RESOLVED) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | } else if (getPartialResolvedStack().peek() instanceof YangUses) { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 312 | if (((YangUses) getPartialResolvedStack().peek()) |
| 313 | .getResolvableStatus() == ResolvableStatus.PARTIALLY_RESOLVED) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 314 | return true; |
| 315 | } |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Check if the referred type/uses is resolved. |
| 322 | * |
| 323 | * @return true if reference is resolved, otherwise false |
| 324 | */ |
| 325 | private boolean isReferenceFullyResolved() { |
| 326 | if (getPartialResolvedStack().peek() instanceof YangType) { |
| 327 | /* |
| 328 | * Checks if type is partially resolved. |
| 329 | */ |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 330 | if (((YangType) getPartialResolvedStack().peek()).getResolvableStatus() == ResolvableStatus.RESOLVED) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 331 | return true; |
| 332 | } |
| 333 | } else if (getPartialResolvedStack().peek() instanceof YangUses) { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 334 | if (((YangUses) getPartialResolvedStack().peek()).getResolvableStatus() == ResolvableStatus.RESOLVED) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 335 | return true; |
| 336 | } |
| 337 | } |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Check if node is of resolve type i.e. of type typedef or grouping. |
| 343 | * |
| 344 | * @param node typedef/grouping node |
| 345 | * @return true if node is of resolve type otherwise false |
| 346 | * @throws DataModelException a violation of data model rules |
| 347 | */ |
| 348 | private boolean isNodeOfResolveType(YangNode node) throws DataModelException { |
| 349 | if (getPartialResolvedStack().peek() instanceof YangType && entityToResolve instanceof YangType) { |
| 350 | if (node instanceof YangTypeDef) { |
| 351 | return true; |
| 352 | } |
| 353 | } else if (getPartialResolvedStack().peek() instanceof YangUses && entityToResolve instanceof YangUses) { |
| 354 | if (node instanceof YangGrouping) { |
| 355 | return true; |
| 356 | } |
| 357 | } else { |
| 358 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 359 | } |
| 360 | return false; |
| 361 | } |
| 362 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 363 | /** |
| 364 | * Check if node name is same as name in resolution info, i.e. name of |
| 365 | * typedef/grouping is same as name of type/uses. |
| 366 | * |
| 367 | * @param node typedef/grouping node |
| 368 | * @return true if node name is same as name in resolution info, otherwise |
| 369 | * false |
| 370 | * @throws DataModelException a violation of data model rules |
| 371 | */ |
| 372 | private boolean isNodeNameSameAsResolutionInfoName(YangNode node) throws DataModelException { |
| 373 | if (getPartialResolvedStack().peek() instanceof YangType) { |
| 374 | if (node.getName().equals(((YangType<?>) getPartialResolvedStack().peek()).getDataTypeName())) { |
| 375 | return true; |
| 376 | } |
| 377 | } else if (getPartialResolvedStack().peek() instanceof YangUses) { |
| 378 | if (node.getName().equals(((YangUses) getPartialResolvedStack().peek()).getName())) { |
| 379 | return true; |
| 380 | } |
| 381 | } else { |
| 382 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 383 | } |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 388 | * Adds reference of grouping/typedef in uses/type. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 389 | * |
| 390 | * @param node grouping/typedef node |
| 391 | * @throws DataModelException a violation of data model rules |
| 392 | */ |
| 393 | private void addReferredEntityLink(YangNode node) throws DataModelException { |
| 394 | if (getPartialResolvedStack().peek() instanceof YangType) { |
| 395 | YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) ((YangType<?>) getPartialResolvedStack().peek()) |
| 396 | .getDataTypeExtendedInfo(); |
| 397 | derivedInfo.setReferredTypeDef((YangTypeDef) node); |
| 398 | } else if (getPartialResolvedStack().peek() instanceof YangUses) { |
| 399 | ((YangUses) getPartialResolvedStack().peek()).setRefGroup((YangGrouping) node); |
| 400 | } else { |
| 401 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Checks if typedef/grouping has further reference to type/typedef. |
| 407 | * |
| 408 | * @param node grouping/typedef node |
| 409 | * @return true if referred entity is resolved, otherwise false |
| 410 | * @throws DataModelException a violation of data model rules |
| 411 | */ |
| 412 | private boolean isMoreReferencePresent(YangNode node) throws DataModelException { |
| 413 | if (getEntityToResolve() instanceof YangType) { |
| 414 | /* |
| 415 | * Checks if typedef type is built-in type |
| 416 | */ |
| 417 | if ((((YangTypeDef) node).getDataType().getDataType() != YangDataTypes.DERIVED)) { |
| 418 | return false; |
| 419 | } |
| 420 | } else if (getEntityToResolve() instanceof YangUses) { |
| 421 | /* |
| 422 | * Search if the grouping has any uses child, if so return false, |
| 423 | * else return true. |
| 424 | */ |
| 425 | if (getUsesInGrouping(node) == null) { |
| 426 | return false; |
| 427 | } |
| 428 | } else { |
| 429 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 430 | } |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Return if there is any uses in grouping. |
| 436 | * |
| 437 | * @param node grouping/typedef node |
| 438 | * @return if there is any uses in grouping, otherwise return null |
| 439 | */ |
| 440 | private YangUses getUsesInGrouping(YangNode node) { |
| 441 | YangNode curNode = ((YangGrouping) node).getChild(); |
| 442 | while (curNode != null) { |
| 443 | if (curNode instanceof YangUses) { |
| 444 | break; |
| 445 | } |
| 446 | curNode = curNode.getNextSibling(); |
| 447 | } |
| 448 | return (YangUses) curNode; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Resolve the complete stack. |
| 453 | * |
| 454 | * @throws DataModelException a violation of data model rules |
| 455 | */ |
| 456 | private void resolveCompleteStack() throws DataModelException { |
| 457 | // For all entries in stack calls for the resolution in type/uses. |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 458 | for (T entity : getPartialResolvedStack()) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 459 | if (!(entity instanceof Resolvable)) { |
| 460 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 461 | } |
| 462 | ((Resolvable) entity).resolve(); |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 463 | // Sets the resolution status in inside the type/uses. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 464 | ((Resolvable) entity).setResolvableStatus(ResolvableStatus.RESOLVED); |
| 465 | } |
| 466 | /* |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 467 | * Sets the resolution status in resolution info present in resolution |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 468 | * list. |
| 469 | */ |
| 470 | setIsResolved(true); |
| 471 | } |
| 472 | |
| 473 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 474 | * Adds to partial resolved stack. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 475 | * |
| 476 | * @param node grouping/typedef node |
| 477 | * @throws DataModelException a violation of data model rules |
| 478 | */ |
| 479 | private void addToPartialResolvedStack(YangNode node) throws DataModelException { |
| 480 | if (getPartialResolvedStack().peek() instanceof YangType) { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 481 | // Adds to the stack only for YANG typedef. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 482 | getPartialResolvedStack().push((T) ((YangTypeDef) node).getDataType()); |
| 483 | } else if (getPartialResolvedStack().peek() instanceof YangUses) { |
| 484 | getPartialResolvedStack().push((T) getUsesInGrouping(node)); |
| 485 | } else { |
| 486 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Check if prefix is associated with type/uses. |
| 492 | * |
| 493 | * @param node typedef/grouping node |
| 494 | * @return true if prefix is present, otherwise false |
| 495 | * @throws DataModelException a violation of data model rules |
| 496 | */ |
| 497 | private boolean isExternalPrefixPresent(YangNode node) throws DataModelException { |
| 498 | if (getEntityToResolve() instanceof YangType) { |
| 499 | if (((YangTypeDef) node).getDataType().getPrefix() != null && |
| 500 | (!((YangTypeDef) node).getDataType().getPrefix().equals(resolutionInfoRootNodePrefix))) { |
| 501 | return true; |
| 502 | } |
| 503 | } else if (getEntityToResolve() instanceof YangUses) { |
| 504 | if (getUsesInGrouping(node).getPrefix() != null) { |
| 505 | return true; |
| 506 | } |
| 507 | } else { |
| 508 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 509 | } |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Returns prefix of imported module. |
| 515 | * |
| 516 | * @return prefix of imported module |
| 517 | */ |
| 518 | public String getPrefix() { |
| 519 | return prefix; |
| 520 | } |
| 521 | |
| 522 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 523 | * Sets prefix of imported module. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 524 | * |
| 525 | * @param prefix of imported module |
| 526 | */ |
| 527 | public void setPrefix(String prefix) { |
| 528 | this.prefix = prefix; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Returns parsable entity which is to be resolved. |
| 533 | * |
| 534 | * @return parsable entity which is to be resolved |
| 535 | */ |
| 536 | public T getEntityToResolve() { |
| 537 | return entityToResolve; |
| 538 | } |
| 539 | |
| 540 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 541 | * Sets parsable entity to be resolved. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 542 | * |
| 543 | * @param entityToResolve YANG entity to be resolved |
| 544 | */ |
| 545 | public void setEntityToResolve(T entityToResolve) { |
| 546 | this.entityToResolve = entityToResolve; |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Returns parent YANG node holder for the entity to be resolved. |
| 551 | * |
| 552 | * @return parent YANG node holder |
| 553 | */ |
| 554 | public YangNode getHolderOfEntityToResolve() { |
| 555 | return holderOfEntityToResolve; |
| 556 | } |
| 557 | |
| 558 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 559 | * Sets parent YANG node holder for the entity to be resolved. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 560 | * |
| 561 | * @param holderOfEntityToResolve parent YANG node holder |
| 562 | */ |
| 563 | public void setHolderOfEntityToResolve(YangNode holderOfEntityToResolve) { |
| 564 | this.holderOfEntityToResolve = holderOfEntityToResolve; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Returns error position. |
| 569 | * |
| 570 | * @return error position |
| 571 | */ |
| 572 | public int getCharPosition() { |
| 573 | return charPosition; |
| 574 | } |
| 575 | |
| 576 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 577 | * Sets error position. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 578 | * |
| 579 | * @param charPosition position of error |
| 580 | */ |
| 581 | public void setCharPosition(int charPosition) { |
| 582 | this.charPosition = charPosition; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Returns error character position in line. |
| 587 | * |
| 588 | * @return error character position in line |
| 589 | */ |
| 590 | public int getLineNumber() { |
| 591 | return lineNumber; |
| 592 | } |
| 593 | |
| 594 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 595 | * Sets error character position in line. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 596 | * |
| 597 | * @param lineNumber error character position in line |
| 598 | */ |
| 599 | public void setLineNumber(int lineNumber) { |
| 600 | this.lineNumber = lineNumber; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Returns status of resolution. |
| 605 | * |
| 606 | * @return resolution status |
| 607 | */ |
| 608 | public boolean isResolved() { |
| 609 | return isResolved; |
| 610 | } |
| 611 | |
| 612 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 613 | * Sets status of resolution. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 614 | * |
| 615 | * @param isResolved resolution status |
| 616 | */ |
| 617 | public void setIsResolved(boolean isResolved) { |
| 618 | this.isResolved = isResolved; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Returns stack of YANG type with partially resolved YANG construct hierarchy. |
| 623 | * |
| 624 | * @return partial resolved YANG construct stack |
| 625 | */ |
| 626 | public Stack<T> getPartialResolvedStack() { |
| 627 | return partialResolvedStack; |
| 628 | } |
| 629 | |
| 630 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 631 | * Sets stack of YANG type with partially resolved YANG construct hierarchy. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 632 | * |
| 633 | * @param partialResolvedStack partial resolved YANG construct stack |
| 634 | */ |
| 635 | public void setPartialResolvedStack(Stack<T> partialResolvedStack) { |
| 636 | this.partialResolvedStack = partialResolvedStack; |
| 637 | } |
| 638 | } |