blob: 4740699a970d14f3dabe797a13c54c69353de2b3 [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;
23import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053027import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
30import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053031
32/*
33 * Reference: RFC6020 and YANG ANTLR Grammar
34 *
35 * ABNF grammar as per RFC6020
36 * linkage-stmts = ;; these stmts can appear in any order
37 * *(import-stmt stmtsep)
38 * *(include-stmt stmtsep)
39 *
40 * import-stmt = import-keyword sep identifier-arg-str optsep
41 * "{" stmtsep
42 * prefix-stmt stmtsep
43 * [revision-date-stmt stmtsep]
44 * "}"
45 *
46 * ANTLR grammar rule
47 * linkage_stmts : (import_stmt
48 * | include_stmt)*;
49 * import_stmt : IMPORT_KEYWORD IDENTIFIER LEFT_CURLY_BRACE import_stmt_body
50 * RIGHT_CURLY_BRACE;
51 * import_stmt_body : prefix_stmt revision_date_stmt?;
52 */
53
54/**
55 * Implements listener based call back function corresponding to the "import"
56 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
57 */
58public final class ImportListener {
59
60 /**
61 * Creates a new import listener.
62 */
63 private ImportListener() {
64 }
65
66 /**
67 * It is called when parser receives an input matching the grammar
68 * rule (import), perform validations and update the data model
69 * tree.
70 *
71 * @param listener Listener's object.
72 * @param ctx context object of the grammar rule.
73 */
74 public static void processImportEntry(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053075
76 // Check for stack to be non empty.
77 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
78 ParsableDataType.IMPORT_DATA,
79 String.valueOf(ctx.IDENTIFIER().getText()),
80 ListenerErrorLocation.ENTRY);
81
82 YangImport importNode = new YangImport();
83 importNode.setModuleName(String.valueOf(ctx.IDENTIFIER().getText()));
84
85 // Push import node to the stack.
86 listener.getParsedDataStack().push(importNode);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053087 }
88
89 /**
90 * It is called when parser exits from grammar rule (import), it perform
91 * validations and update the data model tree.
92 *
93 * @param listener Listener's object.
94 * @param ctx context object of the grammar rule.
95 */
96 public static void processImportExit(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053097
98 // Check for stack to be non empty.
99 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
100 ParsableDataType.IMPORT_DATA,
101 String.valueOf(ctx.IDENTIFIER().getText()),
102 ListenerErrorLocation.EXIT);
103
104 Parsable tmpImportNode = listener.getParsedDataStack().peek();
105 if (tmpImportNode instanceof YangImport) {
106 listener.getParsedDataStack().pop();
107
108 // Check for stack to be non empty.
109 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
110 ParsableDataType.IMPORT_DATA,
111 String.valueOf(ctx.IDENTIFIER().getText()),
112 ListenerErrorLocation.EXIT);
113
114 Parsable tmpNode = listener.getParsedDataStack().peek();
115 switch (tmpNode.getParsableDataType()) {
116 case MODULE_DATA: {
117 YangModule module = (YangModule) tmpNode;
118 module.addImportedInfo((YangImport) tmpImportNode);
119 break;
120 }
121 case SUB_MODULE_DATA: {
122 YangSubModule subModule = (YangSubModule) tmpNode;
123 subModule.addImportedInfo((YangImport) tmpImportNode);
124 break;
125 }
126 default:
127 throw new ParserException(
128 ListenerErrorMessageConstruction
129 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
130 ParsableDataType.IMPORT_DATA,
131 String.valueOf(ctx.IDENTIFIER()
132 .getText()),
133 ListenerErrorLocation.EXIT));
134 }
135 } else {
136 throw new ParserException(
137 ListenerErrorMessageConstruction
138 .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
139 ParsableDataType.IMPORT_DATA, String
140 .valueOf(ctx.IDENTIFIER()
141 .getText()),
142 ListenerErrorLocation.EXIT));
143 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530144 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530145}