blob: 3f6fc4045b213c0d53cae403b94811d10287ee8b [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;
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 * submodule-stmt = optsep submodule-keyword sep identifier-arg-str
34 * optsep
35 * "{" stmtsep
36 * submodule-header-stmts
37 * linkage-stmts
38 * meta-stmts
39 * revision-stmts
40 * body-stmts
41 * "}" optsep
42 *
43 * ANTLR grammar rule
44 * submodule_stmt : SUBMODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE submodule_body* RIGHT_CURLY_BRACE;
45 * submodule_body : submodule_header_statement linkage_stmts meta_stmts revision_stmts body_stmts;
46 */
47
48/**
49 * Implements listener based call back function corresponding to the "submodule"
50 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
51 */
52public final class SubModuleListener {
53
54 /**
55 * Creates a new sub module listener.
56 */
57 private SubModuleListener() {
58 }
59
60 /**
61 * It is called when parser receives an input matching the grammar
62 * rule (sub module), perform validations and update the data model
63 * tree.
64 *
65 * @param listener Listener's object.
66 * @param ctx context object of the grammar rule.
67 */
68 public static void processSubModuleEntry(TreeWalkListener listener,
69 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053070
71 // Check if stack is empty.
72 ListenerValidation
73 .checkStackIsEmpty(listener, ListenerErrorType.INVALID_HOLDER, ParsableDataType.SUB_MODULE_DATA,
74 String.valueOf(ctx.IDENTIFIER().getText()), ListenerErrorLocation.ENTRY);
75
76 YangSubModule yangSubModule = new YangSubModule();
77 yangSubModule.setName(ctx.IDENTIFIER().getText());
78
79 listener.getParsedDataStack().push(yangSubModule);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053080 }
81
82 /**
83 * It is called when parser exits from grammar rule (submodule), it perform
84 * validations and update the data model tree.
85 *
86 * @param listener Listener's object.
87 * @param ctx context object of the grammar rule.
88 */
89 public static void processSubModuleExit(TreeWalkListener listener,
90 GeneratedYangParser.SubModuleStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053091
92 // Check for stack to be non empty.
93 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
94 ParsableDataType.SUB_MODULE_DATA,
95 String.valueOf(ctx.IDENTIFIER().getText()),
96 ListenerErrorLocation.EXIT);
97
98 if (!(listener.getParsedDataStack().peek() instanceof YangSubModule)) {
99 throw new ParserException(
100 ListenerErrorMessageConstruction
101 .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
102 ParsableDataType.SUB_MODULE_DATA,
103 String.valueOf(ctx.IDENTIFIER()
104 .getText()),
105 ListenerErrorLocation.EXIT));
106 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530107 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530108}