blob: d2f90ce526e55721fabc300a6978368f93386f2e [file] [log] [blame]
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +05301/*
2 * Copyright 2014-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
19/*
20 * Reference: RFC6020 and YANG ANTLR Grammar
21 *
22 * body-stmts = *((extension-stmt /
23 * feature-stmt /
24 * identity-stmt /
25 * typedef-stmt /
26 * grouping-stmt /
27 * data-def-stmt /
28 * augment-stmt /
29 * rpc-stmt /
30 * notification-stmt /
31 * deviation-stmt) stmtsep)
32 *
33 * typedef-stmt = typedef-keyword sep identifier-arg-str optsep
34 * "{" stmtsep
35 * ;; these stmts can appear in any order
36 * type-stmt stmtsep
37 * [units-stmt stmtsep]
38 * [default-stmt stmtsep]
39 * [status-stmt stmtsep]
40 * [description-stmt stmtsep]
41 * [reference-stmt stmtsep]
42 * "}"
43 *
44 * ANTLR grammar rule
45 * typedefStatement : TYPEDEF_KEYWORD IDENTIFIER LEFT_CURLY_BRACE
46 * (typeStatement | unitsStatement | defaultStatement | statusStatement
47 * | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
48 */
49
50import org.onosproject.yangutils.datamodel.YangContainer;
51import org.onosproject.yangutils.datamodel.YangList;
52import org.onosproject.yangutils.datamodel.YangModule;
53import org.onosproject.yangutils.datamodel.YangNode;
54import org.onosproject.yangutils.datamodel.YangSubModule;
55import org.onosproject.yangutils.datamodel.YangTypeDef;
56import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
57import org.onosproject.yangutils.parser.Parsable;
58import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053059import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
60import org.onosproject.yangutils.parser.exceptions.ParserException;
61import org.onosproject.yangutils.parser.impl.TreeWalkListener;
62import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053063
64import static org.onosproject.yangutils.parser.ParsableDataType.DEFAULT_DATA;
65import static org.onosproject.yangutils.parser.ParsableDataType.DESCRIPTION_DATA;
66import static org.onosproject.yangutils.parser.ParsableDataType.REFERENCE_DATA;
67import static org.onosproject.yangutils.parser.ParsableDataType.STATUS_DATA;
68import static org.onosproject.yangutils.parser.ParsableDataType.TYPEDEF_DATA;
69import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA;
70import static org.onosproject.yangutils.parser.ParsableDataType.UNITS_DATA;
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053071import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
72import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
73import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
74import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053075import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CARDINALITY;
76import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053077import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053078import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
79import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053080import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
81
82/**
83 * Implements listener based call back function corresponding to the "typedef"
84 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
85 */
86public final class TypeDefListener {
87
88 private static ParsableDataType yangConstruct;
89
90 /**
91 * Creates a new typedef listener.
92 */
93 private TypeDefListener() {
94 }
95
96 /**
97 * It is called when parser enters grammar rule (typedef), it perform
98 * validations and updates the data model tree.
99 *
100 * @param listener listener's object.
101 * @param ctx context object of the grammar rule.
102 */
103 public static void processTypeDefEntry(TreeWalkListener listener,
104 GeneratedYangParser.TypedefStatementContext ctx) {
105
106 // Check for stack to be non empty.
107 checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY);
108
109 boolean result = validateSubStatementsCardinality(ctx);
110 if (!result) {
111 throw new ParserException(constructListenerErrorMessage(INVALID_CARDINALITY, yangConstruct, "", ENTRY));
112 }
113
114 YangTypeDef typeDefNode = new YangTypeDef();
115 typeDefNode.setDerivedName(ctx.IDENTIFIER().getText());
116
117 Parsable curData = listener.getParsedDataStack().peek();
118
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530119 if ((curData instanceof YangModule) | (curData instanceof YangSubModule) | (curData instanceof YangContainer)
120 | (curData instanceof YangList)) {
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530121 /*
122 * TODO YangGrouping, YangRpc, YangInput, YangOutput, Notification.
123 */
124 YangNode curNode = (YangNode) curData;
125 try {
126 curNode.addChild(typeDefNode);
127 } catch (DataModelException e) {
128 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
129 TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage()));
130 }
131 listener.getParsedDataStack().push(typeDefNode);
132 } else {
133 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
134 TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY));
135 }
136 }
137
138 /**
139 * It is called when parser exits from grammar rule (typedef), it perform
140 * validations and updates the data model tree.
141 *
142 * @param listener listener's object.
143 * @param ctx context object of the grammar rule.
144 */
145 public static void processTypeDefExit(TreeWalkListener listener,
146 GeneratedYangParser.TypedefStatementContext ctx) {
147
148 // Check for stack to be non empty.
149 checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.IDENTIFIER().getText(), EXIT);
150
151 if (listener.getParsedDataStack().peek() instanceof YangTypeDef) {
152 listener.getParsedDataStack().pop();
153 } else {
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +0530154 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, TYPEDEF_DATA,
155 ctx.IDENTIFIER().getText(), EXIT));
156 }
157 }
158
159 /**
160 * Validates the cardinality of typedef sub-statements as per grammar.
161 *
162 * @param ctx context object of the grammar rule.
163 * @return true/false validation success or failure.
164 */
165 private static boolean validateSubStatementsCardinality(GeneratedYangParser.TypedefStatementContext ctx) {
166
167 if ((!ctx.unitsStatement().isEmpty())
168 && (ctx.unitsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
169 yangConstruct = UNITS_DATA;
170 return false;
171 }
172
173 if ((!ctx.defaultStatement().isEmpty())
174 && (ctx.defaultStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
175 yangConstruct = DEFAULT_DATA;
176 return false;
177 }
178
179 if (ctx.typeStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY) {
180 yangConstruct = TYPE_DATA;
181 return false;
182 }
183
184 if ((!ctx.descriptionStatement().isEmpty())
185 && (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
186 yangConstruct = DESCRIPTION_DATA;
187 return false;
188 }
189
190 if ((!ctx.referenceStatement().isEmpty())
191 && (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
192 yangConstruct = REFERENCE_DATA;
193 return false;
194 }
195
196 if ((!ctx.statusStatement().isEmpty())
197 && (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
198 yangConstruct = STATUS_DATA;
199 return false;
200 }
201 return true;
202 }
203}