blob: abfae712d13e0d2a5b3d75f8b39063911c35911c [file] [log] [blame]
Bharat saraswalb1170bd2016-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
Bharat saraswald50c6382016-07-14 21:57:13 +053019import java.util.ArrayList;
20import java.util.Iterator;
21import java.util.List;
22import java.util.Set;
23import java.util.regex.Pattern;
24
Bharat saraswalb1170bd2016-07-14 13:26:18 +053025import org.onosproject.yangutils.datamodel.YangAugment;
26import org.onosproject.yangutils.datamodel.YangAugmentableNode;
27import org.onosproject.yangutils.datamodel.YangAugmentedInfo;
janani b23ccc312016-07-14 19:35:22 +053028import org.onosproject.yangutils.datamodel.YangCase;
Bharat saraswalb551aae2016-07-14 15:18:20 +053029import org.onosproject.yangutils.datamodel.YangChoice;
Bharat saraswald50c6382016-07-14 21:57:13 +053030import org.onosproject.yangutils.datamodel.YangImport;
31import org.onosproject.yangutils.datamodel.YangInclude;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053032import org.onosproject.yangutils.datamodel.YangLeaf;
33import org.onosproject.yangutils.datamodel.YangLeafList;
janani b23ccc312016-07-14 19:35:22 +053034import org.onosproject.yangutils.datamodel.YangLeafRef;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053035import org.onosproject.yangutils.datamodel.YangLeavesHolder;
36import org.onosproject.yangutils.datamodel.YangNode;
janani b23ccc312016-07-14 19:35:22 +053037import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Bharat saraswald50c6382016-07-14 21:57:13 +053038import org.onosproject.yangutils.datamodel.YangReferenceResolver;
janani b23ccc312016-07-14 19:35:22 +053039import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053040import org.onosproject.yangutils.linker.exceptions.LinkerException;
41
janani b23ccc312016-07-14 19:35:22 +053042import static org.onosproject.yangutils.utils.UtilConstants.COLON;
43import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
44import static org.onosproject.yangutils.utils.UtilConstants.SLASH_FOR_STRING;
45
Bharat saraswalb1170bd2016-07-14 13:26:18 +053046/**
47 * Represent utilities for YANG linker.
48 */
49public final class YangLinkerUtils {
50
janani b23ccc312016-07-14 19:35:22 +053051 private static final int IDENTIFIER_LENGTH = 64;
52 private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*");
53 private static final String XML = "xml";
54
Bharat saraswald50c6382016-07-14 21:57:13 +053055 private YangLinkerUtils() {
56 }
57
Bharat saraswalb1170bd2016-07-14 13:26:18 +053058 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +053059 * Detects collision between target nodes leaf/leaf-list or child node with augmented leaf/leaf-list or child node.
Bharat saraswalb1170bd2016-07-14 13:26:18 +053060 *
61 * @param targetNode target node
62 * @param augment augment node
63 */
64 private static void detectCollision(YangNode targetNode, YangAugment augment) {
65 YangNode targetNodesChild = targetNode.getChild();
66 YangNode augmentsChild = augment.getChild();
Bharat saraswald50c6382016-07-14 21:57:13 +053067 YangNode parent = targetNode;
68 if (targetNode instanceof YangAugment) {
69 parent = targetNode.getParent();
Bharat saraswalb551aae2016-07-14 15:18:20 +053070 } else {
Bharat saraswald50c6382016-07-14 21:57:13 +053071 while (parent.getParent() != null) {
72 parent = parent.getParent();
73 }
74 }
75 if (targetNode instanceof YangChoice) {
76 // no need to check here.
77 } else {
78 detectCollisionInLeaveHolders(targetNode, augment);
79 while (augmentsChild != null) {
80 detectCollisionInChildNodes(targetNodesChild, augmentsChild, targetNode.getName(), parent.getName());
81 augmentsChild = augmentsChild.getNextSibling();
82 }
83 }
84 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +053085
Bharat saraswald50c6382016-07-14 21:57:13 +053086 /*Detects collision between leaves/leaflists*/
87 private static void detectCollisionInLeaveHolders(YangNode targetNode, YangAugment augment) {
88 YangLeavesHolder targetNodesLeavesHolder = (YangLeavesHolder) targetNode;
89 YangNode parent = targetNode;
90 if (targetNode instanceof YangAugment) {
91 parent = targetNode.getParent();
92 } else {
93 while (parent.getParent() != null) {
94 parent = parent.getParent();
95 }
96 }
97 if (augment.getListOfLeaf() != null && augment.getListOfLeaf().size() != 0
98 && targetNodesLeavesHolder.getListOfLeaf() != null) {
99 for (YangLeaf leaf : augment.getListOfLeaf()) {
100 for (YangLeaf targetLeaf : targetNodesLeavesHolder.getListOfLeaf()) {
101 if (targetLeaf.getName().equals(leaf.getName())) {
102 throw new LinkerException("target node " + targetNode.getName()
103 + " contains augmented leaf " + leaf.getName() + " in module "
104 + parent.getName());
105 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530106 }
107 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530108 } else if (augment.getListOfLeafList() != null
109 && augment.getListOfLeafList().size() != 0
110 && augment.getListOfLeafList() != null) {
111 for (YangLeafList leafList : augment.getListOfLeafList()) {
112 for (YangLeafList targetLeafList : targetNodesLeavesHolder.getListOfLeafList()) {
113 if (targetLeafList.getName().equals(leafList.getName())) {
114 throw new LinkerException("target node " + targetNode.getName()
115 + " contains augmented leaf-list" + leafList.getName() + " in module "
116 + parent.getName());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530117 }
118 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530119 }
120 }
121 }
122
Bharat saraswald50c6382016-07-14 21:57:13 +0530123 /*Detects collision for child nodes.*/
124 private static void detectCollisionInChildNodes(YangNode targetNodesChild, YangNode augmentsChild, String
125 targetName, String parentName) {
126 while (augmentsChild != null) {
127 while (targetNodesChild != null) {
128 if (targetNodesChild.getName().equals(augmentsChild.getName())) {
129 throw new LinkerException("target node " + targetName
130 + " contains augmented child node" + augmentsChild.getName() + " in module "
131 + parentName);
132 }
133 targetNodesChild = targetNodesChild.getNextSibling();
134 }
135 augmentsChild = augmentsChild.getNextSibling();
136 }
137 }
138
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530139 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530140 * Detects collision between target nodes and its all leaf/leaf-list or child node with augmented leaf/leaf-list or
141 * child node.
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530142 *
143 * @param targetNode target node
144 * @param augment augment node
145 */
Bharat saraswald50c6382016-07-14 21:57:13 +0530146 static void detectCollisionForAugmentedNode(YangNode targetNode, YangAugment augment) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530147 // Detect collision for target node and augment node.
148 detectCollision(targetNode, augment);
149 List<YangAugmentedInfo> yangAugmentedInfo = ((YangAugmentableNode) targetNode).getAugmentedInfoList();
150 // Detect collision for target augment node and current augment node.
151 for (YangAugmentedInfo info : yangAugmentedInfo) {
152 detectCollision((YangAugment) info, augment);
153 }
154 }
janani b23ccc312016-07-14 19:35:22 +0530155
156 /**
157 * Returns list of path names that are needed from augment.
158 *
159 * @param augment instance of YANG augment
160 * @param remainingAncestors ancestor count to move in augment path
161 * @return list of path names needed in leafref
162 */
163 public static List<String> getPathWithAugment(YangAugment augment, int remainingAncestors) {
164 String augmentName = augment.getName();
165 List<String> listOfPathName = new ArrayList<>();
166 if (augmentName.contains(SLASH_FOR_STRING)) {
167 String[] augmentNodeNames = augmentName.split(SLASH_FOR_STRING);
168 for (String valueInAugment : augmentNodeNames) {
169 if (valueInAugment != null && valueInAugment != EMPTY_STRING && !valueInAugment.isEmpty()) {
170 listOfPathName.add(valueInAugment);
171 }
172 }
173 }
174 for (int countOfAncestor = 0; countOfAncestor < remainingAncestors; countOfAncestor++) {
175 listOfPathName.remove(listOfPathName.size() - 1);
176 }
177 return listOfPathName;
178 }
179
180 /**
181 * Skips the invalid nodes which cannot have data from YANG.
182 *
183 * @param currentParent current parent node reference
184 * @param leafref instance of YANG leafref
185 * @return parent node which can hold data
186 * @throws LinkerException a violation of linker rules
187 */
188 public static YangNode skipInvalidDataNodes(YangNode currentParent, YangLeafRef leafref) throws LinkerException {
189 while (currentParent instanceof YangChoice || currentParent instanceof YangCase) {
190 if (currentParent.getParent() == null) {
191 throw new LinkerException("YANG file error: The target node, in the leafref path " +
192 leafref.getPath() + ", is invalid.");
193 }
194 currentParent = currentParent.getParent();
195 }
196 return currentParent;
197 }
198
199 /**
200 * Checks and return valid node identifier.
201 *
202 * @param nodeIdentifierString string from yang file
203 * @param yangConstruct yang construct for creating error message
204 * @return valid node identifier
205 */
206 public static YangNodeIdentifier getValidNodeIdentifier(String nodeIdentifierString,
207 YangConstructType yangConstruct) {
208 String[] tmpData = nodeIdentifierString.split(Pattern.quote(COLON));
209 if (tmpData.length == 1) {
210 YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier();
211 nodeIdentifier.setName(getValidIdentifier(tmpData[0], yangConstruct));
212 return nodeIdentifier;
213 } else if (tmpData.length == 2) {
214 YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier();
215 nodeIdentifier.setPrefix(getValidIdentifier(tmpData[0], yangConstruct));
216 nodeIdentifier.setName(getValidIdentifier(tmpData[1], yangConstruct));
217 return nodeIdentifier;
218 } else {
219 throw new LinkerException("YANG file error : " +
220 YangConstructType.getYangConstructType(yangConstruct) + " name " + nodeIdentifierString +
221 " is not valid.");
222 }
223 }
224
225 /**
226 * Validates identifier and returns concatenated string if string contains plus symbol.
227 *
228 * @param identifier string from yang file
229 * @param yangConstruct yang construct for creating error message=
230 * @return concatenated string after removing double quotes
231 */
232 public static String getValidIdentifier(String identifier, YangConstructType yangConstruct) {
233
234 if (identifier.length() > IDENTIFIER_LENGTH) {
235 throw new LinkerException("YANG file error : " +
236 YangConstructType.getYangConstructType(yangConstruct) + " name " + identifier + " is " +
237 "greater than 64 characters.");
238 } else if (!IDENTIFIER_PATTERN.matcher(identifier).matches()) {
239 throw new LinkerException("YANG file error : " +
240 YangConstructType.getYangConstructType(yangConstruct) + " name " + identifier + " is not " +
241 "valid.");
242 } else if (identifier.toLowerCase().startsWith(XML)) {
243 throw new LinkerException("YANG file error : " +
244 YangConstructType.getYangConstructType(yangConstruct) + " identifier " + identifier +
245 " must not start with (('X'|'x') ('M'|'m') ('L'|'l')).");
246 } else {
247 return identifier;
248 }
249 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530250
251 /**
252 * Updates the priority for all the input files.
253 *
254 * @param yangNodeSet set of YANG files info
255 */
256 public static void updateFilePriority(Set<YangNode> yangNodeSet) {
257 for (YangNode yangNode : yangNodeSet) {
258 updateFilePriorityOfNode(yangNode);
259 }
260 }
261
262 /**
263 * Updates priority of the node.
264 *
265 * @param yangNode YANG node information
266 */
267 public static void updateFilePriorityOfNode(YangNode yangNode) {
268 int curNodePriority = yangNode.getPriority();
269 if (yangNode instanceof YangReferenceResolver) {
270 List<YangImport> yangImportList = ((YangReferenceResolver) yangNode).getImportList();
271 Iterator<YangImport> importInfoIterator = yangImportList.iterator();
272 // Run through the imported list to update priority.
273 while (importInfoIterator.hasNext()) {
274 YangImport yangImport = importInfoIterator.next();
275 YangNode importedNode = yangImport.getImportedNode();
276 if (curNodePriority >= importedNode.getPriority()) {
277 importedNode.setPriority(curNodePriority + 1);
278 updateFilePriorityOfNode(importedNode);
279 }
280 }
281
282 List<YangInclude> yangIncludeList = ((YangReferenceResolver) yangNode).getIncludeList();
283 Iterator<YangInclude> includeInfoIterator = yangIncludeList.iterator();
284 // Run through the imported list to update priority.
285 while (includeInfoIterator.hasNext()) {
286 YangInclude yangInclude = includeInfoIterator.next();
287 YangNode includedNode = yangInclude.getIncludedNode();
288 if (curNodePriority >= includedNode.getPriority()) {
289 includedNode.setPriority(curNodePriority + 1);
290 updateFilePriorityOfNode(includedNode);
291 }
292 }
293 }
294 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530295}