blob: fc9793f815192eb3dec154773f0a9b0bb74431a1 [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.YangBelongsTo;
20import org.onosproject.yangutils.datamodel.YangSubModule;
21import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053022import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053025
26import static org.onosproject.yangutils.parser.ParsableDataType.BELONGS_TO_DATA;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * submodule-header-stmts =
38 * ;; these stmts can appear in any order
39 * [yang-version-stmt stmtsep]
40 * belongs-to-stmt stmtsep
41 *
42 * belongs-to-stmt = belongs-to-keyword sep identifier-arg-str
43 * optsep
44 * "{" stmtsep
45 * prefix-stmt stmtsep
46 * "}"
47 *
48 * ANTLR grammar rule
49 * submodule_header_statement : yang_version_stmt? belongs_to_stmt
50 * | belongs_to_stmt yang_version_stmt?
51 * ;
52 * belongs_to_stmt : BELONGS_TO_KEYWORD IDENTIFIER LEFT_CURLY_BRACE belongs_to_stmt_body RIGHT_CURLY_BRACE;
53 * belongs_to_stmt_body : prefix_stmt;
54 */
55
56/**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053057 * Implements listener based call back function corresponding to the
58 * "belongs to" rule defined in ANTLR grammar file for corresponding ABNF rule
59 * in RFC 6020.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053060 */
61public final class BelongsToListener {
62
63 /**
64 * Creates a new belongto listener.
65 */
66 private BelongsToListener() {
67 }
68
69 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053070 * It is called when parser receives an input matching the grammar rule
71 * (belongsto), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053072 *
73 * @param listener Listener's object.
74 * @param ctx context object of the grammar rule.
75 */
76 public static void processBelongsToEntry(TreeWalkListener listener,
77 GeneratedYangParser.BelongstoStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053078
79 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053080 checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.IDENTIFIER().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053081 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053082
83 YangBelongsTo belongstoNode = new YangBelongsTo();
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053084 belongstoNode.setBelongsToModuleName(ctx.IDENTIFIER().getText());
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085
86 // Push belongsto into the stack.
87 listener.getParsedDataStack().push(belongstoNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053088 }
89
90 /**
91 * It is called when parser exits from grammar rule (belongsto), it perform
92 * validations and update the data model tree.
93 *
94 * @param listener Listener's object.
95 * @param ctx context object of the grammar rule.
96 */
97 public static void processBelongsToExit(TreeWalkListener listener,
98 GeneratedYangParser.BelongstoStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053099
100 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530101 checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.IDENTIFIER().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530102 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530103
104 Parsable tmpBelongstoNode = listener.getParsedDataStack().peek();
105 if (tmpBelongstoNode instanceof YangBelongsTo) {
106 listener.getParsedDataStack().pop();
107
108 // Check for stack to be empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530109 checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530110 ctx.IDENTIFIER().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530111
112 Parsable tmpNode = listener.getParsedDataStack().peek();
113 switch (tmpNode.getParsableDataType()) {
114 case SUB_MODULE_DATA: {
115 YangSubModule subModule = (YangSubModule) tmpNode;
116 subModule.setBelongsTo((YangBelongsTo) tmpBelongstoNode);
117 break;
118 }
119 default:
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530120 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BELONGS_TO_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530121 ctx.IDENTIFIER().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530122 EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530123 }
124 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530125 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BELONGS_TO_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530126 ctx.IDENTIFIER().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530127 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530128 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530129}