blob: 4807f8af7b9b15955ccd4def0c1ff4425873d220 [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.YangSubModule;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053020import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053021import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053022import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Ramad3221322016-03-04 19:08:35 +053023
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053024import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
25import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangSubModuleNode;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053026import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053035import static org.onosproject.yangutils.utils.YangConstructType.SUB_MODULE_DATA;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053036
37/*
38 * Reference: RFC6020 and YANG ANTLR Grammar
39 *
40 * ABNF grammar as per RFC6020
41 * submodule-stmt = optsep submodule-keyword sep identifier-arg-str
42 * optsep
43 * "{" stmtsep
44 * submodule-header-stmts
45 * linkage-stmts
46 * meta-stmts
47 * revision-stmts
48 * body-stmts
49 * "}" optsep
50 *
51 * ANTLR grammar rule
Vidyashree Ramad3221322016-03-04 19:08:35 +053052 * submodule_stmt : SUBMODULE_KEYWORD identifier LEFT_CURLY_BRACE submodule_body* RIGHT_CURLY_BRACE;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053053 * submodule_body : submodule_header_statement linkage_stmts meta_stmts revision_stmts body_stmts;
54 */
55
56/**
57 * Implements listener based call back function corresponding to the "submodule"
58 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
59 */
60public final class SubModuleListener {
61
62 /**
63 * Creates a new sub module listener.
64 */
65 private SubModuleListener() {
66 }
67
68 /**
Gaurav Agrawal800a8e72016-02-19 12:57:13 +053069 * It is called when parser receives an input matching the grammar rule (sub
70 * module), perform validations and update the data model tree.
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053071 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053072 * @param listener Listener's object
73 * @param ctx context object of the grammar rule
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053074 */
75 public static void processSubModuleEntry(TreeWalkListener listener,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053076 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053077
78 // Check if stack is empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +053079 checkStackIsEmpty(listener, INVALID_HOLDER, SUB_MODULE_DATA, ctx.identifier().getText(),
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053080 ENTRY);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053081
Vidyashree Ramad3221322016-03-04 19:08:35 +053082 String identifier = getValidIdentifier(ctx.identifier().getText(), SUB_MODULE_DATA, ctx);
83
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053084 YangSubModule yangSubModule = getYangSubModuleNode(JAVA_GENERATION);
Vidyashree Ramad3221322016-03-04 19:08:35 +053085 yangSubModule.setName(identifier);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053086
Vidyashree Rama07021a22016-03-09 20:41:44 +053087 if (ctx.submoduleBody().submoduleHeaderStatement().yangVersionStatement() == null) {
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053088 yangSubModule.setVersion((byte) 1);
89 }
90
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053091 listener.getParsedDataStack().push(yangSubModule);
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053092 }
93
94 /**
95 * It is called when parser exits from grammar rule (submodule), it perform
96 * validations and update the data model tree.
97 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053098 * @param listener Listener's object
99 * @param ctx context object of the grammar rule
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530100 */
101 public static void processSubModuleExit(TreeWalkListener listener,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530102 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530103
104 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530105 checkStackIsNotEmpty(listener, MISSING_HOLDER, SUB_MODULE_DATA, ctx.identifier().getText(),
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530106 EXIT);
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530107
108 if (!(listener.getParsedDataStack().peek() instanceof YangSubModule)) {
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530109 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SUB_MODULE_DATA,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530110 ctx.identifier().getText(), EXIT));
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530111 }
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530112 }
Gaurav Agrawal800a8e72016-02-19 12:57:13 +0530113}