Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +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 | |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.TraversalType; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangAtomicPath; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangAugment; |
| 22 | import org.onosproject.yangutils.datamodel.YangAugmentableNode; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangBase; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangCase; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangChoice; |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangGrouping; |
| 27 | import org.onosproject.yangutils.datamodel.YangIdentityRef; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangIfFeature; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangImport; |
| 30 | import org.onosproject.yangutils.datamodel.YangInclude; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 32 | import org.onosproject.yangutils.datamodel.YangLeafList; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.datamodel.YangLeafRef; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 35 | import org.onosproject.yangutils.datamodel.YangNode; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.datamodel.YangType; |
| 39 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 40 | import org.onosproject.yangutils.datamodel.YangUses; |
| 41 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 42 | import org.onosproject.yangutils.datamodel.utils.ResolvableStatus; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 43 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 44 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 45 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 46 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | import java.util.Iterator; |
| 49 | import java.util.List; |
| 50 | import java.util.Set; |
| 51 | import java.util.regex.Pattern; |
| 52 | |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 53 | import static org.onosproject.yangutils.datamodel.TraversalType.CHILD; |
| 54 | import static org.onosproject.yangutils.datamodel.TraversalType.PARENT; |
| 55 | import static org.onosproject.yangutils.datamodel.TraversalType.ROOT; |
| 56 | import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 57 | import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.CASE; |
| 58 | import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.COLLISION_DETECTION; |
| 59 | import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.FAILED_TO_ADD_CASE; |
| 60 | import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.TARGET_NODE; |
| 61 | import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.TARGET_NODE_LEAF_INFO; |
| 62 | import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.getErrorMsg; |
| 63 | import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.getErrorMsgCollision; |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 64 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 65 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 66 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.getYangConstructType; |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 67 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED; |
| 68 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.IDENTITYREF; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 69 | import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangCaseNode; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 70 | import static org.onosproject.yangutils.utils.UtilConstants.BASE_LINKER_ERROR; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 71 | import static org.onosproject.yangutils.utils.UtilConstants.COLON; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 72 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 73 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 74 | import static org.onosproject.yangutils.utils.UtilConstants.FEATURE_LINKER_ERROR; |
| 75 | import static org.onosproject.yangutils.utils.UtilConstants.GROUPING_LINKER_ERROR; |
| 76 | import static org.onosproject.yangutils.utils.UtilConstants.IDENTITYREF_LINKER_ERROR; |
| 77 | import static org.onosproject.yangutils.utils.UtilConstants.IS_INVALID; |
| 78 | import static org.onosproject.yangutils.utils.UtilConstants.LEAFREF_ERROR; |
| 79 | import static org.onosproject.yangutils.utils.UtilConstants.LEAFREF_LINKER_ERROR; |
| 80 | import static org.onosproject.yangutils.utils.UtilConstants.TYPEDEF_LINKER_ERROR; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 81 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 82 | /** |
| 83 | * Represent utilities for YANG linker. |
| 84 | */ |
| 85 | public final class YangLinkerUtils { |
| 86 | |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 87 | private static final int IDENTIFIER_LENGTH = 64; |
| 88 | private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*"); |
| 89 | private static final String XML = "xml"; |
| 90 | |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 91 | private YangLinkerUtils() { |
| 92 | } |
| 93 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 94 | /** |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 95 | * Detects collision between target nodes leaf/leaf-list or child node with augmented leaf/leaf-list or child node. |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 96 | * |
| 97 | * @param targetNode target node |
| 98 | * @param augment augment node |
| 99 | */ |
| 100 | private static void detectCollision(YangNode targetNode, YangAugment augment) { |
| 101 | YangNode targetNodesChild = targetNode.getChild(); |
| 102 | YangNode augmentsChild = augment.getChild(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 103 | if (targetNode instanceof YangChoice) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 104 | addCaseNodeToChoiceTarget(augment); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 105 | } else { |
| 106 | detectCollisionInLeaveHolders(targetNode, augment); |
| 107 | while (augmentsChild != null) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 108 | detectCollisionInChildNodes(targetNodesChild, augmentsChild); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 109 | augmentsChild = augmentsChild.getNextSibling(); |
| 110 | } |
| 111 | } |
| 112 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 113 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 114 | /*Detects collision between leaves/leaf-lists*/ |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 115 | private static void detectCollisionInLeaveHolders(YangNode targetNode, YangAugment augment) { |
| 116 | YangLeavesHolder targetNodesLeavesHolder = (YangLeavesHolder) targetNode; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 117 | if (augment.getListOfLeaf() != null && augment.getListOfLeaf().isEmpty() && |
| 118 | targetNodesLeavesHolder.getListOfLeaf() != null) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 119 | for (YangLeaf leaf : augment.getListOfLeaf()) { |
| 120 | for (YangLeaf targetLeaf : targetNodesLeavesHolder.getListOfLeaf()) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 121 | detectCollision(targetLeaf.getName(), leaf.getName(), |
| 122 | leaf.getLineNumber(), |
| 123 | leaf.getCharPosition(), |
| 124 | leaf.getFileName(), TARGET_NODE_LEAF_INFO); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 125 | } |
| 126 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 127 | } |
| 128 | if (augment.getListOfLeafList() != null && |
| 129 | augment.getListOfLeafList().isEmpty() && |
| 130 | targetNodesLeavesHolder.getListOfLeafList() != null) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 131 | for (YangLeafList leafList : augment.getListOfLeafList()) { |
| 132 | for (YangLeafList targetLeafList : targetNodesLeavesHolder.getListOfLeafList()) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 133 | detectCollision(targetLeafList.getName(), leafList.getName(), |
| 134 | leafList.getLineNumber(), |
| 135 | leafList.getCharPosition(), |
| 136 | leafList.getFileName(), TARGET_NODE_LEAF_INFO); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 137 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 142 | |
| 143 | private static void detectCollision(String first, String second, |
| 144 | int line, int position, String |
| 145 | fileName, String type) { |
| 146 | if (first.equals(second)) { |
| 147 | throw new LinkerException(getErrorMsgCollision( |
| 148 | COLLISION_DETECTION, second, line, position, type, |
| 149 | fileName)); |
| 150 | } |
| 151 | } |
| 152 | |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 153 | /*Detects collision for child nodes.*/ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 154 | private static void detectCollisionInChildNodes(YangNode targetNodesChild, |
| 155 | YangNode augmentsChild) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 156 | while (augmentsChild != null) { |
| 157 | while (targetNodesChild != null) { |
| 158 | if (targetNodesChild.getName().equals(augmentsChild.getName())) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 159 | detectCollision(targetNodesChild.getName(), augmentsChild.getName(), |
| 160 | augmentsChild.getLineNumber(), |
| 161 | augmentsChild.getCharPosition(), |
| 162 | augmentsChild.getFileName(), TARGET_NODE); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 163 | } |
| 164 | targetNodesChild = targetNodesChild.getNextSibling(); |
| 165 | } |
| 166 | augmentsChild = augmentsChild.getNextSibling(); |
| 167 | } |
| 168 | } |
| 169 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 170 | /** |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 171 | * Adds a case node in augment when augmenting a choice node. |
| 172 | * |
| 173 | * @param augment augment node |
| 174 | */ |
| 175 | private static void addCaseNodeToChoiceTarget(YangAugment augment) { |
| 176 | try { |
| 177 | YangNode child = augment.getChild(); |
| 178 | List<YangNode> childNodes = new ArrayList<>(); |
| 179 | while (child != null) { |
| 180 | childNodes.add(child); |
| 181 | child = child.getNextSibling(); |
| 182 | } |
| 183 | augment.setChild(null); |
| 184 | |
| 185 | for (YangNode node : childNodes) { |
| 186 | YangCase javaCase = getYangCaseNode(JAVA_GENERATION); |
| 187 | javaCase.setName(node.getName()); |
| 188 | augment.addChild(javaCase); |
| 189 | node.setParent(javaCase); |
| 190 | node.setNextSibling(null); |
| 191 | node.setPreviousSibling(null); |
| 192 | javaCase.addChild(node); |
| 193 | } |
| 194 | if (augment.getListOfLeaf() != null) { |
| 195 | for (YangLeaf leaf : augment.getListOfLeaf()) { |
| 196 | YangCase javaCase = getYangCaseNode(JAVA_GENERATION); |
| 197 | javaCase.setName(leaf.getName()); |
| 198 | javaCase.addLeaf(leaf); |
| 199 | augment.addChild(javaCase); |
| 200 | |
| 201 | } |
| 202 | augment.getListOfLeaf().clear(); |
| 203 | } |
| 204 | if (augment.getListOfLeafList() != null) { |
| 205 | for (YangLeafList leafList : augment.getListOfLeafList()) { |
| 206 | YangCase javaCase = getYangCaseNode(JAVA_GENERATION); |
| 207 | javaCase.setName(leafList.getName()); |
| 208 | javaCase.addLeafList(leafList); |
| 209 | augment.addChild(javaCase); |
| 210 | } |
| 211 | augment.getListOfLeafList().clear(); |
| 212 | } |
| 213 | |
| 214 | } catch (DataModelException e) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 215 | throw new TranslatorException( |
| 216 | getErrorMsg(FAILED_TO_ADD_CASE, augment.getName(), |
| 217 | augment.getLineNumber(), augment.getCharPosition(), |
| 218 | augment.getFileName())); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 222 | /** |
| 223 | * Returns error messages. |
| 224 | * |
| 225 | * @param resolvable resolvable entity |
| 226 | * @return error message |
| 227 | */ |
| 228 | static String getErrorInfoForLinker(Object resolvable) { |
| 229 | if (resolvable instanceof YangType) { |
| 230 | return TYPEDEF_LINKER_ERROR; |
| 231 | } |
| 232 | if (resolvable instanceof YangUses) { |
| 233 | return GROUPING_LINKER_ERROR; |
| 234 | } |
| 235 | if (resolvable instanceof YangIfFeature) { |
| 236 | return FEATURE_LINKER_ERROR; |
| 237 | } |
| 238 | if (resolvable instanceof YangBase) { |
| 239 | return BASE_LINKER_ERROR; |
| 240 | } |
| 241 | if (resolvable instanceof YangIdentityRef) { |
| 242 | return IDENTITYREF_LINKER_ERROR; |
| 243 | } |
| 244 | return LEAFREF_LINKER_ERROR; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Returns leafref's error message. |
| 249 | * |
| 250 | * @param leafref leaf ref |
| 251 | * @return error message |
| 252 | */ |
| 253 | static String getLeafRefErrorInfo(YangLeafRef leafref) { |
| 254 | return getErrorMsg( |
| 255 | LEAFREF_ERROR + leafref.getPath() + COMMA + IS_INVALID, EMPTY_STRING, |
| 256 | leafref.getLineNumber(), leafref.getCharPosition(), leafref |
| 257 | .getFileName()); |
| 258 | } |
| 259 | |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 260 | //Detect collision between augment and choice children. |
| 261 | private void detectCollisionForChoiceNode(YangNode choice, YangNode augment) { |
| 262 | YangNode choiceChild = choice.getChild(); |
| 263 | YangNode augmentChild = augment.getChild(); |
| 264 | |
| 265 | List<YangNode> choiceChildren = new ArrayList<>(); |
| 266 | List<YangNode> augmentChildren = new ArrayList<>(); |
| 267 | while (choiceChild != null) { |
| 268 | choiceChildren.add(choiceChild); |
| 269 | } |
| 270 | while (augmentChild != null) { |
| 271 | augmentChildren.add(augmentChild); |
| 272 | } |
| 273 | |
| 274 | for (YangNode cChild : choiceChildren) { |
| 275 | for (YangNode aChild : augmentChildren) { |
| 276 | if (cChild.getName().equals(aChild.getName())) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 277 | ; |
| 278 | throw new LinkerException(getErrorMsgCollision( |
| 279 | COLLISION_DETECTION, cChild.getName(), |
| 280 | cChild.getLineNumber(), cChild.getCharPosition(), |
| 281 | CASE, cChild.getFileName())); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 288 | * Detects collision between target nodes and its all leaf/leaf-list or child node with augmented leaf/leaf-list or |
| 289 | * child node. |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 290 | * |
| 291 | * @param targetNode target node |
| 292 | * @param augment augment node |
| 293 | */ |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 294 | static void detectCollisionForAugmentedNode(YangNode targetNode, YangAugment augment) { |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 295 | // Detect collision for target node and augment node. |
| 296 | detectCollision(targetNode, augment); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 297 | List<YangAugment> yangAugmentedInfo = ((YangAugmentableNode) targetNode).getAugmentedInfoList(); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 298 | // Detect collision for target augment node and current augment node. |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 299 | for (YangAugment info : yangAugmentedInfo) { |
| 300 | detectCollision(info, augment); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 301 | } |
| 302 | } |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * Returns list of path names that are needed from augment. |
| 306 | * |
| 307 | * @param augment instance of YANG augment |
| 308 | * @param remainingAncestors ancestor count to move in augment path |
| 309 | * @return list of path names needed in leafref |
| 310 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 311 | static List<String> getPathWithAugment(YangAugment augment, int remainingAncestors) { |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 312 | List<String> listOfPathName = new ArrayList<>(); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 313 | for (YangAtomicPath atomicPath : augment.getTargetNode()) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 314 | if (atomicPath.getNodeIdentifier().getPrefix() != null && |
| 315 | !atomicPath.getNodeIdentifier().getPrefix().equals(EMPTY_STRING)) { |
| 316 | listOfPathName.add(atomicPath.getNodeIdentifier().getPrefix() |
| 317 | + COLON + atomicPath.getNodeIdentifier().getName()); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 318 | } else { |
| 319 | listOfPathName.add(atomicPath.getNodeIdentifier().getName()); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | for (int countOfAncestor = 0; countOfAncestor < remainingAncestors; countOfAncestor++) { |
| 323 | listOfPathName.remove(listOfPathName.size() - 1); |
| 324 | } |
| 325 | return listOfPathName; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Skips the invalid nodes which cannot have data from YANG. |
| 330 | * |
| 331 | * @param currentParent current parent node reference |
| 332 | * @param leafref instance of YANG leafref |
| 333 | * @return parent node which can hold data |
| 334 | * @throws LinkerException a violation of linker rules |
| 335 | */ |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 336 | static YangNode skipInvalidDataNodes(YangNode currentParent, YangLeafRef leafref) |
| 337 | throws LinkerException { |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 338 | while (currentParent instanceof YangChoice || currentParent instanceof YangCase) { |
| 339 | if (currentParent.getParent() == null) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 340 | LinkerException ex = new LinkerException( |
| 341 | LEAFREF_ERROR + leafref.getPath() + IS_INVALID); |
| 342 | ex.setCharPosition(leafref.getCharPosition()); |
| 343 | ex.setLine(leafref.getLineNumber()); |
| 344 | ex.setFileName(leafref.getFileName()); |
| 345 | throw ex; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 346 | } |
| 347 | currentParent = currentParent.getParent(); |
| 348 | } |
| 349 | return currentParent; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Checks and return valid node identifier. |
| 354 | * |
| 355 | * @param nodeIdentifierString string from yang file |
| 356 | * @param yangConstruct yang construct for creating error message |
| 357 | * @return valid node identifier |
| 358 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 359 | static YangNodeIdentifier getValidNodeIdentifier(String nodeIdentifierString, |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 360 | YangConstructType yangConstruct) { |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 361 | String[] tmpData = nodeIdentifierString.split(Pattern.quote(COLON)); |
| 362 | if (tmpData.length == 1) { |
| 363 | YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); |
| 364 | nodeIdentifier.setName(getValidIdentifier(tmpData[0], yangConstruct)); |
| 365 | return nodeIdentifier; |
| 366 | } else if (tmpData.length == 2) { |
| 367 | YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); |
| 368 | nodeIdentifier.setPrefix(getValidIdentifier(tmpData[0], yangConstruct)); |
| 369 | nodeIdentifier.setName(getValidIdentifier(tmpData[1], yangConstruct)); |
| 370 | return nodeIdentifier; |
| 371 | } else { |
| 372 | throw new LinkerException("YANG file error : " + |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 373 | getYangConstructType(yangConstruct) + " name " + nodeIdentifierString + |
| 374 | " is not valid."); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Validates identifier and returns concatenated string if string contains plus symbol. |
| 380 | * |
| 381 | * @param identifier string from yang file |
| 382 | * @param yangConstruct yang construct for creating error message= |
| 383 | * @return concatenated string after removing double quotes |
| 384 | */ |
| 385 | public static String getValidIdentifier(String identifier, YangConstructType yangConstruct) { |
| 386 | |
| 387 | if (identifier.length() > IDENTIFIER_LENGTH) { |
| 388 | throw new LinkerException("YANG file error : " + |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 389 | getYangConstructType(yangConstruct) + " name " + identifier + " is " + |
| 390 | "greater than 64 characters."); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 391 | } else if (!IDENTIFIER_PATTERN.matcher(identifier).matches()) { |
| 392 | throw new LinkerException("YANG file error : " + |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 393 | getYangConstructType(yangConstruct) + " name " + identifier + " is not " + |
| 394 | "valid."); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 395 | } else if (identifier.toLowerCase().startsWith(XML)) { |
| 396 | throw new LinkerException("YANG file error : " + |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 397 | getYangConstructType(yangConstruct) + " identifier " + identifier + |
| 398 | " must not start with (('X'|'x') ('M'|'m') ('L'|'l'))."); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 399 | } else { |
| 400 | return identifier; |
| 401 | } |
| 402 | } |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 403 | |
| 404 | /** |
| 405 | * Updates the priority for all the input files. |
| 406 | * |
| 407 | * @param yangNodeSet set of YANG files info |
| 408 | */ |
| 409 | public static void updateFilePriority(Set<YangNode> yangNodeSet) { |
| 410 | for (YangNode yangNode : yangNodeSet) { |
| 411 | updateFilePriorityOfNode(yangNode); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Updates priority of the node. |
| 417 | * |
| 418 | * @param yangNode YANG node information |
| 419 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 420 | private static void updateFilePriorityOfNode(YangNode yangNode) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 421 | int curNodePriority = yangNode.getPriority(); |
| 422 | if (yangNode instanceof YangReferenceResolver) { |
| 423 | List<YangImport> yangImportList = ((YangReferenceResolver) yangNode).getImportList(); |
| 424 | Iterator<YangImport> importInfoIterator = yangImportList.iterator(); |
| 425 | // Run through the imported list to update priority. |
| 426 | while (importInfoIterator.hasNext()) { |
| 427 | YangImport yangImport = importInfoIterator.next(); |
| 428 | YangNode importedNode = yangImport.getImportedNode(); |
| 429 | if (curNodePriority >= importedNode.getPriority()) { |
| 430 | importedNode.setPriority(curNodePriority + 1); |
| 431 | updateFilePriorityOfNode(importedNode); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | List<YangInclude> yangIncludeList = ((YangReferenceResolver) yangNode).getIncludeList(); |
| 436 | Iterator<YangInclude> includeInfoIterator = yangIncludeList.iterator(); |
| 437 | // Run through the imported list to update priority. |
| 438 | while (includeInfoIterator.hasNext()) { |
| 439 | YangInclude yangInclude = includeInfoIterator.next(); |
| 440 | YangNode includedNode = yangInclude.getIncludedNode(); |
| 441 | if (curNodePriority >= includedNode.getPriority()) { |
| 442 | includedNode.setPriority(curNodePriority + 1); |
| 443 | updateFilePriorityOfNode(includedNode); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | } |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 448 | |
| 449 | /** |
| 450 | * Add the unresolved data under the root leve grouping to be resolved, since it will be used in interfile uses. |
| 451 | * |
| 452 | * @param referenceResolver module / sub-module |
| 453 | */ |
| 454 | public static void resolveGroupingInDefinationScope(YangReferenceResolver referenceResolver) { |
| 455 | YangNode potentialInterFileGrouping = ((YangNode) referenceResolver).getChild(); |
| 456 | |
| 457 | while (potentialInterFileGrouping != null) { |
| 458 | if (potentialInterFileGrouping instanceof YangGrouping) { |
| 459 | addGroupingResolvableEntitiesToResolutionList((YangGrouping) potentialInterFileGrouping); |
| 460 | } |
| 461 | |
| 462 | potentialInterFileGrouping = potentialInterFileGrouping.getNextSibling(); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Add the interfile grouping resolvable entities to reesolution list. |
| 468 | * |
| 469 | * @param interFileGrouping interfile grouping |
| 470 | */ |
| 471 | private static void addGroupingResolvableEntitiesToResolutionList(YangGrouping interFileGrouping) { |
| 472 | YangNode curNode = interFileGrouping; |
| 473 | TraversalType curTraversal = ROOT; |
| 474 | addResolvableLeavesToResolutionList((YangLeavesHolder) curNode); |
| 475 | curTraversal = CHILD; |
| 476 | curNode = interFileGrouping.getChild(); |
| 477 | if (curNode == null) { |
| 478 | return; |
| 479 | } |
| 480 | while (curNode != interFileGrouping) { |
| 481 | if (curTraversal != PARENT) { |
| 482 | if (curNode instanceof YangGrouping || curNode instanceof YangUses) { |
| 483 | if (curNode.getNextSibling() != null) { |
| 484 | curTraversal = SIBILING; |
| 485 | curNode = curNode.getNextSibling(); |
| 486 | } else { |
| 487 | curTraversal = PARENT; |
| 488 | curNode = curNode.getParent(); |
| 489 | } |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | if (curNode instanceof YangLeavesHolder) { |
| 494 | addResolvableLeavesToResolutionList((YangLeavesHolder) curNode); |
| 495 | } else if (curNode instanceof YangTypeDef) { |
| 496 | List<YangType<?>> typeList = ((YangTypeDef) curNode).getTypeList(); |
| 497 | if (!typeList.isEmpty()) { |
| 498 | YangType<?> type = typeList.get(0); |
| 499 | if (type.getDataType() == DERIVED) { |
| 500 | if (type.getResolvableStatus() != ResolvableStatus.RESOLVED) { |
| 501 | |
| 502 | type.setTypeForInterFileGroupingResolution(true); |
| 503 | |
| 504 | // Add resolution information to the list |
| 505 | YangResolutionInfoImpl resolutionInfo = |
| 506 | new YangResolutionInfoImpl<YangType>(type, curNode, type.getLineNumber(), |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 507 | type.getCharPosition()); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 508 | try { |
| 509 | addResolutionInfo(resolutionInfo); |
| 510 | } catch (DataModelException e) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 511 | String errorInfo = "Error in file: " + curNode.getName() + " in " + |
| 512 | curNode.getFileName() + " at " + |
| 513 | "line: " + e.getLineNumber() + |
| 514 | " at position: " + e.getCharPositionInLine() |
| 515 | + e.getLocalizedMessage(); |
| 516 | throw new LinkerException("Failed to add type info in grouping to resolution " |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 517 | + errorInfo); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | } |
| 525 | if (curTraversal != PARENT && curNode.getChild() != null) { |
| 526 | curTraversal = CHILD; |
| 527 | curNode = curNode.getChild(); |
| 528 | } else if (curNode.getNextSibling() != null) { |
| 529 | |
| 530 | curTraversal = SIBILING; |
| 531 | curNode = curNode.getNextSibling(); |
| 532 | } else { |
| 533 | curTraversal = PARENT; |
| 534 | curNode = curNode.getParent(); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Add resolvable leaves type info to resolution list. |
| 541 | * |
| 542 | * @param leavesHolder leaves holder node |
| 543 | */ |
| 544 | private static void addResolvableLeavesToResolutionList(YangLeavesHolder leavesHolder) { |
| 545 | if (leavesHolder.getListOfLeaf() != null && !leavesHolder.getListOfLeaf().isEmpty()) { |
| 546 | for (YangLeaf leaf : leavesHolder.getListOfLeaf()) { |
| 547 | YangType type = leaf.getDataType(); |
| 548 | if (type.getDataType() == DERIVED) { |
| 549 | |
| 550 | type.setTypeForInterFileGroupingResolution(true); |
| 551 | |
| 552 | // Add resolution information to the list |
| 553 | YangResolutionInfoImpl resolutionInfo = |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 554 | new YangResolutionInfoImpl<>(type, (YangNode) leavesHolder, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 555 | type.getLineNumber(), type.getCharPosition()); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 556 | try { |
| 557 | addResolutionInfo(resolutionInfo); |
| 558 | } catch (DataModelException e) { |
| 559 | throw new LinkerException("Failed to add leaf type info in grouping, to resolution "); |
| 560 | } |
| 561 | } else if (type.getDataType() == IDENTITYREF) { |
| 562 | YangIdentityRef identityRef = (YangIdentityRef) type.getDataTypeExtendedInfo(); |
| 563 | |
| 564 | identityRef.setIdentityForInterFileGroupingResolution(true); |
| 565 | |
| 566 | // Add resolution information to the list |
| 567 | YangResolutionInfoImpl resolutionInfo = |
| 568 | new YangResolutionInfoImpl<YangIdentityRef>(identityRef, (YangNode) leavesHolder, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 569 | identityRef.getLineNumber(), identityRef.getCharPosition()); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 570 | try { |
| 571 | addResolutionInfo(resolutionInfo); |
| 572 | } catch (DataModelException e) { |
| 573 | throw new LinkerException("Failed to add leaf identity ref info in grouping, to resolution "); |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | if (leavesHolder.getListOfLeafList() != null && !leavesHolder.getListOfLeafList().isEmpty()) { |
| 580 | for (YangLeafList leafList : leavesHolder.getListOfLeafList()) { |
| 581 | YangType type = leafList.getDataType(); |
| 582 | if (type.getDataType() == DERIVED) { |
| 583 | |
| 584 | type.setTypeForInterFileGroupingResolution(true); |
| 585 | |
| 586 | // Add resolution information to the list |
| 587 | YangResolutionInfoImpl resolutionInfo = |
| 588 | new YangResolutionInfoImpl<YangType>(type, (YangNode) leavesHolder, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 589 | type.getLineNumber(), type.getCharPosition()); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 590 | try { |
| 591 | addResolutionInfo(resolutionInfo); |
| 592 | } catch (DataModelException e) { |
| 593 | throw new LinkerException("Failed to add leaf type info in grouping, to resolution "); |
| 594 | } |
| 595 | } else if (type.getDataType() == IDENTITYREF) { |
| 596 | YangIdentityRef identityRef = (YangIdentityRef) type.getDataTypeExtendedInfo(); |
| 597 | |
| 598 | identityRef.setIdentityForInterFileGroupingResolution(true); |
| 599 | // Add resolution information to the list |
| 600 | YangResolutionInfoImpl resolutionInfo = |
| 601 | new YangResolutionInfoImpl<YangIdentityRef>(identityRef, (YangNode) leavesHolder, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 602 | identityRef.getLineNumber(), identityRef.getCharPosition()); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 603 | try { |
| 604 | addResolutionInfo(resolutionInfo); |
| 605 | } catch (DataModelException e) { |
| 606 | throw new LinkerException("Failed to add leaf identity ref info in grouping, to resolution "); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 613 | } |