blob: 3c7fba959622f4ff0084db6c1d2aa2f7598898ba [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
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053097 // validate type sub-statement cardinality
Vidyashree Rama528ef302016-06-30 14:31:18 +053098 validateTypeSubStatementCardinality(ctx, yangDataTypes);
99
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530100 // Create YANG type object and fill the values.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530101 YangType<?> type = getYangType(JAVA_GENERATION);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530102 type.setNodeIdentifier(nodeIdentifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530103 type.setDataType(yangDataTypes);
104
janani be18b5342016-07-13 21:06:41 +0530105 // Set default require instance value as true for instance identifier.
106 setDefaultRequireInstanceForInstanceIdentifier(type);
107
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530108 int errorLine = ctx.getStart().getLine();
109 int errorPosition = ctx.getStart().getCharPositionInLine();
110
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530111 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530112 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530113 case LEAF_DATA:
114 YangLeaf leaf = (YangLeaf) tmpData;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530115 leaf.setDataType(type);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530116
117 /*
118 * If data type is derived, resolution information to be added
119 * in resolution list.
120 */
121 if (yangDataTypes == YangDataTypes.DERIVED) {
122 // Parent YANG node of leaf to be added in resolution information.
123 Parsable leafData = listener.getParsedDataStack().pop();
124 Parsable parentNodeOfLeaf = listener.getParsedDataStack().peek();
125 listener.getParsedDataStack().push(leafData);
126
127 // Verify parent node of leaf
128 if (!(parentNodeOfLeaf instanceof YangNode)) {
129 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
130 ctx.string().getText(), EXIT));
131 }
132
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530133 // Create empty derived info and attach it to type extended info.
134 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
135 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
136
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530137 type.setResolvableStatus(UNRESOLVED);
138
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530139 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530140 YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangType>(type,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530141 (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530142 addToResolutionList(resolutionInfo, ctx);
143 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530144 break;
145 case LEAF_LIST_DATA:
146 YangLeafList leafList = (YangLeafList) tmpData;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530147 leafList.setDataType(type);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530148
149 /*
150 * If data type is derived, resolution information to be added
151 * in resolution list.
152 */
153 if (yangDataTypes == YangDataTypes.DERIVED) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530154 // Parent YANG node of leaf list to be added in resolution information.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530155 Parsable leafListData = listener.getParsedDataStack().pop();
156 Parsable parentNodeOfLeafList = listener.getParsedDataStack().peek();
157 listener.getParsedDataStack().push(leafListData);
158
159 // Verify parent node of leaf
160 if (!(parentNodeOfLeafList instanceof YangNode)) {
161 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
162 ctx.string().getText(), EXIT));
163 }
164
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530165 // Create empty derived info and attach it to type extended info.
166 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
167 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
168
169 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530170 YangResolutionInfoImpl resolutionInfo =
171 new YangResolutionInfoImpl<YangType>(type, (YangNode) parentNodeOfLeafList, errorLine,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530172 errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530173 addToResolutionList(resolutionInfo, ctx);
174 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530175 break;
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530176 case UNION_DATA:
177 YangUnion unionNode = (YangUnion) tmpData;
178 try {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530179 unionNode.addType(type);
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530180 } catch (DataModelException e) {
181 ParserException parserException = new ParserException(e.getMessage());
182 parserException.setLine(ctx.getStart().getLine());
183 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
184 throw parserException;
185 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530186
187 /*
188 * If data type is derived, resolution information to be added
189 * in resolution list.
190 */
191 if (yangDataTypes == YangDataTypes.DERIVED) {
192
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530193 // Create empty derived info and attach it to type extended info.
194 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
195 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
196
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530197 type.setResolvableStatus(UNRESOLVED);
198
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530199 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530200 YangResolutionInfoImpl resolutionInfo =
201 new YangResolutionInfoImpl<YangType>(type, unionNode, errorLine, errorPosition);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530202 addToResolutionList(resolutionInfo, ctx);
203 }
204
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530205 break;
Vinod Kumar S71cba682016-02-25 15:52:16 +0530206 case TYPEDEF_DATA:
Vinod Kumar S71cba682016-02-25 15:52:16 +0530207 /* Prepare the base type info and set in derived type */
208 YangTypeDef typeDef = (YangTypeDef) tmpData;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530209 typeDef.setDataType(type);
Vinod Kumar S71cba682016-02-25 15:52:16 +0530210
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530211 /*
212 * If data type is derived, resolution information to be added
213 * in resolution list.
214 */
215 if (yangDataTypes == YangDataTypes.DERIVED) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530216 // Create empty derived info and attach it to type extended info.
217 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
218 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
219
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530220 type.setResolvableStatus(UNRESOLVED);
221
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530222 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530223 YangResolutionInfoImpl resolutionInfo =
224 new YangResolutionInfoImpl<YangType>(type, typeDef, errorLine, errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530225 addToResolutionList(resolutionInfo, ctx);
226 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530227 break;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530228 //TODO: deviate replacement statement.
Vinod Kumar S71cba682016-02-25 15:52:16 +0530229
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530230 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +0530231 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530232 ctx.string().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530233 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530234
235 // Push the type to the stack.
236 listener.getParsedDataStack().push(type);
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530237 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530238
239 /**
janani be18b5342016-07-13 21:06:41 +0530240 * Sets the default require instance value as true when the type is instance identifier.
241 *
242 * @param type type to which the value has to be set
243 */
244 private static void setDefaultRequireInstanceForInstanceIdentifier(YangType<?> type) {
245
246 if (type.getDataType() == YangDataTypes.INSTANCE_IDENTIFIER) {
247 ((YangType<Boolean>) type).setDataTypeExtendedInfo(true);
248 }
249 }
250
251 /**
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530252 * It is called when parser exits from grammar rule (type), it perform
253 * validations and update the data model tree.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530254 *
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530255 * @param listener Listener's object
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530256 * @param ctx context object of the grammar rule
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530257 */
258 public static void processTypeExit(TreeWalkListener listener,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530259 GeneratedYangParser.TypeStatementContext ctx) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530260
261 // Check for stack to be non empty.
262 checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
263
264 Parsable parsableType = listener.getParsedDataStack().pop();
265 if (!(parsableType instanceof YangType)) {
266 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
267 ctx.string().getText(), EXIT));
268 }
269 }
270
271 /**
272 * Adds to resolution list.
273 *
274 * @param resolutionInfo resolution information
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530275 * @param ctx context object of the grammar rule
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530276 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530277 private static void addToResolutionList(YangResolutionInfoImpl<YangType> resolutionInfo,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530278 GeneratedYangParser.TypeStatementContext ctx) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530279 try {
280 addResolutionInfo(resolutionInfo);
281 } catch (DataModelException e) {
282 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530283 TYPE_DATA, ctx.string().getText(), ENTRY, e.getMessage()));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530284 }
285 }
Vidyashree Rama528ef302016-06-30 14:31:18 +0530286
287 /**
288 * Validates type body statements cardinality.
289 *
290 * @param ctx context object of the grammar rule
291 * @param yangDataType yang data type
292 */
293 private static void validateTypeSubStatementCardinality(GeneratedYangParser.TypeStatementContext ctx,
294 YangDataTypes yangDataType) {
295 if (ctx.typeBodyStatements() == null || ctx.typeBodyStatements().isEmpty()) {
296 ParserException parserException;
297 switch (yangDataType) {
298 case UNION:
299 parserException = new ParserException("YANG file error : a type union" +
300 " must have atleast one type statement.");
301 break;
302 case ENUMERATION:
303 parserException = new ParserException("YANG file error : a type enumeration" +
304 " must have atleast one enum statement.");
305 break;
306 case BITS:
307 parserException = new ParserException("YANG file error : a type bits" +
308 " must have atleast one bit statement.");
309 break;
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530310 case DECIMAL64:
311 parserException = new ParserException("YANG file error : a type decimal64" +
312 " must have fraction-digits statement.");
313 break;
janani be18b5342016-07-13 21:06:41 +0530314 case LEAFREF:
315 parserException = new ParserException("YANG file error : a type leafref" +
316 " must have one path statement.");
317 break;
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530318 case IDENTITYREF:
319 parserException = new ParserException("YANG file error : a type identityref" +
320 " must have base statement.");
321 break;
Vidyashree Rama528ef302016-06-30 14:31:18 +0530322 default:
323 return;
324 }
325 parserException.setLine(ctx.getStart().getLine());
326 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
327 throw parserException;
328 }
329 }
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530330}