blob: 08de56b59f0131911923357bd13024db52f81b22 [file] [log] [blame]
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +05303 *
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;
Vidyashree Rama468f8282016-03-04 19:08:35 +053025
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053026import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053034import static org.onosproject.yangutils.utils.YangConstructType.BELONGS_TO_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053035
36/*
37 * Reference: RFC6020 and YANG ANTLR Grammar
38 *
39 * ABNF grammar as per RFC6020
40 * submodule-header-stmts =
41 * ;; these stmts can appear in any order
42 * [yang-version-stmt stmtsep]
43 * belongs-to-stmt stmtsep
44 *
45 * belongs-to-stmt = belongs-to-keyword sep identifier-arg-str
46 * optsep
47 * "{" stmtsep
48 * prefix-stmt stmtsep
49 * "}"
50 *
51 * ANTLR grammar rule
52 * submodule_header_statement : yang_version_stmt? belongs_to_stmt
53 * | belongs_to_stmt yang_version_stmt?
54 * ;
Vidyashree Rama468f8282016-03-04 19:08:35 +053055 * belongs_to_stmt : BELONGS_TO_KEYWORD identifier LEFT_CURLY_BRACE belongs_to_stmt_body RIGHT_CURLY_BRACE;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053056 * belongs_to_stmt_body : prefix_stmt;
57 */
58
59/**
Bharat saraswald9822e92016-04-05 15:13:44 +053060 * Represents listener based call back function corresponding to the
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053061 * "belongs to" rule defined in ANTLR grammar file for corresponding ABNF rule
62 * in RFC 6020.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053063 */
64public final class BelongsToListener {
65
66 /**
67 * Creates a new belongto listener.
68 */
69 private BelongsToListener() {
70 }
71
72 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053073 * It is called when parser receives an input matching the grammar rule
74 * (belongsto), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053075 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053076 * @param listener Listener's object
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053077 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053078 */
79 public static void processBelongsToEntry(TreeWalkListener listener,
80 GeneratedYangParser.BelongstoStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053081
82 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053083 checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.identifier().getText(),
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053084 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085
Vidyashree Rama468f8282016-03-04 19:08:35 +053086 String identifier = getValidIdentifier(ctx.identifier().getText(), BELONGS_TO_DATA, ctx);
87
Gaurav Agrawala04483c2016-02-13 14:23:40 +053088 YangBelongsTo belongstoNode = new YangBelongsTo();
Vidyashree Rama468f8282016-03-04 19:08:35 +053089 belongstoNode.setBelongsToModuleName(identifier);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053090
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053091 // Set the line number and character position in line for the belongs to.
92 int errorLine = ctx.getStart().getLine();
93 int errorPosition = ctx.getStart().getCharPositionInLine();
94 belongstoNode.setLineNumber(errorLine);
95 belongstoNode.setCharPosition(errorPosition);
96
Gaurav Agrawala04483c2016-02-13 14:23:40 +053097 // Push belongsto into the stack.
98 listener.getParsedDataStack().push(belongstoNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053099 }
100
101 /**
102 * It is called when parser exits from grammar rule (belongsto), it perform
103 * validations and update the data model tree.
104 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530105 * @param listener Listener's object
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530106 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530107 */
108 public static void processBelongsToExit(TreeWalkListener listener,
109 GeneratedYangParser.BelongstoStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530110
111 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530112 checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.identifier().getText(),
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530113 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530114
115 Parsable tmpBelongstoNode = listener.getParsedDataStack().peek();
116 if (tmpBelongstoNode instanceof YangBelongsTo) {
117 listener.getParsedDataStack().pop();
118
119 // Check for stack to be empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530120 checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530121 ctx.identifier().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530122
123 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530124 switch (tmpNode.getYangConstructType()) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530125 case SUB_MODULE_DATA: {
126 YangSubModule subModule = (YangSubModule) tmpNode;
127 subModule.setBelongsTo((YangBelongsTo) tmpBelongstoNode);
128 subModule.setPrefix(subModule.getBelongsTo().getPrefix());
129 break;
130 }
131 default:
132 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BELONGS_TO_DATA,
133 ctx.identifier().getText(),
134 EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530135 }
136 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530137 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BELONGS_TO_DATA,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530138 ctx.identifier().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530139 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530140 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530141}