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