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 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 23 | import static org.onosproject.yangutils.datamodel.ResolvableStatus.INTRA_FILE_RESOLVED; |
| 24 | import static org.onosproject.yangutils.datamodel.ResolvableStatus.LINKED; |
| 25 | import static org.onosproject.yangutils.datamodel.ResolvableStatus.RESOLVED; |
| 26 | import static org.onosproject.yangutils.datamodel.ResolvableStatus.UNRESOLVED; |
| 27 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 28 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 29 | * Represents resolution object which will be resolved by linker. |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 30 | * |
| 31 | * @param <T> type of resolution entity uses / type |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 32 | */ |
| 33 | public class YangResolutionInfo<T> { |
| 34 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 35 | /** |
| 36 | * Information about the entity that needs to be resolved. |
| 37 | */ |
| 38 | private YangEntityToResolveInfo<T> entityToResolveInfo; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 39 | |
| 40 | // Error Line number. |
| 41 | private int lineNumber; |
| 42 | |
| 43 | // Error character position. |
| 44 | private int charPosition; |
| 45 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 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 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 50 | private Stack<YangEntityToResolveInfo<T>> partialResolvedStack; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 51 | |
| 52 | // Module/Sub-module prefix. |
| 53 | private String resolutionInfoRootNodePrefix; |
| 54 | |
| 55 | /** |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 56 | * It is private to ensure the overloaded method be invoked to create an |
| 57 | * object. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 58 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 59 | @SuppressWarnings("unused") |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 60 | private YangResolutionInfo() { |
| 61 | |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a resolution information object with all the inputs. |
| 66 | * |
| 67 | * @param dataNode current parsable data node |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 68 | * @param holderNode parent YANG node |
| 69 | * @param lineNumber error line number |
| 70 | * @param charPositionInLine error character position in line |
| 71 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 72 | public YangResolutionInfo(T dataNode, YangNode holderNode, int lineNumber, int charPositionInLine) { |
| 73 | setEntityToResolveInfo(new YangEntityToResolveInfo<T>()); |
| 74 | getEntityToResolveInfo().setEntityToResolve(dataNode); |
| 75 | getEntityToResolveInfo().setHolderOfEntityToResolve(holderNode); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 76 | this.setLineNumber(lineNumber); |
| 77 | this.setCharPosition(charPositionInLine); |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 78 | setPartialResolvedStack(new Stack<YangEntityToResolveInfo<T>>()); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Resolve linking with all the ancestors node for a resolution info. |
| 83 | * |
| 84 | * @param resolutionInfoNodePrefix module/sub-module prefix |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 85 | * @throws DataModelException DataModelException a violation of data model |
| 86 | * rules |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 87 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 88 | public void resolveLinkingForResolutionInfo(String resolutionInfoNodePrefix) |
| 89 | throws DataModelException { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 90 | |
| 91 | this.resolutionInfoRootNodePrefix = resolutionInfoNodePrefix; |
| 92 | |
| 93 | // Current node to resolve, it can be a YANG type or YANG uses. |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 94 | T entityToResolve = getEntityToResolveInfo().getEntityToResolve(); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 95 | |
| 96 | // Check if linking is already done |
| 97 | if (entityToResolve instanceof Resolvable) { |
| 98 | Resolvable resolvable = (Resolvable) entityToResolve; |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 99 | if (resolvable.getResolvableStatus() == RESOLVED) { |
| 100 | /** |
| 101 | * entity is already resolved, so nothing to do |
| 102 | */ |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | } else { |
| 106 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 107 | } |
| 108 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 109 | // Push the initial entity to resolve in stack. |
| 110 | addInPartialResolvedStack(getEntityToResolveInfo()); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 111 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 112 | linkAndResolvePartialResolvedStack(); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Resolves linking with ancestors. |
| 117 | * |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 118 | * @throws DataModelException a violation of data model rules |
| 119 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 120 | private void linkAndResolvePartialResolvedStack() |
| 121 | throws DataModelException { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 122 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 123 | while (getPartialResolvedStack().size() != 0) { |
| 124 | |
| 125 | // Current node to resolve, it can be a YANG type or YANG uses. |
| 126 | T entityToResolve = getCurrentEntityToResolveFromStack(); |
| 127 | // Check if linking is already done |
| 128 | if (entityToResolve instanceof Resolvable) { |
| 129 | |
| 130 | Resolvable resolvable = (Resolvable) entityToResolve; |
| 131 | switch (resolvable.getResolvableStatus()) { |
| 132 | case RESOLVED: { |
| 133 | /* |
| 134 | * If the entity is already resolved in the stack, then |
| 135 | * pop it and continue with the remaining stack elements |
| 136 | * to resolve |
| 137 | */ |
| 138 | getPartialResolvedStack().pop(); |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | case LINKED: { |
| 143 | /* |
| 144 | * If the top of the stack is already linked then |
| 145 | * resolve the references and pop the entity and |
| 146 | * continue with remaining stack elements to resolve |
| 147 | */ |
| 148 | resolveTopOfStack(); |
| 149 | getPartialResolvedStack().pop(); |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | case INTRA_FILE_RESOLVED: { |
| 154 | /* |
| 155 | * TODO: this needs to be deleted, when inter-file |
| 156 | * resolution is implmeneted |
| 157 | */ |
| 158 | getPartialResolvedStack().pop(); |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | case UNRESOLVED: { |
| 163 | linkTopOfStackReferenceUpdateStack(); |
| 164 | |
| 165 | if (resolvable.getResolvableStatus() == UNRESOLVED) { |
| 166 | // If current entity is still not resolved, then linking/resolution has failed. |
| 167 | DataModelException dataModelException = |
| 168 | new DataModelException("YANG file error: Unable to find base " |
| 169 | + "typedef/grouping for given type/uses"); |
| 170 | dataModelException.setLine(getLineNumber()); |
| 171 | dataModelException.setCharPosition(getCharPosition()); |
| 172 | throw dataModelException; |
| 173 | } |
| 174 | break; |
| 175 | } |
| 176 | default: { |
| 177 | throw new DataModelException("Data Model Exception: Unsupported, linker state"); |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
| 182 | } else { |
| 183 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 184 | } |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 185 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 186 | } |
| 187 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Resolve the current entity in the stack. |
| 192 | */ |
| 193 | private void resolveTopOfStack() { |
| 194 | ((Resolvable) getCurrentEntityToResolveFromStack()).resolve(); |
| 195 | |
| 196 | if (((Resolvable) getCurrentEntityToResolveFromStack()).getResolvableStatus() |
| 197 | != INTRA_FILE_RESOLVED) { |
| 198 | // Sets the resolution status in inside the type/uses. |
| 199 | ((Resolvable) getCurrentEntityToResolveFromStack()).setResolvableStatus(RESOLVED); |
| 200 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Resolves linking for a node child and siblings. |
| 205 | * |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 206 | * @throws DataModelException data model error |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 207 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 208 | private void linkTopOfStackReferenceUpdateStack() |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 209 | throws DataModelException { |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 210 | |
| 211 | if (!isSelfFileReference()) { |
| 212 | /* |
| 213 | * TODO: use mojo utilities to load the referred module/sub-module |
| 214 | * and get the reference to the corresponding referred entity |
| 215 | */ |
| 216 | |
| 217 | ((Resolvable) getCurrentEntityToResolveFromStack()).setResolvableStatus(INTRA_FILE_RESOLVED); |
| 218 | return; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 219 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 220 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 221 | /** |
| 222 | * Try to resolve the top of the stack and update partial resolved stack |
| 223 | * if there is recursive references |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 224 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 225 | YangNode potentialAncestorWithReferredNode = getPartialResolvedStack().peek() |
| 226 | .getHolderOfEntityToResolve(); |
| 227 | |
| 228 | /** |
| 229 | * Traverse up in the ancestor tree to check if the referred node is |
| 230 | * defined |
| 231 | */ |
| 232 | while (potentialAncestorWithReferredNode != null) { |
| 233 | |
| 234 | /** |
| 235 | * Check for the referred node defined in a ancestor scope |
| 236 | */ |
| 237 | YangNode potentialReferredNode = potentialAncestorWithReferredNode.getChild(); |
| 238 | if (isReferredNodeInSiblingListProcessed(potentialReferredNode)) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | potentialAncestorWithReferredNode = potentialAncestorWithReferredNode.getParent(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Check if the reference in self file or in external file. |
| 248 | * |
| 249 | * @return true if self file reference, false otherwise |
| 250 | * @throws DataModelException a violation of data model rules |
| 251 | */ |
| 252 | private boolean isSelfFileReference() |
| 253 | throws DataModelException { |
| 254 | String prefix; |
| 255 | if (getCurrentEntityToResolveFromStack() instanceof YangType) { |
| 256 | prefix = ((YangType<?>) getCurrentEntityToResolveFromStack()).getPrefix(); |
| 257 | } else if (getCurrentEntityToResolveFromStack() instanceof YangUses) { |
| 258 | prefix = ((YangUses) getCurrentEntityToResolveFromStack()).getPrefix(); |
| 259 | } else { |
| 260 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 261 | } |
| 262 | |
| 263 | if (prefix == null) { |
| 264 | return true; |
| 265 | } |
| 266 | return prefix.contentEquals(resolutionInfoRootNodePrefix); |
| 267 | |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Check for the referred node defined in a ancestor scope. |
| 272 | * |
| 273 | * @param potentialReferredNode potential referred node |
| 274 | * @return status of resolution and updating the partial resolved stack with |
| 275 | * the any recursive references |
| 276 | * @throws DataModelException data model errors |
| 277 | */ |
| 278 | private boolean isReferredNodeInSiblingListProcessed(YangNode potentialReferredNode) |
| 279 | throws DataModelException { |
| 280 | while (potentialReferredNode != null) { |
| 281 | |
| 282 | // Check if the potential referred node is the actual referred node |
| 283 | if (isReferredNode(potentialReferredNode)) { |
| 284 | |
| 285 | // Adds reference link of entity to the node under resolution. |
| 286 | addReferredEntityLink(potentialReferredNode); |
| 287 | |
| 288 | /** |
| 289 | * resolve the reference and update the partial resolution stack |
| 290 | * with any further recursive references |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 291 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 292 | addUnresolvedRecursiveReferenceToStack(potentialReferredNode); |
| 293 | |
| 294 | /* |
| 295 | * return true, since the reference is linked and any recursive |
| 296 | * unresolved references is added to the stack |
| 297 | */ |
| 298 | return true; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 299 | } |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 300 | |
| 301 | potentialReferredNode = potentialReferredNode.getNextSibling(); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 302 | } |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | /** |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 307 | * Check if the potential referred node is the actual referred node. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 308 | * |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 309 | * @param potentialReferredNode typedef/grouping node |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 310 | * @return true if node is of resolve type otherwise false |
| 311 | * @throws DataModelException a violation of data model rules |
| 312 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 313 | private boolean isReferredNode(YangNode potentialReferredNode) |
| 314 | throws DataModelException { |
| 315 | if (getCurrentEntityToResolveFromStack() instanceof YangType) { |
| 316 | if (potentialReferredNode instanceof YangTypeDef) { |
| 317 | /* |
| 318 | * Check if name of node name matches with the entity being |
| 319 | * resolved |
| 320 | */ |
| 321 | return isNodeNameSameAsResolutionInfoName(potentialReferredNode); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 322 | } |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 323 | } else if (getCurrentEntityToResolveFromStack() instanceof YangUses) { |
| 324 | if (potentialReferredNode instanceof YangGrouping) { |
| 325 | /* |
| 326 | * Check if name of node name matches with the entity being |
| 327 | * resolved |
| 328 | */ |
| 329 | return isNodeNameSameAsResolutionInfoName(potentialReferredNode); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 330 | } |
| 331 | } else { |
| 332 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 333 | } |
| 334 | return false; |
| 335 | } |
| 336 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 337 | /** |
| 338 | * Check if node name is same as name in resolution info, i.e. name of |
| 339 | * typedef/grouping is same as name of type/uses. |
| 340 | * |
| 341 | * @param node typedef/grouping node |
| 342 | * @return true if node name is same as name in resolution info, otherwise |
| 343 | * false |
| 344 | * @throws DataModelException a violation of data model rules |
| 345 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 346 | |
| 347 | private boolean isNodeNameSameAsResolutionInfoName(YangNode node) |
| 348 | throws DataModelException { |
| 349 | if (getCurrentEntityToResolveFromStack() instanceof YangType) { |
| 350 | if (node.getName().contentEquals( |
| 351 | ((YangType<?>) getCurrentEntityToResolveFromStack()) |
| 352 | .getDataTypeName())) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 353 | return true; |
| 354 | } |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 355 | } else if (getCurrentEntityToResolveFromStack() instanceof YangUses) { |
| 356 | if (node.getName().contentEquals( |
| 357 | ((YangUses) getCurrentEntityToResolveFromStack()).getName())) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 358 | return true; |
| 359 | } |
| 360 | } else { |
| 361 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 362 | } |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 367 | * Adds reference of grouping/typedef in uses/type. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 368 | * |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 369 | * @param referredNode grouping/typedef node being referred |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 370 | * @throws DataModelException a violation of data model rules |
| 371 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 372 | private void addReferredEntityLink(YangNode referredNode) |
| 373 | throws DataModelException { |
| 374 | if (getCurrentEntityToResolveFromStack() instanceof YangType) { |
| 375 | YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) |
| 376 | ((YangType<?>) getCurrentEntityToResolveFromStack()).getDataTypeExtendedInfo(); |
| 377 | derivedInfo.setReferredTypeDef((YangTypeDef) referredNode); |
| 378 | } else if (getCurrentEntityToResolveFromStack() instanceof YangUses) { |
| 379 | ((YangUses) getCurrentEntityToResolveFromStack()) |
| 380 | .setRefGroup((YangGrouping) referredNode); |
| 381 | } else { |
| 382 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 383 | } |
| 384 | |
| 385 | // Sets the resolution status in inside the type/uses. |
| 386 | ((Resolvable) getCurrentEntityToResolveFromStack()).setResolvableStatus(LINKED); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Checks if type/grouping has further reference to typedef/ unresolved |
| 391 | * uses. Add it to the partial resolve stack and return the status of |
| 392 | * addition to stack. |
| 393 | * |
| 394 | * @param referredNode grouping/typedef node |
| 395 | * @throws DataModelException a violation of data model rules |
| 396 | */ |
| 397 | private void addUnresolvedRecursiveReferenceToStack(YangNode referredNode) |
| 398 | throws DataModelException { |
| 399 | if (getCurrentEntityToResolveFromStack() instanceof YangType) { |
| 400 | /* |
| 401 | * Checks if typedef type is derived |
| 402 | */ |
| 403 | if (((YangTypeDef) referredNode).getTypeDefBaseType().getDataType() |
| 404 | == YangDataTypes.DERIVED) { |
| 405 | |
| 406 | YangEntityToResolveInfo<YangType<?>> unResolvedEntityInfo = new YangEntityToResolveInfo<YangType<?>>(); |
| 407 | unResolvedEntityInfo.setEntityToResolve(((YangTypeDef) referredNode) |
| 408 | .getTypeDefBaseType()); |
| 409 | unResolvedEntityInfo.setHolderOfEntityToResolve(referredNode); |
| 410 | addInPartialResolvedStack((YangEntityToResolveInfo<T>) unResolvedEntityInfo); |
| 411 | } |
| 412 | |
| 413 | } else if (getCurrentEntityToResolveFromStack() instanceof YangUses) { |
| 414 | /* |
| 415 | * Search if the grouping has any un resolved uses child, if so |
| 416 | * return true, else return false. |
| 417 | */ |
| 418 | addUnResolvedUsesToStack(referredNode); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 419 | } else { |
| 420 | throw new DataModelException("Data Model Exception: Entity to resolved is other than type/uses"); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /** |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 425 | * Return if there is any unresolved uses in grouping. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 426 | * |
| 427 | * @param node grouping/typedef node |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 428 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 429 | private void addUnResolvedUsesToStack(YangNode node) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 430 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 431 | /** |
| 432 | * Search the grouping node's children for presence of uses node. |
| 433 | */ |
| 434 | YangNode curNode = node.getChild(); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 435 | while (curNode != null) { |
| 436 | if (curNode instanceof YangUses) { |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 437 | ResolvableStatus curResolveStatus = ((Resolvable) curNode).getResolvableStatus(); |
| 438 | if (curResolveStatus == UNRESOLVED) { |
| 439 | /** |
| 440 | * The current uses is not resolved, add it to partial |
| 441 | * resolved stack |
| 442 | */ |
| 443 | YangEntityToResolveInfo<YangUses> unResolvedEntityInfo = new YangEntityToResolveInfo<YangUses>(); |
| 444 | unResolvedEntityInfo.setEntityToResolve((YangUses) curNode); |
| 445 | unResolvedEntityInfo.setHolderOfEntityToResolve(node); |
| 446 | addInPartialResolvedStack((YangEntityToResolveInfo<T>) unResolvedEntityInfo); |
| 447 | |
| 448 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 449 | } |
| 450 | curNode = curNode.getNextSibling(); |
| 451 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 452 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 453 | return; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Returns error position. |
| 458 | * |
| 459 | * @return error position |
| 460 | */ |
| 461 | public int getCharPosition() { |
| 462 | return charPosition; |
| 463 | } |
| 464 | |
| 465 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 466 | * Sets error position. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 467 | * |
| 468 | * @param charPosition position of error |
| 469 | */ |
| 470 | public void setCharPosition(int charPosition) { |
| 471 | this.charPosition = charPosition; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Returns error character position in line. |
| 476 | * |
| 477 | * @return error character position in line |
| 478 | */ |
| 479 | public int getLineNumber() { |
| 480 | return lineNumber; |
| 481 | } |
| 482 | |
| 483 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 484 | * Sets error character position in line. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 485 | * |
| 486 | * @param lineNumber error character position in line |
| 487 | */ |
| 488 | public void setLineNumber(int lineNumber) { |
| 489 | this.lineNumber = lineNumber; |
| 490 | } |
| 491 | |
| 492 | /** |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 493 | * Returns stack of YANG type with partially resolved YANG construct |
| 494 | * hierarchy. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 495 | * |
| 496 | * @return partial resolved YANG construct stack |
| 497 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 498 | private Stack<YangEntityToResolveInfo<T>> getPartialResolvedStack() { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 499 | return partialResolvedStack; |
| 500 | } |
| 501 | |
| 502 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 503 | * Sets stack of YANG type with partially resolved YANG construct hierarchy. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 504 | * |
| 505 | * @param partialResolvedStack partial resolved YANG construct stack |
| 506 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 507 | private void setPartialResolvedStack(Stack<YangEntityToResolveInfo<T>> partialResolvedStack) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 508 | this.partialResolvedStack = partialResolvedStack; |
| 509 | } |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 510 | |
| 511 | /** |
| 512 | * Sets stack of YANG type with partially resolved YANG construct hierarchy. |
| 513 | * |
| 514 | * @param partialResolvedInfo partial resolved YANG construct stack |
| 515 | */ |
| 516 | private void addInPartialResolvedStack(YangEntityToResolveInfo<T> partialResolvedInfo) { |
| 517 | getPartialResolvedStack().push(partialResolvedInfo); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Retrieves the next entity in the stack that needs to be resolved. It is |
| 522 | * assumed that the caller ensures that the stack is not empty. |
| 523 | * |
| 524 | * @return next entity in the stack that needs to be resolved |
| 525 | */ |
| 526 | private T getCurrentEntityToResolveFromStack() { |
| 527 | return getPartialResolvedStack().peek().getEntityToResolve(); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Retrieves information about the entity that needs to be resolved. |
| 532 | * |
| 533 | * @return information about the entity that needs to be resolved |
| 534 | */ |
| 535 | public YangEntityToResolveInfo<T> getEntityToResolveInfo() { |
| 536 | return entityToResolveInfo; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Sets information about the entity that needs to be resolved. |
| 541 | * |
| 542 | * @param entityToResolveInfo information about the entity that needs to be |
| 543 | * resolved |
| 544 | */ |
| 545 | public void setEntityToResolveInfo(YangEntityToResolveInfo<T> entityToResolveInfo) { |
| 546 | this.entityToResolveInfo = entityToResolveInfo; |
| 547 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 548 | } |