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 | |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 19 | import java.text.ParseException; |
| 20 | import java.text.SimpleDateFormat; |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 21 | import java.util.Calendar; |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 22 | import java.util.LinkedList; |
| 23 | import java.util.List; |
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 | |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 26 | import org.antlr.v4.runtime.ParserRuleContext; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.utils.YangConstructType; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 31 | |
| 32 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 33 | * Represents an utility for listener. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 34 | */ |
| 35 | public final class ListenerUtil { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 36 | |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 37 | 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] | 38 | private static final String NON_NEGATIVE_INTEGER_PATTERN = "[0-9]+"; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 39 | private static final String PLUS = "+"; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 40 | private static final String ONE = "1"; |
| 41 | private static final String TRUE_KEYWORD = "true"; |
| 42 | private static final String FALSE_KEYWORD = "false"; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 43 | private static final int IDENTIFIER_LENGTH = 64; |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 44 | private static final String DATE_FORMAT = "yyyy-MM-dd"; |
| 45 | private static final String EMPTY_STRING = ""; |
| 46 | private static final String HYPHEN = "-"; |
| 47 | private static final String SLASH = "/"; |
| 48 | private static final String SPACE = " "; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 49 | private static final String COLON = ":"; |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 50 | private static final String CARET = "^"; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Creates a new listener util. |
| 54 | */ |
| 55 | private ListenerUtil() { |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Removes doubles quotes and concatenates if string has plus symbol. |
| 60 | * |
| 61 | * @param yangStringData string from yang file |
| 62 | * @return concatenated string after removing double quotes |
| 63 | */ |
| 64 | public static String removeQuotesAndHandleConcat(String yangStringData) { |
| 65 | |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 66 | yangStringData = yangStringData.replace("\"", EMPTY_STRING); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 67 | String[] tmpData = yangStringData.split(Pattern.quote(PLUS)); |
| 68 | StringBuilder builder = new StringBuilder(); |
| 69 | for (String yangString : tmpData) { |
| 70 | builder.append(yangString); |
| 71 | } |
| 72 | return builder.toString(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Validates identifier and returns concatenated string if string contains plus symbol. |
| 77 | * |
| 78 | * @param identifier string from yang file |
| 79 | * @param yangConstruct yang construct for creating error message |
| 80 | * @param ctx yang construct's context to get the line number and character position |
| 81 | * @return concatenated string after removing double quotes |
| 82 | */ |
| 83 | public static String getValidIdentifier(String identifier, YangConstructType yangConstruct, ParserRuleContext ctx) { |
| 84 | |
| 85 | String identifierString = removeQuotesAndHandleConcat(identifier); |
| 86 | ParserException parserException; |
| 87 | |
| 88 | if (identifierString.length() > IDENTIFIER_LENGTH) { |
| 89 | parserException = new ParserException("YANG file error : " + |
| 90 | YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is " + |
| 91 | "greater than 64 characters."); |
| 92 | } else if (!IDENTIFIER_PATTERN.matcher(identifierString).matches()) { |
| 93 | parserException = new ParserException("YANG file error : " + |
| 94 | YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is not " + |
| 95 | "valid."); |
| 96 | } else { |
| 97 | return identifierString; |
| 98 | } |
| 99 | |
| 100 | parserException.setLine(ctx.getStart().getLine()); |
| 101 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 102 | throw parserException; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Validates the revision date. |
| 107 | * |
| 108 | * @param dateToValidate input revision date |
| 109 | * @return validation result, true for success, false for failure |
| 110 | */ |
| 111 | public static boolean isDateValid(String dateToValidate) { |
| 112 | |
| 113 | if (dateToValidate == null || !dateToValidate.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}")) { |
| 114 | return false; |
| 115 | } |
| 116 | |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 117 | SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 118 | sdf.setLenient(false); |
| 119 | |
| 120 | try { |
| 121 | //if not valid, it will throw ParseException |
| 122 | sdf.parse(dateToValidate); |
| 123 | } catch (ParseException e) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Validates YANG version. |
| 132 | * |
| 133 | * @param ctx version context object of the grammar rule |
| 134 | * @return valid version |
| 135 | */ |
| 136 | public static byte getValidVersion(GeneratedYangParser.YangVersionStatementContext ctx) { |
| 137 | |
| 138 | String value = removeQuotesAndHandleConcat(ctx.version().getText()); |
| 139 | if (!value.equals(ONE)) { |
| 140 | ParserException parserException = new ParserException("YANG file error: Input version not supported"); |
| 141 | parserException.setLine(ctx.getStart().getLine()); |
| 142 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 143 | throw parserException; |
| 144 | } |
| 145 | |
| 146 | return Byte.valueOf(value); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Validates non negative integer value. |
| 151 | * |
| 152 | * @param integerValue integer to be validated |
| 153 | * @param yangConstruct yang construct for creating error message |
| 154 | * @param ctx context object of the grammar rule |
| 155 | * @return valid non negative integer value |
| 156 | */ |
| 157 | public static int getValidNonNegativeIntegerValue(String integerValue, YangConstructType yangConstruct, |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 158 | ParserRuleContext ctx) { |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 159 | |
| 160 | String value = removeQuotesAndHandleConcat(integerValue); |
| 161 | if (!value.matches(NON_NEGATIVE_INTEGER_PATTERN)) { |
| 162 | ParserException parserException = new ParserException("YANG file error : " + |
| 163 | YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " + |
| 164 | "valid."); |
| 165 | parserException.setLine(ctx.getStart().getLine()); |
| 166 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 167 | throw parserException; |
| 168 | } |
| 169 | |
| 170 | return Integer.parseInt(value); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Validates boolean value. |
| 175 | * |
| 176 | * @param booleanValue value to be validated |
| 177 | * @param yangConstruct yang construct for creating error message |
| 178 | * @param ctx context object of the grammar rule |
| 179 | * @return boolean value either true or false |
| 180 | */ |
| 181 | public static boolean getValidBooleanValue(String booleanValue, YangConstructType yangConstruct, |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 182 | ParserRuleContext ctx) { |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 183 | |
| 184 | String value = removeQuotesAndHandleConcat(booleanValue); |
| 185 | if (value.equals(TRUE_KEYWORD)) { |
| 186 | return true; |
| 187 | } else if (value.equals(FALSE_KEYWORD)) { |
| 188 | return false; |
| 189 | } else { |
| 190 | ParserException parserException = new ParserException("YANG file error : " + |
| 191 | YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " + |
| 192 | "valid."); |
| 193 | parserException.setLine(ctx.getStart().getLine()); |
| 194 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 195 | throw parserException; |
| 196 | } |
| 197 | } |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Sets current date and makes it in usable format for revision. |
| 201 | * |
| 202 | * @return usable current date format for revision |
| 203 | */ |
| 204 | public static String setCurrentDateForRevision() { |
| 205 | |
| 206 | Calendar date = Calendar.getInstance(); |
| 207 | SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); |
| 208 | String dateForRevision = ((dateFormat.format(date.getTime())).replaceAll(SLASH, HYPHEN)).replaceAll(SPACE, |
| 209 | EMPTY_STRING); |
| 210 | return dateForRevision; |
| 211 | } |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * Checks and return valid node identifier. |
| 215 | * |
| 216 | * @param nodeIdentifierString string from yang file |
| 217 | * @param yangConstruct yang construct for creating error message |
| 218 | * @param ctx yang construct's context to get the line number and character position |
| 219 | * @return valid node identifier |
| 220 | */ |
| 221 | public static YangNodeIdentifier getValidNodeIdentifier(String nodeIdentifierString, YangConstructType |
| 222 | yangConstruct, ParserRuleContext ctx) { |
| 223 | String tmpIdentifierString = removeQuotesAndHandleConcat(nodeIdentifierString); |
| 224 | String[] tmpData = tmpIdentifierString.split(Pattern.quote(COLON)); |
| 225 | if (tmpData.length == 1) { |
| 226 | YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); |
| 227 | nodeIdentifier.setName(getValidIdentifier(tmpData[0], yangConstruct, ctx)); |
| 228 | return nodeIdentifier; |
| 229 | } else if (tmpData.length == 2) { |
| 230 | YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); |
| 231 | nodeIdentifier.setPrefix(getValidIdentifier(tmpData[0], yangConstruct, ctx)); |
| 232 | nodeIdentifier.setName(getValidIdentifier(tmpData[1], yangConstruct, ctx)); |
| 233 | return nodeIdentifier; |
| 234 | } else { |
| 235 | ParserException parserException = new ParserException("YANG file error : " + |
| 236 | YangConstructType.getYangConstructType(yangConstruct) + " name " + nodeIdentifierString + |
| 237 | " is not valid."); |
| 238 | parserException.setLine(ctx.getStart().getLine()); |
| 239 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 240 | throw parserException; |
| 241 | } |
| 242 | } |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 243 | |
| 244 | /** |
| 245 | * Checks and return valid absolute schema node id. |
| 246 | * |
| 247 | * @param argumentString string from yang file |
| 248 | * @param yangConstructType yang construct for creating error message |
| 249 | * @param ctx yang construct's context to get the line number and character position |
| 250 | * @return target nodes list of absolute schema node id |
| 251 | */ |
| 252 | public static List<YangNodeIdentifier> getValidAbsoluteSchemaNodeId(String argumentString, |
| 253 | YangConstructType yangConstructType, ParserRuleContext ctx) { |
| 254 | |
| 255 | List<YangNodeIdentifier> targetNodes = new LinkedList<>(); |
| 256 | YangNodeIdentifier yangNodeIdentifier; |
| 257 | String tmpSchemaNodeId = removeQuotesAndHandleConcat(argumentString); |
| 258 | |
| 259 | // absolute-schema-nodeid = 1*("/" node-identifier) |
| 260 | if (!tmpSchemaNodeId.startsWith(SLASH)) { |
| 261 | ParserException parserException = new ParserException("YANG file error : " + |
| 262 | YangConstructType.getYangConstructType(yangConstructType) + " name " + argumentString + |
| 263 | "is not valid"); |
| 264 | parserException.setLine(ctx.getStart().getLine()); |
| 265 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 266 | throw parserException; |
| 267 | } |
| 268 | String[] tmpData = tmpSchemaNodeId.replaceFirst(CARET + SLASH, EMPTY_STRING).split(SLASH); |
| 269 | for (String nodeIdentifiers : tmpData) { |
| 270 | yangNodeIdentifier = getValidNodeIdentifier(nodeIdentifiers, yangConstructType, ctx); |
| 271 | targetNodes.add(yangNodeIdentifier); |
| 272 | } |
| 273 | return targetNodes; |
| 274 | } |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 275 | } |