blob: b9325da494f9169ce850ce7dfe3d924cdd66447d [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.YangInclude;
20import org.onosproject.yangutils.datamodel.YangModule;
21import org.onosproject.yangutils.datamodel.YangSubModule;
22import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053026
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053035import static org.onosproject.yangutils.utils.YangConstructType.INCLUDE_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053036
37/*
38 * Reference: RFC6020 and YANG ANTLR Grammar
39 *
40 * ABNF grammar as per RFC6020
41 * linkage-stmts = ;; these stmts can appear in any order
42 * *(import-stmt stmtsep)
43 * *(include-stmt stmtsep)
44 *
45 * include-stmt = include-keyword sep identifier-arg-str optsep
46 * (";" /
47 * "{" stmtsep
48 * [revision-date-stmt stmtsep]
49 * "}")
50 *
51 * ANTLR grammar rule
52 * linkage_stmts : (import_stmt
53 * | include_stmt)*;
Vidyashree Rama468f8282016-03-04 19:08:35 +053054 * include_stmt : INCLUDE_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE
Gaurav Agrawala04483c2016-02-13 14:23:40 +053055 * revision_date_stmt? RIGHT_CURLY_BRACE);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053056 */
57
58/**
59 * Implements listener based call back function corresponding to the "include"
60 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
61 */
62public final class IncludeListener {
63
64 /**
65 * Creates a new include listener.
66 */
67 private IncludeListener() {
68 }
69
70 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053071 * It is called when parser receives an input matching the grammar rule
72 * (include), 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 processIncludeEntry(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053078
79 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053080 checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.identifier().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053081 ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053082
Vidyashree Rama468f8282016-03-04 19:08:35 +053083 String identifier = getValidIdentifier(ctx.identifier().getText(), INCLUDE_DATA, ctx);
84
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085 YangInclude includeNode = new YangInclude();
Vidyashree Rama468f8282016-03-04 19:08:35 +053086 includeNode.setSubModuleName(identifier);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053087
88 listener.getParsedDataStack().push(includeNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053089 }
90
91 /**
92 * It is called when parser exits from grammar rule (include), 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 processIncludeExit(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053099
100 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530101 checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530102
103 Parsable tmpIncludeNode = listener.getParsedDataStack().peek();
104 if (tmpIncludeNode instanceof YangInclude) {
105 listener.getParsedDataStack().pop();
106
107 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530108 checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.identifier().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530109 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530110
111 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530112 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530113 case MODULE_DATA: {
114 YangModule module = (YangModule) tmpNode;
115 module.addIncludedInfo((YangInclude) tmpIncludeNode);
116 break;
117 }
118 case SUB_MODULE_DATA: {
119 YangSubModule subModule = (YangSubModule) tmpNode;
120 subModule.addIncludedInfo((YangInclude) tmpIncludeNode);
121 break;
122 }
123 default:
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530124 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, INCLUDE_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530125 ctx.identifier().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530126 EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530127 }
128 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530129 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, INCLUDE_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530130 ctx.identifier().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530131 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530132 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530133}