blob: 03f1cb1c3e42a25e0f321fe8328d56ea06625c12 [file] [log] [blame]
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +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.listeners;
18
Vinod Kumar Sc4216002016-03-03 19:55:30 +053019import java.text.ParseException;
20import java.text.SimpleDateFormat;
21import java.util.Date;
22
Gaurav Agrawala04483c2016-02-13 14:23:40 +053023import org.onosproject.yangutils.datamodel.YangImport;
24import org.onosproject.yangutils.datamodel.YangInclude;
25import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053026import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053027import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053028import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053029
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053030import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053035import static org.onosproject.yangutils.utils.YangConstructType.REVISION_DATE_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053036
37/*
38 * Reference: RFC6020 and YANG ANTLR Grammar
39 *
40 * ABNF grammar as per RFC6020
41 * import-stmt = import-keyword sep identifier-arg-str optsep
42 * "{" stmtsep
43 * prefix-stmt stmtsep
44 * [revision-date-stmt stmtsep]
45 * "}"
46 * include-stmt = include-keyword sep identifier-arg-str optsep
47 * (";" /
48 * "{" stmtsep
49 * [revision-date-stmt stmtsep]
50 * "}")
51 * revision-date-stmt = revision-date-keyword sep revision-date stmtend
52 *
53 * ANTLR grammar rule
54 * import_stmt : IMPORT_KEYWORD IDENTIFIER LEFT_CURLY_BRACE import_stmt_body
55 * RIGHT_CURLY_BRACE;
56 * import_stmt_body : prefix_stmt revision_date_stmt?;
57 *
58 * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE
59 * revision_date_stmt_body? RIGHT_CURLY_BRACE);
60 *
61 * revision_date_stmt : REVISION_DATE_KEYWORD DATE_ARG STMTEND;
62 *
63 */
64
65/**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053066 * Implements listener based call back function corresponding to the
67 * "revision date" rule defined in ANTLR grammar file for corresponding ABNF
68 * rule in RFC 6020.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053069 */
70public final class RevisionDateListener {
71
72 /**
73 * Creates a new revision date listener.
74 */
75 private RevisionDateListener() {
76 }
77
78 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053079 * It is called when parser receives an input matching the grammar rule
80 * (revision date),perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053081 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053082 * @param listener Listener's object
83 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053084 */
85 public static void processRevisionDateEntry(TreeWalkListener listener,
Vinod Kumar Sc4216002016-03-03 19:55:30 +053086 GeneratedYangParser.RevisionDateStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053087
88 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053089 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, ctx.DATE_ARG().getText(),
Vinod Kumar Sc4216002016-03-03 19:55:30 +053090 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053091
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053092 if (!isDateValid(ctx.DATE_ARG().getText())) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053093 ParserException parserException = new ParserException("YANG file error: Input date is not correct");
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053094 parserException.setLine(ctx.DATE_ARG().getSymbol().getLine());
95 parserException.setCharPosition(ctx.DATE_ARG().getSymbol().getCharPositionInLine());
96 throw parserException;
97 }
98
Gaurav Agrawala04483c2016-02-13 14:23:40 +053099 // Obtain the node of the stack.
100 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530101 switch (tmpNode.getYangConstructType()) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530102 case IMPORT_DATA: {
103 YangImport importNode = (YangImport) tmpNode;
104 importNode.setRevision(ctx.DATE_ARG().getText());
105 break;
106 }
107 case INCLUDE_DATA: {
108 YangInclude includeNode = (YangInclude) tmpNode;
109 includeNode.setRevision(ctx.DATE_ARG().getText());
110 break;
111 }
112 default:
113 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATE_DATA,
114 ctx.DATE_ARG().getText(), ENTRY));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530115 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530116 }
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530117
118 /**
119 * Validates the revision date.
120 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530121 * @param dateToValidate input revision date
122 * @return validation result, true for success, false for failure
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530123 */
124 private static boolean isDateValid(String dateToValidate) {
125
126 if (dateToValidate == null) {
127 return false;
128 }
129
130 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
131 sdf.setLenient(false);
132
133 try {
134 //if not valid, it will throw ParseException
135 Date date = sdf.parse(dateToValidate);
136 System.out.println(date);
137 } catch (ParseException e) {
138 return false;
139 }
140 return true;
141 }
142}