blob: 0da2f017245bc77b442bac0e28f535910a8931a3 [file] [log] [blame]
Gaurav Agrawal22db16d2016-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 Agrawal2737d2a2016-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;
23import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053027import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
30import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053031
32/*
33 * Reference: RFC6020 and YANG ANTLR Grammar
34 *
35 * ABNF grammar as per RFC6020
36 * module-stmt = optsep module-keyword sep identifier-arg-str
37 * optsep
38 * "{" stmtsep
39 * module-header-stmts
40 * linkage-stmts
41 * meta-stmts
42 * revision-stmts
43 * body-stmts
44 * "}" optsep
45 *
46 * revision-stmt = revision-keyword sep revision-date optsep
47 * (";" /
48 * "{" stmtsep
49 * [description-stmt stmtsep]
50 * [reference-stmt stmtsep]
51 * "}")
52 *
53 * ANTLR grammar rule
54 * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
55 *
56 * revision_stmt : REVISION_KEYWORD DATE_ARG (STMTEND | LEFT_CURLY_BRACE revision_stmt_body RIGHT_CURLY_BRACE);
57 * revision_stmt_body : description_stmt? reference_stmt?;
58 */
59
60/**
61 * Implements listener based call back function corresponding to the "revision"
62 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
63 */
64public final class RevisionListener {
65
66 /**
67 * Creates a new revision listener.
68 */
69 private RevisionListener() {
70 }
71
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053072 public static void processRevisionEntry(TreeWalkListener listener,
73 GeneratedYangParser.RevisionStatementContext ctx) {
74
75 // Check for stack to be non empty.
76 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
77 ParsableDataType.REVISION_DATA,
78 String.valueOf(ctx.DATE_ARG().getText()),
79 ListenerErrorLocation.ENTRY);
80
81 // Validate for reverse chronological order of revision & for revision value.
82 if (!validateRevision(listener, ctx)) {
83 return;
84 // TODO to be implemented.
85 }
86
87 YangRevision revisionNode = new YangRevision();
88 revisionNode.setRevDate(String.valueOf(ctx.DATE_ARG().getText()));
89
90 listener.getParsedDataStack().push(revisionNode);
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053091 }
92
93 /**
94 * It is called when parser exits from grammar rule (revision), it perform
95 * validations and update the data model tree.
96 *
97 * @param listener Listener's object.
98 * @param ctx context object of the grammar rule.
99 */
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530100 public static void processRevisionExit(TreeWalkListener listener,
101 GeneratedYangParser.RevisionStatementContext ctx) {
102
103 // Check for stack to be non empty.
104 ListenerValidation
105 .checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, ParsableDataType.REVISION_DATA,
106 String.valueOf(ctx.DATE_ARG().getText()), ListenerErrorLocation.EXIT);
107
108 Parsable tmpRevisionNode = listener.getParsedDataStack().peek();
109 if (tmpRevisionNode instanceof YangRevision) {
110 listener.getParsedDataStack().pop();
111
112 // Check for stack to be non empty.
113 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
114 ParsableDataType.REVISION_DATA,
115 String.valueOf(ctx.DATE_ARG().getText()),
116 ListenerErrorLocation.EXIT);
117
118 Parsable tmpNode = listener.getParsedDataStack().peek();
119 switch (tmpNode.getParsableDataType()) {
120 case MODULE_DATA: {
121 YangModule module = (YangModule) tmpNode;
122 module.setRevision((YangRevision) tmpRevisionNode);
123 break;
124 }
125 case SUB_MODULE_DATA: {
126 YangSubModule subModule = (YangSubModule) tmpNode;
127 subModule.setRevision((YangRevision) tmpRevisionNode);
128 break;
129 }
130 default:
131 throw new ParserException(
132 ListenerErrorMessageConstruction
133 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
134 ParsableDataType.REVISION_DATA,
135 String.valueOf(ctx.DATE_ARG()
136 .getText()),
137 ListenerErrorLocation.EXIT));
138 }
139 } else {
140 throw new ParserException(
141 ListenerErrorMessageConstruction
142 .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
143 ParsableDataType.REVISION_DATA, String
144 .valueOf(ctx.DATE_ARG()
145 .getText()),
146 ListenerErrorLocation.EXIT));
147 }
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530148 }
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530149
150 /**
151 * Validate revision.
152 *
153 * @param listener Listener's object.
154 * @param ctx context object of the grammar rule.
155 * @return validation result
156 */
157 private static boolean validateRevision(TreeWalkListener listener,
158 GeneratedYangParser.RevisionStatementContext ctx) {
159 // TODO to be implemented
160 return true;
161 }
162}