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