blob: 42c753c57a8709b11e61c3b029e7fd146fe420e6 [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.YangSubModule;
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 +053023
24import static org.onosproject.yangutils.parser.ParsableDataType.SUB_MODULE_DATA;
25import 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;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053031
32/*
33 * Reference: RFC6020 and YANG ANTLR Grammar
34 *
35 * ABNF grammar as per RFC6020
36 * submodule-stmt = optsep submodule-keyword sep identifier-arg-str
37 * optsep
38 * "{" stmtsep
39 * submodule-header-stmts
40 * linkage-stmts
41 * meta-stmts
42 * revision-stmts
43 * body-stmts
44 * "}" optsep
45 *
46 * ANTLR grammar rule
47 * submodule_stmt : SUBMODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE submodule_body* RIGHT_CURLY_BRACE;
48 * submodule_body : submodule_header_statement linkage_stmts meta_stmts revision_stmts body_stmts;
49 */
50
51/**
52 * Implements listener based call back function corresponding to the "submodule"
53 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
54 */
55public final class SubModuleListener {
56
57 /**
58 * Creates a new sub module listener.
59 */
60 private SubModuleListener() {
61 }
62
63 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053064 * It is called when parser receives an input matching the grammar rule (sub
65 * module), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053066 *
67 * @param listener Listener's object.
68 * @param ctx context object of the grammar rule.
69 */
70 public static void processSubModuleEntry(TreeWalkListener listener,
71 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053072
73 // Check if stack is empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053074 checkStackIsEmpty(listener, INVALID_HOLDER, SUB_MODULE_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
75 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053076
77 YangSubModule yangSubModule = new YangSubModule();
78 yangSubModule.setName(ctx.IDENTIFIER().getText());
79
80 listener.getParsedDataStack().push(yangSubModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053081 }
82
83 /**
84 * It is called when parser exits from grammar rule (submodule), it perform
85 * validations and update the data model tree.
86 *
87 * @param listener Listener's object.
88 * @param ctx context object of the grammar rule.
89 */
90 public static void processSubModuleExit(TreeWalkListener listener,
91 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053092
93 // Check for stack to be non empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053094 checkStackIsNotEmpty(listener, MISSING_HOLDER, SUB_MODULE_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
95 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053096
97 if (!(listener.getParsedDataStack().peek() instanceof YangSubModule)) {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053098 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SUB_MODULE_DATA,
99 String.valueOf(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}