blob: 416ce9157d97f23d2efbb30805ab8edce99ea935 [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
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053019import org.onosproject.yangutils.datamodel.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;
25import org.onosproject.yangutils.datamodel.YangResolutionInfo;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053026import org.onosproject.yangutils.datamodel.YangType;
Vinod Kumar S71cba682016-02-25 15:52:16 +053027import org.onosproject.yangutils.datamodel.YangTypeDef;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053028import org.onosproject.yangutils.datamodel.YangUnion;
29import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053030import org.onosproject.yangutils.parser.Parsable;
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;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053034import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar S71cba682016-02-25 15:52:16 +053035
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053036import static org.onosproject.yangutils.datamodel.ResolvableStatus.UNRESOLVED;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053037import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
Vidyashree Rama59071f32016-02-20 19:27:56 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
41 .constructExtendedListenerErrorMessage;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
43 .constructListenerErrorMessage;
Vidyashree Rama59071f32016-02-20 19:27:56 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053045import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053046import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053047import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
48import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier;
Vidyashree Rama59071f32016-02-20 19:27:56 +053049import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053050import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
Vidyashree Rama59071f32016-02-20 19:27:56 +053051
Vidyashree Rama92fc5562016-02-12 18:44:12 +053052/*
53 * Reference: RFC6020 and YANG ANTLR Grammar
54 *
55 * ABNF grammar as per RFC6020
56 * type-stmt = type-keyword sep identifier-ref-arg-str optsep
57 * (";" /
58 * "{" stmtsep
59 * type-body-stmts
60 * "}")
61 *
62 * ANTLR grammar rule
63 * typeStatement : TYPE_KEYWORD string (STMTEND | LEFT_CURLY_BRACE typeBodyStatements RIGHT_CURLY_BRACE);
64 */
65
66/**
Bharat saraswald9822e92016-04-05 15:13:44 +053067 * Represents listener based call back function corresponding to the "type" rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053068 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053069 */
70public final class TypeListener {
71
72 /**
73 * Creates a new type listener.
74 */
75 private TypeListener() {
76 }
77
78 /**
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053079 * It is called when parser receives an input matching the grammar rule
80 * (type), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053081 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053082 * @param listener listener's object
83 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053084 */
85 public static void processTypeEntry(TreeWalkListener listener,
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053086 GeneratedYangParser.TypeStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053087
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053088 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053089 checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053090
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053091 // Validate node identifier.
92 YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), YangConstructType.TYPE_DATA,
93 ctx);
Vinod Kumar S71cba682016-02-25 15:52:16 +053094
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053095 // Obtain the YANG data type.
96 YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText());
97
98 // Create YANG type object and fill the values.
99 YangType<?> type = new YangType();
100 type.setNodeIdentifier(nodeIdentifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530101 type.setDataType(yangDataTypes);
102
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530103 int errorLine = ctx.getStart().getLine();
104 int errorPosition = ctx.getStart().getCharPositionInLine();
105
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530106 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530107 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530108 case LEAF_DATA:
109 YangLeaf leaf = (YangLeaf) tmpData;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530110 leaf.setDataType((YangType<?>) type);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530111
112 /*
113 * If data type is derived, resolution information to be added
114 * in resolution list.
115 */
116 if (yangDataTypes == YangDataTypes.DERIVED) {
117 // Parent YANG node of leaf to be added in resolution information.
118 Parsable leafData = listener.getParsedDataStack().pop();
119 Parsable parentNodeOfLeaf = listener.getParsedDataStack().peek();
120 listener.getParsedDataStack().push(leafData);
121
122 // Verify parent node of leaf
123 if (!(parentNodeOfLeaf instanceof YangNode)) {
124 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
125 ctx.string().getText(), EXIT));
126 }
127
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530128 // Create empty derived info and attach it to type extended info.
129 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
130 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
131
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530132 type.setResolvableStatus(UNRESOLVED);
133
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530134 // Add resolution information to the list
135 YangResolutionInfo resolutionInfo = new YangResolutionInfo<YangType>(type,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530136 (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530137 addToResolutionList(resolutionInfo, ctx);
138 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530139 break;
140 case LEAF_LIST_DATA:
141 YangLeafList leafList = (YangLeafList) tmpData;
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530142 leafList.setDataType((YangType<?>) type);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530143
144 /*
145 * If data type is derived, resolution information to be added
146 * in resolution list.
147 */
148 if (yangDataTypes == YangDataTypes.DERIVED) {
149 // Parent YANG node of leaf to be added in resolution information.
150 Parsable leafListData = listener.getParsedDataStack().pop();
151 Parsable parentNodeOfLeafList = listener.getParsedDataStack().peek();
152 listener.getParsedDataStack().push(leafListData);
153
154 // Verify parent node of leaf
155 if (!(parentNodeOfLeafList instanceof YangNode)) {
156 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
157 ctx.string().getText(), EXIT));
158 }
159
160 // Get the prefix information
161 String prefix = ((YangType<?>) type).getPrefix();
162
163 // Create empty derived info and attach it to type extended info.
164 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
165 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
166
167 // Add resolution information to the list
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530168 YangResolutionInfo resolutionInfo =
169 new YangResolutionInfo<YangType>(type, (YangNode) parentNodeOfLeafList, errorLine,
170 errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530171 addToResolutionList(resolutionInfo, ctx);
172 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530173 break;
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530174 case UNION_DATA:
175 YangUnion unionNode = (YangUnion) tmpData;
176 try {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530177 unionNode.addType((YangType<?>) type);
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530178 } catch (DataModelException e) {
179 ParserException parserException = new ParserException(e.getMessage());
180 parserException.setLine(ctx.getStart().getLine());
181 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
182 throw parserException;
183 }
184 break;
Vinod Kumar S71cba682016-02-25 15:52:16 +0530185 case TYPEDEF_DATA:
Vinod Kumar S71cba682016-02-25 15:52:16 +0530186 /* Prepare the base type info and set in derived type */
187 YangTypeDef typeDef = (YangTypeDef) tmpData;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530188 typeDef.setDataType((YangType<?>) type);
Vinod Kumar S71cba682016-02-25 15:52:16 +0530189
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530190 /*
191 * If data type is derived, resolution information to be added
192 * in resolution list.
193 */
194 if (yangDataTypes == YangDataTypes.DERIVED) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530195
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530196 // Get the prefix information
197 String prefix = ((YangType<?>) type).getPrefix();
198
199 // Create empty derived info and attach it to type extended info.
200 YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
201 ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
202
203 // Add resolution information to the list
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530204 YangResolutionInfo resolutionInfo =
205 new YangResolutionInfo<YangType>(type, (YangNode) typeDef, errorLine, errorPosition);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530206 addToResolutionList(resolutionInfo, ctx);
207 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530208 break;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530209 //TODO: deviate replacement statement.case TYPEDEF_DATA: //TODO
Vinod Kumar S71cba682016-02-25 15:52:16 +0530210
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530211 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +0530212 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530213 ctx.string().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530214 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530215
216 // Push the type to the stack.
217 listener.getParsedDataStack().push(type);
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530218 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530219
220 /**
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530221 * It is called when parser exits from grammar rule (type), it perform
222 * validations and update the data model tree.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530223 *
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530224 * @param listener Listener's object
225 * @param ctx context object of the grammar rule
226 */
227 public static void processTypeExit(TreeWalkListener listener,
228 GeneratedYangParser.TypeStatementContext ctx) {
229
230 // Check for stack to be non empty.
231 checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT);
232
233 Parsable parsableType = listener.getParsedDataStack().pop();
234 if (!(parsableType instanceof YangType)) {
235 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
236 ctx.string().getText(), EXIT));
237 }
238 }
239
240 /**
241 * Adds to resolution list.
242 *
243 * @param resolutionInfo resolution information
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530244 * @param ctx context object of the grammar rule
245 */
246 private static void addToResolutionList(YangResolutionInfo<YangType> resolutionInfo,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530247 GeneratedYangParser.TypeStatementContext ctx) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530248 try {
249 addResolutionInfo(resolutionInfo);
250 } catch (DataModelException e) {
251 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
252 TYPE_DATA, ctx.string().getText(), EXIT, e.getMessage()));
253 }
254 }
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530255}