Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2016 Open Networking Laboratory |
| 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 | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 20 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 21 | import org.onosproject.yangutils.datamodel.YangDerivedType; |
| 22 | import org.onosproject.yangutils.datamodel.YangList; |
| 23 | import org.onosproject.yangutils.datamodel.YangModule; |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 26 | import org.onosproject.yangutils.datamodel.YangType; |
| 27 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 28 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 29 | import org.onosproject.yangutils.parser.Parsable; |
| 30 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 31 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 32 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 33 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 34 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
| 35 | import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangTypeDefNode; |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT; |
| 41 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 42 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 43 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 44 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne; |
| 48 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; |
| 49 | import static org.onosproject.yangutils.utils.YangConstructType.DEFAULT_DATA; |
| 50 | import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; |
| 51 | import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA; |
| 52 | import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA; |
| 53 | import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA; |
| 54 | import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; |
| 55 | import static org.onosproject.yangutils.utils.YangConstructType.UNITS_DATA; |
| 56 | |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 57 | /* |
| 58 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 59 | * |
| 60 | * body-stmts = *((extension-stmt / |
| 61 | * feature-stmt / |
| 62 | * identity-stmt / |
| 63 | * typedef-stmt / |
| 64 | * grouping-stmt / |
| 65 | * data-def-stmt / |
| 66 | * augment-stmt / |
| 67 | * rpc-stmt / |
| 68 | * notification-stmt / |
| 69 | * deviation-stmt) stmtsep) |
| 70 | * |
| 71 | * typedef-stmt = typedef-keyword sep identifier-arg-str optsep |
| 72 | * "{" stmtsep |
| 73 | * ;; these stmts can appear in any order |
| 74 | * type-stmt stmtsep |
| 75 | * [units-stmt stmtsep] |
| 76 | * [default-stmt stmtsep] |
| 77 | * [status-stmt stmtsep] |
| 78 | * [description-stmt stmtsep] |
| 79 | * [reference-stmt stmtsep] |
| 80 | * "}" |
| 81 | * |
| 82 | * ANTLR grammar rule |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 83 | * typedefStatement : TYPEDEF_KEYWORD identifier LEFT_CURLY_BRACE |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 84 | * (typeStatement | unitsStatement | defaultStatement | statusStatement |
| 85 | * | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE; |
| 86 | */ |
| 87 | |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 88 | /** |
| 89 | * Implements listener based call back function corresponding to the "typedef" |
| 90 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 91 | */ |
| 92 | public final class TypeDefListener { |
| 93 | |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 94 | /** |
| 95 | * Creates a new typedef listener. |
| 96 | */ |
| 97 | private TypeDefListener() { |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * It is called when parser enters grammar rule (typedef), it perform |
| 102 | * validations and updates the data model tree. |
| 103 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 104 | * @param listener listener's object |
| 105 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 106 | */ |
| 107 | public static void processTypeDefEntry(TreeWalkListener listener, |
| 108 | GeneratedYangParser.TypedefStatementContext ctx) { |
| 109 | |
| 110 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 111 | checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.identifier().getText(), ENTRY); |
| 112 | |
| 113 | String identifier = getValidIdentifier(ctx.identifier().getText(), TYPEDEF_DATA, ctx); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 114 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 115 | // Validate sub statement cardinality. |
| 116 | validateSubStatementsCardinality(ctx); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 117 | |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 118 | /* |
| 119 | * Create a derived type information, the base type must be set in type |
| 120 | * listener. |
| 121 | */ |
| 122 | YangType<YangDerivedType> derivedType = new YangType<YangDerivedType>(); |
| 123 | derivedType.setDataType(YangDataTypes.DERIVED); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 124 | derivedType.setDataTypeName(identifier); |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 125 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 126 | YangTypeDef typeDefNode = getYangTypeDefNode(JAVA_GENERATION); |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 127 | typeDefNode.setDerivedType(derivedType); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 128 | |
| 129 | Parsable curData = listener.getParsedDataStack().peek(); |
| 130 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 131 | if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangContainer |
| 132 | || curData instanceof YangList) { |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 133 | /* |
| 134 | * TODO YangGrouping, YangRpc, YangInput, YangOutput, Notification. |
| 135 | */ |
| 136 | YangNode curNode = (YangNode) curData; |
| 137 | try { |
| 138 | curNode.addChild(typeDefNode); |
| 139 | } catch (DataModelException e) { |
| 140 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 141 | TYPEDEF_DATA, ctx.identifier().getText(), ENTRY, e.getMessage())); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 142 | } |
| 143 | listener.getParsedDataStack().push(typeDefNode); |
| 144 | } else { |
| 145 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 146 | TYPEDEF_DATA, ctx.identifier().getText(), ENTRY)); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * It is called when parser exits from grammar rule (typedef), it perform |
| 152 | * validations and updates the data model tree. |
| 153 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 154 | * @param listener listener's object |
| 155 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 156 | */ |
| 157 | public static void processTypeDefExit(TreeWalkListener listener, |
| 158 | GeneratedYangParser.TypedefStatementContext ctx) { |
| 159 | |
| 160 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 161 | checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.identifier().getText(), EXIT); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 162 | |
| 163 | if (listener.getParsedDataStack().peek() instanceof YangTypeDef) { |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 164 | YangTypeDef typeDefNode = (YangTypeDef) listener.getParsedDataStack().peek(); |
| 165 | try { |
| 166 | typeDefNode.validateDataOnExit(); |
| 167 | } catch (DataModelException e) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 168 | throw new ParserException(constructListenerErrorMessage(INVALID_CONTENT, TYPEDEF_DATA, |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 169 | ctx.identifier().getText(), EXIT)); |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 170 | } |
| 171 | |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 172 | listener.getParsedDataStack().pop(); |
| 173 | } else { |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 174 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, TYPEDEF_DATA, |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 175 | ctx.identifier().getText(), EXIT)); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Validates the cardinality of typedef sub-statements as per grammar. |
| 181 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 182 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 183 | */ |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 184 | private static void validateSubStatementsCardinality(GeneratedYangParser.TypedefStatementContext ctx) { |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 185 | |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 186 | validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, TYPEDEF_DATA, ctx.identifier().getText()); |
| 187 | validateCardinalityMaxOne(ctx.defaultStatement(), DEFAULT_DATA, TYPEDEF_DATA, ctx.identifier().getText()); |
| 188 | validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, TYPEDEF_DATA, ctx.identifier().getText(), ctx); |
| 189 | validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, TYPEDEF_DATA, |
| 190 | ctx.identifier().getText()); |
| 191 | validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, TYPEDEF_DATA, ctx.identifier().getText()); |
| 192 | validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, TYPEDEF_DATA, ctx.identifier().getText()); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 193 | } |
| 194 | } |