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.YangDerivedInfo; |
Vidyashree Rama | b24ca90 | 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 | 1fbfae1 | 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 | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangType; |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
Gaurav Agrawal | f6ccc97 | 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 | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Vidyashree Rama | 84e4ba7 | 2016-06-30 14:31:18 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 34 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
Bharat saraswal | c2d3be1 | 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 | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame^] | 41 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction |
| 42 | .constructExtendedListenerErrorMessage; |
| 43 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction |
| 44 | .constructListenerErrorMessage; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
| 49 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangType; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 52 | |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 53 | /* |
| 54 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 55 | * |
| 56 | * ABNF grammar as per RFC6020 |
| 57 | * type-stmt = type-keyword sep identifier-ref-arg-str optsep |
| 58 | * (";" / |
| 59 | * "{" stmtsep |
| 60 | * type-body-stmts |
| 61 | * "}") |
| 62 | * |
| 63 | * ANTLR grammar rule |
| 64 | * typeStatement : TYPE_KEYWORD string (STMTEND | LEFT_CURLY_BRACE typeBodyStatements RIGHT_CURLY_BRACE); |
| 65 | */ |
| 66 | |
| 67 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 68 | * Represents listener based call back function corresponding to the "type" rule |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 69 | * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 70 | */ |
| 71 | public final class TypeListener { |
| 72 | |
| 73 | /** |
| 74 | * Creates a new type listener. |
| 75 | */ |
| 76 | private TypeListener() { |
| 77 | } |
| 78 | |
| 79 | /** |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 80 | * It is called when parser receives an input matching the grammar rule |
| 81 | * (type), performs validation and updates the data model tree. |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 82 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 83 | * @param listener listener's object |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 84 | * @param ctx context object of the grammar rule |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 85 | */ |
| 86 | public static void processTypeEntry(TreeWalkListener listener, |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame^] | 87 | GeneratedYangParser.TypeStatementContext ctx) { |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 88 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 89 | // Check for stack to be non empty. |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 90 | checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 91 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 92 | // Validate node identifier. |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 93 | YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), TYPE_DATA, |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 94 | ctx); |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 95 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 96 | // Obtain the YANG data type. |
| 97 | YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText()); |
| 98 | |
Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 99 | // validate type sub-statement cardinality |
Vidyashree Rama | 84e4ba7 | 2016-06-30 14:31:18 +0530 | [diff] [blame] | 100 | validateTypeSubStatementCardinality(ctx, yangDataTypes); |
| 101 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 102 | // Create YANG type object and fill the values. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 103 | YangType<?> type = getYangType(JAVA_GENERATION); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 104 | type.setNodeIdentifier(nodeIdentifier); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 105 | type.setDataType(yangDataTypes); |
| 106 | |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 107 | // Set default require instance value as true for instance identifier. |
| 108 | setDefaultRequireInstanceForInstanceIdentifier(type); |
| 109 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 110 | int errorLine = ctx.getStart().getLine(); |
| 111 | int errorPosition = ctx.getStart().getCharPositionInLine(); |
| 112 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 113 | Parsable tmpData = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 114 | switch (tmpData.getYangConstructType()) { |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 115 | case LEAF_DATA: |
| 116 | YangLeaf leaf = (YangLeaf) tmpData; |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 117 | leaf.setDataType(type); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * If data type is derived, resolution information to be added |
| 121 | * in resolution list. |
| 122 | */ |
| 123 | if (yangDataTypes == YangDataTypes.DERIVED) { |
| 124 | // Parent YANG node of leaf to be added in resolution information. |
| 125 | Parsable leafData = listener.getParsedDataStack().pop(); |
| 126 | Parsable parentNodeOfLeaf = listener.getParsedDataStack().peek(); |
| 127 | listener.getParsedDataStack().push(leafData); |
| 128 | |
| 129 | // Verify parent node of leaf |
| 130 | if (!(parentNodeOfLeaf instanceof YangNode)) { |
| 131 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
| 132 | ctx.string().getText(), EXIT)); |
| 133 | } |
| 134 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 135 | // Create empty derived info and attach it to type extended info. |
| 136 | YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>(); |
| 137 | ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo); |
| 138 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 139 | type.setResolvableStatus(UNRESOLVED); |
| 140 | |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame^] | 141 | if (listener.getGroupingDepth() == 0) { |
| 142 | // Add resolution information to the list |
| 143 | YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangType>(type, |
| 144 | (YangNode) parentNodeOfLeaf, errorLine, errorPosition); |
| 145 | addToResolutionList(resolutionInfo, ctx); |
| 146 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 147 | } |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 148 | break; |
| 149 | case LEAF_LIST_DATA: |
| 150 | YangLeafList leafList = (YangLeafList) tmpData; |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 151 | leafList.setDataType(type); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * If data type is derived, resolution information to be added |
| 155 | * in resolution list. |
| 156 | */ |
| 157 | if (yangDataTypes == YangDataTypes.DERIVED) { |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 158 | // Parent YANG node of leaf list to be added in resolution information. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 159 | Parsable leafListData = listener.getParsedDataStack().pop(); |
| 160 | Parsable parentNodeOfLeafList = listener.getParsedDataStack().peek(); |
| 161 | listener.getParsedDataStack().push(leafListData); |
| 162 | |
| 163 | // Verify parent node of leaf |
| 164 | if (!(parentNodeOfLeafList instanceof YangNode)) { |
| 165 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
| 166 | ctx.string().getText(), EXIT)); |
| 167 | } |
| 168 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 169 | // Create empty derived info and attach it to type extended info. |
| 170 | YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>(); |
| 171 | ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo); |
| 172 | |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame^] | 173 | if (listener.getGroupingDepth() == 0) { |
| 174 | // Add resolution information to the list |
| 175 | YangResolutionInfoImpl resolutionInfo = |
| 176 | new YangResolutionInfoImpl<YangType>(type, (YangNode) parentNodeOfLeafList, errorLine, |
| 177 | errorPosition); |
| 178 | addToResolutionList(resolutionInfo, ctx); |
| 179 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 180 | } |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 181 | break; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 182 | case UNION_DATA: |
| 183 | YangUnion unionNode = (YangUnion) tmpData; |
| 184 | try { |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 185 | unionNode.addType(type); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 186 | } catch (DataModelException e) { |
| 187 | ParserException parserException = new ParserException(e.getMessage()); |
| 188 | parserException.setLine(ctx.getStart().getLine()); |
| 189 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 190 | throw parserException; |
| 191 | } |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * If data type is derived, resolution information to be added |
| 195 | * in resolution list. |
| 196 | */ |
| 197 | if (yangDataTypes == YangDataTypes.DERIVED) { |
| 198 | |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 199 | // Create empty derived info and attach it to type extended info. |
| 200 | YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>(); |
| 201 | ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo); |
| 202 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 203 | type.setResolvableStatus(UNRESOLVED); |
| 204 | |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame^] | 205 | if (listener.getGroupingDepth() == 0) { |
| 206 | // Add resolution information to the list |
| 207 | YangResolutionInfoImpl resolutionInfo = |
| 208 | new YangResolutionInfoImpl<YangType>(type, unionNode, errorLine, errorPosition); |
| 209 | addToResolutionList(resolutionInfo, ctx); |
| 210 | } |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 211 | } |
| 212 | |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 213 | break; |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 214 | case TYPEDEF_DATA: |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 215 | /* Prepare the base type info and set in derived type */ |
| 216 | YangTypeDef typeDef = (YangTypeDef) tmpData; |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 217 | typeDef.setDataType(type); |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 218 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 219 | /* |
| 220 | * If data type is derived, resolution information to be added |
| 221 | * in resolution list. |
| 222 | */ |
| 223 | if (yangDataTypes == YangDataTypes.DERIVED) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 224 | // Create empty derived info and attach it to type extended info. |
| 225 | YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>(); |
| 226 | ((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo); |
| 227 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 228 | type.setResolvableStatus(UNRESOLVED); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame^] | 229 | if (listener.getGroupingDepth() == 0) { |
| 230 | // Add resolution information to the list |
| 231 | YangResolutionInfoImpl resolutionInfo = |
| 232 | new YangResolutionInfoImpl<YangType>(type, typeDef, errorLine, errorPosition); |
| 233 | addToResolutionList(resolutionInfo, ctx); |
| 234 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 235 | } |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 236 | break; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 237 | //TODO: deviate replacement statement. |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 238 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 239 | default: |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 240 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 241 | ctx.string().getText(), EXIT)); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 242 | } |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 243 | |
| 244 | // Push the type to the stack. |
| 245 | listener.getParsedDataStack().push(type); |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 246 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 247 | |
| 248 | /** |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 249 | * Sets the default require instance value as true when the type is instance identifier. |
| 250 | * |
| 251 | * @param type type to which the value has to be set |
| 252 | */ |
| 253 | private static void setDefaultRequireInstanceForInstanceIdentifier(YangType<?> type) { |
| 254 | |
| 255 | if (type.getDataType() == YangDataTypes.INSTANCE_IDENTIFIER) { |
| 256 | ((YangType<Boolean>) type).setDataTypeExtendedInfo(true); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 261 | * It is called when parser exits from grammar rule (type), it perform |
| 262 | * validations and update the data model tree. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 263 | * |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 264 | * @param listener Listener's object |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 265 | * @param ctx context object of the grammar rule |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 266 | */ |
| 267 | public static void processTypeExit(TreeWalkListener listener, |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame^] | 268 | GeneratedYangParser.TypeStatementContext ctx) { |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 269 | |
| 270 | // Check for stack to be non empty. |
| 271 | checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT); |
| 272 | |
| 273 | Parsable parsableType = listener.getParsedDataStack().pop(); |
| 274 | if (!(parsableType instanceof YangType)) { |
| 275 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
| 276 | ctx.string().getText(), EXIT)); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Adds to resolution list. |
| 282 | * |
| 283 | * @param resolutionInfo resolution information |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 284 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 285 | */ |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 286 | private static void addToResolutionList(YangResolutionInfoImpl<YangType> resolutionInfo, |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame^] | 287 | GeneratedYangParser.TypeStatementContext ctx) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 288 | try { |
| 289 | addResolutionInfo(resolutionInfo); |
| 290 | } catch (DataModelException e) { |
| 291 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 292 | TYPE_DATA, ctx.string().getText(), ENTRY, e.getMessage())); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 293 | } |
| 294 | } |
Vidyashree Rama | 84e4ba7 | 2016-06-30 14:31:18 +0530 | [diff] [blame] | 295 | |
| 296 | /** |
| 297 | * Validates type body statements cardinality. |
| 298 | * |
| 299 | * @param ctx context object of the grammar rule |
| 300 | * @param yangDataType yang data type |
| 301 | */ |
| 302 | private static void validateTypeSubStatementCardinality(GeneratedYangParser.TypeStatementContext ctx, |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame^] | 303 | YangDataTypes yangDataType) { |
Vidyashree Rama | 84e4ba7 | 2016-06-30 14:31:18 +0530 | [diff] [blame] | 304 | if (ctx.typeBodyStatements() == null || ctx.typeBodyStatements().isEmpty()) { |
| 305 | ParserException parserException; |
| 306 | switch (yangDataType) { |
| 307 | case UNION: |
| 308 | parserException = new ParserException("YANG file error : a type union" + |
| 309 | " must have atleast one type statement."); |
| 310 | break; |
| 311 | case ENUMERATION: |
| 312 | parserException = new ParserException("YANG file error : a type enumeration" + |
| 313 | " must have atleast one enum statement."); |
| 314 | break; |
| 315 | case BITS: |
| 316 | parserException = new ParserException("YANG file error : a type bits" + |
| 317 | " must have atleast one bit statement."); |
| 318 | break; |
Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 319 | case DECIMAL64: |
| 320 | parserException = new ParserException("YANG file error : a type decimal64" + |
| 321 | " must have fraction-digits statement."); |
| 322 | break; |
janani b | 0e4e8ae | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 323 | case LEAFREF: |
| 324 | parserException = new ParserException("YANG file error : a type leafref" + |
| 325 | " must have one path statement."); |
| 326 | break; |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 327 | case IDENTITYREF: |
| 328 | parserException = new ParserException("YANG file error : a type identityref" + |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame^] | 329 | " must have base statement."); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 330 | break; |
Vidyashree Rama | 84e4ba7 | 2016-06-30 14:31:18 +0530 | [diff] [blame] | 331 | default: |
| 332 | return; |
| 333 | } |
| 334 | parserException.setLine(ctx.getStart().getLine()); |
| 335 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 336 | throw parserException; |
| 337 | } |
| 338 | } |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 339 | } |