blob: 225aff506408607ebf79ed94c951fa094ea5cab5 [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 Agrawald9d6cc82016-03-29 02:17:23 +053019import org.onosproject.yangutils.datamodel.HasResolutionInfo;
janani bcc9ac302016-03-24 12:43:48 +053020import org.onosproject.yangutils.datamodel.YangRevision;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053021import org.onosproject.yangutils.datamodel.YangSubModule;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053022import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
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;
Vidyashree Rama468f8282016-03-04 19:08:35 +053026
Vinod Kumar S38046502016-03-23 15:30:27 +053027import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
28import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangSubModuleNode;
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;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +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;
Vinod Kumar S38046502016-03-23 15:30:27 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
janani bcc9ac302016-03-24 12:43:48 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.setCurrentDateForRevision;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053039import static org.onosproject.yangutils.utils.YangConstructType.SUB_MODULE_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053040
41/*
42 * Reference: RFC6020 and YANG ANTLR Grammar
43 *
44 * ABNF grammar as per RFC6020
45 * submodule-stmt = optsep submodule-keyword sep identifier-arg-str
46 * optsep
47 * "{" stmtsep
48 * submodule-header-stmts
49 * linkage-stmts
50 * meta-stmts
51 * revision-stmts
52 * body-stmts
53 * "}" optsep
54 *
55 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053056 * submodule_stmt : SUBMODULE_KEYWORD identifier LEFT_CURLY_BRACE submodule_body* RIGHT_CURLY_BRACE;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053057 * submodule_body : submodule_header_statement linkage_stmts meta_stmts revision_stmts body_stmts;
58 */
59
60/**
61 * Implements listener based call back function corresponding to the "submodule"
62 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
63 */
64public final class SubModuleListener {
65
66 /**
67 * Creates a new sub module listener.
68 */
69 private SubModuleListener() {
70 }
71
72 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053073 * It is called when parser receives an input matching the grammar rule (sub
74 * module), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053075 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053076 * @param listener Listener's object
77 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053078 */
79 public static void processSubModuleEntry(TreeWalkListener listener,
Vinod Kumar S38046502016-03-23 15:30:27 +053080 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053081
82 // Check if stack is empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053083 checkStackIsEmpty(listener, INVALID_HOLDER, SUB_MODULE_DATA, ctx.identifier().getText(),
Vinod Kumar S38046502016-03-23 15:30:27 +053084 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085
Vidyashree Rama468f8282016-03-04 19:08:35 +053086 String identifier = getValidIdentifier(ctx.identifier().getText(), SUB_MODULE_DATA, ctx);
87
Vinod Kumar S38046502016-03-23 15:30:27 +053088 YangSubModule yangSubModule = getYangSubModuleNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +053089 yangSubModule.setName(identifier);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053090
Vidyashree Ramabcd7fba2016-03-09 20:41:44 +053091 if (ctx.submoduleBody().submoduleHeaderStatement().yangVersionStatement() == null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053092 yangSubModule.setVersion((byte) 1);
93 }
94
janani bcc9ac302016-03-24 12:43:48 +053095 if (ctx.submoduleBody().revisionStatements().revisionStatement().isEmpty()) {
96 String currentDate = setCurrentDateForRevision();
97 YangRevision currentRevision = new YangRevision();
98 currentRevision.setRevDate(currentDate);
99 yangSubModule.setRevision(currentRevision);
100 }
101
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530102 listener.getParsedDataStack().push(yangSubModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530103 }
104
105 /**
106 * It is called when parser exits from grammar rule (submodule), it perform
107 * validations and update the data model tree.
108 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530109 * @param listener Listener's object
110 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530111 */
112 public static void processSubModuleExit(TreeWalkListener listener,
Vinod Kumar S38046502016-03-23 15:30:27 +0530113 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530114
115 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530116 checkStackIsNotEmpty(listener, MISSING_HOLDER, SUB_MODULE_DATA, ctx.identifier().getText(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530117 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530118
119 if (!(listener.getParsedDataStack().peek() instanceof YangSubModule)) {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530120 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SUB_MODULE_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 ctx.identifier().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530122 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530123 try {
124 ((HasResolutionInfo) listener.getParsedDataStack().peek()).resolveSelfFileLinking();
125 } catch (DataModelException e) {
126 ParserException parserException = new ParserException(e.getMessage());
127 parserException.setLine(e.getLineNumber());
128 parserException.setCharPosition(e.getCharPositionInLine());
129 throw parserException;
130 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530131 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530132}