blob: cf6309a6d8e8d724490a886cca2b4dffe5212f87 [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;
20import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053021import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053022import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053024import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
25import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
26import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
27import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053028
29/*
30 * Reference: RFC6020 and YANG ANTLR Grammar
31 *
32 * ABNF grammar as per RFC6020
33 * module-stmt = optsep module-keyword sep identifier-arg-str
34 * optsep
35 * "{" stmtsep
36 * module-header-stmts
37 * linkage-stmts
38 * meta-stmts
39 * revision-stmts
40 * body-stmts
41 * "}" optsep
42 *
43 * ANTLR grammar rule
44 * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
45 */
46
47/**
48 * Implements listener based call back function corresponding to the "module"
49 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
50 */
51public final class ModuleListener {
52
53 /**
54 * Creates a new module listener.
55 */
56 private ModuleListener() {
57 }
58
59 /**
60 * It is called when parser receives an input matching the grammar
61 * rule (module), perform validations and update the data model
62 * tree.
63 *
64 * @param listener Listener's object.
65 * @param ctx context object of the grammar rule.
66 */
67 public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053068
69 // Check if stack is empty.
70 ListenerValidation
71 .checkStackIsEmpty(listener, ListenerErrorType.INVALID_HOLDER, ParsableDataType.MODULE_DATA,
72 String.valueOf(ctx.IDENTIFIER().getText()), ListenerErrorLocation.ENTRY);
73
74 YangModule yangModule = new YangModule();
75 yangModule.setName(ctx.IDENTIFIER().getText());
76
77 listener.getParsedDataStack().push(yangModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053078 }
79
80 /**
81 * It is called when parser exits from grammar rule (module), it perform
82 * validations and update the data model tree.
83 *
84 * @param listener Listener's object.
85 * @param ctx context object of the grammar rule.
86 */
87 public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053088
89 // Check for stack to be non empty.
90 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
91 ParsableDataType.MODULE_DATA,
92 String.valueOf(ctx.IDENTIFIER().getText()),
93 ListenerErrorLocation.EXIT);
94
95 if (!(listener.getParsedDataStack().peek() instanceof YangModule)) {
96 throw new ParserException(
97 ListenerErrorMessageConstruction
98 .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
99 ParsableDataType.MODULE_DATA, String
100 .valueOf(ctx.IDENTIFIER()
101 .getText()),
102 ListenerErrorLocation.EXIT));
103 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530104 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530105}