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