blob: 2159888cdf2b2ce2948c85895d05ed1fd037d332 [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 Agrawala04483c2016-02-13 14:23:40 +053019import org.onosproject.yangutils.datamodel.YangModule;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053020import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053021import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053022import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053023
Vinod Kumar S38046502016-03-23 15:30:27 +053024import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
25import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangModuleNode;
Gaurav Agrawal02b05d22016-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 Agrawal8e8770a2016-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 S38046502016-03-23 15:30:27 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Gaurav Agrawal02b05d22016-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 Agrawal8e8770a2016-02-27 03:57:50 +053035import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053036
37/*
38 * Reference: RFC6020 and YANG ANTLR Grammar
39 *
40 * ABNF grammar as per RFC6020
41 * module-stmt = optsep module-keyword sep identifier-arg-str
42 * optsep
43 * "{" stmtsep
44 * module-header-stmts
45 * linkage-stmts
46 * meta-stmts
47 * revision-stmts
48 * body-stmts
49 * "}" optsep
50 *
51 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053052 * module_stmt : MODULE_KEYWORD identifier LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053053 */
54
55/**
56 * Implements listener based call back function corresponding to the "module"
57 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
58 */
59public final class ModuleListener {
60
61 /**
62 * Creates a new module listener.
63 */
64 private ModuleListener() {
65 }
66
67 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053068 * It is called when parser receives an input matching the grammar rule
69 * (module), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053070 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053071 * @param listener Listener's object
72 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053073 */
74 public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053075
76 // Check if stack is empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053077 checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.identifier().getText(), ENTRY);
78
79 String identifier = getValidIdentifier(ctx.identifier().getText(), MODULE_DATA, ctx);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053080
Vinod Kumar S38046502016-03-23 15:30:27 +053081 YangModule yangModule = getYangModuleNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +053082 yangModule.setName(identifier);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053083
Vidyashree Ramabcd7fba2016-03-09 20:41:44 +053084 if (ctx.moduleBody().moduleHeaderStatement().yangVersionStatement() == null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053085 yangModule.setVersion((byte) 1);
86 }
87
Gaurav Agrawala04483c2016-02-13 14:23:40 +053088 listener.getParsedDataStack().push(yangModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053089 }
90
91 /**
92 * It is called when parser exits from grammar rule (module), it perform
93 * validations and update the data model tree.
94 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053095 * @param listener Listener's object
96 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053097 */
98 public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053099
100 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530101 checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530102
103 if (!(listener.getParsedDataStack().peek() instanceof YangModule)) {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530104 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 ctx.identifier().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530106 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530107 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530108}