blob: 8cd6d67f3812407b2303524d42605c18afe9415a [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.YangImport;
20import org.onosproject.yangutils.datamodel.YangInclude;
21import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053022import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053025
26import static org.onosproject.yangutils.parser.ParsableDataType.REVISION_DATE_DATA;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * import-stmt = import-keyword sep identifier-arg-str optsep
38 * "{" stmtsep
39 * prefix-stmt stmtsep
40 * [revision-date-stmt stmtsep]
41 * "}"
42 * include-stmt = include-keyword sep identifier-arg-str optsep
43 * (";" /
44 * "{" stmtsep
45 * [revision-date-stmt stmtsep]
46 * "}")
47 * revision-date-stmt = revision-date-keyword sep revision-date stmtend
48 *
49 * ANTLR grammar rule
50 * import_stmt : IMPORT_KEYWORD IDENTIFIER LEFT_CURLY_BRACE import_stmt_body
51 * RIGHT_CURLY_BRACE;
52 * import_stmt_body : prefix_stmt revision_date_stmt?;
53 *
54 * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE
55 * revision_date_stmt_body? RIGHT_CURLY_BRACE);
56 *
57 * revision_date_stmt : REVISION_DATE_KEYWORD DATE_ARG STMTEND;
58 *
59 */
60
61/**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053062 * Implements listener based call back function corresponding to the
63 * "revision date" rule defined in ANTLR grammar file for corresponding ABNF
64 * rule in RFC 6020.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053065 */
66public final class RevisionDateListener {
67
68 /**
69 * Creates a new revision date listener.
70 */
71 private RevisionDateListener() {
72 }
73
74 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053075 * It is called when parser receives an input matching the grammar rule
76 * (revision date),perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053077 *
78 * @param listener Listener's object.
79 * @param ctx context object of the grammar rule.
80 */
81 public static void processRevisionDateEntry(TreeWalkListener listener,
82 GeneratedYangParser.RevisionDateStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053083
84 // Check for stack to be non empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053085 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, String.valueOf(ctx.DATE_ARG().getText()),
86 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053087
88 // Obtain the node of the stack.
89 Parsable tmpNode = listener.getParsedDataStack().peek();
90 switch (tmpNode.getParsableDataType()) {
91 case IMPORT_DATA: {
92 YangImport importNode = (YangImport) tmpNode;
93 importNode.setRevision(String.valueOf(ctx.DATE_ARG().getText()));
94 break;
95 }
96 case INCLUDE_DATA: {
97 YangInclude includeNode = (YangInclude) tmpNode;
98 includeNode.setRevision(String.valueOf(ctx.DATE_ARG().getText()));
99 break;
100 }
101 default:
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530102 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATE_DATA,
103 String.valueOf(ctx.DATE_ARG().getText()), ENTRY));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530104 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530105 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530106 // TODO Implement the DATE_ARG validation as per RFC 6020.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530107}