blob: f4f6ef50fa3063c994bb14cdb77e94ae59635ade [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;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053023import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
24import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
25import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053026import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053031import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * module-stmt = optsep module-keyword sep identifier-arg-str
38 * optsep
39 * "{" stmtsep
40 * module-header-stmts
41 * linkage-stmts
42 * meta-stmts
43 * revision-stmts
44 * body-stmts
45 * "}" optsep
46 *
47 * ANTLR grammar rule
48 * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
49 */
50
51/**
52 * Implements listener based call back function corresponding to the "module"
53 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
54 */
55public final class ModuleListener {
56
57 /**
58 * Creates a new module listener.
59 */
60 private ModuleListener() {
61 }
62
63 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053064 * It is called when parser receives an input matching the grammar rule
65 * (module), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053066 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053067 * @param listener Listener's object
68 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053069 */
70 public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053071
72 // Check if stack is empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053073 checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.IDENTIFIER().getText(), ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053074
75 YangModule yangModule = new YangModule();
76 yangModule.setName(ctx.IDENTIFIER().getText());
77
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053078 if (ctx.moduleBody(0).moduleHeaderStatement().yangVersionStatement() == null) {
79 yangModule.setVersion((byte) 1);
80 }
81
Gaurav Agrawala04483c2016-02-13 14:23:40 +053082 listener.getParsedDataStack().push(yangModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053083 }
84
85 /**
86 * It is called when parser exits from grammar rule (module), it perform
87 * validations and update the data model tree.
88 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053089 * @param listener Listener's object
90 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053091 */
92 public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053093
94 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053095 checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.IDENTIFIER().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053096
97 if (!(listener.getParsedDataStack().peek() instanceof YangModule)) {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053098 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053099 ctx.IDENTIFIER().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530100 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530101 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530102}