blob: bc8ee029623887d3183851a3ed350c13648b214b [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
24import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053025import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
26import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053033import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053034
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ABNF grammar as per RFC6020
39 * module-stmt = optsep module-keyword sep identifier-arg-str
40 * optsep
41 * "{" stmtsep
42 * module-header-stmts
43 * linkage-stmts
44 * meta-stmts
45 * revision-stmts
46 * body-stmts
47 * "}" optsep
48 *
49 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053050 * module_stmt : MODULE_KEYWORD identifier LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053051 */
52
53/**
54 * Implements listener based call back function corresponding to the "module"
55 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
56 */
57public final class ModuleListener {
58
59 /**
60 * Creates a new module listener.
61 */
62 private ModuleListener() {
63 }
64
65 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053066 * It is called when parser receives an input matching the grammar rule
67 * (module), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053068 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053069 * @param listener Listener's object
70 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053071 */
72 public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053073
74 // Check if stack is empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053075 checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.identifier().getText(), ENTRY);
76
77 String identifier = getValidIdentifier(ctx.identifier().getText(), MODULE_DATA, ctx);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053078
79 YangModule yangModule = new YangModule();
Vidyashree Rama468f8282016-03-04 19:08:35 +053080 yangModule.setName(identifier);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053081
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053082 if (ctx.moduleBody(0).moduleHeaderStatement().yangVersionStatement() == null) {
83 yangModule.setVersion((byte) 1);
84 }
85
Gaurav Agrawala04483c2016-02-13 14:23:40 +053086 listener.getParsedDataStack().push(yangModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053087 }
88
89 /**
90 * It is called when parser exits from grammar rule (module), it perform
91 * validations and update the data model tree.
92 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053093 * @param listener Listener's object
94 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053095 */
96 public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053097
98 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053099 checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530100
101 if (!(listener.getParsedDataStack().peek() instanceof YangModule)) {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530102 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530103 ctx.identifier().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530104 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530105 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530106}