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