blob: 3ff67fec255d323bf05f05569edd213fcc8bca94 [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
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053019import java.util.Date;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053020import org.onosproject.yangutils.datamodel.YangImport;
21import org.onosproject.yangutils.datamodel.YangInclude;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022import org.onosproject.yangutils.datamodel.utils.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
Bharat saraswal96dfef02016-06-16 00:29:12 +053027import static org.onosproject.yangutils.datamodel.utils.YangConstructType.REVISION_DATE_DATA;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidDateFromString;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053034
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ABNF grammar as per RFC6020
39 * import-stmt = import-keyword sep identifier-arg-str optsep
40 * "{" stmtsep
41 * prefix-stmt stmtsep
42 * [revision-date-stmt stmtsep]
43 * "}"
44 * include-stmt = include-keyword sep identifier-arg-str optsep
45 * (";" /
46 * "{" stmtsep
47 * [revision-date-stmt stmtsep]
48 * "}")
49 * revision-date-stmt = revision-date-keyword sep revision-date stmtend
50 *
51 * ANTLR grammar rule
52 * import_stmt : IMPORT_KEYWORD IDENTIFIER LEFT_CURLY_BRACE import_stmt_body
53 * RIGHT_CURLY_BRACE;
54 * import_stmt_body : prefix_stmt revision_date_stmt?;
55 *
56 * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE
57 * revision_date_stmt_body? RIGHT_CURLY_BRACE);
58 *
59 * revision_date_stmt : REVISION_DATE_KEYWORD DATE_ARG STMTEND;
60 *
61 */
62
63/**
Bharat saraswald9822e92016-04-05 15:13:44 +053064 * Represents listener based call back function corresponding to the
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053065 * "revision date" rule defined in ANTLR grammar file for corresponding ABNF
66 * rule in RFC 6020.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053067 */
68public final class RevisionDateListener {
69
70 /**
71 * Creates a new revision date listener.
72 */
73 private RevisionDateListener() {
74 }
75
76 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053077 * It is called when parser receives an input matching the grammar rule
78 * (revision date),perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053079 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053080 * @param listener Listener's object
81 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053082 */
83 public static void processRevisionDateEntry(TreeWalkListener listener,
Vinod Kumar Sc4216002016-03-03 19:55:30 +053084 GeneratedYangParser.RevisionDateStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085
86 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053087 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, ctx.dateArgumentString().getText(),
Vinod Kumar Sc4216002016-03-03 19:55:30 +053088 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053089
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053090 Date date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053091
Gaurav Agrawala04483c2016-02-13 14:23:40 +053092 // Obtain the node of the stack.
93 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053094 switch (tmpNode.getYangConstructType()) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053095 case IMPORT_DATA: {
96 YangImport importNode = (YangImport) tmpNode;
Vidyashree Rama468f8282016-03-04 19:08:35 +053097 importNode.setRevision(date);
Vinod Kumar Sc4216002016-03-03 19:55:30 +053098 break;
99 }
100 case INCLUDE_DATA: {
101 YangInclude includeNode = (YangInclude) tmpNode;
Vidyashree Rama468f8282016-03-04 19:08:35 +0530102 includeNode.setRevision(date);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530103 break;
104 }
105 default:
106 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATE_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530107 ctx.dateArgumentString().getText(), ENTRY));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530108 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530109 }
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530110}