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