blob: 03938b12e2d1704cdc5682b7986131fb785fdbc0 [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.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;
Vidyashree Rama468f8282016-03-04 19:08:35 +053026
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053027import 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;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053030import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Bharat saraswald9822e92016-04-05 15:13:44 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
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.IMPORT_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 * import-stmt = import-keyword sep identifier-arg-str optsep
46 * "{" stmtsep
47 * prefix-stmt 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 * import_stmt : IMPORT_KEYWORD identifier LEFT_CURLY_BRACE import_stmt_body
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053055 * RIGHT_CURLY_BRACE;
56 * import_stmt_body : prefix_stmt revision_date_stmt?;
57 */
58
59/**
Bharat saraswald9822e92016-04-05 15:13:44 +053060 * Represents listener based call back function corresponding to the "import"
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053061 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
62 */
63public final class ImportListener {
64
65 /**
66 * Creates a new import listener.
67 */
68 private ImportListener() {
69 }
70
71 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053072 * It is called when parser receives an input matching the grammar rule
73 * (import), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053074 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053075 * @param listener Listener's object
76 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053077 */
78 public static void processImportEntry(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053079
80 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +053081 checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.identifier().getText(), ENTRY);
82
83 String identifier = getValidIdentifier(ctx.identifier().getText(), IMPORT_DATA, ctx);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053084
85 YangImport importNode = new YangImport();
Vidyashree Rama468f8282016-03-04 19:08:35 +053086 importNode.setModuleName(identifier);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053087
88 // Push import node to the stack.
89 listener.getParsedDataStack().push(importNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053090 }
91
92 /**
93 * It is called when parser exits from grammar rule (import), it perform
94 * validations and update the data model tree.
95 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053096 * @param listener Listener's object
97 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053098 */
99 public static void processImportExit(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530100
101 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530102 checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530103
104 Parsable tmpImportNode = listener.getParsedDataStack().peek();
105 if (tmpImportNode instanceof YangImport) {
106 listener.getParsedDataStack().pop();
107
108 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530109 checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.identifier().getText(),
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530110 EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530111
112 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530113 switch (tmpNode.getYangConstructType()) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530114 case MODULE_DATA: {
115 YangModule module = (YangModule) tmpNode;
116 module.addToImportList((YangImport) tmpImportNode);
117 break;
118 }
119 case SUB_MODULE_DATA: {
120 YangSubModule subModule = (YangSubModule) tmpNode;
121 subModule.addToImportList((YangImport) tmpImportNode);
122 break;
123 }
124 default:
125 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IMPORT_DATA,
126 ctx.identifier().getText(),
127 EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530128 }
129 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530130 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, IMPORT_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530131 ctx.identifier().getText(), EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530132 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530133 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530134}