blob: a135891d9fedd46d8b2368f5561098b56e832f0c [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
27import static org.onosproject.yangutils.parser.ParsableDataType.REVISION_DATA;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053033
34/*
35 * Reference: RFC6020 and YANG ANTLR Grammar
36 *
37 * ABNF grammar as per RFC6020
38 * module-stmt = optsep module-keyword sep identifier-arg-str
39 * optsep
40 * "{" stmtsep
41 * module-header-stmts
42 * linkage-stmts
43 * meta-stmts
44 * revision-stmts
45 * body-stmts
46 * "}" optsep
47 *
48 * revision-stmt = revision-keyword sep revision-date optsep
49 * (";" /
50 * "{" stmtsep
51 * [description-stmt stmtsep]
52 * [reference-stmt stmtsep]
53 * "}")
54 *
55 * ANTLR grammar rule
56 * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
57 *
58 * revision_stmt : REVISION_KEYWORD DATE_ARG (STMTEND | LEFT_CURLY_BRACE revision_stmt_body RIGHT_CURLY_BRACE);
59 * revision_stmt_body : description_stmt? reference_stmt?;
60 */
61
62/**
63 * Implements listener based call back function corresponding to the "revision"
64 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
65 */
66public final class RevisionListener {
67
68 /**
69 * Creates a new revision listener.
70 */
71 private RevisionListener() {
72 }
73
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053074 /**
75 * It is called when parser receives an input matching the grammar rule
76 * (revision),perform validations and update the data model tree.
77 *
78 * @param listener Listener's object.
79 * @param ctx context object of the grammar rule.
80 */
Gaurav Agrawala04483c2016-02-13 14:23:40 +053081 public static void processRevisionEntry(TreeWalkListener listener,
82 GeneratedYangParser.RevisionStatementContext ctx) {
83
84 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053085 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053086
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053087 // Validate for reverse chronological order of revision & for revision
88 // value.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053089 if (!validateRevision(listener, ctx)) {
90 return;
91 // TODO to be implemented.
92 }
93
94 YangRevision revisionNode = new YangRevision();
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053095 revisionNode.setRevDate(ctx.DATE_ARG().getText());
Gaurav Agrawala04483c2016-02-13 14:23:40 +053096
97 listener.getParsedDataStack().push(revisionNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053098 }
99
100 /**
101 * It is called when parser exits from grammar rule (revision), it perform
102 * validations and update the data model tree.
103 *
104 * @param listener Listener's object.
105 * @param ctx context object of the grammar rule.
106 */
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530107 public static void processRevisionExit(TreeWalkListener listener, GeneratedYangParser.RevisionStatementContext
108 ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530109
110 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530111 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530112
113 Parsable tmpRevisionNode = listener.getParsedDataStack().peek();
114 if (tmpRevisionNode instanceof YangRevision) {
115 listener.getParsedDataStack().pop();
116
117 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530118 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530119 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530120
121 Parsable tmpNode = listener.getParsedDataStack().peek();
122 switch (tmpNode.getParsableDataType()) {
123 case MODULE_DATA: {
124 YangModule module = (YangModule) tmpNode;
125 module.setRevision((YangRevision) tmpRevisionNode);
126 break;
127 }
128 case SUB_MODULE_DATA: {
129 YangSubModule subModule = (YangSubModule) tmpNode;
130 subModule.setRevision((YangRevision) tmpRevisionNode);
131 break;
132 }
133 default:
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530134 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530135 ctx.DATE_ARG().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530136 EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530137 }
138 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530139 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, REVISION_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530140 ctx.DATE_ARG().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530141 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530142 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530143
144 /**
145 * Validate revision.
146 *
147 * @param listener Listener's object.
148 * @param ctx context object of the grammar rule.
149 * @return validation result
150 */
151 private static boolean validateRevision(TreeWalkListener listener,
152 GeneratedYangParser.RevisionStatementContext ctx) {
153 // TODO to be implemented
154 return true;
155 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530156}