blob: 8c3f36fcdb18ae5036768aa69be1b6f67b12be45 [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
Gaurav Agrawalbd804472016-03-25 11:25:36 +053026import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053027import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama468f8282016-03-04 19:08:35 +053028import org.onosproject.yangutils.utils.YangConstructType;
29import org.onosproject.yangutils.parser.exceptions.ParserException;
30
31/**
32 * It's a utility for listener.
33 */
34public final class ListenerUtil {
35 private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*");
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053036 private static final String NON_NEGATIVE_INTEGER_PATTERN = "[0-9]+";
Vidyashree Rama468f8282016-03-04 19:08:35 +053037 private static final String PLUS = "+";
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053038 private static final String ONE = "1";
39 private static final String TRUE_KEYWORD = "true";
40 private static final String FALSE_KEYWORD = "false";
Vidyashree Rama468f8282016-03-04 19:08:35 +053041 private static final int IDENTIFIER_LENGTH = 64;
janani bcc9ac302016-03-24 12:43:48 +053042 private static final String DATE_FORMAT = "yyyy-MM-dd";
43 private static final String EMPTY_STRING = "";
44 private static final String HYPHEN = "-";
45 private static final String SLASH = "/";
46 private static final String SPACE = " ";
Gaurav Agrawalbd804472016-03-25 11:25:36 +053047 private static final String COLON = ":";
Vidyashree Rama468f8282016-03-04 19:08:35 +053048
49 /**
50 * Creates a new listener util.
51 */
52 private ListenerUtil() {
53 }
54
55 /**
56 * Removes doubles quotes and concatenates if string has plus symbol.
57 *
58 * @param yangStringData string from yang file
59 * @return concatenated string after removing double quotes
60 */
61 public static String removeQuotesAndHandleConcat(String yangStringData) {
62
janani bcc9ac302016-03-24 12:43:48 +053063 yangStringData = yangStringData.replace("\"", EMPTY_STRING);
Vidyashree Rama468f8282016-03-04 19:08:35 +053064 String[] tmpData = yangStringData.split(Pattern.quote(PLUS));
65 StringBuilder builder = new StringBuilder();
66 for (String yangString : tmpData) {
67 builder.append(yangString);
68 }
69 return builder.toString();
70 }
71
72 /**
73 * Validates identifier and returns concatenated string if string contains plus symbol.
74 *
75 * @param identifier string from yang file
76 * @param yangConstruct yang construct for creating error message
77 * @param ctx yang construct's context to get the line number and character position
78 * @return concatenated string after removing double quotes
79 */
80 public static String getValidIdentifier(String identifier, YangConstructType yangConstruct, ParserRuleContext ctx) {
81
82 String identifierString = removeQuotesAndHandleConcat(identifier);
83 ParserException parserException;
84
85 if (identifierString.length() > IDENTIFIER_LENGTH) {
86 parserException = new ParserException("YANG file error : " +
87 YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is " +
88 "greater than 64 characters.");
89 } else if (!IDENTIFIER_PATTERN.matcher(identifierString).matches()) {
90 parserException = new ParserException("YANG file error : " +
91 YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is not " +
92 "valid.");
93 } else {
94 return identifierString;
95 }
96
97 parserException.setLine(ctx.getStart().getLine());
98 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
99 throw parserException;
100 }
101
102 /**
103 * Validates the revision date.
104 *
105 * @param dateToValidate input revision date
106 * @return validation result, true for success, false for failure
107 */
108 public static boolean isDateValid(String dateToValidate) {
109
110 if (dateToValidate == null || !dateToValidate.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}")) {
111 return false;
112 }
113
janani bcc9ac302016-03-24 12:43:48 +0530114 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530115 sdf.setLenient(false);
116
117 try {
118 //if not valid, it will throw ParseException
119 sdf.parse(dateToValidate);
120 } catch (ParseException e) {
121 return false;
122 }
123
124 return true;
125 }
Vidyashree Rama8a6b1282016-03-15 10:18:25 +0530126
127 /**
128 * Validates YANG version.
129 *
130 * @param ctx version context object of the grammar rule
131 * @return valid version
132 */
133 public static byte getValidVersion(GeneratedYangParser.YangVersionStatementContext ctx) {
134
135 String value = removeQuotesAndHandleConcat(ctx.version().getText());
136 if (!value.equals(ONE)) {
137 ParserException parserException = new ParserException("YANG file error: Input version not supported");
138 parserException.setLine(ctx.getStart().getLine());
139 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
140 throw parserException;
141 }
142
143 return Byte.valueOf(value);
144 }
145
146 /**
147 * Validates non negative integer value.
148 *
149 * @param integerValue integer to be validated
150 * @param yangConstruct yang construct for creating error message
151 * @param ctx context object of the grammar rule
152 * @return valid non negative integer value
153 */
154 public static int getValidNonNegativeIntegerValue(String integerValue, YangConstructType yangConstruct,
155 ParserRuleContext ctx) {
156
157 String value = removeQuotesAndHandleConcat(integerValue);
158 if (!value.matches(NON_NEGATIVE_INTEGER_PATTERN)) {
159 ParserException parserException = new ParserException("YANG file error : " +
160 YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " +
161 "valid.");
162 parserException.setLine(ctx.getStart().getLine());
163 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
164 throw parserException;
165 }
166
167 return Integer.parseInt(value);
168 }
169
170 /**
171 * Validates boolean value.
172 *
173 * @param booleanValue value to be validated
174 * @param yangConstruct yang construct for creating error message
175 * @param ctx context object of the grammar rule
176 * @return boolean value either true or false
177 */
178 public static boolean getValidBooleanValue(String booleanValue, YangConstructType yangConstruct,
179 ParserRuleContext ctx) {
180
181 String value = removeQuotesAndHandleConcat(booleanValue);
182 if (value.equals(TRUE_KEYWORD)) {
183 return true;
184 } else if (value.equals(FALSE_KEYWORD)) {
185 return false;
186 } else {
187 ParserException parserException = new ParserException("YANG file error : " +
188 YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " +
189 "valid.");
190 parserException.setLine(ctx.getStart().getLine());
191 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
192 throw parserException;
193 }
194 }
janani bcc9ac302016-03-24 12:43:48 +0530195
196 /**
197 * Sets current date and makes it in usable format for revision.
198 *
199 * @return usable current date format for revision
200 */
201 public static String setCurrentDateForRevision() {
202
203 Calendar date = Calendar.getInstance();
204 SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
205 String dateForRevision = ((dateFormat.format(date.getTime())).replaceAll(SLASH, HYPHEN)).replaceAll(SPACE,
206 EMPTY_STRING);
207 return dateForRevision;
208 }
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530209
210 /**
211 * Checks and return valid node identifier.
212 *
213 * @param nodeIdentifierString string from yang file
214 * @param yangConstruct yang construct for creating error message
215 * @param ctx yang construct's context to get the line number and character position
216 * @return valid node identifier
217 */
218 public static YangNodeIdentifier getValidNodeIdentifier(String nodeIdentifierString, YangConstructType
219 yangConstruct, ParserRuleContext ctx) {
220 String tmpIdentifierString = removeQuotesAndHandleConcat(nodeIdentifierString);
221 String[] tmpData = tmpIdentifierString.split(Pattern.quote(COLON));
222 if (tmpData.length == 1) {
223 YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier();
224 nodeIdentifier.setName(getValidIdentifier(tmpData[0], yangConstruct, ctx));
225 return nodeIdentifier;
226 } else if (tmpData.length == 2) {
227 YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier();
228 nodeIdentifier.setPrefix(getValidIdentifier(tmpData[0], yangConstruct, ctx));
229 nodeIdentifier.setName(getValidIdentifier(tmpData[1], yangConstruct, ctx));
230 return nodeIdentifier;
231 } else {
232 ParserException parserException = new ParserException("YANG file error : " +
233 YangConstructType.getYangConstructType(yangConstruct) + " name " + nodeIdentifierString +
234 " is not valid.");
235 parserException.setLine(ctx.getStart().getLine());
236 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
237 throw parserException;
238 }
239 }
Vidyashree Rama468f8282016-03-04 19:08:35 +0530240}