blob: 6573a43a23c9e13e613b5bfb14ed158a06b14cea [file] [log] [blame]
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +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.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 Agrawal02b05d22016-02-19 12:57:13 +053027import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Bharat saraswald9822e92016-04-05 15:13:44 +053030import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.isDateValid;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswald9822e92016-04-05 15:13:44 +053036import static org.onosproject.yangutils.utils.YangConstructType.REVISION_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053037
38/*
39 * Reference: RFC6020 and YANG ANTLR Grammar
40 *
41 * ABNF grammar as per RFC6020
42 * module-stmt = optsep module-keyword sep identifier-arg-str
43 * optsep
44 * "{" stmtsep
45 * module-header-stmts
46 * linkage-stmts
47 * meta-stmts
48 * revision-stmts
49 * body-stmts
50 * "}" optsep
51 *
52 * revision-stmt = revision-keyword sep revision-date optsep
53 * (";" /
54 * "{" stmtsep
55 * [description-stmt stmtsep]
56 * [reference-stmt stmtsep]
57 * "}")
58 *
59 * ANTLR grammar rule
60 * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
61 *
62 * revision_stmt : REVISION_KEYWORD DATE_ARG (STMTEND | LEFT_CURLY_BRACE revision_stmt_body RIGHT_CURLY_BRACE);
63 * revision_stmt_body : description_stmt? reference_stmt?;
64 */
65
66/**
Bharat saraswald9822e92016-04-05 15:13:44 +053067 * Represents listener based call back function corresponding to the "revision"
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053068 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
69 */
70public final class RevisionListener {
71
72 /**
73 * Creates a new revision listener.
74 */
75 private RevisionListener() {
76 }
77
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053078 /**
79 * It is called when parser receives an input matching the grammar rule
80 * (revision),perform validations and update the data model tree.
81 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053082 * @param listener Listener's object
83 * @param ctx context object of the grammar rule
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053084 */
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085 public static void processRevisionEntry(TreeWalkListener listener,
86 GeneratedYangParser.RevisionStatementContext ctx) {
87
88 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053089 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(), ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053090
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053091 // Validate for reverse chronological order of revision & for revision
92 // value.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053093 if (!validateRevision(listener, ctx)) {
94 return;
95 // TODO to be implemented.
96 }
97
Vidyashree Rama468f8282016-03-04 19:08:35 +053098 String date = removeQuotesAndHandleConcat(ctx.dateArgumentString().getText());
99 if (!isDateValid(date)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530100 ParserException parserException = new ParserException("YANG file error: Input date is not correct");
Vidyashree Rama468f8282016-03-04 19:08:35 +0530101 parserException.setLine(ctx.getStart().getLine());
102 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530103 throw parserException;
104 }
105
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530106 YangRevision revisionNode = new YangRevision();
Vidyashree Rama468f8282016-03-04 19:08:35 +0530107 revisionNode.setRevDate(date);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530108
109 listener.getParsedDataStack().push(revisionNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530110 }
111
112 /**
113 * It is called when parser exits from grammar rule (revision), it perform
114 * validations and update the data model tree.
115 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530116 * @param listener Listener's object
117 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530118 */
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530119 public static void processRevisionExit(TreeWalkListener listener, GeneratedYangParser.RevisionStatementContext
120 ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530121
122 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530123 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530124
125 Parsable tmpRevisionNode = listener.getParsedDataStack().peek();
126 if (tmpRevisionNode instanceof YangRevision) {
127 listener.getParsedDataStack().pop();
128
129 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530130 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530131 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530132
133 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530134 switch (tmpNode.getYangConstructType()) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530135 case MODULE_DATA: {
136 YangModule module = (YangModule) tmpNode;
137 module.setRevision((YangRevision) tmpRevisionNode);
138 break;
139 }
140 case SUB_MODULE_DATA: {
141 YangSubModule subModule = (YangSubModule) tmpNode;
142 subModule.setRevision((YangRevision) tmpRevisionNode);
143 break;
144 }
145 default:
146 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATA,
147 ctx.dateArgumentString().getText(),
148 EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530149 }
150 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530151 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, REVISION_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530152 ctx.dateArgumentString().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530153 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530154 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530155
156 /**
157 * Validate revision.
158 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530159 * @param listener Listener's object
160 * @param ctx context object of the grammar rule
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530161 * @return validation result
162 */
163 private static boolean validateRevision(TreeWalkListener listener,
164 GeneratedYangParser.RevisionStatementContext ctx) {
165 // TODO to be implemented
166 return true;
167 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530168}