blob: 9fab09c113d02e4cc7e49568ce85493f2a86c8c7 [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 Agrawal72cd1b72016-06-30 13:28:14 +053019import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053020import org.onosproject.yangutils.datamodel.YangDerivedInfo;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053021import org.onosproject.yangutils.datamodel.YangLeaf;
22import org.onosproject.yangutils.datamodel.YangLeafList;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053023import org.onosproject.yangutils.datamodel.YangNode;
24import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053025import org.onosproject.yangutils.datamodel.YangType;
Vinod Kumar S71cba682016-02-25 15:52:16 +053026import org.onosproject.yangutils.datamodel.YangTypeDef;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053027import org.onosproject.yangutils.datamodel.YangUnion;
28import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053029import org.onosproject.yangutils.datamodel.utils.Parsable;
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
97 // Create YANG type object and fill the values.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053098 YangType<?> type = getYangType(JAVA_GENERATION);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053099 type.setNodeIdentifier(nodeIdentifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530100 type.setDataType(yangDataTypes);
101
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530102 int errorLine = ctx.getStart().getLine();
103 int errorPosition = ctx.getStart().getCharPositionInLine();
104
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530105 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530106 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530107 case LEAF_DATA:
108 YangLeaf leaf = (YangLeaf) tmpData;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530109 leaf.setDataType(type);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530110
111 /*
112 * If data type is derived, resolution information to be added
113 * in resolution list.
114 */
115 if (yangDataTypes == YangDataTypes.DERIVED) {
116 // Parent YANG node of leaf to be added in resolution information.
117 Parsable leafData = listener.getParsedDataStack().pop();
118 Parsable parentNodeOfLeaf = listener.getParsedDataStack().peek();
119 listener.getParsedDataStack().push(leafData);
120
121 // Verify parent node of leaf
122 if (!(parentNodeOfLeaf instanceof YangNode)) {
123 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
124 ctx.string().getText(), EXIT));
125 }
126
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530127 // Create empty derived info and attach it to type extended info.
128 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
129 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
130
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530131 type.setResolvableStatus(UNRESOLVED);
132
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530133 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530134 YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangType>(type,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530135 (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530136 addToResolutionList(resolutionInfo, ctx);
137 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530138 break;
139 case LEAF_LIST_DATA:
140 YangLeafList leafList = (YangLeafList) tmpData;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530141 leafList.setDataType(type);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530142
143 /*
144 * If data type is derived, resolution information to be added
145 * in resolution list.
146 */
147 if (yangDataTypes == YangDataTypes.DERIVED) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530148 // Parent YANG node of leaf list to be added in resolution information.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530149 Parsable leafListData = listener.getParsedDataStack().pop();
150 Parsable parentNodeOfLeafList = listener.getParsedDataStack().peek();
151 listener.getParsedDataStack().push(leafListData);
152
153 // Verify parent node of leaf
154 if (!(parentNodeOfLeafList instanceof YangNode)) {
155 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
156 ctx.string().getText(), EXIT));
157 }
158
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530159 // Create empty derived info and attach it to type extended info.
160 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
161 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
162
163 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530164 YangResolutionInfoImpl resolutionInfo =
165 new YangResolutionInfoImpl<YangType>(type, (YangNode) parentNodeOfLeafList, errorLine,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530166 errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530167 addToResolutionList(resolutionInfo, ctx);
168 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530169 break;
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530170 case UNION_DATA:
171 YangUnion unionNode = (YangUnion) tmpData;
172 try {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530173 unionNode.addType(type);
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530174 } catch (DataModelException e) {
175 ParserException parserException = new ParserException(e.getMessage());
176 parserException.setLine(ctx.getStart().getLine());
177 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
178 throw parserException;
179 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530180
181 /*
182 * If data type is derived, resolution information to be added
183 * in resolution list.
184 */
185 if (yangDataTypes == YangDataTypes.DERIVED) {
186
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530187 // Create empty derived info and attach it to type extended info.
188 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
189 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
190
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530191 type.setResolvableStatus(UNRESOLVED);
192
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530193 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530194 YangResolutionInfoImpl resolutionInfo =
195 new YangResolutionInfoImpl<YangType>(type, unionNode, errorLine, errorPosition);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530196 addToResolutionList(resolutionInfo, ctx);
197 }
198
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530199 break;
Vinod Kumar S71cba682016-02-25 15:52:16 +0530200 case TYPEDEF_DATA:
Vinod Kumar S71cba682016-02-25 15:52:16 +0530201 /* Prepare the base type info and set in derived type */
202 YangTypeDef typeDef = (YangTypeDef) tmpData;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530203 typeDef.setDataType(type);
Vinod Kumar S71cba682016-02-25 15:52:16 +0530204
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530205 /*
206 * If data type is derived, resolution information to be added
207 * in resolution list.
208 */
209 if (yangDataTypes == YangDataTypes.DERIVED) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530210 // Create empty derived info and attach it to type extended info.
211 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
212 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
213
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530214 type.setResolvableStatus(UNRESOLVED);
215
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530216 // Add resolution information to the list
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530217 YangResolutionInfoImpl resolutionInfo =
218 new YangResolutionInfoImpl<YangType>(type, typeDef, errorLine, errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530219 addToResolutionList(resolutionInfo, ctx);
220 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530221 break;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530222 //TODO: deviate replacement statement.
Vinod Kumar S71cba682016-02-25 15:52:16 +0530223
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530224 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +0530225 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530226 ctx.string().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530227 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530228
229 // Push the type to the stack.
230 listener.getParsedDataStack().push(type);
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530231 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530232
233 /**
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530234 * It is called when parser exits from grammar rule (type), it perform
235 * validations and update the data model tree.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530236 *
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530237 * @param listener Listener's object
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530238 * @param ctx context object of the grammar rule
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530239 */
240 public static void processTypeExit(TreeWalkListener listener,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530241 GeneratedYangParser.TypeStatementContext ctx) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530242
243 // Check for stack to be non empty.
244 checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
245
246 Parsable parsableType = listener.getParsedDataStack().pop();
247 if (!(parsableType instanceof YangType)) {
248 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
249 ctx.string().getText(), EXIT));
250 }
251 }
252
253 /**
254 * Adds to resolution list.
255 *
256 * @param resolutionInfo resolution information
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530257 * @param ctx context object of the grammar rule
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530258 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530259 private static void addToResolutionList(YangResolutionInfoImpl<YangType> resolutionInfo,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530260 GeneratedYangParser.TypeStatementContext ctx) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530261 try {
262 addResolutionInfo(resolutionInfo);
263 } catch (DataModelException e) {
264 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530265 TYPE_DATA, ctx.string().getText(), ENTRY, e.getMessage()));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530266 }
267 }
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530268}