Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 468f828 | 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 | |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 19 | import java.text.ParseException; |
| 20 | import java.text.SimpleDateFormat; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame^] | 21 | import java.util.Date; |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 22 | import java.util.LinkedList; |
| 23 | import java.util.List; |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 24 | import java.util.regex.Pattern; |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 25 | |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 26 | import org.antlr.v4.runtime.ParserRuleContext; |
Gaurav Agrawal | bd80447 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 31 | |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.utils.UtilConstants.ADD; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.UtilConstants.CARET; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame^] | 34 | import static org.onosproject.yangutils.utils.UtilConstants.COLON; |
janani b | 6240d29 | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.utils.UtilConstants.CURRENTLY_UNSUPPORTED; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.UtilConstants.FALSE; |
janani b | 6240d29 | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.IDENTITYREF; |
janani b | 6240d29 | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.INSTANCE_IDENTIFIER; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame^] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.LEAFREF; |
| 41 | import static org.onosproject.yangutils.utils.UtilConstants.QUOTES; |
| 42 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 43 | import static org.onosproject.yangutils.utils.UtilConstants.TRUE; |
| 44 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_FILE_ERROR; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 45 | |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 46 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 47 | * Represents an utility for listener. |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 48 | */ |
| 49 | public final class ListenerUtil { |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 50 | |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 51 | private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*"); |
Vidyashree Rama | be0f6da | 2016-05-09 11:45:05 +0530 | [diff] [blame] | 52 | private static final String DATE_PATTERN = "[0-9]{4}-([0-9]{2}|[0-9])-([0-9]{2}|[0-9])"; |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 53 | private static final String NON_NEGATIVE_INTEGER_PATTERN = "[0-9]+"; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 54 | private static final Pattern INTEGER_PATTERN = Pattern.compile("[-][0-9]+|[0-9]+"); |
Vidyashree Rama | 210c01d | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 55 | private static final String XML = "xml"; |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 56 | private static final String ONE = "1"; |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 57 | private static final int IDENTIFIER_LENGTH = 64; |
janani b | cc9ac30 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 58 | private static final String DATE_FORMAT = "yyyy-MM-dd"; |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Creates a new listener util. |
| 62 | */ |
| 63 | private ListenerUtil() { |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Removes doubles quotes and concatenates if string has plus symbol. |
| 68 | * |
| 69 | * @param yangStringData string from yang file |
| 70 | * @return concatenated string after removing double quotes |
| 71 | */ |
| 72 | public static String removeQuotesAndHandleConcat(String yangStringData) { |
| 73 | |
janani b | cc9ac30 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 74 | yangStringData = yangStringData.replace("\"", EMPTY_STRING); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 75 | String[] tmpData = yangStringData.split(Pattern.quote(ADD)); |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 76 | StringBuilder builder = new StringBuilder(); |
| 77 | for (String yangString : tmpData) { |
| 78 | builder.append(yangString); |
| 79 | } |
| 80 | return builder.toString(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Validates identifier and returns concatenated string if string contains plus symbol. |
| 85 | * |
| 86 | * @param identifier string from yang file |
| 87 | * @param yangConstruct yang construct for creating error message |
| 88 | * @param ctx yang construct's context to get the line number and character position |
| 89 | * @return concatenated string after removing double quotes |
| 90 | */ |
| 91 | public static String getValidIdentifier(String identifier, YangConstructType yangConstruct, ParserRuleContext ctx) { |
| 92 | |
| 93 | String identifierString = removeQuotesAndHandleConcat(identifier); |
| 94 | ParserException parserException; |
| 95 | |
| 96 | if (identifierString.length() > IDENTIFIER_LENGTH) { |
| 97 | parserException = new ParserException("YANG file error : " + |
| 98 | YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is " + |
| 99 | "greater than 64 characters."); |
| 100 | } else if (!IDENTIFIER_PATTERN.matcher(identifierString).matches()) { |
| 101 | parserException = new ParserException("YANG file error : " + |
| 102 | YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is not " + |
| 103 | "valid."); |
Vidyashree Rama | 210c01d | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 104 | } else if (identifierString.toLowerCase().startsWith(XML)) { |
| 105 | parserException = new ParserException("YANG file error : " + |
| 106 | YangConstructType.getYangConstructType(yangConstruct) + " identifier " + identifierString + |
| 107 | " must not start with (('X'|'x') ('M'|'m') ('L'|'l'))."); |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 108 | } else { |
| 109 | return identifierString; |
| 110 | } |
| 111 | |
| 112 | parserException.setLine(ctx.getStart().getLine()); |
| 113 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 114 | throw parserException; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Validates the revision date. |
| 119 | * |
| 120 | * @param dateToValidate input revision date |
| 121 | * @return validation result, true for success, false for failure |
| 122 | */ |
| 123 | public static boolean isDateValid(String dateToValidate) { |
Vidyashree Rama | be0f6da | 2016-05-09 11:45:05 +0530 | [diff] [blame] | 124 | if (dateToValidate == null || !dateToValidate.matches(DATE_PATTERN)) { |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
janani b | cc9ac30 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 128 | SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 129 | sdf.setLenient(false); |
| 130 | |
| 131 | try { |
| 132 | //if not valid, it will throw ParseException |
| 133 | sdf.parse(dateToValidate); |
| 134 | } catch (ParseException e) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | return true; |
| 139 | } |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * Validates YANG version. |
| 143 | * |
| 144 | * @param ctx version context object of the grammar rule |
| 145 | * @return valid version |
| 146 | */ |
| 147 | public static byte getValidVersion(GeneratedYangParser.YangVersionStatementContext ctx) { |
| 148 | |
| 149 | String value = removeQuotesAndHandleConcat(ctx.version().getText()); |
| 150 | if (!value.equals(ONE)) { |
| 151 | ParserException parserException = new ParserException("YANG file error: Input version not supported"); |
| 152 | parserException.setLine(ctx.getStart().getLine()); |
| 153 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 154 | throw parserException; |
| 155 | } |
| 156 | |
| 157 | return Byte.valueOf(value); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Validates non negative integer value. |
| 162 | * |
| 163 | * @param integerValue integer to be validated |
| 164 | * @param yangConstruct yang construct for creating error message |
| 165 | * @param ctx context object of the grammar rule |
| 166 | * @return valid non negative integer value |
| 167 | */ |
| 168 | public static int getValidNonNegativeIntegerValue(String integerValue, YangConstructType yangConstruct, |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 169 | ParserRuleContext ctx) { |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 170 | |
| 171 | String value = removeQuotesAndHandleConcat(integerValue); |
| 172 | if (!value.matches(NON_NEGATIVE_INTEGER_PATTERN)) { |
| 173 | ParserException parserException = new ParserException("YANG file error : " + |
| 174 | YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " + |
| 175 | "valid."); |
| 176 | parserException.setLine(ctx.getStart().getLine()); |
| 177 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 178 | throw parserException; |
| 179 | } |
| 180 | |
Vidyashree Rama | 210c01d | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 181 | int valueInInteger; |
| 182 | try { |
| 183 | valueInInteger = Integer.parseInt(value); |
| 184 | } catch (NumberFormatException e) { |
| 185 | ParserException parserException = new ParserException("YANG file error : " + |
| 186 | YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " + |
| 187 | "valid."); |
| 188 | parserException.setLine(ctx.getStart().getLine()); |
| 189 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 190 | throw parserException; |
| 191 | } |
| 192 | return valueInInteger; |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 196 | * Validates integer value. |
| 197 | * |
| 198 | * @param integerValue integer to be validated |
| 199 | * @param yangConstruct yang construct for creating error message |
| 200 | * @param ctx context object of the grammar rule |
| 201 | * @return valid integer value |
| 202 | */ |
| 203 | public static int getValidIntegerValue(String integerValue, YangConstructType yangConstruct, |
| 204 | ParserRuleContext ctx) { |
| 205 | |
| 206 | String value = removeQuotesAndHandleConcat(integerValue); |
| 207 | if (!INTEGER_PATTERN.matcher(value).matches()) { |
| 208 | ParserException parserException = new ParserException("YANG file error : " + |
| 209 | YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " + |
| 210 | "valid."); |
| 211 | parserException.setLine(ctx.getStart().getLine()); |
| 212 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 213 | throw parserException; |
| 214 | } |
| 215 | |
| 216 | int valueInInteger; |
| 217 | try { |
| 218 | valueInInteger = Integer.parseInt(value); |
| 219 | } catch (NumberFormatException e) { |
| 220 | ParserException parserException = new ParserException("YANG file error : " + |
| 221 | YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " + |
| 222 | "valid."); |
| 223 | parserException.setLine(ctx.getStart().getLine()); |
| 224 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 225 | throw parserException; |
| 226 | } |
| 227 | return valueInInteger; |
| 228 | } |
| 229 | |
| 230 | /** |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 231 | * Validates boolean value. |
| 232 | * |
| 233 | * @param booleanValue value to be validated |
| 234 | * @param yangConstruct yang construct for creating error message |
| 235 | * @param ctx context object of the grammar rule |
| 236 | * @return boolean value either true or false |
| 237 | */ |
| 238 | public static boolean getValidBooleanValue(String booleanValue, YangConstructType yangConstruct, |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 239 | ParserRuleContext ctx) { |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 240 | |
| 241 | String value = removeQuotesAndHandleConcat(booleanValue); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 242 | if (value.equals(TRUE)) { |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 243 | return true; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 244 | } else if (value.equals(FALSE)) { |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 245 | return false; |
| 246 | } else { |
| 247 | ParserException parserException = new ParserException("YANG file error : " + |
| 248 | YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " + |
| 249 | "valid."); |
| 250 | parserException.setLine(ctx.getStart().getLine()); |
| 251 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 252 | throw parserException; |
| 253 | } |
| 254 | } |
janani b | cc9ac30 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 255 | |
| 256 | /** |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame^] | 257 | * Returns current date and makes it in usable format for revision. |
janani b | cc9ac30 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 258 | * |
| 259 | * @return usable current date format for revision |
| 260 | */ |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame^] | 261 | public static Date getCurrentDateForRevision() { |
janani b | cc9ac30 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 262 | |
janani b | cc9ac30 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 263 | SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame^] | 264 | |
| 265 | Date date = new Date(); |
| 266 | String dateInString = dateFormat.format(date); |
| 267 | try { |
| 268 | //if not valid, it will throw ParseException |
| 269 | Date now = dateFormat.parse(dateInString); |
| 270 | return date; |
| 271 | } catch (ParseException e) { |
| 272 | ParserException parserException = new ParserException("YANG file error: Input date is not correct"); |
| 273 | throw parserException; |
| 274 | } |
janani b | cc9ac30 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 275 | } |
Gaurav Agrawal | bd80447 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * Checks and return valid node identifier. |
| 279 | * |
| 280 | * @param nodeIdentifierString string from yang file |
| 281 | * @param yangConstruct yang construct for creating error message |
| 282 | * @param ctx yang construct's context to get the line number and character position |
| 283 | * @return valid node identifier |
| 284 | */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 285 | public static YangNodeIdentifier getValidNodeIdentifier(String nodeIdentifierString, |
| 286 | YangConstructType yangConstruct, ParserRuleContext ctx) { |
Gaurav Agrawal | bd80447 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 287 | String tmpIdentifierString = removeQuotesAndHandleConcat(nodeIdentifierString); |
| 288 | String[] tmpData = tmpIdentifierString.split(Pattern.quote(COLON)); |
| 289 | if (tmpData.length == 1) { |
| 290 | YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); |
janani b | 6240d29 | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 291 | checkForUnsupportedTypes(tmpData[0], yangConstruct, ctx); |
Gaurav Agrawal | bd80447 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 292 | nodeIdentifier.setName(getValidIdentifier(tmpData[0], yangConstruct, ctx)); |
| 293 | return nodeIdentifier; |
| 294 | } else if (tmpData.length == 2) { |
| 295 | YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); |
| 296 | nodeIdentifier.setPrefix(getValidIdentifier(tmpData[0], yangConstruct, ctx)); |
| 297 | nodeIdentifier.setName(getValidIdentifier(tmpData[1], yangConstruct, ctx)); |
| 298 | return nodeIdentifier; |
| 299 | } else { |
| 300 | ParserException parserException = new ParserException("YANG file error : " + |
| 301 | YangConstructType.getYangConstructType(yangConstruct) + " name " + nodeIdentifierString + |
| 302 | " is not valid."); |
| 303 | parserException.setLine(ctx.getStart().getLine()); |
| 304 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 305 | throw parserException; |
| 306 | } |
| 307 | } |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 308 | |
| 309 | /** |
janani b | 6240d29 | 2016-05-17 18:20:33 +0530 | [diff] [blame] | 310 | * Checks whether the type is an unsupported type. |
| 311 | * |
| 312 | * @param typeName name of the type |
| 313 | * @param yangConstruct yang construct to check if it is type |
| 314 | * @param ctx yang construct's context to get the line number and character position |
| 315 | */ |
| 316 | private static void checkForUnsupportedTypes(String typeName, |
| 317 | YangConstructType yangConstruct, ParserRuleContext ctx) { |
| 318 | |
| 319 | if (yangConstruct == YangConstructType.TYPE_DATA) { |
| 320 | if (typeName.equalsIgnoreCase(LEAFREF)) { |
| 321 | handleUnsupportedYangConstruct(YangConstructType.LEAFREF_DATA, |
| 322 | ctx, CURRENTLY_UNSUPPORTED); |
| 323 | } else if (typeName.equalsIgnoreCase(IDENTITYREF)) { |
| 324 | handleUnsupportedYangConstruct(YangConstructType.IDENTITYREF_DATA, |
| 325 | ctx, CURRENTLY_UNSUPPORTED); |
| 326 | } else if (typeName.equalsIgnoreCase(INSTANCE_IDENTIFIER)) { |
| 327 | handleUnsupportedYangConstruct(YangConstructType.INSTANCE_IDENTIFIER_DATA, |
| 328 | ctx, CURRENTLY_UNSUPPORTED); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 334 | * Checks and return valid absolute schema node id. |
| 335 | * |
| 336 | * @param argumentString string from yang file |
| 337 | * @param yangConstructType yang construct for creating error message |
| 338 | * @param ctx yang construct's context to get the line number and character position |
| 339 | * @return target nodes list of absolute schema node id |
| 340 | */ |
| 341 | public static List<YangNodeIdentifier> getValidAbsoluteSchemaNodeId(String argumentString, |
| 342 | YangConstructType yangConstructType, ParserRuleContext ctx) { |
| 343 | |
| 344 | List<YangNodeIdentifier> targetNodes = new LinkedList<>(); |
| 345 | YangNodeIdentifier yangNodeIdentifier; |
| 346 | String tmpSchemaNodeId = removeQuotesAndHandleConcat(argumentString); |
| 347 | |
| 348 | // absolute-schema-nodeid = 1*("/" node-identifier) |
| 349 | if (!tmpSchemaNodeId.startsWith(SLASH)) { |
| 350 | ParserException parserException = new ParserException("YANG file error : " + |
| 351 | YangConstructType.getYangConstructType(yangConstructType) + " name " + argumentString + |
| 352 | "is not valid"); |
| 353 | parserException.setLine(ctx.getStart().getLine()); |
| 354 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 355 | throw parserException; |
| 356 | } |
| 357 | String[] tmpData = tmpSchemaNodeId.replaceFirst(CARET + SLASH, EMPTY_STRING).split(SLASH); |
| 358 | for (String nodeIdentifiers : tmpData) { |
| 359 | yangNodeIdentifier = getValidNodeIdentifier(nodeIdentifiers, yangConstructType, ctx); |
| 360 | targetNodes.add(yangNodeIdentifier); |
| 361 | } |
| 362 | return targetNodes; |
| 363 | } |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 364 | |
| 365 | /** |
| 366 | * Throws parser exception for unsupported YANG constructs. |
| 367 | * |
| 368 | * @param yangConstructType yang construct for creating error message |
| 369 | * @param ctx yang construct's context to get the line number and character position |
| 370 | * @param errorInfo error information |
| 371 | */ |
| 372 | public static void handleUnsupportedYangConstruct(YangConstructType yangConstructType, |
| 373 | ParserRuleContext ctx, String errorInfo) { |
| 374 | ParserException parserException = new ParserException(YANG_FILE_ERROR |
| 375 | + QUOTES + YangConstructType.getYangConstructType(yangConstructType) + QUOTES |
| 376 | + errorInfo); |
| 377 | parserException.setLine(ctx.getStart().getLine()); |
| 378 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 379 | throw parserException; |
| 380 | } |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame^] | 381 | |
| 382 | /** |
| 383 | * Returns date and makes it in usable format for revision. |
| 384 | * |
| 385 | * @param dateInString date argument string from yang file |
| 386 | * @param ctx yang construct's context to get the line number and character position |
| 387 | * @return date format for revision |
| 388 | */ |
| 389 | public static Date getValidDateFromString(String dateInString, ParserRuleContext ctx) { |
| 390 | String dateArgument = removeQuotesAndHandleConcat(dateInString); |
| 391 | if (dateArgument == null || !dateArgument.matches(DATE_PATTERN)) { |
| 392 | ParserException parserException = new ParserException("YANG file error: Input date is not correct"); |
| 393 | parserException.setLine(ctx.getStart().getLine()); |
| 394 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 395 | throw parserException; |
| 396 | } |
| 397 | |
| 398 | SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); |
| 399 | sdf.setLenient(false); |
| 400 | |
| 401 | try { |
| 402 | //if not valid, it will throw ParseException |
| 403 | Date date = sdf.parse(dateArgument); |
| 404 | return date; |
| 405 | } catch (ParseException e) { |
| 406 | ParserException parserException = new ParserException("YANG file error: Input date is not correct"); |
| 407 | parserException.setLine(ctx.getStart().getLine()); |
| 408 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 409 | throw parserException; |
| 410 | } |
| 411 | } |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 412 | } |