blob: bd120ad0bbd4da1aad8584218f4825f55a087a5d [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.YangImport;
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;
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 Agrawal02b05d22016-02-19 12:57:13 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053033import static org.onosproject.yangutils.utils.YangConstructType.IMPORT_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053034
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ABNF grammar as per RFC6020
39 * linkage-stmts = ;; these stmts can appear in any order
40 * *(import-stmt stmtsep)
41 * *(include-stmt stmtsep)
42 *
43 * import-stmt = import-keyword sep identifier-arg-str optsep
44 * "{" stmtsep
45 * prefix-stmt stmtsep
46 * [revision-date-stmt stmtsep]
47 * "}"
48 *
49 * ANTLR grammar rule
50 * linkage_stmts : (import_stmt
51 * | include_stmt)*;
52 * import_stmt : IMPORT_KEYWORD IDENTIFIER LEFT_CURLY_BRACE import_stmt_body
53 * RIGHT_CURLY_BRACE;
54 * import_stmt_body : prefix_stmt revision_date_stmt?;
55 */
56
57/**
58 * Implements listener based call back function corresponding to the "import"
59 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
60 */
61public final class ImportListener {
62
63 /**
64 * Creates a new import listener.
65 */
66 private ImportListener() {
67 }
68
69 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053070 * It is called when parser receives an input matching the grammar rule
71 * (import), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053072 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053073 * @param listener Listener's object
74 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053075 */
76 public static void processImportEntry(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053077
78 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053079 checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.IDENTIFIER().getText(), ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053080
81 YangImport importNode = new YangImport();
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053082 importNode.setModuleName(ctx.IDENTIFIER().getText());
Gaurav Agrawala04483c2016-02-13 14:23:40 +053083
84 // Push import node to the stack.
85 listener.getParsedDataStack().push(importNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053086 }
87
88 /**
89 * It is called when parser exits from grammar rule (import), it perform
90 * validations and update the data model tree.
91 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053092 * @param listener Listener's object
93 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053094 */
95 public static void processImportExit(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053096
97 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053098 checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.IDENTIFIER().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053099
100 Parsable tmpImportNode = listener.getParsedDataStack().peek();
101 if (tmpImportNode instanceof YangImport) {
102 listener.getParsedDataStack().pop();
103
104 // Check for stack to be non empty.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530105 checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.IDENTIFIER().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530106 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530107
108 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530109 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530110 case MODULE_DATA: {
111 YangModule module = (YangModule) tmpNode;
112 module.addImportedInfo((YangImport) tmpImportNode);
113 break;
114 }
115 case SUB_MODULE_DATA: {
116 YangSubModule subModule = (YangSubModule) tmpNode;
117 subModule.addImportedInfo((YangImport) tmpImportNode);
118 break;
119 }
120 default:
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530121 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IMPORT_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, IMPORT_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}