blob: cb633b827de441de37a63bf3ebb831b3ed4ac869 [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.YangModule;
21import org.onosproject.yangutils.datamodel.YangRevision;
22import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswal96dfef02016-06-16 00:29:12 +053023import org.onosproject.yangutils.datamodel.utils.Parsable;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053027
Bharat saraswal96dfef02016-06-16 00:29:12 +053028import static org.onosproject.yangutils.datamodel.utils.YangConstructType.REVISION_DATA;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Bharat saraswald9822e92016-04-05 15:13:44 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidDateFromString;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053037
38/*
39 * Reference: RFC6020 and YANG ANTLR Grammar
40 *
41 * ABNF grammar as per RFC6020
42 * module-stmt = optsep module-keyword sep identifier-arg-str
43 * optsep
44 * "{" stmtsep
45 * module-header-stmts
46 * linkage-stmts
47 * meta-stmts
48 * revision-stmts
49 * body-stmts
50 * "}" optsep
51 *
52 * revision-stmt = revision-keyword sep revision-date optsep
53 * (";" /
54 * "{" stmtsep
55 * [description-stmt stmtsep]
56 * [reference-stmt stmtsep]
57 * "}")
58 *
59 * ANTLR grammar rule
60 * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
61 *
62 * revision_stmt : REVISION_KEYWORD DATE_ARG (STMTEND | LEFT_CURLY_BRACE revision_stmt_body RIGHT_CURLY_BRACE);
63 * revision_stmt_body : description_stmt? reference_stmt?;
64 */
65
66/**
Bharat saraswald9822e92016-04-05 15:13:44 +053067 * Represents listener based call back function corresponding to the "revision"
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053068 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
69 */
70public final class RevisionListener {
71
72 /**
73 * Creates a new revision listener.
74 */
75 private RevisionListener() {
76 }
77
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053078 /**
79 * It is called when parser receives an input matching the grammar rule
80 * (revision),perform validations and update the data model tree.
81 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053082 * @param listener Listener's object
83 * @param ctx context object of the grammar rule
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053084 */
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085 public static void processRevisionEntry(TreeWalkListener listener,
86 GeneratedYangParser.RevisionStatementContext ctx) {
87
88 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053089 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(), ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053090
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053091 Date date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053092
Gaurav Agrawala04483c2016-02-13 14:23:40 +053093 YangRevision revisionNode = new YangRevision();
Vidyashree Rama468f8282016-03-04 19:08:35 +053094 revisionNode.setRevDate(date);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053095
96 listener.getParsedDataStack().push(revisionNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053097 }
98
99 /**
100 * It is called when parser exits from grammar rule (revision), it perform
101 * validations and update the data model tree.
102 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530103 * @param listener Listener's object
104 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530105 */
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530106 public static void processRevisionExit(TreeWalkListener listener, GeneratedYangParser.RevisionStatementContext
107 ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530108
109 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530110 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530111
112 Parsable tmpRevisionNode = listener.getParsedDataStack().peek();
113 if (tmpRevisionNode instanceof YangRevision) {
114 listener.getParsedDataStack().pop();
115
116 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530117 checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530118 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530119
120 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530121 switch (tmpNode.getYangConstructType()) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530122 case MODULE_DATA: {
123 YangModule module = (YangModule) tmpNode;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530124 if (module.getRevision() != null) {
125 Date curRevisionDate = module.getRevision().getRevDate();
126 if (curRevisionDate.before(((YangRevision) tmpRevisionNode).getRevDate())) {
127 module.setRevision((YangRevision) tmpRevisionNode);
128 }
129 } else {
130 module.setRevision((YangRevision) tmpRevisionNode);
131 }
Bharat saraswald9822e92016-04-05 15:13:44 +0530132 break;
133 }
134 case SUB_MODULE_DATA: {
135 YangSubModule subModule = (YangSubModule) tmpNode;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530136 if (subModule.getRevision() != null) {
137 Date curRevisionDate = subModule.getRevision().getRevDate();
138 if (curRevisionDate.before(((YangRevision) tmpRevisionNode).getRevDate())) {
139 subModule.setRevision((YangRevision) tmpRevisionNode);
140 }
141 } else {
142 subModule.setRevision((YangRevision) tmpRevisionNode);
143 }
Bharat saraswald9822e92016-04-05 15:13:44 +0530144 break;
145 }
146 default:
147 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATA,
148 ctx.dateArgumentString().getText(),
149 EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530150 }
151 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530152 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, REVISION_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530153 ctx.dateArgumentString().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530154 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530155 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530156
157 /**
158 * Validate revision.
159 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530160 * @param listener Listener's object
161 * @param ctx context object of the grammar rule
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530162 * @return validation result
163 */
164 private static boolean validateRevision(TreeWalkListener listener,
165 GeneratedYangParser.RevisionStatementContext ctx) {
166 // TODO to be implemented
167 return true;
168 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530169}