Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.yangutils.parser.impl.listeners; |
| 18 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.ResolutionType; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangDerivedInfo; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 23 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
| 26 | import org.onosproject.yangutils.datamodel.YangResolutionInfo; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangType; |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangUnion; |
| 30 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.parser.Parsable; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.utils.YangConstructType; |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 36 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 42 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
| 46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 49 | |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 50 | /* |
| 51 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 52 | * |
| 53 | * ABNF grammar as per RFC6020 |
| 54 | * type-stmt = type-keyword sep identifier-ref-arg-str optsep |
| 55 | * (";" / |
| 56 | * "{" stmtsep |
| 57 | * type-body-stmts |
| 58 | * "}") |
| 59 | * |
| 60 | * ANTLR grammar rule |
| 61 | * typeStatement : TYPE_KEYWORD string (STMTEND | LEFT_CURLY_BRACE typeBodyStatements RIGHT_CURLY_BRACE); |
| 62 | */ |
| 63 | |
| 64 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame^] | 65 | * Represents listener based call back function corresponding to the "type" rule |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 66 | * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 67 | */ |
| 68 | public final class TypeListener { |
| 69 | |
| 70 | /** |
| 71 | * Creates a new type listener. |
| 72 | */ |
| 73 | private TypeListener() { |
| 74 | } |
| 75 | |
| 76 | /** |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 77 | * It is called when parser receives an input matching the grammar rule |
| 78 | * (type), performs validation and updates the data model tree. |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 79 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 80 | * @param listener listener's object |
| 81 | * @param ctx context object of the grammar rule |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 82 | */ |
| 83 | public static void processTypeEntry(TreeWalkListener listener, |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 84 | GeneratedYangParser.TypeStatementContext ctx) { |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 85 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 86 | // Check for stack to be non empty. |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 87 | checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 88 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 89 | // Validate node identifier. |
| 90 | YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), YangConstructType.TYPE_DATA, |
| 91 | ctx); |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 92 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 93 | // Obtain the YANG data type. |
| 94 | YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText()); |
| 95 | |
| 96 | // Create YANG type object and fill the values. |
| 97 | YangType<?> type = new YangType(); |
| 98 | type.setNodeIdentifier(nodeIdentifier); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 99 | type.setDataType(yangDataTypes); |
| 100 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 101 | // Push the type to the stack. |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 102 | listener.getParsedDataStack().push(type); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * It is called when parser exits from grammar rule (type), it perform |
| 107 | * validations and update the data model tree. |
| 108 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 109 | * @param listener Listener's object |
| 110 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 111 | */ |
| 112 | public static void processTypeExit(TreeWalkListener listener, |
| 113 | GeneratedYangParser.TypeStatementContext ctx) { |
| 114 | |
| 115 | // Check for stack to be non empty. |
| 116 | checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT); |
| 117 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 118 | Parsable parsableType = listener.getParsedDataStack().pop(); |
| 119 | if (!(parsableType instanceof YangType)) { |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 120 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
| 121 | ctx.string().getText(), EXIT)); |
| 122 | } |
| 123 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 124 | YangType<?> type = (YangType<?>) parsableType; |
| 125 | |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 126 | // Check for stack to be non empty. |
| 127 | checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT); |
| 128 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 129 | YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText()); |
| 130 | |
| 131 | int errorLine = ctx.getStart().getLine(); |
| 132 | int errorPosition = ctx.getStart().getCharPositionInLine(); |
| 133 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 134 | Parsable tmpData = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 135 | switch (tmpData.getYangConstructType()) { |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 136 | case LEAF_DATA: |
| 137 | YangLeaf leaf = (YangLeaf) tmpData; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 138 | leaf.setDataType((YangType<?>) type); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * If data type is derived, resolution information to be added |
| 142 | * in resolution list. |
| 143 | */ |
| 144 | if (yangDataTypes == YangDataTypes.DERIVED) { |
| 145 | // Parent YANG node of leaf to be added in resolution information. |
| 146 | Parsable leafData = listener.getParsedDataStack().pop(); |
| 147 | Parsable parentNodeOfLeaf = listener.getParsedDataStack().peek(); |
| 148 | listener.getParsedDataStack().push(leafData); |
| 149 | |
| 150 | // Verify parent node of leaf |
| 151 | if (!(parentNodeOfLeaf instanceof YangNode)) { |
| 152 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
| 153 | ctx.string().getText(), EXIT)); |
| 154 | } |
| 155 | |
| 156 | // Get the prefix information |
| 157 | String prefix = ((YangType<?>) type).getPrefix(); |
| 158 | |
| 159 | // 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 |
| 164 | YangResolutionInfo resolutionInfo = new YangResolutionInfo<YangType>(type, |
| 165 | ResolutionType.TYPEDEF_RESOLUTION, (YangNode) parentNodeOfLeaf, prefix, errorLine, |
| 166 | errorPosition); |
| 167 | addToResolutionList(resolutionInfo, ctx); |
| 168 | } |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 169 | break; |
| 170 | case LEAF_LIST_DATA: |
| 171 | YangLeafList leafList = (YangLeafList) tmpData; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 172 | leafList.setDataType((YangType<?>) type); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * If data type is derived, resolution information to be added |
| 176 | * in resolution list. |
| 177 | */ |
| 178 | if (yangDataTypes == YangDataTypes.DERIVED) { |
| 179 | // Parent YANG node of leaf to be added in resolution information. |
| 180 | Parsable leafListData = listener.getParsedDataStack().pop(); |
| 181 | Parsable parentNodeOfLeafList = listener.getParsedDataStack().peek(); |
| 182 | listener.getParsedDataStack().push(leafListData); |
| 183 | |
| 184 | // Verify parent node of leaf |
| 185 | if (!(parentNodeOfLeafList instanceof YangNode)) { |
| 186 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
| 187 | ctx.string().getText(), EXIT)); |
| 188 | } |
| 189 | |
| 190 | // Get the prefix information |
| 191 | String prefix = ((YangType<?>) type).getPrefix(); |
| 192 | |
| 193 | // Create empty derived info and attach it to type extended info. |
| 194 | YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>(); |
| 195 | ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo); |
| 196 | |
| 197 | // Add resolution information to the list |
| 198 | YangResolutionInfo resolutionInfo = new YangResolutionInfo<YangType>(type, |
| 199 | ResolutionType.TYPEDEF_RESOLUTION, (YangNode) parentNodeOfLeafList, prefix, errorLine, |
| 200 | errorPosition); |
| 201 | addToResolutionList(resolutionInfo, ctx); |
| 202 | } |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 203 | break; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 204 | case UNION_DATA: |
| 205 | YangUnion unionNode = (YangUnion) tmpData; |
| 206 | try { |
| 207 | unionNode.addToTypeList((YangType<?>) type); |
| 208 | } catch (DataModelException e) { |
| 209 | ParserException parserException = new ParserException(e.getMessage()); |
| 210 | parserException.setLine(ctx.getStart().getLine()); |
| 211 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 212 | throw parserException; |
| 213 | } |
| 214 | break; |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 215 | case TYPEDEF_DATA: |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 216 | /* Prepare the base type info and set in derived type */ |
| 217 | YangTypeDef typeDef = (YangTypeDef) tmpData; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 218 | typeDef.setDataType((YangType<?>) type); |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 219 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 220 | /* |
| 221 | * If data type is derived, resolution information to be added |
| 222 | * in resolution list. |
| 223 | */ |
| 224 | if (yangDataTypes == YangDataTypes.DERIVED) { |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 225 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 226 | // Get the prefix information |
| 227 | String prefix = ((YangType<?>) type).getPrefix(); |
| 228 | |
| 229 | // Create empty derived info and attach it to type extended info. |
| 230 | YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>(); |
| 231 | ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo); |
| 232 | |
| 233 | // Add resolution information to the list |
| 234 | YangResolutionInfo resolutionInfo = new YangResolutionInfo<YangType>(type, |
| 235 | ResolutionType.TYPEDEF_RESOLUTION, (YangNode) typeDef, prefix, errorLine, errorPosition); |
| 236 | addToResolutionList(resolutionInfo, ctx); |
| 237 | } |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 238 | break; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 239 | //TODO: deviate replacement statement.case TYPEDEF_DATA: //TODO |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 240 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 241 | default: |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 242 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 243 | ctx.string().getText(), EXIT)); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 244 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 245 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 246 | |
| 247 | /** |
| 248 | * Add to resolution list. |
| 249 | * |
| 250 | * @param resolutionInfo resolution information. |
| 251 | * @param ctx context object of the grammar rule |
| 252 | */ |
| 253 | private static void addToResolutionList(YangResolutionInfo<YangType> resolutionInfo, |
| 254 | GeneratedYangParser.TypeStatementContext ctx) { |
| 255 | try { |
| 256 | addResolutionInfo(resolutionInfo); |
| 257 | } catch (DataModelException e) { |
| 258 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 259 | TYPE_DATA, ctx.string().getText(), EXIT, e.getMessage())); |
| 260 | } |
| 261 | } |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 262 | } |