Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 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.parser.impl.parserutils; |
| 18 | |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 19 | import java.text.ParseException; |
| 20 | import java.text.SimpleDateFormat; |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Date; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Iterator; |
| 25 | import java.util.LinkedList; |
| 26 | import java.util.List; |
| 27 | import java.util.regex.Pattern; |
| 28 | |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 29 | import org.antlr.v4.runtime.ParserRuleContext; |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 30 | import org.apache.commons.logging.Log; |
| 31 | import org.apache.commons.logging.LogFactory; |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.YangAtomicPath; |
| 33 | import org.onosproject.yangutils.datamodel.YangImport; |
| 34 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 35 | import org.onosproject.yangutils.datamodel.YangLeafRef; |
| 36 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 37 | import org.onosproject.yangutils.datamodel.YangList; |
| 38 | import org.onosproject.yangutils.datamodel.YangModule; |
| 39 | import org.onosproject.yangutils.datamodel.YangNode; |
| 40 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
| 41 | import org.onosproject.yangutils.datamodel.YangPathPredicate; |
| 42 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
| 43 | import org.onosproject.yangutils.datamodel.YangRelativePath; |
| 44 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 45 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
| 46 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 47 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 48 | |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.datamodel.YangPathArgType.ABSOLUTE_PATH; |
| 50 | import static org.onosproject.yangutils.datamodel.YangPathArgType.RELATIVE_PATH; |
| 51 | import static org.onosproject.yangutils.datamodel.YangPathOperator.EQUALTO; |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 52 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.getYangConstructType; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 53 | import static org.onosproject.yangutils.utils.UtilConstants.ADD; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.utils.UtilConstants.ANCESTOR_ACCESSOR; |
| 55 | import static org.onosproject.yangutils.utils.UtilConstants.ANCESTOR_ACCESSOR_IN_PATH; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 56 | import static org.onosproject.yangutils.utils.UtilConstants.CARET; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 57 | import static org.onosproject.yangutils.utils.UtilConstants.CHAR_OF_CLOSE_SQUARE_BRACKET; |
| 58 | import static org.onosproject.yangutils.utils.UtilConstants.CHAR_OF_OPEN_SQUARE_BRACKET; |
| 59 | import static org.onosproject.yangutils.utils.UtilConstants.CHAR_OF_SLASH; |
| 60 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 61 | import static org.onosproject.yangutils.utils.UtilConstants.COLON; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.utils.UtilConstants.CURRENT; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 63 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 64 | import static org.onosproject.yangutils.utils.UtilConstants.FALSE; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 65 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_SQUARE_BRACKET; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 66 | import static org.onosproject.yangutils.utils.UtilConstants.QUOTES; |
| 67 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH_FOR_STRING; |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 69 | import static org.onosproject.yangutils.utils.UtilConstants.TRUE; |
| 70 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_FILE_ERROR; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 71 | |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 72 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 73 | * Represents an utility for listener. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 74 | */ |
| 75 | public final class ListenerUtil { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 76 | |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 77 | private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*"); |
Vidyashree Rama | 1ae576c | 2016-05-09 11:45:05 +0530 | [diff] [blame] | 78 | private static final String DATE_PATTERN = "[0-9]{4}-([0-9]{2}|[0-9])-([0-9]{2}|[0-9])"; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 79 | private static final String NON_NEGATIVE_INTEGER_PATTERN = "[0-9]+"; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 80 | private static final Pattern INTEGER_PATTERN = Pattern.compile("[-][0-9]+|[0-9]+"); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 81 | private static final Pattern PATH_PREDICATE_PATTERN = Pattern.compile("\\[(.*?)\\]"); |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 82 | private static final String XML = "xml"; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 83 | private static final String ONE = "1"; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 84 | private static final int IDENTIFIER_LENGTH = 64; |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 85 | private static final String DATE_FORMAT = "yyyy-MM-dd"; |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 86 | private static final Log log = LogFactory.getLog(ListenerUtil.class); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Creates a new listener util. |
| 90 | */ |
| 91 | private ListenerUtil() { |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Removes doubles quotes and concatenates if string has plus symbol. |
| 96 | * |
| 97 | * @param yangStringData string from yang file |
| 98 | * @return concatenated string after removing double quotes |
| 99 | */ |
| 100 | public static String removeQuotesAndHandleConcat(String yangStringData) { |
| 101 | |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 102 | yangStringData = yangStringData.replace("\"", EMPTY_STRING); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 103 | String[] tmpData = yangStringData.split(Pattern.quote(ADD)); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 104 | StringBuilder builder = new StringBuilder(); |
| 105 | for (String yangString : tmpData) { |
| 106 | builder.append(yangString); |
| 107 | } |
| 108 | return builder.toString(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Validates identifier and returns concatenated string if string contains plus symbol. |
| 113 | * |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 114 | * @param identifier string from yang file |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 115 | * @param yangConstruct yang construct for creating error message |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 116 | * @param ctx yang construct's context to get the line number and character position |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 117 | * @return concatenated string after removing double quotes |
| 118 | */ |
| 119 | public static String getValidIdentifier(String identifier, YangConstructType yangConstruct, ParserRuleContext ctx) { |
| 120 | |
| 121 | String identifierString = removeQuotesAndHandleConcat(identifier); |
| 122 | ParserException parserException; |
| 123 | |
| 124 | if (identifierString.length() > IDENTIFIER_LENGTH) { |
| 125 | parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 126 | getYangConstructType(yangConstruct) + " name " + identifierString + " is " + |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 127 | "greater than 64 characters."); |
| 128 | } else if (!IDENTIFIER_PATTERN.matcher(identifierString).matches()) { |
| 129 | parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 130 | getYangConstructType(yangConstruct) + " name " + identifierString + " is not " + |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 131 | "valid."); |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 132 | } else if (identifierString.toLowerCase().startsWith(XML)) { |
| 133 | parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 134 | getYangConstructType(yangConstruct) + " identifier " + identifierString + |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 135 | " must not start with (('X'|'x') ('M'|'m') ('L'|'l'))."); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 136 | } else { |
| 137 | return identifierString; |
| 138 | } |
| 139 | |
| 140 | parserException.setLine(ctx.getStart().getLine()); |
| 141 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 142 | throw parserException; |
| 143 | } |
| 144 | |
| 145 | /** |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 146 | * Validates identifier and returns concatenated string if string contains plus symbol. |
| 147 | * |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 148 | * @param identifier string from yang file |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 149 | * @param yangConstruct yang construct for creating error message |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 150 | * @param ctx yang construct's context to get the line number and character position |
| 151 | * @param yangLeafRef instance of leafref where the path argument has to be set |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 152 | * @return concatenated string after removing double quotes |
| 153 | */ |
| 154 | public static String getValidIdentifierForLeafref(String identifier, YangConstructType yangConstruct, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 155 | ParserRuleContext ctx, YangLeafRef yangLeafRef) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 156 | |
| 157 | String identifierString = removeQuotesAndHandleConcat(identifier); |
| 158 | ParserException parserException; |
| 159 | |
| 160 | if (identifierString.length() > IDENTIFIER_LENGTH) { |
| 161 | parserException = new ParserException("YANG file error : " + " identifier " + identifierString + " in " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 162 | getYangConstructType(yangConstruct) + " " + yangLeafRef.getPath() + " is " + |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 163 | "greater than 64 characters."); |
| 164 | } else if (!IDENTIFIER_PATTERN.matcher(identifierString).matches()) { |
| 165 | parserException = new ParserException("YANG file error : " + " identifier " + identifierString + " in " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 166 | getYangConstructType(yangConstruct) + " " + yangLeafRef.getPath() + " is not " + |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 167 | "valid."); |
| 168 | } else if (identifierString.toLowerCase().startsWith(XML)) { |
| 169 | parserException = new ParserException("YANG file error : " + " identifier " + identifierString + " in " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 170 | getYangConstructType(yangConstruct) + " " + yangLeafRef.getPath() + |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 171 | " must not start with (('X'|'x') ('M'|'m') ('L'|'l'))."); |
| 172 | } else { |
| 173 | return identifierString; |
| 174 | } |
| 175 | |
| 176 | parserException.setLine(ctx.getStart().getLine()); |
| 177 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 178 | throw parserException; |
| 179 | } |
| 180 | |
| 181 | /** |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 182 | * Validates the revision date. |
| 183 | * |
| 184 | * @param dateToValidate input revision date |
| 185 | * @return validation result, true for success, false for failure |
| 186 | */ |
| 187 | public static boolean isDateValid(String dateToValidate) { |
Vidyashree Rama | 1ae576c | 2016-05-09 11:45:05 +0530 | [diff] [blame] | 188 | if (dateToValidate == null || !dateToValidate.matches(DATE_PATTERN)) { |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 192 | SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 193 | sdf.setLenient(false); |
| 194 | |
| 195 | try { |
| 196 | //if not valid, it will throw ParseException |
| 197 | sdf.parse(dateToValidate); |
| 198 | } catch (ParseException e) { |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | return true; |
| 203 | } |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * Validates YANG version. |
| 207 | * |
| 208 | * @param ctx version context object of the grammar rule |
| 209 | * @return valid version |
| 210 | */ |
| 211 | public static byte getValidVersion(GeneratedYangParser.YangVersionStatementContext ctx) { |
| 212 | |
| 213 | String value = removeQuotesAndHandleConcat(ctx.version().getText()); |
| 214 | if (!value.equals(ONE)) { |
| 215 | ParserException parserException = new ParserException("YANG file error: Input version not supported"); |
| 216 | parserException.setLine(ctx.getStart().getLine()); |
| 217 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 218 | throw parserException; |
| 219 | } |
| 220 | |
| 221 | return Byte.valueOf(value); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Validates non negative integer value. |
| 226 | * |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 227 | * @param integerValue integer to be validated |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 228 | * @param yangConstruct yang construct for creating error message |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 229 | * @param ctx context object of the grammar rule |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 230 | * @return valid non negative integer value |
| 231 | */ |
| 232 | public static int getValidNonNegativeIntegerValue(String integerValue, YangConstructType yangConstruct, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 233 | ParserRuleContext ctx) { |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 234 | |
| 235 | String value = removeQuotesAndHandleConcat(integerValue); |
| 236 | if (!value.matches(NON_NEGATIVE_INTEGER_PATTERN)) { |
| 237 | ParserException parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 238 | getYangConstructType(yangConstruct) + " value " + value + " is not " + |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 239 | "valid."); |
| 240 | parserException.setLine(ctx.getStart().getLine()); |
| 241 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 242 | throw parserException; |
| 243 | } |
| 244 | |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 245 | int valueInInteger; |
| 246 | try { |
| 247 | valueInInteger = Integer.parseInt(value); |
| 248 | } catch (NumberFormatException e) { |
| 249 | ParserException parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 250 | getYangConstructType(yangConstruct) + " value " + value + " is not " + |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 251 | "valid."); |
| 252 | parserException.setLine(ctx.getStart().getLine()); |
| 253 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 254 | throw parserException; |
| 255 | } |
| 256 | return valueInInteger; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 260 | * Validates integer value. |
| 261 | * |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 262 | * @param integerValue integer to be validated |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 263 | * @param yangConstruct yang construct for creating error message |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 264 | * @param ctx context object of the grammar rule |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 265 | * @return valid integer value |
| 266 | */ |
| 267 | public static int getValidIntegerValue(String integerValue, YangConstructType yangConstruct, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 268 | ParserRuleContext ctx) { |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 269 | |
| 270 | String value = removeQuotesAndHandleConcat(integerValue); |
| 271 | if (!INTEGER_PATTERN.matcher(value).matches()) { |
| 272 | ParserException parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 273 | getYangConstructType(yangConstruct) + " value " + value + " is not " + |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 274 | "valid."); |
| 275 | parserException.setLine(ctx.getStart().getLine()); |
| 276 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 277 | throw parserException; |
| 278 | } |
| 279 | |
| 280 | int valueInInteger; |
| 281 | try { |
| 282 | valueInInteger = Integer.parseInt(value); |
| 283 | } catch (NumberFormatException e) { |
| 284 | ParserException parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 285 | getYangConstructType(yangConstruct) + " value " + value + " is not " + |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 286 | "valid."); |
| 287 | parserException.setLine(ctx.getStart().getLine()); |
| 288 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 289 | throw parserException; |
| 290 | } |
| 291 | return valueInInteger; |
| 292 | } |
| 293 | |
| 294 | /** |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 295 | * Validates boolean value. |
| 296 | * |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 297 | * @param booleanValue value to be validated |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 298 | * @param yangConstruct yang construct for creating error message |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 299 | * @param ctx context object of the grammar rule |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 300 | * @return boolean value either true or false |
| 301 | */ |
| 302 | public static boolean getValidBooleanValue(String booleanValue, YangConstructType yangConstruct, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 303 | ParserRuleContext ctx) { |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 304 | |
| 305 | String value = removeQuotesAndHandleConcat(booleanValue); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 306 | if (value.equals(TRUE)) { |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 307 | return true; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 308 | } else if (value.equals(FALSE)) { |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 309 | return false; |
| 310 | } else { |
| 311 | ParserException parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 312 | getYangConstructType(yangConstruct) + " value " + value + " is not " + |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 313 | "valid."); |
| 314 | parserException.setLine(ctx.getStart().getLine()); |
| 315 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 316 | throw parserException; |
| 317 | } |
| 318 | } |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 319 | |
| 320 | /** |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 321 | * Returns current date and makes it in usable format for revision. |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 322 | * |
| 323 | * @return usable current date format for revision |
| 324 | */ |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 325 | public static Date getCurrentDateForRevision() { |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 326 | |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 327 | SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 328 | |
| 329 | Date date = new Date(); |
| 330 | String dateInString = dateFormat.format(date); |
| 331 | try { |
| 332 | //if not valid, it will throw ParseException |
| 333 | Date now = dateFormat.parse(dateInString); |
| 334 | return date; |
| 335 | } catch (ParseException e) { |
| 336 | ParserException parserException = new ParserException("YANG file error: Input date is not correct"); |
| 337 | throw parserException; |
| 338 | } |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 339 | } |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 340 | |
| 341 | /** |
| 342 | * Checks and return valid node identifier. |
| 343 | * |
| 344 | * @param nodeIdentifierString string from yang file |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 345 | * @param yangConstruct yang construct for creating error message |
| 346 | * @param ctx yang construct's context to get the line number and character position |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 347 | * @return valid node identifier |
| 348 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 349 | public static YangNodeIdentifier getValidNodeIdentifier(String nodeIdentifierString, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 350 | YangConstructType yangConstruct, ParserRuleContext ctx) { |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 351 | String tmpIdentifierString = removeQuotesAndHandleConcat(nodeIdentifierString); |
| 352 | String[] tmpData = tmpIdentifierString.split(Pattern.quote(COLON)); |
| 353 | if (tmpData.length == 1) { |
| 354 | YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); |
| 355 | nodeIdentifier.setName(getValidIdentifier(tmpData[0], yangConstruct, ctx)); |
| 356 | return nodeIdentifier; |
| 357 | } else if (tmpData.length == 2) { |
| 358 | YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); |
| 359 | nodeIdentifier.setPrefix(getValidIdentifier(tmpData[0], yangConstruct, ctx)); |
| 360 | nodeIdentifier.setName(getValidIdentifier(tmpData[1], yangConstruct, ctx)); |
| 361 | return nodeIdentifier; |
| 362 | } else { |
| 363 | ParserException parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 364 | getYangConstructType(yangConstruct) + " name " + nodeIdentifierString + |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 365 | " is not valid."); |
| 366 | parserException.setLine(ctx.getStart().getLine()); |
| 367 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 368 | throw parserException; |
| 369 | } |
| 370 | } |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 371 | |
| 372 | /** |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 373 | * Checks and return valid node identifier specific to nodes in leafref path. |
| 374 | * |
| 375 | * @param nodeIdentifierString string from yang file |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 376 | * @param yangConstruct yang construct for creating error message |
| 377 | * @param ctx yang construct's context to get the line number and character position |
| 378 | * @param yangLeafRef instance of leafref where the path argument has to be set |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 379 | * @return valid node identifier |
| 380 | */ |
| 381 | public static YangNodeIdentifier getValidNodeIdentifierForLeafref(String nodeIdentifierString, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 382 | YangConstructType yangConstruct, |
| 383 | ParserRuleContext ctx, YangLeafRef yangLeafRef) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 384 | |
| 385 | String tmpIdentifierString = removeQuotesAndHandleConcat(nodeIdentifierString); |
| 386 | String[] tmpData = tmpIdentifierString.split(Pattern.quote(COLON)); |
| 387 | if (tmpData.length == 1) { |
| 388 | YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 389 | nodeIdentifier.setName(getValidIdentifierForLeafref(tmpData[0], yangConstruct, ctx, yangLeafRef)); |
| 390 | return nodeIdentifier; |
| 391 | } else if (tmpData.length == 2) { |
| 392 | YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); |
| 393 | nodeIdentifier.setPrefix(getValidIdentifierForLeafref(tmpData[0], yangConstruct, ctx, yangLeafRef)); |
| 394 | nodeIdentifier.setName(getValidIdentifierForLeafref(tmpData[1], yangConstruct, ctx, yangLeafRef)); |
| 395 | return nodeIdentifier; |
| 396 | } else { |
| 397 | ParserException parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 398 | getYangConstructType(yangConstruct) + yangLeafRef.getPath() + |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 399 | " is not valid."); |
| 400 | parserException.setLine(ctx.getStart().getLine()); |
| 401 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 402 | throw parserException; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Validates the path argument. It can be either absolute or relative path. |
| 408 | * |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 409 | * @param pathString the path string from the path type |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 410 | * @param yangConstruct yang construct for creating error message |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 411 | * @param ctx yang construct's context to get the line number and character position |
| 412 | * @param yangLeafRef instance of leafref where the path argument has to be set |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 413 | */ |
| 414 | public static void validatePathArgument(String pathString, YangConstructType yangConstruct, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 415 | ParserRuleContext ctx, YangLeafRef yangLeafRef) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 416 | |
| 417 | String completePathString = removeQuotesAndHandleConcat(pathString); |
| 418 | yangLeafRef.setPath(completePathString); |
| 419 | if (completePathString.startsWith(SLASH)) { |
| 420 | yangLeafRef.setPathType(ABSOLUTE_PATH); |
| 421 | List<YangAtomicPath> yangAtomicPathListList = validateAbsolutePath(completePathString, yangConstruct, ctx, |
| 422 | yangLeafRef); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 423 | validatePrefixAndYangNode(yangAtomicPathListList, yangLeafRef); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 424 | yangLeafRef.setAtomicPath(yangAtomicPathListList); |
| 425 | } else if (completePathString.startsWith(ANCESTOR_ACCESSOR)) { |
| 426 | yangLeafRef.setPathType(RELATIVE_PATH); |
| 427 | validateRelativePath(completePathString, yangConstruct, ctx, yangLeafRef); |
| 428 | } else { |
| 429 | ParserException parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 430 | getYangConstructType(yangConstruct) + yangLeafRef.getPath() + |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 431 | " does not follow valid path syntax"); |
| 432 | parserException.setLine(ctx.getStart().getLine()); |
| 433 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 434 | throw parserException; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 439 | * Validates the prefixes in the leafref and assigns them to the respective imported name in map. |
| 440 | * |
| 441 | * @param yangAtomicPathList list of atomic poth |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 442 | * @param yangLeafRef instance YANG leafref |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 443 | */ |
| 444 | private static void validatePrefixAndYangNode(List<YangAtomicPath> yangAtomicPathList, YangLeafRef yangLeafRef) { |
| 445 | Iterator<YangAtomicPath> yangAtomicPathIterator = yangAtomicPathList.listIterator(); |
| 446 | while (yangAtomicPathIterator.hasNext()) { |
| 447 | YangAtomicPath atomicPath = yangAtomicPathIterator.next(); |
| 448 | String prefix = atomicPath.getNodeIdentifier().getPrefix(); |
| 449 | YangNode parentNodeOfLeafref = yangLeafRef.getParentNodeOfLeafref(); |
| 450 | YangNode moduleOrSubModule = getModuleOrSubmoduleInFileOfTheCurrentNode(parentNodeOfLeafref); |
| 451 | YangModule moduleNode = null; |
| 452 | if (moduleOrSubModule instanceof YangModule) { |
| 453 | moduleNode = (YangModule) moduleOrSubModule; |
| 454 | } |
| 455 | if (moduleNode != null) { |
| 456 | updatePrefixWithTheImportedList(moduleNode, prefix, yangLeafRef); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Updates the prefix with the imported list in the module. |
| 463 | * |
| 464 | * @param moduleNode root node of the leafref |
| 465 | * @param prefixInPath prefix in the path |
| 466 | * @param yangLeafRef instance YANG leafref |
| 467 | */ |
| 468 | private static void updatePrefixWithTheImportedList(YangModule moduleNode, String prefixInPath, YangLeafRef |
| 469 | yangLeafRef) { |
| 470 | if (prefixInPath != null && prefixInPath != EMPTY_STRING && !prefixInPath.equals(moduleNode.getPrefix())) { |
| 471 | List<YangImport> moduleImportList = moduleNode.getImportList(); |
| 472 | if (moduleImportList != null && !moduleImportList.isEmpty()) { |
| 473 | Iterator<YangImport> yangImportIterator = moduleImportList.listIterator(); |
| 474 | while (yangImportIterator.hasNext()) { |
| 475 | YangImport yangImport = yangImportIterator.next(); |
| 476 | if (yangImport.getPrefixId().equals(prefixInPath)) { |
| 477 | HashMap prefixMap = new HashMap(); |
| 478 | prefixMap.put(prefixInPath, yangImport.getModuleName()); |
| 479 | yangLeafRef.setPrefixAndItsImportedModule(prefixMap); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } else { |
| 484 | HashMap prefixMap = new HashMap(); |
| 485 | prefixMap.put(prefixInPath, moduleNode.getName()); |
| 486 | yangLeafRef.setPrefixAndItsImportedModule(prefixMap); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Returns module or submodule node from the current node. |
| 492 | * |
| 493 | * @param node current node |
| 494 | * @return root node |
| 495 | */ |
| 496 | private static YangNode getModuleOrSubmoduleInFileOfTheCurrentNode(YangNode node) { |
| 497 | while (!(node instanceof YangModule) && !(node instanceof YangSubModule)) { |
| 498 | if (node == null) { |
| 499 | throw new ParserException("Internal datamodel error: Datamodel tree is not correct"); |
| 500 | } |
| 501 | node = node.getParent(); |
| 502 | } |
| 503 | return node; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Validates the unique syntax from the reference path. |
| 508 | * |
| 509 | * @param uniquePath path of unique |
| 510 | * @param prefixOfFile current file's prefix |
| 511 | * @param ctx yang construct's context to get the line number and character position |
| 512 | * @return list of absolute path |
| 513 | */ |
| 514 | private static List<YangAtomicPath> validateUniqueValues(String uniquePath, String prefixOfFile, |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 515 | ParserRuleContext ctx) { |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 516 | List<YangAtomicPath> atomicPath = new LinkedList<>(); |
| 517 | String[] pathInUnique = uniquePath.split(SLASH_FOR_STRING); |
| 518 | for (String uniqueValue : pathInUnique) { |
| 519 | YangAtomicPath yangAtomicPathPath = new YangAtomicPath(); |
| 520 | YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(uniqueValue, YangConstructType.UNIQUE_DATA, ctx); |
| 521 | yangAtomicPathPath.setNodeIdentifier(nodeIdentifier); |
| 522 | atomicPath.add(yangAtomicPathPath); |
| 523 | if (nodeIdentifier.getPrefix() != null && nodeIdentifier.getPrefix() != prefixOfFile) { |
| 524 | ParserException parserException = new ParserException("YANG file error : A leaf reference, in unique," + |
| 525 | " must refer to a leaf in the list"); |
| 526 | parserException.setLine(ctx.getStart().getLine()); |
| 527 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 528 | throw parserException; |
| 529 | } |
| 530 | } |
| 531 | return atomicPath; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Validates unique field from the list. |
| 536 | * |
| 537 | * @param yangList instance of YANG list |
| 538 | * @param ctx yang construct's context to get the line number and character position |
| 539 | */ |
| 540 | public static void validateUniqueInList(YangList yangList, ParserRuleContext ctx) { |
| 541 | YangLeaf leaf; |
| 542 | // Returns the prefix for the file where unique is present. |
| 543 | String prefixOfTheFile = getPrefixInFileOfTheCurrentNode(yangList); |
| 544 | List<String> uniques = yangList.getUniqueList(); |
| 545 | if (uniques != null && !uniques.isEmpty()) { |
| 546 | Iterator<String> uniqueList = uniques.listIterator(); |
| 547 | while (uniqueList.hasNext()) { |
| 548 | String pathInUnique = uniqueList.next(); |
| 549 | List<YangAtomicPath> atomicPathInUnique = validateUniqueValues(pathInUnique, prefixOfTheFile, ctx); |
| 550 | YangAtomicPath leafInPath = atomicPathInUnique.get(atomicPathInUnique.size() - 1); |
| 551 | if (atomicPathInUnique.size() == 1) { |
| 552 | leaf = getReferenceLeafFromUnique(yangList, leafInPath); |
| 553 | } else { |
| 554 | atomicPathInUnique.remove(atomicPathInUnique.size() - 1); |
| 555 | YangNode holderOfLeaf = getNodeUnderListFromPath(atomicPathInUnique, yangList, ctx); |
| 556 | leaf = getReferenceLeafFromUnique(holderOfLeaf, leafInPath); |
| 557 | } |
| 558 | if (leaf == null) { |
| 559 | ParserException parserException = new ParserException("YANG file error : A leaf reference, in " + |
| 560 | "unique," + |
| 561 | " must refer to a leaf under the list"); |
| 562 | parserException.setLine(ctx.getStart().getLine()); |
| 563 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 564 | throw parserException; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Returns the last node under the unique path. |
| 572 | * |
| 573 | * @param uniquePath atomic path list |
| 574 | * @param node root node from where it starts searching |
| 575 | * @param ctx yang construct's context to get the line number and character position |
| 576 | * @return last node in the list |
| 577 | */ |
| 578 | private static YangNode getNodeUnderListFromPath(List<YangAtomicPath> uniquePath, YangNode node, |
| 579 | ParserRuleContext ctx) { |
| 580 | Iterator<YangAtomicPath> nodesInReference = uniquePath.listIterator(); |
| 581 | YangNode potentialReferredNode = node.getChild(); |
| 582 | while (nodesInReference.hasNext()) { |
| 583 | YangAtomicPath nodeInUnique = nodesInReference.next(); |
| 584 | YangNode referredNode = getReferredNodeFromTheUniqueNodes(nodeInUnique.getNodeIdentifier(), |
| 585 | potentialReferredNode); |
| 586 | if (referredNode == null) { |
| 587 | ParserException parserException = new ParserException("YANG file error : The target node in unique " + |
| 588 | "reference path is invalid"); |
| 589 | parserException.setLine(ctx.getStart().getLine()); |
| 590 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 591 | throw parserException; |
| 592 | } else { |
| 593 | potentialReferredNode = referredNode.getChild(); |
| 594 | } |
| 595 | } |
| 596 | return potentialReferredNode; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Returns the node that matches with the name of the node in path. |
| 601 | * |
| 602 | * @param nodeInUnique node name in path |
| 603 | * @param potentialReferredNode node under which it has to match |
| 604 | * @return referred node |
| 605 | */ |
| 606 | private static YangNode getReferredNodeFromTheUniqueNodes(YangNodeIdentifier nodeInUnique, YangNode |
| 607 | potentialReferredNode) { |
| 608 | while (potentialReferredNode != null) { |
| 609 | // Check if the potential referred node is the actual referred node |
| 610 | if (potentialReferredNode.getName().equals(nodeInUnique.getName())) { |
| 611 | return potentialReferredNode; |
| 612 | } |
| 613 | potentialReferredNode = potentialReferredNode.getNextSibling(); |
| 614 | } |
| 615 | return null; |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Returns the leaf which unique refers. |
| 620 | * |
| 621 | * @param nodeForLeaf last node where leaf is referred |
| 622 | * @param leafInUnique leaf in unique path |
| 623 | * @return YANG leaf |
| 624 | */ |
| 625 | private static YangLeaf getReferenceLeafFromUnique(YangNode nodeForLeaf, YangAtomicPath leafInUnique) { |
| 626 | YangLeavesHolder leavesHolder = (YangLeavesHolder) nodeForLeaf; |
| 627 | List<YangLeaf> leaves = leavesHolder.getListOfLeaf(); |
| 628 | if (leaves != null && !leaves.isEmpty()) { |
| 629 | for (YangLeaf leaf : leaves) { |
| 630 | if (leafInUnique.getNodeIdentifier().getName().equals(leaf.getName())) { |
| 631 | return leaf; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | return null; |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Returns the prefix of the current file. |
| 640 | * |
| 641 | * @param node node where it needs to find the root node |
| 642 | * @return prefix of root node |
| 643 | */ |
| 644 | public static String getPrefixInFileOfTheCurrentNode(YangNode node) { |
| 645 | String prefixInFile; |
| 646 | while (!(node instanceof YangReferenceResolver)) { |
| 647 | node = node.getParent(); |
| 648 | if (node == null) { |
| 649 | throw new ParserException("Internal datamodel error: Datamodel tree is not correct"); |
| 650 | } |
| 651 | } |
| 652 | if (node instanceof YangModule) { |
| 653 | YangModule yangModule = (YangModule) node; |
| 654 | prefixInFile = yangModule.getPrefix(); |
| 655 | } else { |
| 656 | YangSubModule yangSubModule = (YangSubModule) node; |
| 657 | prefixInFile = yangSubModule.getPrefix(); |
| 658 | } |
| 659 | return prefixInFile; |
| 660 | } |
| 661 | |
| 662 | /** |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 663 | * Validates the relative path. |
| 664 | * |
| 665 | * @param completePathString the path string of relative path |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 666 | * @param yangConstruct yang construct for creating error message |
| 667 | * @param ctx yang construct's context to get the line number and character position |
| 668 | * @param yangLeafRef instance of leafref where the path argument has to be set |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 669 | */ |
| 670 | private static void validateRelativePath(String completePathString, YangConstructType yangConstruct, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 671 | ParserRuleContext ctx, YangLeafRef yangLeafRef) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 672 | |
| 673 | YangRelativePath relativePath = new YangRelativePath(); |
| 674 | int numberOfAncestors = 0; |
| 675 | while (completePathString.startsWith(ANCESTOR_ACCESSOR_IN_PATH)) { |
| 676 | completePathString = completePathString.replaceFirst(ANCESTOR_ACCESSOR_IN_PATH, EMPTY_STRING); |
| 677 | numberOfAncestors = numberOfAncestors + 1; |
| 678 | } |
| 679 | if (completePathString == null || completePathString.length() == 0) { |
| 680 | ParserException parserException = new ParserException("YANG file error : " |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 681 | + getYangConstructType(yangConstruct) + yangLeafRef.getPath() + |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 682 | " does not follow valid path syntax"); |
| 683 | parserException.setLine(ctx.getStart().getLine()); |
| 684 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 685 | throw parserException; |
| 686 | } |
| 687 | relativePath.setAncestorNodeCount(numberOfAncestors); |
| 688 | List<YangAtomicPath> atomicPath = validateAbsolutePath(SLASH_FOR_STRING + completePathString, |
| 689 | yangConstruct, |
| 690 | ctx, yangLeafRef); |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 691 | validatePrefixAndYangNode(atomicPath, yangLeafRef); |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 692 | relativePath.setAtomicPathList(atomicPath); |
| 693 | yangLeafRef.setRelativePath(relativePath); |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Validates the absolute path. |
| 698 | * |
| 699 | * @param completePathString the path string of absolute path |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 700 | * @param yangConstruct yang construct for creating error message |
| 701 | * @param ctx yang construct's context to get the line number and character position |
| 702 | * @param yangLeafRef instance of leafref where the path argument has to be set |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 703 | * @return list of object of node in absolute path |
| 704 | */ |
| 705 | private static List<YangAtomicPath> validateAbsolutePath(String completePathString, |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 706 | YangConstructType yangConstruct, ParserRuleContext |
| 707 | ctx, YangLeafRef yangLeafRef) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 708 | |
| 709 | List<YangAtomicPath> absolutePathList = new LinkedList<>(); |
| 710 | YangPathPredicate yangPathPredicate = new YangPathPredicate(); |
| 711 | YangNodeIdentifier yangNodeIdentifier; |
| 712 | |
| 713 | while (completePathString != null) { |
| 714 | String path = completePathString.replaceFirst(SLASH_FOR_STRING, EMPTY_STRING); |
| 715 | if (path == null || path.length() == 0) { |
| 716 | ParserException parserException = new ParserException("YANG file error : " |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 717 | + getYangConstructType(yangConstruct) + " " + yangLeafRef.getPath() + |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 718 | " does not follow valid path syntax"); |
| 719 | parserException.setLine(ctx.getStart().getLine()); |
| 720 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 721 | throw parserException; |
| 722 | } |
| 723 | String matchedPathPredicate; |
| 724 | String nodeIdentifier; |
| 725 | String[] differentiate = new String[2]; |
| 726 | int forNodeIdentifier = path.indexOf(CHAR_OF_SLASH); |
| 727 | int forPathPredicate = path.indexOf(CHAR_OF_OPEN_SQUARE_BRACKET); |
| 728 | |
| 729 | // Checks if path predicate is present for the node. |
| 730 | if ((forPathPredicate < forNodeIdentifier) && (forPathPredicate != -1)) { |
| 731 | List<String> pathPredicate = new ArrayList<>(); |
| 732 | matchedPathPredicate = matchForPathPredicate(path); |
| 733 | |
| 734 | if (matchedPathPredicate == null || matchedPathPredicate.length() == 0) { |
| 735 | ParserException parserException = new ParserException("YANG file error : " |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 736 | + getYangConstructType(yangConstruct) + " " + yangLeafRef.getPath() + |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 737 | " does not follow valid path syntax"); |
| 738 | parserException.setLine(ctx.getStart().getLine()); |
| 739 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 740 | throw parserException; |
| 741 | } |
| 742 | int indexOfMatchedFirstOpenBrace = path.indexOf(CHAR_OF_OPEN_SQUARE_BRACKET); |
| 743 | differentiate[0] = path.substring(0, indexOfMatchedFirstOpenBrace); |
| 744 | differentiate[1] = path.substring(indexOfMatchedFirstOpenBrace); |
| 745 | pathPredicate.add(matchedPathPredicate); |
| 746 | nodeIdentifier = differentiate[0]; |
| 747 | // Starts adding all path predicates of a node into the list. |
| 748 | if (!differentiate[1].isEmpty()) { |
| 749 | while (differentiate[1].startsWith(OPEN_SQUARE_BRACKET)) { |
| 750 | matchedPathPredicate = matchForPathPredicate(differentiate[1]); |
| 751 | if (matchedPathPredicate == null || matchedPathPredicate.length() == 0) { |
| 752 | ParserException parserException = new ParserException( |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 753 | "YANG file error : " + getYangConstructType(yangConstruct) + " " |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 754 | + yangLeafRef.getPath() + |
| 755 | " does not follow valid path syntax"); |
| 756 | parserException.setLine(ctx.getStart().getLine()); |
| 757 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 758 | throw parserException; |
| 759 | } |
| 760 | pathPredicate.add(matchedPathPredicate); |
| 761 | differentiate[1] = differentiate[1].substring(matchedPathPredicate.length()); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | List<YangPathPredicate> pathPredicateList = validatePathPredicate(pathPredicate, yangConstruct, ctx, |
| 766 | yangPathPredicate, yangLeafRef); |
| 767 | YangAtomicPath atomicPath = new YangAtomicPath(); |
| 768 | yangNodeIdentifier = getValidNodeIdentifierForLeafref(nodeIdentifier, yangConstruct, ctx, yangLeafRef); |
| 769 | atomicPath.setNodeIdentifier(yangNodeIdentifier); |
| 770 | atomicPath.setPathPredicatesList(pathPredicateList); |
| 771 | absolutePathList.add(atomicPath); |
| 772 | } else { |
| 773 | if (path.contains(SLASH_FOR_STRING)) { |
| 774 | nodeIdentifier = path.substring(0, path.indexOf(CHAR_OF_SLASH)); |
| 775 | differentiate[1] = path.substring(path.indexOf(CHAR_OF_SLASH)); |
| 776 | } else { |
| 777 | nodeIdentifier = path; |
| 778 | differentiate[1] = null; |
| 779 | } |
| 780 | yangNodeIdentifier = getValidNodeIdentifierForLeafref(nodeIdentifier, yangConstruct, ctx, yangLeafRef); |
| 781 | |
| 782 | YangAtomicPath atomicPath = new YangAtomicPath(); |
| 783 | atomicPath.setNodeIdentifier(yangNodeIdentifier); |
| 784 | atomicPath.setPathPredicatesList(null); |
| 785 | absolutePathList.add(atomicPath); |
| 786 | } |
| 787 | if (differentiate[1] == null || differentiate[1].length() == 0) { |
| 788 | completePathString = null; |
| 789 | } else { |
| 790 | completePathString = differentiate[1]; |
| 791 | } |
| 792 | } |
| 793 | return absolutePathList; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * Validates path predicate in the absolute path's node. |
| 798 | * |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 799 | * @param pathPredicate list of path predicates in the node of absolute path |
| 800 | * @param yangConstruct yang construct for creating error message |
| 801 | * @param ctx yang construct's context to get the line number and character position |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 802 | * @param yangPathPredicate instance of path predicate where it has to be set |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 803 | * @param yangLeafRef instance of leafref where the path argument has to be set |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 804 | * @return list of object of path predicates in absolute path's node |
| 805 | */ |
| 806 | private static List<YangPathPredicate> validatePathPredicate(List<String> pathPredicate, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 807 | YangConstructType yangConstruct, ParserRuleContext |
| 808 | ctx, YangPathPredicate yangPathPredicate, |
| 809 | YangLeafRef yangLeafRef) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 810 | |
| 811 | Iterator<String> pathPredicateString = pathPredicate.iterator(); |
| 812 | List<String> pathEqualityExpression = new ArrayList<>(); |
| 813 | |
| 814 | while (pathPredicateString.hasNext()) { |
| 815 | String pathPredicateForNode = pathPredicateString.next(); |
| 816 | pathPredicateForNode = (pathPredicateForNode.substring(1)).trim(); |
| 817 | pathPredicateForNode = pathPredicateForNode.substring(0, |
| 818 | pathPredicateForNode.indexOf(CHAR_OF_CLOSE_SQUARE_BRACKET)); |
| 819 | pathEqualityExpression.add(pathPredicateForNode); |
| 820 | } |
| 821 | List<YangPathPredicate> validatedPathPredicateList = validatePathEqualityExpression(pathEqualityExpression, |
| 822 | yangConstruct, ctx, yangPathPredicate, yangLeafRef); |
| 823 | return validatedPathPredicateList; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Validates the path equality expression. |
| 828 | * |
| 829 | * @param pathEqualityExpression list of path equality expression in the path predicates of the node |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 830 | * @param yangConstruct yang construct for creating error message |
| 831 | * @param ctx yang construct's context to get the line number and character position |
| 832 | * @param yangPathPredicate instance of path predicate where it has to be set |
| 833 | * @param yangLeafRef instance of leafref where the path argument has to be set |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 834 | * @return list of object of path predicates in absolute path's node |
| 835 | */ |
| 836 | private static List<YangPathPredicate> validatePathEqualityExpression(List<String> pathEqualityExpression, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 837 | YangConstructType yangConstruct, |
| 838 | ParserRuleContext ctx, YangPathPredicate |
| 839 | yangPathPredicate, |
| 840 | YangLeafRef yangLeafRef) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 841 | |
| 842 | Iterator<String> pathEqualityExpressionString = pathEqualityExpression.iterator(); |
| 843 | List<YangPathPredicate> yangPathPredicateList = new ArrayList<>(); |
| 844 | |
| 845 | while (pathEqualityExpressionString.hasNext()) { |
| 846 | String pathEqualityExpressionForNode = pathEqualityExpressionString.next(); |
| 847 | String[] pathEqualityExpressionArray = pathEqualityExpressionForNode.split("[=]"); |
| 848 | |
| 849 | YangNodeIdentifier yangNodeIdentifierForPredicate; |
| 850 | YangRelativePath yangRelativePath; |
| 851 | yangNodeIdentifierForPredicate = getValidNodeIdentifierForLeafref(pathEqualityExpressionArray[0].trim(), |
| 852 | yangConstruct, ctx, yangLeafRef); |
| 853 | yangRelativePath = validatePathKeyExpression(pathEqualityExpressionArray[1].trim(), yangConstruct, ctx, |
| 854 | yangLeafRef); |
| 855 | yangPathPredicate.setNodeIdentifier(yangNodeIdentifierForPredicate); |
| 856 | yangPathPredicate.setPathOperator(EQUALTO); |
| 857 | yangPathPredicate.setRightRelativePath(yangRelativePath); |
| 858 | yangPathPredicateList.add(yangPathPredicate); |
| 859 | } |
| 860 | return yangPathPredicateList; |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Validate the path key expression. |
| 865 | * |
| 866 | * @param rightRelativePath relative path in the path predicate |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 867 | * @param yangConstruct yang construct for creating error message |
| 868 | * @param ctx yang construct's context to get the line number and character position |
| 869 | * @param yangLeafRef instance of leafref where the path argument has to be set |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 870 | * @return object of right relative path in path predicate |
| 871 | */ |
| 872 | private static YangRelativePath validatePathKeyExpression(String rightRelativePath, |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 873 | YangConstructType yangConstruct, ParserRuleContext ctx, |
| 874 | YangLeafRef yangLeafRef) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 875 | |
| 876 | YangRelativePath yangRelativePath = new YangRelativePath(); |
| 877 | String[] relativePath = rightRelativePath.split(SLASH_FOR_STRING); |
| 878 | List<String> rightAbsolutePath = new ArrayList<>(); |
| 879 | int accessAncestor = 0; |
| 880 | for (String path : relativePath) { |
| 881 | if (path.trim().equals(ANCESTOR_ACCESSOR)) { |
| 882 | accessAncestor = accessAncestor + 1; |
| 883 | } else { |
| 884 | rightAbsolutePath.add(path); |
| 885 | } |
| 886 | } |
| 887 | List<YangAtomicPath> atomicPathInRelativePath = validateRelativePathKeyExpression(rightAbsolutePath, |
| 888 | yangConstruct, ctx, yangLeafRef); |
| 889 | yangRelativePath.setAtomicPathList(atomicPathInRelativePath); |
| 890 | yangRelativePath.setAncestorNodeCount(accessAncestor); |
| 891 | return yangRelativePath; |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * Validates the relative path key expression. |
| 896 | * |
| 897 | * @param rightAbsolutePath absolute path nodes present in the relative path |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 898 | * @param yangConstruct yang construct for creating error message |
| 899 | * @param ctx yang construct's context to get the line number and character position |
| 900 | * @param yangLeafRef instance of leafref where the path argument has to be set |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 901 | * @return list of object of absolute path nodes present in the relative path |
| 902 | */ |
| 903 | private static List<YangAtomicPath> validateRelativePathKeyExpression(List<String> rightAbsolutePath, |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 904 | YangConstructType yangConstruct, |
| 905 | ParserRuleContext ctx, YangLeafRef |
| 906 | yangLeafRef) { |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 907 | |
| 908 | List<YangAtomicPath> atomicPathList = new ArrayList<>(); |
| 909 | YangNodeIdentifier yangNodeIdentifier; |
| 910 | |
| 911 | Iterator<String> nodes = rightAbsolutePath.iterator(); |
| 912 | String currentInvocationFunction = nodes.next(); |
| 913 | currentInvocationFunction = currentInvocationFunction.trim(); |
| 914 | String[] currentFunction = currentInvocationFunction.split("[(]"); |
| 915 | |
| 916 | if (!(currentFunction[0].trim().equals(CURRENT)) || !(currentFunction[1].trim().equals(CLOSE_PARENTHESIS))) { |
| 917 | ParserException parserException = new ParserException("YANG file error : " |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 918 | + getYangConstructType(yangConstruct) + " " + yangLeafRef.getPath() + |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 919 | " does not follow valid path syntax"); |
| 920 | parserException.setLine(ctx.getStart().getLine()); |
| 921 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 922 | throw parserException; |
| 923 | } |
| 924 | |
| 925 | while (nodes.hasNext()) { |
| 926 | YangAtomicPath atomicPath = new YangAtomicPath(); |
| 927 | String node = nodes.next(); |
| 928 | yangNodeIdentifier = getValidNodeIdentifierForLeafref(node.trim(), yangConstruct, ctx, yangLeafRef); |
| 929 | atomicPath.setNodeIdentifier(yangNodeIdentifier); |
| 930 | atomicPathList.add(atomicPath); |
| 931 | } |
| 932 | return atomicPathList; |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * Validates the match for first path predicate in a given string. |
| 937 | * |
| 938 | * @param matchRequiredString string for which match has to be done |
| 939 | * @return the matched string |
| 940 | */ |
| 941 | private static String matchForPathPredicate(String matchRequiredString) { |
| 942 | |
| 943 | String matchedString = null; |
| 944 | java.util.regex.Matcher matcher = PATH_PREDICATE_PATTERN.matcher(matchRequiredString); |
| 945 | if (matcher.find()) { |
| 946 | matchedString = matcher.group(0); |
| 947 | } |
| 948 | return matchedString; |
| 949 | } |
| 950 | |
| 951 | /** |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 952 | * Checks and return valid absolute schema node id. |
| 953 | * |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 954 | * @param argumentString string from yang file |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 955 | * @param yangConstructType yang construct for creating error message |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 956 | * @param ctx yang construct's context to get the line number and character position |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 957 | * @return target nodes list of absolute schema node id |
| 958 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 959 | public static List<YangAtomicPath> getValidAbsoluteSchemaNodeId(String argumentString, |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 960 | YangConstructType yangConstructType, |
| 961 | ParserRuleContext ctx) { |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 962 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 963 | List<YangAtomicPath> targetNodes = new ArrayList<>(); |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 964 | YangNodeIdentifier yangNodeIdentifier; |
| 965 | String tmpSchemaNodeId = removeQuotesAndHandleConcat(argumentString); |
| 966 | |
| 967 | // absolute-schema-nodeid = 1*("/" node-identifier) |
| 968 | if (!tmpSchemaNodeId.startsWith(SLASH)) { |
| 969 | ParserException parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 970 | getYangConstructType(yangConstructType) + " name " + argumentString + |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 971 | "is not valid"); |
| 972 | parserException.setLine(ctx.getStart().getLine()); |
| 973 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 974 | throw parserException; |
| 975 | } |
| 976 | String[] tmpData = tmpSchemaNodeId.replaceFirst(CARET + SLASH, EMPTY_STRING).split(SLASH); |
| 977 | for (String nodeIdentifiers : tmpData) { |
| 978 | yangNodeIdentifier = getValidNodeIdentifier(nodeIdentifiers, yangConstructType, ctx); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 979 | YangAtomicPath yangAbsPath = new YangAtomicPath(); |
| 980 | yangAbsPath.setNodeIdentifier(yangNodeIdentifier); |
| 981 | targetNodes.add(yangAbsPath); |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 982 | } |
| 983 | return targetNodes; |
| 984 | } |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 985 | |
| 986 | /** |
| 987 | * Throws parser exception for unsupported YANG constructs. |
| 988 | * |
| 989 | * @param yangConstructType yang construct for creating error message |
janani b | ebb143d | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 990 | * @param ctx yang construct's context to get the line number and character position |
| 991 | * @param errorInfo error information |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 992 | * @param fileName YANG file name |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 993 | */ |
| 994 | public static void handleUnsupportedYangConstruct(YangConstructType yangConstructType, |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 995 | ParserRuleContext ctx, String errorInfo, String fileName) { |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 996 | ParserException parserException = new ParserException(YANG_FILE_ERROR |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 997 | + QUOTES + getYangConstructType(yangConstructType) + QUOTES |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 998 | + errorInfo); |
| 999 | parserException.setLine(ctx.getStart().getLine()); |
| 1000 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 1001 | log.info(parserException.getMessage() + " at position " + parserException.getCharPositionInLine() + |
| 1002 | " in line " + parserException.getLineNumber() + " of yang file " + fileName); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 1003 | } |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1004 | |
| 1005 | /** |
| 1006 | * Returns date and makes it in usable format for revision. |
| 1007 | * |
| 1008 | * @param dateInString date argument string from yang file |
| 1009 | * @param ctx yang construct's context to get the line number and character position |
| 1010 | * @return date format for revision |
| 1011 | */ |
| 1012 | public static Date getValidDateFromString(String dateInString, ParserRuleContext ctx) { |
| 1013 | String dateArgument = removeQuotesAndHandleConcat(dateInString); |
| 1014 | if (dateArgument == null || !dateArgument.matches(DATE_PATTERN)) { |
| 1015 | ParserException parserException = new ParserException("YANG file error: Input date is not correct"); |
| 1016 | parserException.setLine(ctx.getStart().getLine()); |
| 1017 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 1018 | throw parserException; |
| 1019 | } |
| 1020 | |
| 1021 | SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); |
| 1022 | sdf.setLenient(false); |
| 1023 | |
| 1024 | try { |
| 1025 | //if not valid, it will throw ParseException |
| 1026 | Date date = sdf.parse(dateArgument); |
| 1027 | return date; |
| 1028 | } catch (ParseException e) { |
| 1029 | ParserException parserException = new ParserException("YANG file error: Input date is not correct"); |
| 1030 | parserException.setLine(ctx.getStart().getLine()); |
| 1031 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 1032 | throw parserException; |
| 1033 | } |
| 1034 | } |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 1035 | |
| 1036 | /** |
| 1037 | * Checks and return valid prefix. |
| 1038 | * |
| 1039 | * @param inputString string from yang file |
| 1040 | * @param yangConstruct yang construct for creating error message |
| 1041 | * @param ctx yang construct's context to get the line number and character position |
| 1042 | * @return valid prefix |
| 1043 | */ |
| 1044 | public static String getValidPrefix(String inputString, |
| 1045 | YangConstructType yangConstruct, ParserRuleContext ctx) { |
| 1046 | String tmpPrefixString = removeQuotesAndHandleConcat(inputString); |
| 1047 | String[] tmpData = tmpPrefixString.split(Pattern.quote(COLON)); |
| 1048 | if (tmpData.length == 2) { |
| 1049 | return tmpData[0]; |
| 1050 | } else { |
| 1051 | ParserException parserException = new ParserException("YANG file error : " + |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame^] | 1052 | getYangConstructType(yangConstruct) + " name " + inputString + |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 1053 | " is not valid."); |
| 1054 | parserException.setLine(ctx.getStart().getLine()); |
| 1055 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 1056 | throw parserException; |
| 1057 | } |
| 1058 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1059 | } |