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