blob: c9381900ee34c027d5a721530cd5bea6ed3ee89a [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 +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.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053032import static org.onosproject.yangutils.utils.YangConstructType.BELONGS_TO_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053033
34/*
35 * Reference: RFC6020 and YANG ANTLR Grammar
36 *
37 * ABNF grammar as per RFC6020
38 * submodule-header-stmts =
39 * ;; these stmts can appear in any order
40 * [yang-version-stmt stmtsep]
41 * belongs-to-stmt stmtsep
42 *
43 * belongs-to-stmt = belongs-to-keyword sep identifier-arg-str
44 * optsep
45 * "{" stmtsep
46 * prefix-stmt stmtsep
47 * "}"
48 *
49 * ANTLR grammar rule
50 * submodule_header_statement : yang_version_stmt? belongs_to_stmt
51 * | belongs_to_stmt yang_version_stmt?
52 * ;
53 * belongs_to_stmt : BELONGS_TO_KEYWORD IDENTIFIER LEFT_CURLY_BRACE belongs_to_stmt_body RIGHT_CURLY_BRACE;
54 * belongs_to_stmt_body : prefix_stmt;
55 */
56
57/**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053058 * Implements listener based call back function corresponding to the
59 * "belongs to" rule defined in ANTLR grammar file for corresponding ABNF rule
60 * in RFC 6020.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053061 */
62public final class BelongsToListener {
63
64 /**
65 * Creates a new belongto listener.
66 */
67 private BelongsToListener() {
68 }
69
70 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053071 * It is called when parser receives an input matching the grammar rule
72 * (belongsto), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053073 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053074 * @param listener Listener's object
75 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053076 */
77 public static void processBelongsToEntry(TreeWalkListener listener,
78 GeneratedYangParser.BelongstoStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053079
80 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053081 checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.IDENTIFIER().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053082 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053083
84 YangBelongsTo belongstoNode = new YangBelongsTo();
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053085 belongstoNode.setBelongsToModuleName(ctx.IDENTIFIER().getText());
Gaurav Agrawala04483c2016-02-13 14:23:40 +053086
87 // Push belongsto into the stack.
88 listener.getParsedDataStack().push(belongstoNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053089 }
90
91 /**
92 * It is called when parser exits from grammar rule (belongsto), it perform
93 * validations and update the data model tree.
94 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053095 * @param listener Listener's object
96 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053097 */
98 public static void processBelongsToExit(TreeWalkListener listener,
99 GeneratedYangParser.BelongstoStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530100
101 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530102 checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.IDENTIFIER().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530103 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530104
105 Parsable tmpBelongstoNode = listener.getParsedDataStack().peek();
106 if (tmpBelongstoNode instanceof YangBelongsTo) {
107 listener.getParsedDataStack().pop();
108
109 // Check for stack to be empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530110 checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530111 ctx.IDENTIFIER().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530112
113 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530114 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530115 case SUB_MODULE_DATA: {
116 YangSubModule subModule = (YangSubModule) tmpNode;
117 subModule.setBelongsTo((YangBelongsTo) tmpBelongstoNode);
118 break;
119 }
120 default:
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530121 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BELONGS_TO_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530122 ctx.IDENTIFIER().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530123 EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530124 }
125 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530126 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BELONGS_TO_DATA,
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530127 ctx.IDENTIFIER().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530128 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530129 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530130}