Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.parser.impl.parserutils; |
| 18 | |
| 19 | import org.antlr.v4.runtime.ParserRuleContext; |
| 20 | |
| 21 | import java.text.ParseException; |
| 22 | import java.text.SimpleDateFormat; |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 23 | import java.util.Calendar; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 24 | import java.util.regex.Pattern; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 25 | |
| 26 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.utils.YangConstructType; |
| 28 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 29 | |
| 30 | /** |
| 31 | * It's a utility for listener. |
| 32 | */ |
| 33 | public final class ListenerUtil { |
| 34 | private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*"); |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 35 | private static final String NON_NEGATIVE_INTEGER_PATTERN = "[0-9]+"; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 36 | private static final String PLUS = "+"; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 37 | private static final String ONE = "1"; |
| 38 | private static final String TRUE_KEYWORD = "true"; |
| 39 | private static final String FALSE_KEYWORD = "false"; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 40 | private static final int IDENTIFIER_LENGTH = 64; |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 41 | private static final String DATE_FORMAT = "yyyy-MM-dd"; |
| 42 | private static final String EMPTY_STRING = ""; |
| 43 | private static final String HYPHEN = "-"; |
| 44 | private static final String SLASH = "/"; |
| 45 | private static final String SPACE = " "; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Creates a new listener util. |
| 49 | */ |
| 50 | private ListenerUtil() { |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Removes doubles quotes and concatenates if string has plus symbol. |
| 55 | * |
| 56 | * @param yangStringData string from yang file |
| 57 | * @return concatenated string after removing double quotes |
| 58 | */ |
| 59 | public static String removeQuotesAndHandleConcat(String yangStringData) { |
| 60 | |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 61 | yangStringData = yangStringData.replace("\"", EMPTY_STRING); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 62 | String[] tmpData = yangStringData.split(Pattern.quote(PLUS)); |
| 63 | StringBuilder builder = new StringBuilder(); |
| 64 | for (String yangString : tmpData) { |
| 65 | builder.append(yangString); |
| 66 | } |
| 67 | return builder.toString(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Validates identifier and returns concatenated string if string contains plus symbol. |
| 72 | * |
| 73 | * @param identifier string from yang file |
| 74 | * @param yangConstruct yang construct for creating error message |
| 75 | * @param ctx yang construct's context to get the line number and character position |
| 76 | * @return concatenated string after removing double quotes |
| 77 | */ |
| 78 | public static String getValidIdentifier(String identifier, YangConstructType yangConstruct, ParserRuleContext ctx) { |
| 79 | |
| 80 | String identifierString = removeQuotesAndHandleConcat(identifier); |
| 81 | ParserException parserException; |
| 82 | |
| 83 | if (identifierString.length() > IDENTIFIER_LENGTH) { |
| 84 | parserException = new ParserException("YANG file error : " + |
| 85 | YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is " + |
| 86 | "greater than 64 characters."); |
| 87 | } else if (!IDENTIFIER_PATTERN.matcher(identifierString).matches()) { |
| 88 | parserException = new ParserException("YANG file error : " + |
| 89 | YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is not " + |
| 90 | "valid."); |
| 91 | } else { |
| 92 | return identifierString; |
| 93 | } |
| 94 | |
| 95 | parserException.setLine(ctx.getStart().getLine()); |
| 96 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 97 | throw parserException; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Validates the revision date. |
| 102 | * |
| 103 | * @param dateToValidate input revision date |
| 104 | * @return validation result, true for success, false for failure |
| 105 | */ |
| 106 | public static boolean isDateValid(String dateToValidate) { |
| 107 | |
| 108 | if (dateToValidate == null || !dateToValidate.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}")) { |
| 109 | return false; |
| 110 | } |
| 111 | |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 112 | SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 113 | sdf.setLenient(false); |
| 114 | |
| 115 | try { |
| 116 | //if not valid, it will throw ParseException |
| 117 | sdf.parse(dateToValidate); |
| 118 | } catch (ParseException e) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return true; |
| 123 | } |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Validates YANG version. |
| 127 | * |
| 128 | * @param ctx version context object of the grammar rule |
| 129 | * @return valid version |
| 130 | */ |
| 131 | public static byte getValidVersion(GeneratedYangParser.YangVersionStatementContext ctx) { |
| 132 | |
| 133 | String value = removeQuotesAndHandleConcat(ctx.version().getText()); |
| 134 | if (!value.equals(ONE)) { |
| 135 | ParserException parserException = new ParserException("YANG file error: Input version not supported"); |
| 136 | parserException.setLine(ctx.getStart().getLine()); |
| 137 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 138 | throw parserException; |
| 139 | } |
| 140 | |
| 141 | return Byte.valueOf(value); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Validates non negative integer value. |
| 146 | * |
| 147 | * @param integerValue integer to be validated |
| 148 | * @param yangConstruct yang construct for creating error message |
| 149 | * @param ctx context object of the grammar rule |
| 150 | * @return valid non negative integer value |
| 151 | */ |
| 152 | public static int getValidNonNegativeIntegerValue(String integerValue, YangConstructType yangConstruct, |
| 153 | ParserRuleContext ctx) { |
| 154 | |
| 155 | String value = removeQuotesAndHandleConcat(integerValue); |
| 156 | if (!value.matches(NON_NEGATIVE_INTEGER_PATTERN)) { |
| 157 | ParserException parserException = new ParserException("YANG file error : " + |
| 158 | YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " + |
| 159 | "valid."); |
| 160 | parserException.setLine(ctx.getStart().getLine()); |
| 161 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 162 | throw parserException; |
| 163 | } |
| 164 | |
| 165 | return Integer.parseInt(value); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Validates boolean value. |
| 170 | * |
| 171 | * @param booleanValue value to be validated |
| 172 | * @param yangConstruct yang construct for creating error message |
| 173 | * @param ctx context object of the grammar rule |
| 174 | * @return boolean value either true or false |
| 175 | */ |
| 176 | public static boolean getValidBooleanValue(String booleanValue, YangConstructType yangConstruct, |
| 177 | ParserRuleContext ctx) { |
| 178 | |
| 179 | String value = removeQuotesAndHandleConcat(booleanValue); |
| 180 | if (value.equals(TRUE_KEYWORD)) { |
| 181 | return true; |
| 182 | } else if (value.equals(FALSE_KEYWORD)) { |
| 183 | return false; |
| 184 | } else { |
| 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 | } |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * Sets current date and makes it in usable format for revision. |
| 196 | * |
| 197 | * @return usable current date format for revision |
| 198 | */ |
| 199 | public static String setCurrentDateForRevision() { |
| 200 | |
| 201 | Calendar date = Calendar.getInstance(); |
| 202 | SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); |
| 203 | String dateForRevision = ((dateFormat.format(date.getTime())).replaceAll(SLASH, HYPHEN)).replaceAll(SPACE, |
| 204 | EMPTY_STRING); |
| 205 | return dateForRevision; |
| 206 | } |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 207 | } |