blob: 32675cdd1a6ead1c5ad6ff54a8a692b17a1f5b3f [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
Gaurav Agrawala04483c2016-02-13 14:23:40 +053019import org.onosproject.yangutils.datamodel.YangModule;
20import org.onosproject.yangutils.datamodel.YangRevision;
21import org.onosproject.yangutils.datamodel.YangSubModule;
22import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053026
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053027import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
30import static org.onosproject.yangutils.utils.YangConstructType.REVISION_DATA;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053035
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053036import java.text.ParseException;
37import java.text.SimpleDateFormat;
38import java.util.Date;
39
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053040/*
41 * Reference: RFC6020 and YANG ANTLR Grammar
42 *
43 * ABNF grammar as per RFC6020
44 * module-stmt = optsep module-keyword sep identifier-arg-str
45 * optsep
46 * "{" stmtsep
47 * module-header-stmts
48 * linkage-stmts
49 * meta-stmts
50 * revision-stmts
51 * body-stmts
52 * "}" optsep
53 *
54 * revision-stmt = revision-keyword sep revision-date optsep
55 * (";" /
56 * "{" stmtsep
57 * [description-stmt stmtsep]
58 * [reference-stmt stmtsep]
59 * "}")
60 *
61 * ANTLR grammar rule
62 * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
63 *
64 * revision_stmt : REVISION_KEYWORD DATE_ARG (STMTEND | LEFT_CURLY_BRACE revision_stmt_body RIGHT_CURLY_BRACE);
65 * revision_stmt_body : description_stmt? reference_stmt?;
66 */
67
68/**
69 * Implements listener based call back function corresponding to the "revision"
70 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
71 */
72public final class RevisionListener {
73
74 /**
75 * Creates a new revision listener.
76 */
77 private RevisionListener() {
78 }
79
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053080 /**
81 * It is called when parser receives an input matching the grammar rule
82 * (revision),perform validations and update the data model tree.
83 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053084 * @param listener Listener's object
85 * @param ctx context object of the grammar rule
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053086 */
Gaurav Agrawala04483c2016-02-13 14:23:40 +053087 public static void processRevisionEntry(TreeWalkListener listener,
88 GeneratedYangParser.RevisionStatementContext ctx) {
89
90 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053091 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053092
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053093 // Validate for reverse chronological order of revision & for revision
94 // value.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053095 if (!validateRevision(listener, ctx)) {
96 return;
97 // TODO to be implemented.
98 }
99
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530100 if (!isDateValid(ctx.DATE_ARG().getText())) {
101 ParserException parserException = new ParserException("YANG file error: Input date is not correct");
102 parserException.setLine(ctx.DATE_ARG().getSymbol().getLine());
103 parserException.setCharPosition(ctx.DATE_ARG().getSymbol().getCharPositionInLine());
104 throw parserException;
105 }
106
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530107 YangRevision revisionNode = new YangRevision();
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530108 revisionNode.setRevDate(ctx.DATE_ARG().getText());
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530109
110 listener.getParsedDataStack().push(revisionNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530111 }
112
113 /**
114 * It is called when parser exits from grammar rule (revision), it perform
115 * validations and update the data model tree.
116 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530117 * @param listener Listener's object
118 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530119 */
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530120 public static void processRevisionExit(TreeWalkListener listener, GeneratedYangParser.RevisionStatementContext
121 ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530122
123 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530124 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530125
126 Parsable tmpRevisionNode = listener.getParsedDataStack().peek();
127 if (tmpRevisionNode instanceof YangRevision) {
128 listener.getParsedDataStack().pop();
129
130 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530131 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530132 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530133
134 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530135 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530136 case MODULE_DATA: {
137 YangModule module = (YangModule) tmpNode;
138 module.setRevision((YangRevision) tmpRevisionNode);
139 break;
140 }
141 case SUB_MODULE_DATA: {
142 YangSubModule subModule = (YangSubModule) tmpNode;
143 subModule.setRevision((YangRevision) tmpRevisionNode);
144 break;
145 }
146 default:
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530147 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530148 ctx.DATE_ARG().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530149 EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530150 }
151 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530152 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, REVISION_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530153 ctx.DATE_ARG().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530154 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530155 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530156
157 /**
158 * Validate revision.
159 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530160 * @param listener Listener's object
161 * @param ctx context object of the grammar rule
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530162 * @return validation result
163 */
164 private static boolean validateRevision(TreeWalkListener listener,
165 GeneratedYangParser.RevisionStatementContext ctx) {
166 // TODO to be implemented
167 return true;
168 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530169
170 /**
171 * Validates the revision date.
172 *
173 * @param dateToValidate input revision date
174 * @return validation result, true for success, false for failure
175 */
176 private static boolean isDateValid(String dateToValidate) {
177
178 if (dateToValidate == null) {
179 return false;
180 }
181
182 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
183 sdf.setLenient(false);
184
185 try {
186 //if not valid, it will throw ParseException
187 Date date = sdf.parse(dateToValidate);
188 System.out.println(date);
189 } catch (ParseException e) {
190 return false;
191 }
192 return true;
193 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530194}