blob: 39a9bae5ea1ac08d21f03e53940aa16c54db03b2 [file] [log] [blame]
Vidyashree Rama468f8282016-03-04 19:08:35 +05301/*
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
17package org.onosproject.yangutils.parser.impl.parserutils;
18
19import org.antlr.v4.runtime.ParserRuleContext;
20
21import java.text.ParseException;
22import java.text.SimpleDateFormat;
janani bcc9ac302016-03-24 12:43:48 +053023import java.util.Calendar;
Vidyashree Rama468f8282016-03-04 19:08:35 +053024import java.util.regex.Pattern;
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053025
26import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama468f8282016-03-04 19:08:35 +053027import org.onosproject.yangutils.utils.YangConstructType;
28import org.onosproject.yangutils.parser.exceptions.ParserException;
29
30/**
31 * It's a utility for listener.
32 */
33public final class ListenerUtil {
34 private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*");
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053035 private static final String NON_NEGATIVE_INTEGER_PATTERN = "[0-9]+";
Vidyashree Rama468f8282016-03-04 19:08:35 +053036 private static final String PLUS = "+";
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053037 private static final String ONE = "1";
38 private static final String TRUE_KEYWORD = "true";
39 private static final String FALSE_KEYWORD = "false";
Vidyashree Rama468f8282016-03-04 19:08:35 +053040 private static final int IDENTIFIER_LENGTH = 64;
janani bcc9ac302016-03-24 12:43:48 +053041 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 Rama468f8282016-03-04 19:08:35 +053046
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 bcc9ac302016-03-24 12:43:48 +053061 yangStringData = yangStringData.replace("\"", EMPTY_STRING);
Vidyashree Rama468f8282016-03-04 19:08:35 +053062 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 bcc9ac302016-03-24 12:43:48 +0530112 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530113 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 Rama8a6b1282016-03-15 10:18:25 +0530124
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 bcc9ac302016-03-24 12:43:48 +0530193
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 Rama468f8282016-03-04 19:08:35 +0530207}