blob: 3f60aef1c6964bbe3f3f34d291ffa9fe5a523d09 [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.YangImport;
20import org.onosproject.yangutils.datamodel.YangInclude;
21import org.onosproject.yangutils.parser.Parsable;
22import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053026import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053030
31/*
32 * Reference: RFC6020 and YANG ANTLR Grammar
33 *
34 * ABNF grammar as per RFC6020
35 * import-stmt = import-keyword sep identifier-arg-str optsep
36 * "{" stmtsep
37 * prefix-stmt stmtsep
38 * [revision-date-stmt stmtsep]
39 * "}"
40 * include-stmt = include-keyword sep identifier-arg-str optsep
41 * (";" /
42 * "{" stmtsep
43 * [revision-date-stmt stmtsep]
44 * "}")
45 * revision-date-stmt = revision-date-keyword sep revision-date stmtend
46 *
47 * ANTLR grammar rule
48 * import_stmt : IMPORT_KEYWORD IDENTIFIER LEFT_CURLY_BRACE import_stmt_body
49 * RIGHT_CURLY_BRACE;
50 * import_stmt_body : prefix_stmt revision_date_stmt?;
51 *
52 * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE
53 * revision_date_stmt_body? RIGHT_CURLY_BRACE);
54 *
55 * revision_date_stmt : REVISION_DATE_KEYWORD DATE_ARG STMTEND;
56 *
57 */
58
59/**
60 * Implements listener based call back function corresponding to the "revision date"
61 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
62 */
63public final class RevisionDateListener {
64
65 /**
66 * Creates a new revision date listener.
67 */
68 private RevisionDateListener() {
69 }
70
71 /**
72 * It is called when parser receives an input matching the grammar
73 * rule (revision date),perform validations and update the data model
74 * tree.
75 *
76 * @param listener Listener's object.
77 * @param ctx context object of the grammar rule.
78 */
79 public static void processRevisionDateEntry(TreeWalkListener listener,
80 GeneratedYangParser.RevisionDateStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053081
82 // Check for stack to be non empty.
83 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
84 ParsableDataType.REVISION_DATE_DATA,
85 String.valueOf(ctx.DATE_ARG().getText()),
86 ListenerErrorLocation.ENTRY);
87
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:
102 throw new ParserException(
103 ListenerErrorMessageConstruction
104 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
105 ParsableDataType.REVISION_DATE_DATA,
106 String.valueOf(ctx.DATE_ARG().getText()),
107 ListenerErrorLocation.ENTRY));
108 }
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530109 }
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530110 // TODO Implement the DATE_ARG validation as per RFC 6020.
111}