blob: ffb50e950715e96d8837a6c319d025a3f47484dd [file] [log] [blame]
Vidyashree Rama468f8282016-03-04 19:08:35 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama468f8282016-03-04 19:08:35 +05303 *
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.parser.exceptions.ParserException;
Bharat saraswald9822e92016-04-05 15:13:44 +053030import org.onosproject.yangutils.utils.YangConstructType;
Vidyashree Rama468f8282016-03-04 19:08:35 +053031
32/**
Bharat saraswald9822e92016-04-05 15:13:44 +053033 * Represents an utility for listener.
Vidyashree Rama468f8282016-03-04 19:08:35 +053034 */
35public final class ListenerUtil {
Bharat saraswald9822e92016-04-05 15:13:44 +053036
Vidyashree Rama468f8282016-03-04 19:08:35 +053037 private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*");
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053038 private static final String NON_NEGATIVE_INTEGER_PATTERN = "[0-9]+";
Vidyashree Rama468f8282016-03-04 19:08:35 +053039 private static final String PLUS = "+";
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053040 private static final String ONE = "1";
41 private static final String TRUE_KEYWORD = "true";
42 private static final String FALSE_KEYWORD = "false";
Vidyashree Rama468f8282016-03-04 19:08:35 +053043 private static final int IDENTIFIER_LENGTH = 64;
janani bcc9ac302016-03-24 12:43:48 +053044 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 Agrawalbd804472016-03-25 11:25:36 +053049 private static final String COLON = ":";
Vidyashree Rama25bf4d02016-03-29 14:37:02 +053050 private static final String CARET = "^";
Vidyashree Rama468f8282016-03-04 19:08:35 +053051
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 bcc9ac302016-03-24 12:43:48 +053066 yangStringData = yangStringData.replace("\"", EMPTY_STRING);
Vidyashree Rama468f8282016-03-04 19:08:35 +053067 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 bcc9ac302016-03-24 12:43:48 +0530117 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530118 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 Rama8a6b1282016-03-15 10:18:25 +0530129
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 saraswald9822e92016-04-05 15:13:44 +0530158 ParserRuleContext ctx) {
Vidyashree Rama8a6b1282016-03-15 10:18:25 +0530159
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 saraswald9822e92016-04-05 15:13:44 +0530182 ParserRuleContext ctx) {
Vidyashree Rama8a6b1282016-03-15 10:18:25 +0530183
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 bcc9ac302016-03-24 12:43:48 +0530198
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 Agrawalbd804472016-03-25 11:25:36 +0530212
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 Rama25bf4d02016-03-29 14:37:02 +0530243
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 Rama468f8282016-03-04 19:08:35 +0530275}