blob: 32955d5f718e1aabc3961f1c62b8c94891971e99 [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama92fc5562016-02-12 18:44:12 +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 Agrawald9d6cc82016-03-29 02:17:23 +053019import org.onosproject.yangutils.datamodel.YangDerivedInfo;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053020import org.onosproject.yangutils.datamodel.YangLeaf;
21import org.onosproject.yangutils.datamodel.YangLeafList;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053022import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053024import org.onosproject.yangutils.datamodel.YangType;
Vinod Kumar S71cba682016-02-25 15:52:16 +053025import org.onosproject.yangutils.datamodel.YangTypeDef;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053026import org.onosproject.yangutils.datamodel.YangUnion;
27import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053028import org.onosproject.yangutils.datamodel.utils.Parsable;
Vidyashree Rama528ef302016-06-30 14:31:18 +053029import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053030import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053031import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053032import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053033import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vinod Kumar S71cba682016-02-25 15:52:16 +053034
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053035import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053036import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
Bharat saraswal96dfef02016-06-16 00:29:12 +053037import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.UNRESOLVED;
38import static org.onosproject.yangutils.datamodel.utils.YangConstructType.TYPE_DATA;
Vidyashree Rama59071f32016-02-20 19:27:56 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Vidyashree Rama59071f32016-02-20 19:27:56 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053045import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053046import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
47import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier;
Vidyashree Rama59071f32016-02-20 19:27:56 +053048import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswal96dfef02016-06-16 00:29:12 +053049import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangType;
Vidyashree Rama59071f32016-02-20 19:27:56 +053050
Vidyashree Rama92fc5562016-02-12 18:44:12 +053051/*
52 * Reference: RFC6020 and YANG ANTLR Grammar
53 *
54 * ABNF grammar as per RFC6020
55 * type-stmt = type-keyword sep identifier-ref-arg-str optsep
56 * (";" /
57 * "{" stmtsep
58 * type-body-stmts
59 * "}")
60 *
61 * ANTLR grammar rule
62 * typeStatement : TYPE_KEYWORD string (STMTEND | LEFT_CURLY_BRACE typeBodyStatements RIGHT_CURLY_BRACE);
63 */
64
65/**
Bharat saraswald9822e92016-04-05 15:13:44 +053066 * Represents listener based call back function corresponding to the "type" rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053067 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053068 */
69public final class TypeListener {
70
71 /**
72 * Creates a new type listener.
73 */
74 private TypeListener() {
75 }
76
77 /**
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053078 * It is called when parser receives an input matching the grammar rule
79 * (type), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053080 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053081 * @param listener listener's object
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053082 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053083 */
84 public static void processTypeEntry(TreeWalkListener listener,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053085 GeneratedYangParser.TypeStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053086
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053087 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053088 checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053089
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053090 // Validate node identifier.
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053091 YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), TYPE_DATA,
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053092 ctx);
Vinod Kumar S71cba682016-02-25 15:52:16 +053093
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053094 // Obtain the YANG data type.
95 YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText());
96
Vidyashree Rama528ef302016-06-30 14:31:18 +053097 validateTypeSubStatementCardinality(ctx, yangDataTypes);
98
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053099 // Create YANG type object and fill the values.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530100 YangType<?> type = getYangType(JAVA_GENERATION);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530101 type.setNodeIdentifier(nodeIdentifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530102 type.setDataType(yangDataTypes);
103
janani be18b5342016-07-13 21:06:41 +0530104 // Set default require instance value as true for instance identifier.
105 setDefaultRequireInstanceForInstanceIdentifier(type);
106
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530107 int errorLine = ctx.getStart().getLine();
108 int errorPosition = ctx.getStart().getCharPositionInLine();
109
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530110 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530111 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530112 case LEAF_DATA:
113 YangLeaf leaf = (YangLeaf) tmpData;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530114 leaf.setDataType(type);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530115
116 /*
117 * If data type is derived, resolution information to be added
118 * in resolution list.
119 */
120 if (yangDataTypes == YangDataTypes.DERIVED) {
121 // Parent YANG node of leaf to be added in resolution information.
122 Parsable leafData = listener.getParsedDataStack().pop();
123 Parsable parentNodeOfLeaf = listener.getParsedDataStack().peek();
124 listener.getParsedDataStack().push(leafData);
125
126 // Verify parent node of leaf
127 if (!(parentNodeOfLeaf instanceof YangNode)) {
128 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
129 ctx.string().getText(), EXIT));
130 }
131
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530132 // Create empty derived info and attach it to type extended info.
133 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
134 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
135
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530136 type.setResolvableStatus(UNRESOLVED);
137
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530138 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530139 YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangType>(type,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530140 (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530141 addToResolutionList(resolutionInfo, ctx);
142 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530143 break;
144 case LEAF_LIST_DATA:
145 YangLeafList leafList = (YangLeafList) tmpData;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530146 leafList.setDataType(type);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530147
148 /*
149 * If data type is derived, resolution information to be added
150 * in resolution list.
151 */
152 if (yangDataTypes == YangDataTypes.DERIVED) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530153 // Parent YANG node of leaf list to be added in resolution information.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530154 Parsable leafListData = listener.getParsedDataStack().pop();
155 Parsable parentNodeOfLeafList = listener.getParsedDataStack().peek();
156 listener.getParsedDataStack().push(leafListData);
157
158 // Verify parent node of leaf
159 if (!(parentNodeOfLeafList instanceof YangNode)) {
160 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
161 ctx.string().getText(), EXIT));
162 }
163
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530164 // Create empty derived info and attach it to type extended info.
165 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
166 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
167
168 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530169 YangResolutionInfoImpl resolutionInfo =
170 new YangResolutionInfoImpl<YangType>(type, (YangNode) parentNodeOfLeafList, errorLine,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530171 errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530172 addToResolutionList(resolutionInfo, ctx);
173 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530174 break;
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530175 case UNION_DATA:
176 YangUnion unionNode = (YangUnion) tmpData;
177 try {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530178 unionNode.addType(type);
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530179 } catch (DataModelException e) {
180 ParserException parserException = new ParserException(e.getMessage());
181 parserException.setLine(ctx.getStart().getLine());
182 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
183 throw parserException;
184 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530185
186 /*
187 * If data type is derived, resolution information to be added
188 * in resolution list.
189 */
190 if (yangDataTypes == YangDataTypes.DERIVED) {
191
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530192 // Create empty derived info and attach it to type extended info.
193 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
194 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
195
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530196 type.setResolvableStatus(UNRESOLVED);
197
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530198 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530199 YangResolutionInfoImpl resolutionInfo =
200 new YangResolutionInfoImpl<YangType>(type, unionNode, errorLine, errorPosition);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530201 addToResolutionList(resolutionInfo, ctx);
202 }
203
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530204 break;
Vinod Kumar S71cba682016-02-25 15:52:16 +0530205 case TYPEDEF_DATA:
Vinod Kumar S71cba682016-02-25 15:52:16 +0530206 /* Prepare the base type info and set in derived type */
207 YangTypeDef typeDef = (YangTypeDef) tmpData;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530208 typeDef.setDataType(type);
Vinod Kumar S71cba682016-02-25 15:52:16 +0530209
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530210 /*
211 * If data type is derived, resolution information to be added
212 * in resolution list.
213 */
214 if (yangDataTypes == YangDataTypes.DERIVED) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530215 // Create empty derived info and attach it to type extended info.
216 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
217 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
218
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530219 type.setResolvableStatus(UNRESOLVED);
220
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530221 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530222 YangResolutionInfoImpl resolutionInfo =
223 new YangResolutionInfoImpl<YangType>(type, typeDef, errorLine, errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530224 addToResolutionList(resolutionInfo, ctx);
225 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530226 break;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530227 //TODO: deviate replacement statement.
Vinod Kumar S71cba682016-02-25 15:52:16 +0530228
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530229 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +0530230 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530231 ctx.string().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530232 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530233
234 // Push the type to the stack.
235 listener.getParsedDataStack().push(type);
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530236 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530237
238 /**
janani be18b5342016-07-13 21:06:41 +0530239 * Sets the default require instance value as true when the type is instance identifier.
240 *
241 * @param type type to which the value has to be set
242 */
243 private static void setDefaultRequireInstanceForInstanceIdentifier(YangType<?> type) {
244
245 if (type.getDataType() == YangDataTypes.INSTANCE_IDENTIFIER) {
246 ((YangType<Boolean>) type).setDataTypeExtendedInfo(true);
247 }
248 }
249
250 /**
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530251 * It is called when parser exits from grammar rule (type), it perform
252 * validations and update the data model tree.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530253 *
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530254 * @param listener Listener's object
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530255 * @param ctx context object of the grammar rule
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530256 */
257 public static void processTypeExit(TreeWalkListener listener,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530258 GeneratedYangParser.TypeStatementContext ctx) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530259
260 // Check for stack to be non empty.
261 checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
262
263 Parsable parsableType = listener.getParsedDataStack().pop();
264 if (!(parsableType instanceof YangType)) {
265 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
266 ctx.string().getText(), EXIT));
267 }
268 }
269
270 /**
271 * Adds to resolution list.
272 *
273 * @param resolutionInfo resolution information
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530274 * @param ctx context object of the grammar rule
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530275 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530276 private static void addToResolutionList(YangResolutionInfoImpl<YangType> resolutionInfo,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530277 GeneratedYangParser.TypeStatementContext ctx) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530278 try {
279 addResolutionInfo(resolutionInfo);
280 } catch (DataModelException e) {
281 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530282 TYPE_DATA, ctx.string().getText(), ENTRY, e.getMessage()));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530283 }
284 }
Vidyashree Rama528ef302016-06-30 14:31:18 +0530285
286 /**
287 * Validates type body statements cardinality.
288 *
289 * @param ctx context object of the grammar rule
290 * @param yangDataType yang data type
291 */
292 private static void validateTypeSubStatementCardinality(GeneratedYangParser.TypeStatementContext ctx,
293 YangDataTypes yangDataType) {
294 if (ctx.typeBodyStatements() == null || ctx.typeBodyStatements().isEmpty()) {
295 ParserException parserException;
296 switch (yangDataType) {
297 case UNION:
298 parserException = new ParserException("YANG file error : a type union" +
299 " must have atleast one type statement.");
300 break;
301 case ENUMERATION:
302 parserException = new ParserException("YANG file error : a type enumeration" +
303 " must have atleast one enum statement.");
304 break;
305 case BITS:
306 parserException = new ParserException("YANG file error : a type bits" +
307 " must have atleast one bit statement.");
308 break;
janani be18b5342016-07-13 21:06:41 +0530309 case LEAFREF:
310 parserException = new ParserException("YANG file error : a type leafref" +
311 " must have one path statement.");
312 break;
313 // TODO : decimal64, identity ref
Vidyashree Rama528ef302016-06-30 14:31:18 +0530314 default:
315 return;
316 }
317 parserException.setLine(ctx.getStart().getLine());
318 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
319 throw parserException;
320 }
321 }
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530322}