Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 92fc556 | 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 | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangDerivedInfo; |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 21 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangNode; |
| 23 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangType; |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
Gaurav Agrawal | bd80447 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangUnion; |
| 27 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Vidyashree Rama | 528ef30 | 2016-06-30 14:31:18 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 34 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.UNRESOLVED; |
| 38 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.TYPE_DATA; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 42 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
| 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangType; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 50 | |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 51 | /* |
| 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 saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 66 | * Represents listener based call back function corresponding to the "type" rule |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 67 | * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 68 | */ |
| 69 | public final class TypeListener { |
| 70 | |
| 71 | /** |
| 72 | * Creates a new type listener. |
| 73 | */ |
| 74 | private TypeListener() { |
| 75 | } |
| 76 | |
| 77 | /** |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 78 | * It is called when parser receives an input matching the grammar rule |
| 79 | * (type), performs validation and updates the data model tree. |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 80 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 81 | * @param listener listener's object |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 82 | * @param ctx context object of the grammar rule |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 83 | */ |
| 84 | public static void processTypeEntry(TreeWalkListener listener, |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 85 | GeneratedYangParser.TypeStatementContext ctx) { |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 86 | |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 87 | // Check for stack to be non empty. |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 88 | checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 89 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 90 | // Validate node identifier. |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 91 | YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), TYPE_DATA, |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 92 | ctx); |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 93 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 94 | // Obtain the YANG data type. |
| 95 | YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText()); |
| 96 | |
Vidyashree Rama | 528ef30 | 2016-06-30 14:31:18 +0530 | [diff] [blame] | 97 | validateTypeSubStatementCardinality(ctx, yangDataTypes); |
| 98 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 99 | // Create YANG type object and fill the values. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 100 | YangType<?> type = getYangType(JAVA_GENERATION); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 101 | type.setNodeIdentifier(nodeIdentifier); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 102 | type.setDataType(yangDataTypes); |
| 103 | |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame^] | 104 | // Set default require instance value as true for instance identifier. |
| 105 | setDefaultRequireInstanceForInstanceIdentifier(type); |
| 106 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 107 | int errorLine = ctx.getStart().getLine(); |
| 108 | int errorPosition = ctx.getStart().getCharPositionInLine(); |
| 109 | |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 110 | Parsable tmpData = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 111 | switch (tmpData.getYangConstructType()) { |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 112 | case LEAF_DATA: |
| 113 | YangLeaf leaf = (YangLeaf) tmpData; |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 114 | leaf.setDataType(type); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 115 | |
| 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 Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 132 | // 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 S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 136 | type.setResolvableStatus(UNRESOLVED); |
| 137 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 138 | // Add resolution information to the list |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 139 | YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangType>(type, |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 140 | (YangNode) parentNodeOfLeaf, errorLine, errorPosition); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 141 | addToResolutionList(resolutionInfo, ctx); |
| 142 | } |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 143 | break; |
| 144 | case LEAF_LIST_DATA: |
| 145 | YangLeafList leafList = (YangLeafList) tmpData; |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 146 | leafList.setDataType(type); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * If data type is derived, resolution information to be added |
| 150 | * in resolution list. |
| 151 | */ |
| 152 | if (yangDataTypes == YangDataTypes.DERIVED) { |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 153 | // Parent YANG node of leaf list to be added in resolution information. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 154 | 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 Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 164 | // 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 Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 169 | YangResolutionInfoImpl resolutionInfo = |
| 170 | new YangResolutionInfoImpl<YangType>(type, (YangNode) parentNodeOfLeafList, errorLine, |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 171 | errorPosition); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 172 | addToResolutionList(resolutionInfo, ctx); |
| 173 | } |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 174 | break; |
Gaurav Agrawal | bd80447 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 175 | case UNION_DATA: |
| 176 | YangUnion unionNode = (YangUnion) tmpData; |
| 177 | try { |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 178 | unionNode.addType(type); |
Gaurav Agrawal | bd80447 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 179 | } 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 Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * If data type is derived, resolution information to be added |
| 188 | * in resolution list. |
| 189 | */ |
| 190 | if (yangDataTypes == YangDataTypes.DERIVED) { |
| 191 | |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 192 | // Create empty derived info and attach it to type extended info. |
| 193 | YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>(); |
| 194 | ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo); |
| 195 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 196 | type.setResolvableStatus(UNRESOLVED); |
| 197 | |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 198 | // Add resolution information to the list |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 199 | YangResolutionInfoImpl resolutionInfo = |
| 200 | new YangResolutionInfoImpl<YangType>(type, unionNode, errorLine, errorPosition); |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 201 | addToResolutionList(resolutionInfo, ctx); |
| 202 | } |
| 203 | |
Gaurav Agrawal | bd80447 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 204 | break; |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 205 | case TYPEDEF_DATA: |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 206 | /* Prepare the base type info and set in derived type */ |
| 207 | YangTypeDef typeDef = (YangTypeDef) tmpData; |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 208 | typeDef.setDataType(type); |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 209 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 210 | /* |
| 211 | * If data type is derived, resolution information to be added |
| 212 | * in resolution list. |
| 213 | */ |
| 214 | if (yangDataTypes == YangDataTypes.DERIVED) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 215 | // Create empty derived info and attach it to type extended info. |
| 216 | YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>(); |
| 217 | ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo); |
| 218 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 219 | type.setResolvableStatus(UNRESOLVED); |
| 220 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 221 | // Add resolution information to the list |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 222 | YangResolutionInfoImpl resolutionInfo = |
| 223 | new YangResolutionInfoImpl<YangType>(type, typeDef, errorLine, errorPosition); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 224 | addToResolutionList(resolutionInfo, ctx); |
| 225 | } |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 226 | break; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 227 | //TODO: deviate replacement statement. |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 228 | |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 229 | default: |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 230 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 231 | ctx.string().getText(), EXIT)); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 232 | } |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 233 | |
| 234 | // Push the type to the stack. |
| 235 | listener.getParsedDataStack().push(type); |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 236 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 237 | |
| 238 | /** |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame^] | 239 | * 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 Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 251 | * It is called when parser exits from grammar rule (type), it perform |
| 252 | * validations and update the data model tree. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 253 | * |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 254 | * @param listener Listener's object |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 255 | * @param ctx context object of the grammar rule |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 256 | */ |
| 257 | public static void processTypeExit(TreeWalkListener listener, |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 258 | GeneratedYangParser.TypeStatementContext ctx) { |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 259 | |
| 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 Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 274 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 275 | */ |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 276 | private static void addToResolutionList(YangResolutionInfoImpl<YangType> resolutionInfo, |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 277 | GeneratedYangParser.TypeStatementContext ctx) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 278 | try { |
| 279 | addResolutionInfo(resolutionInfo); |
| 280 | } catch (DataModelException e) { |
| 281 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 282 | TYPE_DATA, ctx.string().getText(), ENTRY, e.getMessage())); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 283 | } |
| 284 | } |
Vidyashree Rama | 528ef30 | 2016-06-30 14:31:18 +0530 | [diff] [blame] | 285 | |
| 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 b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame^] | 309 | 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 Rama | 528ef30 | 2016-06-30 14:31:18 +0530 | [diff] [blame] | 314 | default: |
| 315 | return; |
| 316 | } |
| 317 | parserException.setLine(ctx.getStart().getLine()); |
| 318 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 319 | throw parserException; |
| 320 | } |
| 321 | } |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 322 | } |