Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +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.yangutils.linker.impl; |
| 18 | |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.DefaultLocationInfo; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.Resolvable; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Vidyashree Rama | 72719fa | 2016-07-15 14:06:56 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.TraversalType; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangAtomicPath; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangAugment; |
| 25 | import org.onosproject.yangutils.datamodel.YangAugmentableNode; |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangBase; |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangCompilerAnnotation; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangDerivedInfo; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangEntityToResolveInfoImpl; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangFeature; |
| 31 | import org.onosproject.yangutils.datamodel.YangFeatureHolder; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.YangGrouping; |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.datamodel.YangIdentity; |
| 34 | import org.onosproject.yangutils.datamodel.YangIdentityRef; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.datamodel.YangIfFeature; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.datamodel.YangImport; |
| 37 | import org.onosproject.yangutils.datamodel.YangInclude; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.datamodel.YangInput; |
| 39 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 40 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 41 | import org.onosproject.yangutils.datamodel.YangLeafRef; |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 42 | import org.onosproject.yangutils.datamodel.YangList; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 43 | import org.onosproject.yangutils.datamodel.YangNode; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 44 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 45 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 46 | import org.onosproject.yangutils.datamodel.YangRelativePath; |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 47 | import org.onosproject.yangutils.datamodel.YangResolutionInfo; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 48 | import org.onosproject.yangutils.datamodel.YangType; |
| 49 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 50 | import org.onosproject.yangutils.datamodel.YangUses; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 51 | import org.onosproject.yangutils.datamodel.YangXPathResolver; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 52 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 53 | import org.onosproject.yangutils.datamodel.utils.ResolvableStatus; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 54 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 55 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 56 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 57 | import java.io.Serializable; |
| 58 | import java.util.Iterator; |
| 59 | import java.util.LinkedList; |
| 60 | import java.util.List; |
| 61 | import java.util.Stack; |
| 62 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 63 | import static org.onosproject.yangutils.datamodel.ResolvableType.YANG_LEAFREF; |
Vidyashree Rama | 72719fa | 2016-07-15 14:06:56 +0530 | [diff] [blame] | 64 | import static org.onosproject.yangutils.datamodel.TraversalType.CHILD; |
| 65 | import static org.onosproject.yangutils.datamodel.TraversalType.PARENT; |
| 66 | import static org.onosproject.yangutils.datamodel.TraversalType.ROOT; |
| 67 | import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.datamodel.YangPathArgType.ABSOLUTE_PATH; |
| 69 | import static org.onosproject.yangutils.datamodel.YangPathArgType.RELATIVE_PATH; |
| 70 | import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.getErrorMsg; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 71 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 72 | import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.INTER_FILE_LINKED; |
| 73 | import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.INTRA_FILE_RESOLVED; |
| 74 | import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.LINKED; |
| 75 | import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 76 | import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.UNDEFINED; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 77 | import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.UNRESOLVED; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 78 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.PATH_DATA; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 79 | import static org.onosproject.yangutils.linker.impl.XpathLinkingTypes.AUGMENT_LINKING; |
| 80 | import static org.onosproject.yangutils.linker.impl.XpathLinkingTypes.LEAF_REF_LINKING; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 81 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.detectCollisionForAugmentedNode; |
janani b | 3a3e326 | 2016-10-19 00:23:28 +0530 | [diff] [blame] | 82 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.fillPathPredicates; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 83 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.getErrorInfoForLinker; |
| 84 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.getLeafRefErrorInfo; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 85 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.getPathWithAugment; |
| 86 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.getValidNodeIdentifier; |
| 87 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.skipInvalidDataNodes; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 88 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 89 | import static org.onosproject.yangutils.utils.UtilConstants.FAILED_TO_FIND_ANNOTATION; |
| 90 | import static org.onosproject.yangutils.utils.UtilConstants.FAILED_TO_FIND_LEAD_INFO_HOLDER; |
| 91 | import static org.onosproject.yangutils.utils.UtilConstants.FAILED_TO_LINK; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 92 | import static org.onosproject.yangutils.utils.UtilConstants.INVALID_ENTITY; |
| 93 | import static org.onosproject.yangutils.utils.UtilConstants.INVALID_LINKER_STATE; |
| 94 | import static org.onosproject.yangutils.utils.UtilConstants.INVALID_RESOLVED_ENTITY; |
| 95 | import static org.onosproject.yangutils.utils.UtilConstants.INVALID_TARGET; |
| 96 | import static org.onosproject.yangutils.utils.UtilConstants.INVALID_TREE; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 97 | import static org.onosproject.yangutils.utils.UtilConstants.LEAFREF; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 98 | import static org.onosproject.yangutils.utils.UtilConstants.LINKER_ERROR; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 99 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH_FOR_STRING; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 100 | import static org.onosproject.yangutils.utils.UtilConstants.UNRESOLVABLE; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 101 | |
| 102 | /** |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 103 | * Represents implementation of resolution object which will be resolved by |
| 104 | * linker. |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 105 | * |
| 106 | * @param <T> type of resolution entity uses / type |
| 107 | */ |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 108 | public class YangResolutionInfoImpl<T> extends DefaultLocationInfo |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 109 | implements YangResolutionInfo<T>, Serializable { |
| 110 | |
| 111 | private static final long serialVersionUID = 806201658L; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Information about the entity that needs to be resolved. |
| 115 | */ |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 116 | private YangEntityToResolveInfoImpl<T> entityToResolveInfo; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 117 | |
| 118 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 119 | * Current module/sub-module reference, will be used in inter-file/ |
| 120 | * inter-jar scenario to get the import/include list. |
| 121 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 122 | private YangReferenceResolver curRefResolver; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Stack for type/uses is maintained for hierarchical references, this is |
| 126 | * used during resolution. |
| 127 | */ |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 128 | private Stack<YangEntityToResolveInfoImpl<T>> partialResolvedStack; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * It is private to ensure the overloaded method be invoked to create an |
| 132 | * object. |
| 133 | */ |
| 134 | @SuppressWarnings("unused") |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 135 | private YangResolutionInfoImpl() { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Creates a resolution information object with all the inputs. |
| 140 | * |
| 141 | * @param dataNode current parsable data node |
| 142 | * @param holderNode parent YANG node |
| 143 | * @param lineNumber error line number |
| 144 | * @param charPositionInLine error character position in line |
| 145 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 146 | public YangResolutionInfoImpl(T dataNode, YangNode holderNode, int lineNumber, |
| 147 | int charPositionInLine) { |
| 148 | entityToResolveInfo = new YangEntityToResolveInfoImpl<>(); |
| 149 | entityToResolveInfo.setEntityToResolve(dataNode); |
| 150 | entityToResolveInfo.setHolderOfEntityToResolve(holderNode); |
| 151 | setLineNumber(lineNumber); |
| 152 | setCharPosition(charPositionInLine); |
| 153 | partialResolvedStack = new Stack<>(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 154 | } |
| 155 | |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 156 | @Override |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 157 | public void resolveLinkingForResolutionInfo(YangReferenceResolver dataModelRootNode) |
| 158 | throws DataModelException { |
| 159 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 160 | curRefResolver = dataModelRootNode; |
| 161 | /* |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 162 | * Current node to resolve, it can be a YANG type, YANG uses or YANG if-feature or |
| 163 | * YANG leafref or YANG base or YANG identityref. |
| 164 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 165 | T entityToResolve = entityToResolveInfo.getEntityToResolve(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 166 | |
| 167 | // Check if linking is already done |
| 168 | if (entityToResolve instanceof Resolvable) { |
| 169 | Resolvable resolvable = (Resolvable) entityToResolve; |
| 170 | if (resolvable.getResolvableStatus() == RESOLVED) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 171 | /* |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 172 | * entity is already resolved, so nothing to do |
| 173 | */ |
| 174 | return; |
| 175 | } |
| 176 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 177 | throw new DataModelException(LINKER_ERROR); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 178 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 179 | // Push the initial entity to resolve in stack. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 180 | addInPartialResolvedStack(entityToResolveInfo); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 181 | linkAndResolvePartialResolvedStack(); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 182 | addDerivedRefTypeToRefTypeResolutionList(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Resolves linking with ancestors. |
| 187 | * |
| 188 | * @throws DataModelException a violation of data model rules |
| 189 | */ |
| 190 | private void linkAndResolvePartialResolvedStack() |
| 191 | throws DataModelException { |
| 192 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 193 | while (!partialResolvedStack.isEmpty()) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 194 | /* |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 195 | * Current node to resolve, it can be a YANG type or YANG uses or |
| 196 | * YANG if-feature or YANG leafref or YANG base or YANG identityref. |
| 197 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 198 | T entityToResolve = getCurEntityToResolveFromStack(); |
| 199 | if (!(entityToResolve instanceof Resolvable)) { |
| 200 | throw new DataModelException(LINKER_ERROR); |
| 201 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 202 | // Check if linking is already done |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 203 | Resolvable resolvable = (Resolvable) entityToResolve; |
| 204 | switch (resolvable.getResolvableStatus()) { |
| 205 | case RESOLVED: |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 206 | /* |
| 207 | * If the entity is already resolved in the stack, then pop |
| 208 | * it and continue with the remaining stack elements to |
| 209 | * resolve |
| 210 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 211 | partialResolvedStack.pop(); |
| 212 | break; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 213 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 214 | case LINKED: |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 215 | /* |
| 216 | * If the top of the stack is already linked then resolve |
| 217 | * the references and pop the entity and continue with |
| 218 | * remaining stack elements to resolve. |
| 219 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 220 | resolveTopOfStack(); |
| 221 | partialResolvedStack.pop(); |
| 222 | break; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 223 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 224 | case INTRA_FILE_RESOLVED: |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 225 | /* |
| 226 | * Pop the top of the stack. |
| 227 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 228 | partialResolvedStack.pop(); |
| 229 | break; |
| 230 | |
| 231 | case UNRESOLVED: |
| 232 | linkTopOfStackReferenceUpdateStack(); |
| 233 | |
| 234 | if (resolvable.getResolvableStatus() == UNRESOLVED) { |
| 235 | // If current entity is still not resolved, then |
| 236 | // linking/resolution has failed. |
| 237 | DataModelException ex = |
| 238 | new DataModelException |
| 239 | (getErrorInfoForLinker(resolvable)); |
| 240 | ex.setLine(getLineNumber()); |
| 241 | ex.setCharPosition(getCharPosition()); |
| 242 | throw ex; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 243 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 244 | break; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 245 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 246 | default: |
| 247 | throw new DataModelException(INVALID_LINKER_STATE); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 248 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 249 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 253 | * Adds leaf-ref to the resolution list, with different context if |
| 254 | * leaf-ref is defined under derived type. Leaf-ref must be resolved from |
| 255 | * where the typedef is referenced. |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 256 | */ |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 257 | private void addDerivedRefTypeToRefTypeResolutionList() |
| 258 | throws DataModelException { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 259 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 260 | YangNode refNode = entityToResolveInfo.getHolderOfEntityToResolve(); |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 261 | YangDerivedInfo info = getValidResolvableType(); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 262 | |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 263 | if (info == null) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 267 | YangType<T> type = |
| 268 | (YangType<T>) entityToResolveInfo.getEntityToResolve(); |
| 269 | |
| 270 | T extType = (T) info.getReferredTypeDef().getTypeDefBaseType() |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 271 | .getDataTypeExtendedInfo(); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 272 | |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 273 | while (extType instanceof YangDerivedInfo) { |
| 274 | info = (YangDerivedInfo) extType; |
| 275 | extType = (T) info.getReferredTypeDef().getTypeDefBaseType() |
| 276 | .getDataTypeExtendedInfo(); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 277 | } |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 278 | /* |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 279 | * Backup the leaf-ref info from derived type and deletes the derived |
| 280 | * type info. Copies the backed up leaf-ref data to the actual type in |
| 281 | * replacement of derived type. Adds to the resolution list in this |
| 282 | * context. |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 283 | */ |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 284 | addRefTypeInfo(extType, type, refNode); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 285 | } |
| 286 | |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 287 | /** |
| 288 | * Returns the derived info if the holder is typedef, the entity is type |
| 289 | * and the effective type is leaf-ref; null otherwise. |
| 290 | * |
| 291 | * @return derived info |
| 292 | */ |
| 293 | private YangDerivedInfo<?> getValidResolvableType() { |
| 294 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 295 | YangNode refNode = entityToResolveInfo.getHolderOfEntityToResolve(); |
| 296 | T entity = entityToResolveInfo.getEntityToResolve(); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 297 | |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 298 | if (!(refNode instanceof YangTypeDef) && entity instanceof YangType) { |
| 299 | YangType<?> type = (YangType) entity; |
| 300 | YangDerivedInfo<?> info = |
| 301 | (YangDerivedInfo) type.getDataTypeExtendedInfo(); |
| 302 | YangDataTypes dataType = info.getEffectiveBuiltInType(); |
| 303 | if ((type.getResolvableStatus() == RESOLVED) && |
| 304 | (dataType == YangDataTypes.LEAFREF)) { |
| 305 | return info; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | return null; |
| 309 | } |
| 310 | |
| 311 | /** |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 312 | * Adds resolvable type (leaf-ref) info to resolution list. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 313 | * |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 314 | * @param extType resolvable type |
| 315 | * @param type YANG type |
| 316 | * @param holder holder node |
| 317 | * @throws DataModelException if there is a data model error |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 318 | */ |
janani b | c27947a | 2016-10-19 01:17:40 +0530 | [diff] [blame^] | 319 | private void addRefTypeInfo(T extType, YangType<T> type, YangNode holder) |
| 320 | throws DataModelException { |
| 321 | |
| 322 | type.resetYangType(); |
| 323 | type.setResolvableStatus(RESOLVED); |
| 324 | type.setDataType(YangDataTypes.LEAFREF); |
| 325 | type.setDataTypeName(LEAFREF); |
| 326 | type.setDataTypeExtendedInfo(extType); |
| 327 | |
| 328 | YangLeafRef leafRef = (YangLeafRef) extType; |
| 329 | (leafRef).setResolvableStatus(UNRESOLVED); |
| 330 | leafRef.setParentNode(holder); |
| 331 | |
| 332 | YangResolutionInfoImpl info = new YangResolutionInfoImpl<>( |
| 333 | leafRef, holder, getLineNumber(), getCharPosition()); |
| 334 | curRefResolver.addToResolutionList(info, YANG_LEAFREF); |
| 335 | curRefResolver.resolveSelfFileLinking(YANG_LEAFREF); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 336 | } |
| 337 | |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 338 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 339 | * Resolves the current entity in the stack. |
| 340 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 341 | private void resolveTopOfStack() |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 342 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 343 | T entity = getCurEntityToResolveFromStack(); |
| 344 | List<T> entityToResolve = (List<T>) ((Resolvable) entity).resolve(); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 345 | if (entityToResolve != null && !entityToResolve.isEmpty()) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 346 | for (T anEntityToResolve : entityToResolve) { |
| 347 | addUnresolvedEntitiesToResolutionList(anEntityToResolve); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 348 | } |
| 349 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 350 | if (((Resolvable) entity).getResolvableStatus() != INTRA_FILE_RESOLVED && |
| 351 | ((Resolvable) entity).getResolvableStatus() != UNDEFINED) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 352 | // Sets the resolution status in inside the type/uses/if-feature/leafref. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 353 | ((Resolvable) entity).setResolvableStatus(RESOLVED); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | /** |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 358 | * Adds the unresolved entities to the resolution list. |
| 359 | * |
| 360 | * @param entityToResolve entity to resolve |
| 361 | * @throws DataModelException a violation of data model rules |
| 362 | */ |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 363 | private void addUnresolvedEntitiesToResolutionList(T entityToResolve) |
| 364 | throws DataModelException { |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 365 | if (entityToResolve instanceof YangEntityToResolveInfoImpl) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 366 | YangEntityToResolveInfoImpl entityToResolveInfo |
| 367 | = (YangEntityToResolveInfoImpl) entityToResolve; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 368 | if (entityToResolveInfo.getEntityToResolve() instanceof YangLeafRef) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 369 | YangLeafRef leafref = (YangLeafRef) entityToResolveInfo |
| 370 | .getEntityToResolve(); |
| 371 | YangNode parentNodeOfLeafref = entityToResolveInfo |
| 372 | .getHolderOfEntityToResolve(); |
janani b | 3a3e326 | 2016-10-19 00:23:28 +0530 | [diff] [blame] | 373 | leafref.setParentNode(parentNodeOfLeafref); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 374 | if (leafref.getResolvableStatus() == UNRESOLVED) { |
| 375 | leafref.setResolvableStatus(INTRA_FILE_RESOLVED); |
| 376 | } |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 377 | } |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 378 | |
| 379 | // Add resolution information to the list. |
| 380 | YangResolutionInfoImpl resolutionInfoImpl = new YangResolutionInfoImpl<>( |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 381 | entityToResolveInfo.getEntityToResolve(), |
| 382 | entityToResolveInfo.getHolderOfEntityToResolve(), |
| 383 | entityToResolveInfo.getLineNumber(), |
| 384 | entityToResolveInfo.getCharPosition()); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 385 | addResolutionInfo(resolutionInfoImpl); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 390 | * Resolves linking for a node child and siblings. |
| 391 | * |
| 392 | * @throws DataModelException data model error |
| 393 | */ |
| 394 | private void linkTopOfStackReferenceUpdateStack() |
| 395 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 396 | T entity = getCurEntityToResolveFromStack(); |
| 397 | if (entity instanceof YangLeafRef) { |
| 398 | ((Resolvable) entity).setResolvableStatus(INTRA_FILE_RESOLVED); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 399 | return; |
| 400 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 401 | /* |
| 402 | * Check if self file reference is there, this will not check for the |
| 403 | * scenario when prefix is not present and type/uses is present in |
| 404 | * sub-module from include list. |
| 405 | */ |
| 406 | if (!isCandidateForSelfFileReference()) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 407 | ((Resolvable) entity).setResolvableStatus(INTRA_FILE_RESOLVED); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 408 | return; |
| 409 | } |
| 410 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 411 | /* |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 412 | * Try to resolve the top of the stack and update partial resolved stack |
| 413 | * if there is recursive references |
| 414 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 415 | YangNode ancestorRefNode = partialResolvedStack.peek() |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 416 | .getHolderOfEntityToResolve(); |
| 417 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 418 | if (entity instanceof YangIfFeature) { |
| 419 | resolveSelfFileLinkingForIfFeature(ancestorRefNode); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 420 | return; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 421 | } |
| 422 | if (entity instanceof YangIdentityRef || entity instanceof YangBase) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 423 | resolveSelfFileLinkingForBaseAndIdentityref(); |
| 424 | return; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 425 | } |
| 426 | YangType type = null; |
| 427 | if (entity instanceof YangType) { |
| 428 | type = (YangType) entity; |
| 429 | } |
| 430 | /* |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 431 | * Traverse up in the ancestor tree to check if the referred node is |
| 432 | * defined |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 433 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 434 | while (ancestorRefNode != null) { |
| 435 | /* |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 436 | * Check for the referred node defined in a ancestor scope |
| 437 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 438 | YangNode curRefNode = ancestorRefNode.getChild(); |
| 439 | if (isReferredNodeInSiblingListProcessed(curRefNode)) { |
| 440 | return; |
| 441 | } |
| 442 | ancestorRefNode = ancestorRefNode.getParent(); |
| 443 | if (type != null && ancestorRefNode != null) { |
| 444 | if (ancestorRefNode.getParent() == null) { |
| 445 | type.setTypeNotResolvedTillRootNode(true); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 446 | } |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 447 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* |
Gaurav Agrawal | 8a966b9 | 2016-08-20 00:07:27 +0530 | [diff] [blame] | 451 | * In case prefix is not present or it's self prefix it's a candidate for inter-file |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 452 | * resolution via include list. |
| 453 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 454 | if (getRefPrefix() == null || |
| 455 | getRefPrefix().contentEquals(curRefResolver.getPrefix())) { |
| 456 | ((Resolvable) entity).setResolvableStatus(INTRA_FILE_RESOLVED); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 461 | * Resolves self file linking for base/identityref. |
| 462 | * |
| 463 | * @throws DataModelException a violation of data model rules |
| 464 | */ |
| 465 | private void resolveSelfFileLinkingForBaseAndIdentityref() |
| 466 | throws DataModelException { |
| 467 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 468 | boolean refIdentity = false; |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 469 | String nodeName = null; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 470 | T entity = getCurEntityToResolveFromStack(); |
| 471 | if (entity instanceof YangIdentityRef) { |
| 472 | nodeName = ((YangIdentityRef) entity).getName(); |
| 473 | } else if (entity instanceof YangBase) { |
| 474 | nodeName = ((YangBase) entity).getBaseIdentifier().getName(); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 475 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 476 | if (curRefResolver instanceof RpcNotificationContainer) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 477 | // Sends list of nodes for finding the target identity. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 478 | refIdentity = isIdentityReferenceFound(nodeName, (YangNode) curRefResolver); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 479 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 480 | if (refIdentity) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | |
Gaurav Agrawal | 8a966b9 | 2016-08-20 00:07:27 +0530 | [diff] [blame] | 484 | /* |
| 485 | * In case prefix is not present or it's self prefix it's a candidate for inter-file |
| 486 | * resolution via include list. |
| 487 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 488 | if (getRefPrefix() == null || getRefPrefix() |
| 489 | .contentEquals(curRefResolver.getPrefix())) { |
| 490 | ((Resolvable) entity).setResolvableStatus(INTRA_FILE_RESOLVED); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | /** |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 495 | * Resolves self file linking for if-feature. |
| 496 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 497 | * @param ancestorRefNode if-feature holder node |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 498 | * @throws DataModelException DataModelException a violation of data model |
| 499 | * rules |
| 500 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 501 | private void resolveSelfFileLinkingForIfFeature(YangNode ancestorRefNode) |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 502 | throws DataModelException { |
| 503 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 504 | YangFeatureHolder featureHolder = getFeatureHolder(ancestorRefNode); |
| 505 | YangNode curRefNode = (YangNode) featureHolder; |
| 506 | if (isReferredNode(curRefNode)) { |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 507 | |
| 508 | // Adds reference link of entity to the node under resolution. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 509 | addReferredEntityLink(curRefNode, LINKED); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 510 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 511 | /* |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 512 | * resolve the reference and update the partial resolution stack |
| 513 | * with any further recursive references |
| 514 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 515 | addUnresolvedRecursiveReferenceToStack(curRefNode); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | |
| 519 | /* |
Gaurav Agrawal | 8a966b9 | 2016-08-20 00:07:27 +0530 | [diff] [blame] | 520 | * In case prefix is not present or it's self prefix it's a candidate for inter-file |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 521 | * resolution via include list. |
| 522 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 523 | if (getRefPrefix() == null || getRefPrefix() |
| 524 | .contentEquals(curRefResolver.getPrefix())) { |
| 525 | ((Resolvable) getCurEntityToResolveFromStack()) |
| 526 | .setResolvableStatus(INTRA_FILE_RESOLVED); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 530 | /** |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 531 | * Returns the status of the referred identity found for base/identityref. |
| 532 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 533 | * @param nodeName the name of the base node |
| 534 | * identifier/identityref node identifier |
| 535 | * @param ancestorRefNode the parent node of base/identityref |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 536 | * @return status of referred base/identityref |
| 537 | * @throws DataModelException a violation of data model rules |
| 538 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 539 | private boolean isIdentityReferenceFound(String nodeName, YangNode ancestorRefNode) |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 540 | throws DataModelException { |
| 541 | |
| 542 | // When child is not present return. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 543 | if (ancestorRefNode.getChild() == null) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 544 | return false; |
| 545 | } |
| 546 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 547 | ancestorRefNode = ancestorRefNode.getChild(); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 548 | |
| 549 | // Checks all the siblings under the node and returns the matched node. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 550 | YangNode nodeFound = isReferredNodeInSiblingProcessedForIdentity(ancestorRefNode, |
| 551 | nodeName); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 552 | |
| 553 | if (nodeFound != null) { |
| 554 | // Adds reference link of entity to the node under resolution. |
| 555 | addReferredEntityLink(nodeFound, LINKED); |
| 556 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 557 | /* |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 558 | * resolve the reference and update the partial resolution stack with any further recursive references |
| 559 | */ |
| 560 | addUnresolvedRecursiveReferenceToStack(nodeFound); |
| 561 | return true; |
| 562 | } |
| 563 | |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | /** |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 568 | * Adds the unresolved constructs to stack which has to be resolved for leafref. |
| 569 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 570 | * @param leavesInfo YANG leaf or leaf list which holds the type |
| 571 | * @param ancestorRefNode holder of the YANG leaf or leaf list |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 572 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 573 | private void addUnResolvedLeafRefTypeToStack(T leavesInfo, YangNode ancestorRefNode) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 574 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 575 | YangType refType; |
| 576 | T extendedInfo; |
| 577 | if (leavesInfo instanceof YangLeaf) { |
| 578 | YangLeaf leaf = (YangLeaf) leavesInfo; |
| 579 | refType = leaf.getDataType(); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 580 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 581 | YangLeafList leafList = (YangLeafList) leavesInfo; |
| 582 | refType = leafList.getDataType(); |
| 583 | } |
| 584 | extendedInfo = (T) refType.getDataTypeExtendedInfo(); |
| 585 | addUnResolvedTypeDataToStack(refType, ancestorRefNode, extendedInfo); |
| 586 | } |
| 587 | |
| 588 | //Adds unresolved type info to stack. |
| 589 | private void addUnResolvedTypeDataToStack(YangType refType, YangNode |
| 590 | ancestorRefNode, T extendedInfo) { |
| 591 | YangEntityToResolveInfoImpl<YangLeafRef<?>> unResolvedLeafRef = |
| 592 | new YangEntityToResolveInfoImpl<>(); |
| 593 | YangEntityToResolveInfoImpl<YangType<?>> unResolvedTypeDef = |
| 594 | new YangEntityToResolveInfoImpl<>(); |
| 595 | if (refType.getDataType() == YangDataTypes.LEAFREF) { |
| 596 | unResolvedLeafRef.setEntityToResolve((YangLeafRef<?>) extendedInfo); |
| 597 | unResolvedLeafRef.setHolderOfEntityToResolve(ancestorRefNode); |
| 598 | addInPartialResolvedStack((YangEntityToResolveInfoImpl<T>) unResolvedLeafRef); |
| 599 | } else if (refType.getDataType() == YangDataTypes.DERIVED) { |
| 600 | unResolvedTypeDef.setEntityToResolve(refType); |
| 601 | unResolvedTypeDef.setHolderOfEntityToResolve(ancestorRefNode); |
| 602 | addInPartialResolvedStack((YangEntityToResolveInfoImpl<T>) unResolvedTypeDef); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | /** |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 607 | * Returns feature holder(module/sub-module node) . |
| 608 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 609 | * @param ancestorRefNode if-feature holder node |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 610 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 611 | private YangFeatureHolder getFeatureHolder(YangNode ancestorRefNode) { |
| 612 | while (ancestorRefNode != null) { |
| 613 | if (ancestorRefNode instanceof YangFeatureHolder) { |
| 614 | return (YangFeatureHolder) ancestorRefNode; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 615 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 616 | ancestorRefNode = ancestorRefNode.getParent(); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 617 | } |
| 618 | return null; |
| 619 | } |
| 620 | |
| 621 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 622 | * Checks if the reference in self file or in external file. |
| 623 | * |
| 624 | * @return true if self file reference, false otherwise |
| 625 | * @throws DataModelException a violation of data model rules |
| 626 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 627 | private boolean isCandidateForSelfFileReference() |
| 628 | throws DataModelException { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 629 | String prefix = getRefPrefix(); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 630 | return prefix == null || prefix.contentEquals(curRefResolver.getPrefix()); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 634 | * Checks for the referred parent node for the base/identity. |
| 635 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 636 | * @param refNode potential referred node |
| 637 | * @return the referred parent node of base/identity. |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 638 | * @throws DataModelException data model errors |
| 639 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 640 | private YangNode isReferredNodeInSiblingProcessedForIdentity(YangNode refNode, |
| 641 | String refName) |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 642 | throws DataModelException { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 643 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 644 | while (refNode != null) { |
| 645 | if (refNode instanceof YangIdentity) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 646 | // Check if the potential referred node is the actual referred node |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 647 | if (isReferredNodeForIdentity(refNode, refName)) { |
| 648 | return refNode; |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 649 | } |
| 650 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 651 | refNode = refNode.getNextSibling(); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 652 | } |
| 653 | return null; |
| 654 | } |
| 655 | |
| 656 | /** |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 657 | * Checks if the current reference node name and the name in the base/identityref base are equal. |
| 658 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 659 | * @param curRefNode the node where the reference is pointed |
| 660 | * @param name name of the base in the base/identityref base |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 661 | * @return status of the match between the name |
| 662 | * @throws DataModelException a violation of data model rules |
| 663 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 664 | private boolean isReferredNodeForIdentity(YangNode curRefNode, String name) |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 665 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 666 | T entity = getCurEntityToResolveFromStack(); |
| 667 | if (entity instanceof YangIdentityRef || entity instanceof YangBase) { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 668 | |
| 669 | //Check if name of node name matches with the current reference node. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 670 | return curRefNode.getName().contentEquals(name); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 671 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 672 | throw new DataModelException(getErrorMsg( |
| 673 | INVALID_ENTITY, curRefNode.getName(), curRefNode.getLineNumber(), |
| 674 | curRefNode.getCharPosition(), curRefNode.getFileName())); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
| 678 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 679 | * Checks for the referred node defined in a ancestor scope. |
| 680 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 681 | * @param refNode potential referred node |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 682 | * @return status of resolution and updating the partial resolved stack with |
| 683 | * the any recursive references |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 684 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 685 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 686 | private boolean isReferredNodeInSiblingListProcessed(YangNode refNode) |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 687 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 688 | while (refNode != null) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 689 | |
| 690 | // Check if the potential referred node is the actual referred node |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 691 | if (isReferredNode(refNode)) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 692 | |
| 693 | // Adds reference link of entity to the node under resolution. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 694 | addReferredEntityLink(refNode, LINKED); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 695 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 696 | /* |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 697 | * resolve the reference and update the partial resolution stack |
| 698 | * with any further recursive references |
| 699 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 700 | addUnresolvedRecursiveReferenceToStack(refNode); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 701 | |
| 702 | /* |
| 703 | * return true, since the reference is linked and any recursive |
| 704 | * unresolved references is added to the stack |
| 705 | */ |
| 706 | return true; |
| 707 | } |
| 708 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 709 | refNode = refNode.getNextSibling(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 710 | } |
| 711 | return false; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Checks if the potential referred node is the actual referred node. |
| 716 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 717 | * @param refNode typedef/grouping node |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 718 | * @return true if node is of resolve type otherwise false |
| 719 | * @throws DataModelException a violation of data model rules |
| 720 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 721 | private boolean isReferredNode(YangNode refNode) |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 722 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 723 | T entity = getCurEntityToResolveFromStack(); |
| 724 | if (entity instanceof YangType) { |
| 725 | if (refNode instanceof YangTypeDef) { |
| 726 | return isNodeNameSameAsResolutionInfoName(refNode); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 727 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 728 | } else if (entity instanceof YangUses) { |
| 729 | if (refNode instanceof YangGrouping) { |
| 730 | return isNodeNameSameAsResolutionInfoName(refNode); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 731 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 732 | } else if (entity instanceof YangIfFeature) { |
| 733 | if (refNode instanceof YangFeatureHolder) { |
| 734 | return isNodeNameSameAsResolutionInfoName(refNode); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 735 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 736 | } else if (entity instanceof YangBase || entity instanceof YangIdentityRef) { |
| 737 | if (refNode instanceof YangIdentity) { |
| 738 | return isNodeNameSameAsResolutionInfoName(refNode); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 739 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 740 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 741 | throw new DataModelException(getErrorMsg( |
| 742 | LINKER_ERROR, refNode.getName(), refNode.getLineNumber(), |
| 743 | refNode.getCharPosition(), refNode.getFileName())); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 744 | } |
| 745 | return false; |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * Checks if node name is same as name in resolution info, i.e. name of |
| 750 | * typedef/grouping is same as name of type/uses. |
| 751 | * |
| 752 | * @param node typedef/grouping node |
| 753 | * @return true if node name is same as name in resolution info, otherwise |
| 754 | * false |
| 755 | * @throws DataModelException a violation of data model rules |
| 756 | */ |
| 757 | |
| 758 | private boolean isNodeNameSameAsResolutionInfoName(YangNode node) |
| 759 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 760 | T entity = getCurEntityToResolveFromStack(); |
| 761 | if (entity instanceof YangType) { |
| 762 | return node.getName().contentEquals(((YangType<?>) entity) |
| 763 | .getDataTypeName()); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 764 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 765 | if (entity instanceof YangUses) { |
| 766 | return node.getName().contentEquals(((YangUses) entity).getName()); |
| 767 | } |
| 768 | if (entity instanceof YangIfFeature) { |
| 769 | return isFeatureDefinedInNode(node); |
| 770 | } |
| 771 | if (entity instanceof YangBase) { |
| 772 | return node.getName().contentEquals((( |
| 773 | YangBase) entity).getBaseIdentifier().getName()); |
| 774 | } |
| 775 | if (entity instanceof YangIdentityRef) { |
| 776 | return node.getName().contentEquals(((YangIdentityRef) entity).getName()); |
| 777 | } |
| 778 | throw new DataModelException(getErrorMsg( |
| 779 | INVALID_RESOLVED_ENTITY, node.getName(), node.getLineNumber(), |
| 780 | node.getCharPosition(), node.getFileName())); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 781 | } |
| 782 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 783 | private boolean isFeatureDefinedInNode(YangNode node) { |
| 784 | T entity = getCurEntityToResolveFromStack(); |
| 785 | YangNodeIdentifier ifFeature = ((YangIfFeature) entity).getName(); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 786 | List<YangFeature> featureList = ((YangFeatureHolder) node).getFeatureList(); |
| 787 | if (featureList != null && !featureList.isEmpty()) { |
| 788 | Iterator<YangFeature> iterator = featureList.iterator(); |
| 789 | while (iterator.hasNext()) { |
| 790 | YangFeature feature = iterator.next(); |
| 791 | if (ifFeature.getName().equals(feature.getName())) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 792 | ((YangIfFeature) entity).setReferredFeature(feature); |
| 793 | ((YangIfFeature) entity).setReferredFeatureHolder(node); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 794 | return true; |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | return false; |
| 799 | } |
| 800 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 801 | /** |
| 802 | * Adds reference of grouping/typedef in uses/type. |
| 803 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 804 | * @param refNode grouping/typedef node being referred |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 805 | * @param linkedStatus linked status if success. |
| 806 | * @throws DataModelException a violation of data model rules |
| 807 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 808 | private void addReferredEntityLink(YangNode refNode, ResolvableStatus linkedStatus) |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 809 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 810 | T entity = getCurEntityToResolveFromStack(); |
| 811 | if (entity instanceof YangType) { |
| 812 | YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) (( |
| 813 | YangType<?>) entity).getDataTypeExtendedInfo(); |
| 814 | derivedInfo.setReferredTypeDef((YangTypeDef) refNode); |
| 815 | } else if (entity instanceof YangUses) { |
| 816 | ((YangUses) entity).setRefGroup((YangGrouping) refNode); |
| 817 | } else if (entity instanceof YangBase) { |
| 818 | ((YangBase) entity).setReferredIdentity((YangIdentity) refNode); |
| 819 | } else if (entity instanceof YangIdentityRef) { |
| 820 | ((YangIdentityRef) entity).setReferredIdentity((YangIdentity) refNode); |
| 821 | } else if (!(entity instanceof YangIfFeature) && |
| 822 | !(entity instanceof YangLeafRef)) { |
| 823 | throw new DataModelException(getErrorMsg( |
| 824 | LINKER_ERROR, refNode.getName(), refNode.getLineNumber(), |
| 825 | refNode.getCharPosition(), refNode.getFileName())); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 826 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 827 | // Sets the resolution status in inside the type/uses. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 828 | ((Resolvable) entity).setResolvableStatus(linkedStatus); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Checks if type/grouping has further reference to typedef/ unresolved |
| 833 | * uses. Add it to the partial resolve stack and return the status of |
| 834 | * addition to stack. |
| 835 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 836 | * @param refNode grouping/typedef node |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 837 | * @throws DataModelException a violation of data model rules |
| 838 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 839 | private void addUnresolvedRecursiveReferenceToStack(YangNode refNode) |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 840 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 841 | T entity = getCurEntityToResolveFromStack(); |
| 842 | if (entity instanceof YangType) { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 843 | |
| 844 | //Checks if typedef type is derived |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 845 | if (((YangTypeDef) refNode).getTypeDefBaseType() |
| 846 | .getDataType() == YangDataTypes.DERIVED) { |
| 847 | addEntityToStack((T) ((YangTypeDef) refNode).getTypeDefBaseType(), |
| 848 | refNode); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 849 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 850 | } else if (entity instanceof YangUses) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 851 | /* |
| 852 | * Search if the grouping has any un resolved uses child, if so |
| 853 | * return true, else return false. |
| 854 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 855 | addUnResolvedUsesToStack(refNode); |
| 856 | } else if (entity instanceof YangIfFeature) { |
| 857 | addUnResolvedIfFeatureToStack(refNode); |
| 858 | } else if (entity instanceof YangLeafRef) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 859 | // do nothing , referred node is already set |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 860 | throw new DataModelException(getErrorMsg( |
| 861 | INVALID_RESOLVED_ENTITY, refNode.getName(), refNode.getLineNumber(), |
| 862 | refNode.getCharPosition(), refNode.getFileName())); |
| 863 | } else if (entity instanceof YangBase || entity instanceof YangIdentityRef) { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 864 | |
| 865 | //Search if the identity has any un resolved base, if so return true, else return false. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 866 | addUnResolvedBaseToStack(refNode); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 867 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 868 | throw new DataModelException(getErrorMsg( |
| 869 | LINKER_ERROR, refNode.getName(), refNode.getLineNumber(), |
| 870 | refNode.getCharPosition(), refNode.getFileName())); |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 871 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * Returns if there is any unresolved uses in grouping. |
| 877 | * |
| 878 | * @param node grouping/typedef node |
| 879 | */ |
| 880 | private void addUnResolvedUsesToStack(YangNode node) { |
| 881 | |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 882 | //Search the grouping node's children for presence of uses node. |
Vidyashree Rama | 72719fa | 2016-07-15 14:06:56 +0530 | [diff] [blame] | 883 | TraversalType curTraversal = ROOT; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 884 | YangNode curNode = node.getChild(); |
| 885 | while (curNode != null) { |
Vidyashree Rama | 72719fa | 2016-07-15 14:06:56 +0530 | [diff] [blame] | 886 | if (curNode.getName().equals(node.getName())) { |
| 887 | // if we have traversed all the child nodes, then exit from loop |
| 888 | return; |
| 889 | } |
| 890 | |
| 891 | // if child nodes has uses, then add it to resolution stack |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 892 | if (curNode instanceof YangUses) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 893 | addEntityToStack((T) curNode, node); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 894 | } |
Vidyashree Rama | 72719fa | 2016-07-15 14:06:56 +0530 | [diff] [blame] | 895 | |
| 896 | // Traversing all the child nodes of grouping |
| 897 | if (curTraversal != PARENT && curNode.getChild() != null) { |
| 898 | curTraversal = CHILD; |
| 899 | curNode = curNode.getChild(); |
| 900 | } else if (curNode.getNextSibling() != null) { |
| 901 | curTraversal = SIBILING; |
| 902 | curNode = curNode.getNextSibling(); |
| 903 | } else { |
| 904 | curTraversal = PARENT; |
| 905 | curNode = curNode.getParent(); |
| 906 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | /** |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 911 | * Returns if there is any unresolved if-feature in feature. |
| 912 | * |
| 913 | * @param node module/submodule node |
| 914 | */ |
| 915 | private void addUnResolvedIfFeatureToStack(YangNode node) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 916 | YangFeature refFeature = ((YangIfFeature) getCurEntityToResolveFromStack()) |
| 917 | .getReferredFeature(); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 918 | List<YangIfFeature> ifFeatureList = refFeature.getIfFeatureList(); |
| 919 | if (ifFeatureList != null && !ifFeatureList.isEmpty()) { |
| 920 | Iterator<YangIfFeature> ifFeatureIterator = ifFeatureList.iterator(); |
| 921 | while (ifFeatureIterator.hasNext()) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 922 | addEntityToStack((T) ifFeatureIterator.next(), node); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | /** |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 928 | * Returns if there is any unresolved base in identity. |
| 929 | * |
| 930 | * @param node module/submodule node |
| 931 | */ |
| 932 | private void addUnResolvedBaseToStack(YangNode node) { |
| 933 | |
| 934 | YangIdentity curNode = (YangIdentity) node; |
| 935 | if (curNode.getBaseNode() != null) { |
| 936 | if (curNode.getBaseNode().getResolvableStatus() != RESOLVED) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 937 | addEntityToStack((T) curNode.getBaseNode(), node); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | } |
| 941 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 942 | private void addEntityToStack(T entity, YangNode holder) { |
| 943 | YangEntityToResolveInfoImpl<T> unResolvedEntityInfo = |
| 944 | new YangEntityToResolveInfoImpl<>(); |
| 945 | unResolvedEntityInfo.setEntityToResolve(entity); |
| 946 | unResolvedEntityInfo.setHolderOfEntityToResolve(holder); |
| 947 | addInPartialResolvedStack(unResolvedEntityInfo); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Sets stack of YANG type with partially resolved YANG construct hierarchy. |
| 952 | * |
| 953 | * @param partialResolvedInfo partial resolved YANG construct stack |
| 954 | */ |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 955 | private void addInPartialResolvedStack(YangEntityToResolveInfoImpl<T> partialResolvedInfo) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 956 | partialResolvedStack.push(partialResolvedInfo); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | /** |
| 960 | * Retrieves the next entity in the stack that needs to be resolved. It is |
| 961 | * assumed that the caller ensures that the stack is not empty. |
| 962 | * |
| 963 | * @return next entity in the stack that needs to be resolved |
| 964 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 965 | private T getCurEntityToResolveFromStack() { |
| 966 | return partialResolvedStack.peek().getEntityToResolve(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 967 | } |
| 968 | |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 969 | @Override |
| 970 | public YangEntityToResolveInfoImpl<T> getEntityToResolveInfo() { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 971 | return entityToResolveInfo; |
| 972 | } |
| 973 | |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 974 | @Override |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 975 | public void linkInterFile(YangReferenceResolver dataModelRootNode) |
| 976 | throws DataModelException { |
| 977 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 978 | curRefResolver = dataModelRootNode; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 979 | |
| 980 | // Current node to resolve, it can be a YANG type or YANG uses. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 981 | T entityToResolve = entityToResolveInfo.getEntityToResolve(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 982 | |
| 983 | // Check if linking is already done |
| 984 | if (entityToResolve instanceof Resolvable) { |
| 985 | Resolvable resolvable = (Resolvable) entityToResolve; |
| 986 | if (resolvable.getResolvableStatus() == RESOLVED) { |
| 987 | return; |
| 988 | } |
| 989 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 990 | throw new DataModelException(UNRESOLVABLE); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 991 | } |
| 992 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 993 | if (entityToResolve instanceof YangXPathResolver && |
| 994 | !(entityToResolve instanceof YangLeafRef)) { |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 995 | //Process x-path linking. |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 996 | processXPathLinking(entityToResolve, dataModelRootNode); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 997 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 998 | } else { |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 999 | // Push the initial entity to resolve in stack. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1000 | addInPartialResolvedStack(entityToResolveInfo); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1001 | // Inter file linking and resolution. |
| 1002 | linkInterFileAndResolve(); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1003 | addDerivedRefTypeToRefTypeResolutionList(); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Process x-path linking for augment and leaf-ref. |
| 1009 | * |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1010 | * @param entityToResolve entity to resolve |
| 1011 | * @param root root node |
janani b | 3a3e326 | 2016-10-19 00:23:28 +0530 | [diff] [blame] | 1012 | * @throws DataModelException if there is a data model error |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1013 | */ |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1014 | private void processXPathLinking(T entityToResolve, |
janani b | 3a3e326 | 2016-10-19 00:23:28 +0530 | [diff] [blame] | 1015 | YangReferenceResolver root) |
| 1016 | throws DataModelException { |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1017 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1018 | YangXpathLinker<T> xPathLinker = new YangXpathLinker<T>(); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1019 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1020 | if (entityToResolve instanceof YangAugment) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1021 | YangNode targetNode; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1022 | YangAugment augment = (YangAugment) entityToResolve; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1023 | targetNode = xPathLinker |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 1024 | .processXpathLinking(augment.getTargetNode(), (YangNode) |
| 1025 | root, AUGMENT_LINKING); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1026 | if (targetNode != null) { |
| 1027 | if (targetNode instanceof YangAugmentableNode) { |
| 1028 | detectCollisionForAugmentedNode(targetNode, augment); |
| 1029 | ((YangAugmentableNode) targetNode).addAugmentation(augment); |
| 1030 | augment.setAugmentedNode(targetNode); |
Vidyashree Rama | b20ba9e | 2016-09-02 14:12:25 +0530 | [diff] [blame] | 1031 | setAugmentedFlagInAncestors(targetNode); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1032 | Resolvable resolvable = (Resolvable) entityToResolve; |
| 1033 | resolvable.setResolvableStatus(RESOLVED); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1034 | if (targetNode instanceof YangInput) { |
| 1035 | xPathLinker.addInModuleIfInput(augment, (YangNode) root); |
| 1036 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1037 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1038 | throw new LinkerException(getErrorMsg( |
| 1039 | INVALID_TARGET + targetNode.getNodeType(), |
| 1040 | augment.getName(), augment.getLineNumber(), |
| 1041 | augment.getCharPosition(), augment.getFileName())); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1042 | } |
| 1043 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1044 | throw new LinkerException(getErrorMsg( |
| 1045 | FAILED_TO_LINK, augment.getName(), augment |
| 1046 | .getLineNumber(), augment.getCharPosition(), |
| 1047 | augment.getFileName())); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1048 | } |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 1049 | } else if (entityToResolve instanceof YangCompilerAnnotation) { |
| 1050 | YangNode targetNode; |
| 1051 | YangCompilerAnnotation ca = (YangCompilerAnnotation) entityToResolve; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 1052 | targetNode = xPathLinker.processXpathLinking(ca.getAtomicPathList(), |
| 1053 | (YangNode) root, |
| 1054 | AUGMENT_LINKING); |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 1055 | if (targetNode != null) { |
| 1056 | if (targetNode instanceof YangList) { |
| 1057 | ((YangList) targetNode).setCompilerAnnotation( |
| 1058 | (YangCompilerAnnotation) entityToResolve); |
| 1059 | Resolvable resolvable = (Resolvable) entityToResolve; |
| 1060 | resolvable.setResolvableStatus(RESOLVED); |
| 1061 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1062 | throw new LinkerException(getErrorMsg( |
| 1063 | INVALID_TARGET + targetNode.getNodeType(), ca.getPath(), |
| 1064 | ca.getLineNumber(), ca.getCharPosition(), ca.getFileName())); |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 1065 | } |
| 1066 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1067 | throw new LinkerException(getErrorMsg( |
| 1068 | FAILED_TO_FIND_ANNOTATION, ca.getPath(), ca.getLineNumber(), |
| 1069 | ca.getCharPosition(), ca.getFileName())); |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 1070 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1071 | } else if (entityToResolve instanceof YangLeafRef) { |
| 1072 | YangLeafRef leafRef = (YangLeafRef) entityToResolve; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1073 | Object target = xPathLinker.processLeafRefXpathLinking( |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 1074 | leafRef.getAtomicPath(), (YangNode) root, leafRef, LEAF_REF_LINKING); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1075 | if (target != null) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 1076 | YangLeaf leaf; |
| 1077 | YangLeafList leafList; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1078 | leafRef.setReferredLeafOrLeafList(target); |
| 1079 | if (target instanceof YangLeaf) { |
| 1080 | leaf = (YangLeaf) target; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1081 | leafRef.setResolvableStatus(INTER_FILE_LINKED); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1082 | addUnResolvedLeafRefTypeToStack((T) leaf, entityToResolveInfo |
| 1083 | .getHolderOfEntityToResolve()); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1084 | } else { |
| 1085 | leafList = (YangLeafList) target; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1086 | leafRef.setResolvableStatus(INTER_FILE_LINKED); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1087 | addUnResolvedLeafRefTypeToStack( |
| 1088 | (T) leafList, entityToResolveInfo.getHolderOfEntityToResolve()); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1089 | } |
janani b | 3a3e326 | 2016-10-19 00:23:28 +0530 | [diff] [blame] | 1090 | fillPathPredicates(leafRef); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1091 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1092 | LinkerException ex = new LinkerException( |
| 1093 | FAILED_TO_FIND_LEAD_INFO_HOLDER + leafRef.getPath()); |
| 1094 | ex.setCharPosition(leafRef.getCharPosition()); |
| 1095 | ex.setLine(leafRef.getLineNumber()); |
| 1096 | ex.setFileName(leafRef.getFileName()); |
| 1097 | throw ex; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1098 | } |
| 1099 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /** |
| 1103 | * Returns the referenced prefix of entity under resolution. |
| 1104 | * |
| 1105 | * @return referenced prefix of entity under resolution |
| 1106 | * @throws DataModelException a violation in data model rule |
| 1107 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1108 | private String getRefPrefix() |
| 1109 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1110 | T entity = getCurEntityToResolveFromStack(); |
| 1111 | if (entity instanceof YangType) { |
| 1112 | return ((YangType<?>) entity).getPrefix(); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 1113 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1114 | if (entity instanceof YangUses) { |
| 1115 | return ((YangUses) entity).getPrefix(); |
| 1116 | } |
| 1117 | if (entity instanceof YangIfFeature) { |
| 1118 | return ((YangIfFeature) entity).getPrefix(); |
| 1119 | } |
| 1120 | if (entity instanceof YangBase) { |
| 1121 | return ((YangBase) entity).getBaseIdentifier() |
| 1122 | .getPrefix(); |
| 1123 | } |
| 1124 | if (entity instanceof YangIdentityRef) { |
| 1125 | return ((YangIdentityRef) entity).getPrefix(); |
| 1126 | } |
| 1127 | throw new DataModelException(LINKER_ERROR); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1131 | * Performs inter file linking and resolution. |
| 1132 | * |
| 1133 | * @throws DataModelException a violation in data model rule |
| 1134 | */ |
| 1135 | private void linkInterFileAndResolve() |
| 1136 | throws DataModelException { |
| 1137 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1138 | while (!partialResolvedStack.isEmpty()) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1139 | |
| 1140 | // Current node to resolve, it can be a YANG type or YANG uses. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1141 | T entityToResolve = getCurEntityToResolveFromStack(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1142 | // Check if linking is already done |
| 1143 | if (entityToResolve instanceof Resolvable) { |
| 1144 | |
| 1145 | Resolvable resolvable = (Resolvable) entityToResolve; |
| 1146 | switch (resolvable.getResolvableStatus()) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1147 | case RESOLVED: |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1148 | /* |
| 1149 | * If the entity is already resolved in the stack, then pop |
| 1150 | * it and continue with the remaining stack elements to |
| 1151 | * resolve |
| 1152 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1153 | partialResolvedStack.pop(); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1154 | break; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1155 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1156 | case INTER_FILE_LINKED: |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1157 | /* |
| 1158 | * If the top of the stack is already linked then resolve |
| 1159 | * the references and pop the entity and continue with |
| 1160 | * remaining stack elements to resolve |
| 1161 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1162 | resolveTopOfStack(); |
| 1163 | partialResolvedStack.pop(); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1164 | break; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1165 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1166 | case INTRA_FILE_RESOLVED: |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1167 | /* |
| 1168 | * If the top of the stack is intra file resolved then check |
| 1169 | * if top of stack is linked, if not link it using |
| 1170 | * import/include list and push the linked referred entity |
| 1171 | * to the stack, otherwise only push it to the stack. |
| 1172 | */ |
| 1173 | linkInterFileTopOfStackRefUpdateStack(); |
| 1174 | break; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1175 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1176 | case UNDEFINED: |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1177 | /* |
| 1178 | * In case of if-feature resolution, if referred "feature" is not |
| 1179 | * defined then the resolvable status will be undefined. |
| 1180 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1181 | partialResolvedStack.pop(); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1182 | break; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1183 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1184 | default: |
| 1185 | throw new DataModelException(INVALID_LINKER_STATE); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1186 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1187 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1188 | throw new DataModelException(INVALID_RESOLVED_ENTITY); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1189 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1190 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | /** |
| 1194 | * Links the top of the stack if it's inter-file and update stack. |
| 1195 | * |
| 1196 | * @throws DataModelException data model error |
| 1197 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 1198 | private void linkInterFileTopOfStackRefUpdateStack() |
| 1199 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1200 | T entity = getCurEntityToResolveFromStack(); |
| 1201 | if (entity instanceof YangLeafRef) { |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1202 | // When leafref path comes with relative path, it will be converted to absolute path. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1203 | setAbsolutePathFromRelativePathInLeafref(entity); |
| 1204 | processXPathLinking(entity, curRefResolver); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1205 | return; |
| 1206 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1207 | /* |
| 1208 | * Obtain the referred node of top of stack entity under resolution |
| 1209 | */ |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 1210 | T refNode = getRefNode(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1211 | |
| 1212 | /* |
| 1213 | * Check for null for scenario when it's not linked and inter-file |
| 1214 | * linking is required. |
| 1215 | */ |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 1216 | if (refNode == null) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1217 | |
| 1218 | /* |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 1219 | * Check if prefix is null or not, to identify whether to search in |
| 1220 | * import list or include list. |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1221 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1222 | if (getRefPrefix() != null && !getRefPrefix() |
| 1223 | .contentEquals(curRefResolver.getPrefix())) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1224 | if (resolveWithImport()) { |
| 1225 | return; |
| 1226 | } |
| 1227 | } else { |
| 1228 | if (resolveWithInclude()) { |
| 1229 | return; |
| 1230 | } |
| 1231 | } |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1232 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1233 | if (entity instanceof YangIfFeature) { |
| 1234 | ((YangIfFeature) entity).setResolvableStatus(UNDEFINED); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1235 | return; |
| 1236 | } |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 1237 | // If current entity is still not resolved, then |
| 1238 | // linking/resolution has failed. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1239 | |
| 1240 | DataModelException ex = new DataModelException( |
| 1241 | getErrorInfoForLinker(entity)); |
| 1242 | ex.setLine(getLineNumber()); |
| 1243 | ex.setCharPosition(getCharPosition()); |
| 1244 | throw ex; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1245 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1246 | ((Resolvable) entity).setResolvableStatus(INTER_FILE_LINKED); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 1247 | addUnresolvedRecursiveReferenceToStack((YangNode) refNode); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * Sets the leafref with absolute path from the relative path. |
| 1253 | * |
| 1254 | * @param resolutionInfo information about the YANG construct which has to be resolved |
| 1255 | * @throws DataModelException a violation of data model rules |
| 1256 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1257 | private void setAbsolutePathFromRelativePathInLeafref(T resolutionInfo) |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 1258 | throws DataModelException { |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1259 | if (resolutionInfo instanceof YangLeafRef) { |
| 1260 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1261 | YangNode leafParent = ((YangLeafRef) resolutionInfo) |
janani b | 3a3e326 | 2016-10-19 00:23:28 +0530 | [diff] [blame] | 1262 | .getParentNode(); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1263 | YangLeafRef leafref = (YangLeafRef) resolutionInfo; |
| 1264 | |
| 1265 | // Checks if the leafref has relative path in it. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1266 | if (leafref.getPathType() == RELATIVE_PATH) { |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1267 | YangRelativePath relativePath = leafref.getRelativePath(); |
| 1268 | List<YangAtomicPath> absoluteInRelative = relativePath.getAtomicPathList(); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1269 | int ancestorCount = relativePath.getAncestorNodeCount(); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1270 | |
| 1271 | // Gets the root node from the ancestor count. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1272 | T nodeOrAugmentList = |
| 1273 | getRootNodeWithAncestorCountForLeafref(ancestorCount, leafParent, |
| 1274 | leafref); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1275 | if (nodeOrAugmentList instanceof YangNode) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1276 | StringBuilder name = new StringBuilder(); |
| 1277 | StringBuilder prefix = new StringBuilder(); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1278 | YangNode rootNode = (YangNode) nodeOrAugmentList; |
| 1279 | // Forms a new absolute path from the relative path |
| 1280 | while (!(rootNode instanceof YangReferenceResolver)) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1281 | name.append(rootNode.getName()); |
| 1282 | prefix.append(SLASH_FOR_STRING).append(name.reverse()); |
| 1283 | name.delete(0, name.length()); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1284 | rootNode = rootNode.getParent(); |
| 1285 | if (rootNode == null) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1286 | throw new DataModelException(INVALID_TREE); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1287 | } |
| 1288 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1289 | prefix.reverse(); |
| 1290 | fillAbsolutePathValuesInLeafref(leafref, prefix.toString(), |
| 1291 | absoluteInRelative); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1292 | } else { |
| 1293 | List<String> listOfAugment = (List<String>) nodeOrAugmentList; |
| 1294 | Iterator<String> listOfAugmentIterator = listOfAugment.listIterator(); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1295 | StringBuilder augment = new StringBuilder(EMPTY_STRING); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1296 | while (listOfAugmentIterator.hasNext()) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1297 | augment.append(SLASH_FOR_STRING) |
| 1298 | .append(listOfAugmentIterator.next()); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1299 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1300 | fillAbsolutePathValuesInLeafref(leafref, augment.toString(), |
| 1301 | absoluteInRelative); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 1302 | } |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 1303 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | /** |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1308 | * Fills the absolute path values in the leafref from relative path. |
| 1309 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1310 | * @param leafref instance of YANG leafref |
| 1311 | * @param path path name which has to be prefixed to relative path |
| 1312 | * @param relative atomic paths in relative |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1313 | * @throws DataModelException a violation of data model rules |
| 1314 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1315 | private void fillAbsolutePathValuesInLeafref(YangLeafRef leafref, String path, |
| 1316 | List<YangAtomicPath> relative) |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 1317 | throws DataModelException { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1318 | leafref.setPathType(ABSOLUTE_PATH); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1319 | String[] pathName = new String[0]; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1320 | if (path != null && !path.equals(EMPTY_STRING)) { |
| 1321 | pathName = path.split(SLASH_FOR_STRING); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1322 | } |
| 1323 | List<YangAtomicPath> finalListForAbsolute = new LinkedList<>(); |
| 1324 | for (String value : pathName) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1325 | if (value != null && !value.isEmpty() && !value.equals(EMPTY_STRING)) { |
| 1326 | YangNodeIdentifier nodeId = getValidNodeIdentifier(value, PATH_DATA); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1327 | YangAtomicPath atomicPath = new YangAtomicPath(); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1328 | atomicPath.setNodeIdentifier(nodeId); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1329 | finalListForAbsolute.add(atomicPath); |
| 1330 | } |
| 1331 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1332 | if (relative != null && !relative.isEmpty()) { |
| 1333 | Iterator<YangAtomicPath> pathIt = relative.listIterator(); |
| 1334 | while (pathIt.hasNext()) { |
| 1335 | YangAtomicPath yangAtomicPath = pathIt.next(); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1336 | finalListForAbsolute.add(yangAtomicPath); |
| 1337 | } |
| 1338 | leafref.setAtomicPath(finalListForAbsolute); |
| 1339 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1340 | DataModelException ex = new DataModelException(getLeafRefErrorInfo(leafref)); |
| 1341 | ex.setCharPosition(leafref.getCharPosition()); |
| 1342 | ex.setLine(leafref.getLineNumber()); |
| 1343 | ex.setFileName(leafref.getFileName()); |
| 1344 | throw ex; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * Returns the root parent with respect to the ancestor count from leafref. |
| 1350 | * |
| 1351 | * @param ancestorCount count of node where parent node can be reached |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1352 | * @param curParent current parent node |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1353 | * @param leafref instance of YANG leafref |
| 1354 | * @return node where the ancestor count stops or augment path name list |
| 1355 | * @throws DataModelException a violation of data model rules |
| 1356 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1357 | private T getRootNodeWithAncestorCountForLeafref( |
| 1358 | int ancestorCount, YangNode curParent, YangLeafRef leafref) |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1359 | throws DataModelException { |
| 1360 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1361 | int curParentCount = 1; |
| 1362 | curParent = skipInvalidDataNodes(curParent, leafref); |
| 1363 | if (curParent instanceof YangAugment) { |
| 1364 | YangAugment augment = (YangAugment) curParent; |
| 1365 | List<String> valueInAugment = getPathWithAugment(augment, |
| 1366 | ancestorCount - curParentCount); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1367 | return (T) valueInAugment; |
| 1368 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1369 | while (curParentCount < ancestorCount) { |
| 1370 | YangNode currentSkippedParent = skipInvalidDataNodes(curParent, leafref); |
| 1371 | if (currentSkippedParent == curParent) { |
| 1372 | if (curParent.getParent() == null) { |
| 1373 | throw new DataModelException(getLeafRefErrorInfo(leafref)); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1374 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1375 | curParent = curParent.getParent(); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1376 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1377 | curParent = currentSkippedParent; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1378 | continue; |
| 1379 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1380 | curParentCount = curParentCount + 1; |
| 1381 | if (curParent instanceof YangAugment) { |
| 1382 | YangAugment augment = (YangAugment) curParent; |
| 1383 | List<String> valueInAugment = getPathWithAugment( |
| 1384 | augment, ancestorCount - curParentCount); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1385 | return (T) valueInAugment; |
| 1386 | } |
| 1387 | } |
| 1388 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1389 | return (T) curParent; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1393 | * Finds and resolves with include list. |
| 1394 | * |
| 1395 | * @return true if resolved, false otherwise |
| 1396 | * @throws DataModelException a violation in data model rule |
| 1397 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1398 | private boolean resolveWithInclude() throws DataModelException { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1399 | /* |
| 1400 | * Run through all the nodes in include list and search for referred |
| 1401 | * typedef/grouping at the root level. |
| 1402 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1403 | for (YangInclude yangInclude : curRefResolver.getIncludeList()) { |
| 1404 | YangNode linkedNode = getLinkedNode(yangInclude.getIncludedNode()); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1405 | if (linkedNode != null) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1406 | return addUnResolvedRefToStack(linkedNode); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | // If referred node can't be found return false. |
| 1410 | return false; |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * Finds and resolves with import list. |
| 1415 | * |
| 1416 | * @return true if resolved, false otherwise |
| 1417 | * @throws DataModelException a violation in data model rule |
| 1418 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1419 | private boolean resolveWithImport() throws DataModelException { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 1420 | |
| 1421 | // Run through import list to find the referred typedef/grouping. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1422 | for (YangImport yangImport : curRefResolver.getImportList()) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1423 | /* |
| 1424 | * Match the prefix attached to entity under resolution with the |
| 1425 | * imported/included module/sub-module's prefix. If found, search |
| 1426 | * for the referred typedef/grouping at the root level. |
| 1427 | */ |
| 1428 | if (yangImport.getPrefixId().contentEquals(getRefPrefix())) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1429 | YangNode linkedNode = getLinkedNode(yangImport.getImportedNode()); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1430 | if (linkedNode != null) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1431 | return addUnResolvedRefToStack(linkedNode); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1432 | } |
| 1433 | /* |
| 1434 | * If referred node can't be found at root level break for loop, |
| 1435 | * and return false. |
| 1436 | */ |
| 1437 | break; |
| 1438 | } |
| 1439 | } |
| 1440 | // If referred node can't be found return false. |
| 1441 | return false; |
| 1442 | } |
| 1443 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1444 | //Add unresolved constructs to stack. |
| 1445 | private boolean addUnResolvedRefToStack(YangNode linkedNode) |
| 1446 | throws DataModelException { |
| 1447 | // Add the link to external entity. |
| 1448 | addReferredEntityLink(linkedNode, INTER_FILE_LINKED); |
| 1449 | |
| 1450 | // Add the type/uses of referred typedef/grouping to the stack. |
| 1451 | addUnresolvedRecursiveReferenceToStack(linkedNode); |
| 1452 | return true; |
| 1453 | } |
| 1454 | |
| 1455 | //Returns linked node from entity of stack. |
| 1456 | private YangNode getLinkedNode(YangNode node) { |
| 1457 | T entity = getCurEntityToResolveFromStack(); |
| 1458 | if (entity instanceof YangType) { |
| 1459 | return findRefTypedef(node); |
| 1460 | } |
| 1461 | if (entity instanceof YangUses) { |
| 1462 | return findRefGrouping(node); |
| 1463 | } |
| 1464 | if (entity instanceof YangIfFeature) { |
| 1465 | return findRefFeature(node); |
| 1466 | } |
| 1467 | if (entity instanceof YangBase) { |
| 1468 | return findRefIdentity(node); |
| 1469 | } |
| 1470 | if (entity instanceof YangIdentityRef) { |
| 1471 | return findRefIdentityRef(node); |
| 1472 | } |
| 1473 | return null; |
| 1474 | } |
| 1475 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1476 | /** |
| 1477 | * Returns referred typedef/grouping node. |
| 1478 | * |
| 1479 | * @return referred typedef/grouping node |
| 1480 | * @throws DataModelException a violation in data model rule |
| 1481 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1482 | private T getRefNode() throws DataModelException { |
| 1483 | T entity = getCurEntityToResolveFromStack(); |
| 1484 | if (entity instanceof YangType) { |
| 1485 | YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) |
| 1486 | ((YangType<?>) entity).getDataTypeExtendedInfo(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1487 | return (T) derivedInfo.getReferredTypeDef(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1488 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1489 | if (entity instanceof YangUses) { |
| 1490 | return (T) ((YangUses) entity).getRefGroup(); |
| 1491 | } |
| 1492 | if (entity instanceof YangIfFeature) { |
| 1493 | return (T) ((YangIfFeature) entity).getReferredFeatureHolder(); |
| 1494 | } |
| 1495 | if (entity instanceof YangLeafRef) { |
| 1496 | return (T) ((YangLeafRef) entity).getReferredLeafOrLeafList(); |
| 1497 | } |
| 1498 | if (entity instanceof YangBase) { |
| 1499 | return (T) ((YangBase) entity).getReferredIdentity(); |
| 1500 | } |
| 1501 | if (entity instanceof YangIdentityRef) { |
| 1502 | return (T) ((YangIdentityRef) entity).getReferredIdentity(); |
| 1503 | } |
| 1504 | throw new DataModelException(LINKER_ERROR); |
| 1505 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * Finds the referred grouping node at the root level of imported/included node. |
| 1510 | * |
| 1511 | * @param refNode module/sub-module node |
| 1512 | * @return referred grouping |
| 1513 | */ |
| 1514 | private YangNode findRefGrouping(YangNode refNode) { |
| 1515 | YangNode tmpNode = refNode.getChild(); |
| 1516 | while (tmpNode != null) { |
| 1517 | if (tmpNode instanceof YangGrouping) { |
| 1518 | if (tmpNode.getName() |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1519 | .equals(((YangUses) getCurEntityToResolveFromStack()) |
| 1520 | .getName())) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1521 | return tmpNode; |
| 1522 | } |
| 1523 | } |
| 1524 | tmpNode = tmpNode.getNextSibling(); |
| 1525 | } |
| 1526 | return null; |
| 1527 | } |
| 1528 | |
| 1529 | /** |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1530 | * Finds the referred feature node at the root level of imported/included node. |
| 1531 | * |
| 1532 | * @param refNode module/sub-module node |
| 1533 | * @return referred feature |
| 1534 | */ |
| 1535 | private YangNode findRefFeature(YangNode refNode) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1536 | T entity = getCurEntityToResolveFromStack(); |
| 1537 | YangNodeIdentifier ifFeature = ((YangIfFeature) entity).getName(); |
| 1538 | List<YangFeature> featureList = ((YangFeatureHolder) refNode) |
| 1539 | .getFeatureList(); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1540 | if (featureList != null && !featureList.isEmpty()) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1541 | for (YangFeature feature : featureList) { |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1542 | if (ifFeature.getName().equals(feature.getName())) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1543 | ((YangIfFeature) entity).setReferredFeature(feature); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1544 | return refNode; |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | return null; |
| 1549 | } |
| 1550 | |
| 1551 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1552 | * Finds the referred typedef node at the root level of imported/included node. |
| 1553 | * |
| 1554 | * @param refNode module/sub-module node |
| 1555 | * @return referred typedef |
| 1556 | */ |
| 1557 | private YangNode findRefTypedef(YangNode refNode) { |
| 1558 | YangNode tmpNode = refNode.getChild(); |
| 1559 | while (tmpNode != null) { |
| 1560 | if (tmpNode instanceof YangTypeDef) { |
| 1561 | if (tmpNode.getName() |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1562 | .equals(((YangType) getCurEntityToResolveFromStack()) |
| 1563 | .getDataTypeName())) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 1564 | return tmpNode; |
| 1565 | } |
| 1566 | } |
| 1567 | tmpNode = tmpNode.getNextSibling(); |
| 1568 | } |
| 1569 | return null; |
| 1570 | } |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 1571 | |
| 1572 | /** |
| 1573 | * Finds the referred identity node at the root level of imported/included node. |
| 1574 | * |
| 1575 | * @param refNode module/sub-module node |
| 1576 | * @return referred identity |
| 1577 | */ |
| 1578 | private YangNode findRefIdentity(YangNode refNode) { |
| 1579 | YangNode tmpNode = refNode.getChild(); |
| 1580 | while (tmpNode != null) { |
| 1581 | if (tmpNode instanceof YangIdentity) { |
| 1582 | if (tmpNode.getName() |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1583 | .equals(((YangBase) getCurEntityToResolveFromStack()) |
| 1584 | .getBaseIdentifier().getName())) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 1585 | return tmpNode; |
| 1586 | } |
| 1587 | } |
| 1588 | tmpNode = tmpNode.getNextSibling(); |
| 1589 | } |
| 1590 | return null; |
| 1591 | } |
| 1592 | |
| 1593 | /** |
| 1594 | * Finds the referred identity node at the root level of imported/included node. |
| 1595 | * |
| 1596 | * @param refNode module/sub-module node |
| 1597 | * @return referred identity |
| 1598 | */ |
| 1599 | private YangNode findRefIdentityRef(YangNode refNode) { |
| 1600 | YangNode tmpNode = refNode.getChild(); |
| 1601 | while (tmpNode != null) { |
| 1602 | if (tmpNode instanceof YangIdentity) { |
| 1603 | if (tmpNode.getName() |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1604 | .equals(((YangIdentityRef) getCurEntityToResolveFromStack()) |
| 1605 | .getBaseIdentity().getName())) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 1606 | return tmpNode; |
| 1607 | } |
| 1608 | } |
| 1609 | tmpNode = tmpNode.getNextSibling(); |
| 1610 | } |
| 1611 | return null; |
| 1612 | } |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 1613 | |
Vidyashree Rama | b20ba9e | 2016-09-02 14:12:25 +0530 | [diff] [blame] | 1614 | /** |
| 1615 | * Sets descendant node augmented flag in ancestors. |
| 1616 | * |
| 1617 | * @param targetNode augmented YANG node |
| 1618 | */ |
| 1619 | private void setAugmentedFlagInAncestors(YangNode targetNode) { |
| 1620 | targetNode = targetNode.getParent(); |
| 1621 | while (targetNode != null) { |
| 1622 | targetNode.setDescendantNodeAugmented(true); |
| 1623 | targetNode = targetNode.getParent(); |
| 1624 | } |
| 1625 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1626 | } |