blob: 507ebf3554d659e880e9fbca7b4801476ed5eede [file] [log] [blame]
Bharat saraswald14cbe82016-07-14 13:26:18 +05301/*
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
17package org.onosproject.yangutils.linker.impl;
18
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +053019import org.onosproject.yangutils.datamodel.TraversalType;
Bharat saraswal8beac342016-08-04 02:00:03 +053020import org.onosproject.yangutils.datamodel.YangAtomicPath;
Bharat saraswald14cbe82016-07-14 13:26:18 +053021import org.onosproject.yangutils.datamodel.YangAugment;
22import org.onosproject.yangutils.datamodel.YangAugmentableNode;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053023import org.onosproject.yangutils.datamodel.YangBase;
janani bebb143d2016-07-14 19:35:22 +053024import org.onosproject.yangutils.datamodel.YangCase;
Bharat saraswalaf413b82016-07-14 15:18:20 +053025import org.onosproject.yangutils.datamodel.YangChoice;
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +053026import org.onosproject.yangutils.datamodel.YangGrouping;
27import org.onosproject.yangutils.datamodel.YangIdentityRef;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053028import org.onosproject.yangutils.datamodel.YangIfFeature;
Bharat saraswal039f59c2016-07-14 21:57:13 +053029import org.onosproject.yangutils.datamodel.YangImport;
30import org.onosproject.yangutils.datamodel.YangInclude;
Bharat saraswald14cbe82016-07-14 13:26:18 +053031import org.onosproject.yangutils.datamodel.YangLeaf;
32import org.onosproject.yangutils.datamodel.YangLeafList;
janani bebb143d2016-07-14 19:35:22 +053033import org.onosproject.yangutils.datamodel.YangLeafRef;
Bharat saraswald14cbe82016-07-14 13:26:18 +053034import org.onosproject.yangutils.datamodel.YangLeavesHolder;
35import org.onosproject.yangutils.datamodel.YangNode;
janani bebb143d2016-07-14 19:35:22 +053036import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Bharat saraswal039f59c2016-07-14 21:57:13 +053037import org.onosproject.yangutils.datamodel.YangReferenceResolver;
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +053038import org.onosproject.yangutils.datamodel.YangType;
39import org.onosproject.yangutils.datamodel.YangTypeDef;
40import org.onosproject.yangutils.datamodel.YangUses;
41import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
42import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
janani bebb143d2016-07-14 19:35:22 +053043import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Bharat saraswald14cbe82016-07-14 13:26:18 +053044import org.onosproject.yangutils.linker.exceptions.LinkerException;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053045import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswald14cbe82016-07-14 13:26:18 +053046
Bharat saraswal9fab16b2016-09-23 23:27:24 +053047import java.util.ArrayList;
48import java.util.Iterator;
49import java.util.List;
50import java.util.Set;
51import java.util.regex.Pattern;
52
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +053053import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
54import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
55import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
56import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053057import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.CASE;
58import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.COLLISION_DETECTION;
59import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.FAILED_TO_ADD_CASE;
60import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.TARGET_NODE;
61import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.TARGET_NODE_LEAF_INFO;
62import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.getErrorMsg;
63import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.getErrorMsgCollision;
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +053064import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053065import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
Bharat saraswale3175d32016-08-31 17:50:11 +053066import static org.onosproject.yangutils.datamodel.utils.YangConstructType.getYangConstructType;
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +053067import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
68import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.IDENTITYREF;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053069import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangCaseNode;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053070import static org.onosproject.yangutils.utils.UtilConstants.BASE_LINKER_ERROR;
janani bebb143d2016-07-14 19:35:22 +053071import static org.onosproject.yangutils.utils.UtilConstants.COLON;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053072import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
janani bebb143d2016-07-14 19:35:22 +053073import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053074import static org.onosproject.yangutils.utils.UtilConstants.FEATURE_LINKER_ERROR;
75import static org.onosproject.yangutils.utils.UtilConstants.GROUPING_LINKER_ERROR;
76import static org.onosproject.yangutils.utils.UtilConstants.IDENTITYREF_LINKER_ERROR;
77import static org.onosproject.yangutils.utils.UtilConstants.IS_INVALID;
78import static org.onosproject.yangutils.utils.UtilConstants.LEAFREF_ERROR;
79import static org.onosproject.yangutils.utils.UtilConstants.LEAFREF_LINKER_ERROR;
80import static org.onosproject.yangutils.utils.UtilConstants.TYPEDEF_LINKER_ERROR;
janani bebb143d2016-07-14 19:35:22 +053081
Bharat saraswald14cbe82016-07-14 13:26:18 +053082/**
83 * Represent utilities for YANG linker.
84 */
85public final class YangLinkerUtils {
86
janani bebb143d2016-07-14 19:35:22 +053087 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 saraswal039f59c2016-07-14 21:57:13 +053091 private YangLinkerUtils() {
92 }
93
Bharat saraswald14cbe82016-07-14 13:26:18 +053094 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +053095 * Detects collision between target nodes leaf/leaf-list or child node with augmented leaf/leaf-list or child node.
Bharat saraswald14cbe82016-07-14 13:26:18 +053096 *
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 saraswal039f59c2016-07-14 21:57:13 +0530103 if (targetNode instanceof YangChoice) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530104 addCaseNodeToChoiceTarget(augment);
Bharat saraswal039f59c2016-07-14 21:57:13 +0530105 } else {
106 detectCollisionInLeaveHolders(targetNode, augment);
107 while (augmentsChild != null) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530108 detectCollisionInChildNodes(targetNodesChild, augmentsChild);
Bharat saraswal039f59c2016-07-14 21:57:13 +0530109 augmentsChild = augmentsChild.getNextSibling();
110 }
111 }
112 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530113
Bharat saraswal8beac342016-08-04 02:00:03 +0530114 /*Detects collision between leaves/leaf-lists*/
Bharat saraswal039f59c2016-07-14 21:57:13 +0530115 private static void detectCollisionInLeaveHolders(YangNode targetNode, YangAugment augment) {
116 YangLeavesHolder targetNodesLeavesHolder = (YangLeavesHolder) targetNode;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530117 if (augment.getListOfLeaf() != null && augment.getListOfLeaf().isEmpty() &&
118 targetNodesLeavesHolder.getListOfLeaf() != null) {
Bharat saraswal039f59c2016-07-14 21:57:13 +0530119 for (YangLeaf leaf : augment.getListOfLeaf()) {
120 for (YangLeaf targetLeaf : targetNodesLeavesHolder.getListOfLeaf()) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530121 detectCollision(targetLeaf.getName(), leaf.getName(),
122 leaf.getLineNumber(),
123 leaf.getCharPosition(),
124 leaf.getFileName(), TARGET_NODE_LEAF_INFO);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530125 }
126 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530127 }
128 if (augment.getListOfLeafList() != null &&
129 augment.getListOfLeafList().isEmpty() &&
130 targetNodesLeavesHolder.getListOfLeafList() != null) {
Bharat saraswal039f59c2016-07-14 21:57:13 +0530131 for (YangLeafList leafList : augment.getListOfLeafList()) {
132 for (YangLeafList targetLeafList : targetNodesLeavesHolder.getListOfLeafList()) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530133 detectCollision(targetLeafList.getName(), leafList.getName(),
134 leafList.getLineNumber(),
135 leafList.getCharPosition(),
136 leafList.getFileName(), TARGET_NODE_LEAF_INFO);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530137 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530138 }
139 }
140 }
141
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530142
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 saraswal039f59c2016-07-14 21:57:13 +0530153 /*Detects collision for child nodes.*/
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530154 private static void detectCollisionInChildNodes(YangNode targetNodesChild,
155 YangNode augmentsChild) {
Bharat saraswal039f59c2016-07-14 21:57:13 +0530156 while (augmentsChild != null) {
157 while (targetNodesChild != null) {
158 if (targetNodesChild.getName().equals(augmentsChild.getName())) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530159 detectCollision(targetNodesChild.getName(), augmentsChild.getName(),
160 augmentsChild.getLineNumber(),
161 augmentsChild.getCharPosition(),
162 augmentsChild.getFileName(), TARGET_NODE);
Bharat saraswal039f59c2016-07-14 21:57:13 +0530163 }
164 targetNodesChild = targetNodesChild.getNextSibling();
165 }
166 augmentsChild = augmentsChild.getNextSibling();
167 }
168 }
169
Bharat saraswald14cbe82016-07-14 13:26:18 +0530170 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530171 * 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 saraswal9fab16b2016-09-23 23:27:24 +0530215 throw new TranslatorException(
216 getErrorMsg(FAILED_TO_ADD_CASE, augment.getName(),
217 augment.getLineNumber(), augment.getCharPosition(),
218 augment.getFileName()));
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530219 }
220 }
221
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530222 /**
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 saraswal2da23bf2016-08-25 15:28:39 +0530260 //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 saraswal9fab16b2016-09-23 23:27:24 +0530277 ;
278 throw new LinkerException(getErrorMsgCollision(
279 COLLISION_DETECTION, cChild.getName(),
280 cChild.getLineNumber(), cChild.getCharPosition(),
281 CASE, cChild.getFileName()));
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530282 }
283 }
284 }
285 }
286
287 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530288 * Detects collision between target nodes and its all leaf/leaf-list or child node with augmented leaf/leaf-list or
289 * child node.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530290 *
291 * @param targetNode target node
292 * @param augment augment node
293 */
Bharat saraswal039f59c2016-07-14 21:57:13 +0530294 static void detectCollisionForAugmentedNode(YangNode targetNode, YangAugment augment) {
Bharat saraswald14cbe82016-07-14 13:26:18 +0530295 // Detect collision for target node and augment node.
296 detectCollision(targetNode, augment);
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530297 List<YangAugment> yangAugmentedInfo = ((YangAugmentableNode) targetNode).getAugmentedInfoList();
Bharat saraswald14cbe82016-07-14 13:26:18 +0530298 // Detect collision for target augment node and current augment node.
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530299 for (YangAugment info : yangAugmentedInfo) {
300 detectCollision(info, augment);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530301 }
302 }
janani bebb143d2016-07-14 19:35:22 +0530303
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 saraswal8beac342016-08-04 02:00:03 +0530311 static List<String> getPathWithAugment(YangAugment augment, int remainingAncestors) {
janani bebb143d2016-07-14 19:35:22 +0530312 List<String> listOfPathName = new ArrayList<>();
Bharat saraswal8beac342016-08-04 02:00:03 +0530313 for (YangAtomicPath atomicPath : augment.getTargetNode()) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530314 if (atomicPath.getNodeIdentifier().getPrefix() != null &&
315 !atomicPath.getNodeIdentifier().getPrefix().equals(EMPTY_STRING)) {
316 listOfPathName.add(atomicPath.getNodeIdentifier().getPrefix()
317 + COLON + atomicPath.getNodeIdentifier().getName());
Bharat saraswal8beac342016-08-04 02:00:03 +0530318 } else {
319 listOfPathName.add(atomicPath.getNodeIdentifier().getName());
janani bebb143d2016-07-14 19:35:22 +0530320 }
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-Huawei423dc9a2016-08-17 22:08:42 +0530336 static YangNode skipInvalidDataNodes(YangNode currentParent, YangLeafRef leafref)
337 throws LinkerException {
janani bebb143d2016-07-14 19:35:22 +0530338 while (currentParent instanceof YangChoice || currentParent instanceof YangCase) {
339 if (currentParent.getParent() == null) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530340 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 bebb143d2016-07-14 19:35:22 +0530346 }
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 saraswal8beac342016-08-04 02:00:03 +0530359 static YangNodeIdentifier getValidNodeIdentifier(String nodeIdentifierString,
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530360 YangConstructType yangConstruct) {
janani bebb143d2016-07-14 19:35:22 +0530361 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 saraswal9fab16b2016-09-23 23:27:24 +0530373 getYangConstructType(yangConstruct) + " name " + nodeIdentifierString +
374 " is not valid.");
janani bebb143d2016-07-14 19:35:22 +0530375 }
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 saraswal9fab16b2016-09-23 23:27:24 +0530389 getYangConstructType(yangConstruct) + " name " + identifier + " is " +
390 "greater than 64 characters.");
janani bebb143d2016-07-14 19:35:22 +0530391 } else if (!IDENTIFIER_PATTERN.matcher(identifier).matches()) {
392 throw new LinkerException("YANG file error : " +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530393 getYangConstructType(yangConstruct) + " name " + identifier + " is not " +
394 "valid.");
janani bebb143d2016-07-14 19:35:22 +0530395 } else if (identifier.toLowerCase().startsWith(XML)) {
396 throw new LinkerException("YANG file error : " +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530397 getYangConstructType(yangConstruct) + " identifier " + identifier +
398 " must not start with (('X'|'x') ('M'|'m') ('L'|'l')).");
janani bebb143d2016-07-14 19:35:22 +0530399 } else {
400 return identifier;
401 }
402 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530403
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 saraswal8beac342016-08-04 02:00:03 +0530420 private static void updateFilePriorityOfNode(YangNode yangNode) {
Bharat saraswal039f59c2016-07-14 21:57:13 +0530421 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-Huawei423dc9a2016-08-17 22:08:42 +0530448
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 saraswal9fab16b2016-09-23 23:27:24 +0530507 type.getCharPosition());
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +0530508 try {
509 addResolutionInfo(resolutionInfo);
510 } catch (DataModelException e) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530511 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 saraswal9fab16b2016-09-23 23:27:24 +0530517 + errorInfo);
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +0530518 }
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 saraswale3175d32016-08-31 17:50:11 +0530554 new YangResolutionInfoImpl<>(type, (YangNode) leavesHolder,
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530555 type.getLineNumber(), type.getCharPosition());
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +0530556 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 saraswal9fab16b2016-09-23 23:27:24 +0530569 identityRef.getLineNumber(), identityRef.getCharPosition());
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +0530570 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 saraswal9fab16b2016-09-23 23:27:24 +0530589 type.getLineNumber(), type.getCharPosition());
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +0530590 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 saraswal9fab16b2016-09-23 23:27:24 +0530602 identityRef.getLineNumber(), identityRef.getCharPosition());
VinodKumarS-Huawei423dc9a2016-08-17 22:08:42 +0530603 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 saraswald14cbe82016-07-14 13:26:18 +0530613}