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 | |
| 19 | /* |
| 20 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 21 | * |
| 22 | * body-stmts = *((extension-stmt / |
| 23 | * feature-stmt / |
| 24 | * identity-stmt / |
| 25 | * typedef-stmt / |
| 26 | * grouping-stmt / |
| 27 | * data-def-stmt / |
| 28 | * augment-stmt / |
| 29 | * rpc-stmt / |
| 30 | * notification-stmt / |
| 31 | * deviation-stmt) stmtsep) |
| 32 | * |
| 33 | * typedef-stmt = typedef-keyword sep identifier-arg-str optsep |
| 34 | * "{" stmtsep |
| 35 | * ;; these stmts can appear in any order |
| 36 | * type-stmt stmtsep |
| 37 | * [units-stmt stmtsep] |
| 38 | * [default-stmt stmtsep] |
| 39 | * [status-stmt stmtsep] |
| 40 | * [description-stmt stmtsep] |
| 41 | * [reference-stmt stmtsep] |
| 42 | * "}" |
| 43 | * |
| 44 | * ANTLR grammar rule |
| 45 | * typedefStatement : TYPEDEF_KEYWORD IDENTIFIER LEFT_CURLY_BRACE |
| 46 | * (typeStatement | unitsStatement | defaultStatement | statusStatement |
| 47 | * | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE; |
| 48 | */ |
| 49 | |
| 50 | import org.onosproject.yangutils.datamodel.YangContainer; |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame^] | 51 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 52 | import org.onosproject.yangutils.datamodel.YangDerivedType; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 53 | import org.onosproject.yangutils.datamodel.YangList; |
| 54 | import org.onosproject.yangutils.datamodel.YangModule; |
| 55 | import org.onosproject.yangutils.datamodel.YangNode; |
| 56 | import org.onosproject.yangutils.datamodel.YangSubModule; |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame^] | 57 | import org.onosproject.yangutils.datamodel.YangType; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 58 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 59 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 60 | import org.onosproject.yangutils.parser.Parsable; |
| 61 | import org.onosproject.yangutils.parser.ParsableDataType; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 62 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 63 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 64 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 65 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 66 | |
| 67 | import static org.onosproject.yangutils.parser.ParsableDataType.DEFAULT_DATA; |
| 68 | import static org.onosproject.yangutils.parser.ParsableDataType.DESCRIPTION_DATA; |
| 69 | import static org.onosproject.yangutils.parser.ParsableDataType.REFERENCE_DATA; |
| 70 | import static org.onosproject.yangutils.parser.ParsableDataType.STATUS_DATA; |
| 71 | import static org.onosproject.yangutils.parser.ParsableDataType.TYPEDEF_DATA; |
| 72 | import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA; |
| 73 | import static org.onosproject.yangutils.parser.ParsableDataType.UNITS_DATA; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 74 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 75 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 76 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 77 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 78 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CARDINALITY; |
| 79 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 80 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 81 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 82 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 83 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 84 | |
| 85 | /** |
| 86 | * Implements listener based call back function corresponding to the "typedef" |
| 87 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 88 | */ |
| 89 | public final class TypeDefListener { |
| 90 | |
| 91 | private static ParsableDataType yangConstruct; |
| 92 | |
| 93 | /** |
| 94 | * Creates a new typedef listener. |
| 95 | */ |
| 96 | private TypeDefListener() { |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * It is called when parser enters grammar rule (typedef), it perform |
| 101 | * validations and updates the data model tree. |
| 102 | * |
| 103 | * @param listener listener's object. |
| 104 | * @param ctx context object of the grammar rule. |
| 105 | */ |
| 106 | public static void processTypeDefEntry(TreeWalkListener listener, |
| 107 | GeneratedYangParser.TypedefStatementContext ctx) { |
| 108 | |
| 109 | // Check for stack to be non empty. |
| 110 | checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY); |
| 111 | |
| 112 | boolean result = validateSubStatementsCardinality(ctx); |
| 113 | if (!result) { |
| 114 | throw new ParserException(constructListenerErrorMessage(INVALID_CARDINALITY, yangConstruct, "", ENTRY)); |
| 115 | } |
| 116 | |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame^] | 117 | /* |
| 118 | * Create a derived type information, the base type must be set in type |
| 119 | * listener. |
| 120 | */ |
| 121 | YangType<YangDerivedType> derivedType = new YangType<YangDerivedType>(); |
| 122 | derivedType.setDataType(YangDataTypes.DERIVED); |
| 123 | derivedType.setDataTypeName(ctx.IDENTIFIER().getText()); |
| 124 | |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 125 | YangTypeDef typeDefNode = new YangTypeDef(); |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame^] | 126 | typeDefNode.setDerivedType(derivedType); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 127 | |
| 128 | Parsable curData = listener.getParsedDataStack().peek(); |
| 129 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 130 | if ((curData instanceof YangModule) | (curData instanceof YangSubModule) | (curData instanceof YangContainer) |
| 131 | | (curData instanceof YangList)) { |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 132 | /* |
| 133 | * TODO YangGrouping, YangRpc, YangInput, YangOutput, Notification. |
| 134 | */ |
| 135 | YangNode curNode = (YangNode) curData; |
| 136 | try { |
| 137 | curNode.addChild(typeDefNode); |
| 138 | } catch (DataModelException e) { |
| 139 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 140 | TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage())); |
| 141 | } |
| 142 | listener.getParsedDataStack().push(typeDefNode); |
| 143 | } else { |
| 144 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, |
| 145 | TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY)); |
| 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 | * |
| 153 | * @param listener listener's object. |
| 154 | * @param ctx context object of the grammar rule. |
| 155 | */ |
| 156 | public static void processTypeDefExit(TreeWalkListener listener, |
| 157 | GeneratedYangParser.TypedefStatementContext ctx) { |
| 158 | |
| 159 | // Check for stack to be non empty. |
| 160 | checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.IDENTIFIER().getText(), EXIT); |
| 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) { |
| 167 | // TODO Auto-generated catch block |
| 168 | e.printStackTrace(); |
| 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, |
| 174 | ctx.IDENTIFIER().getText(), EXIT)); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Validates the cardinality of typedef sub-statements as per grammar. |
| 180 | * |
| 181 | * @param ctx context object of the grammar rule. |
| 182 | * @return true/false validation success or failure. |
| 183 | */ |
| 184 | private static boolean validateSubStatementsCardinality(GeneratedYangParser.TypedefStatementContext ctx) { |
| 185 | |
| 186 | if ((!ctx.unitsStatement().isEmpty()) |
| 187 | && (ctx.unitsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 188 | yangConstruct = UNITS_DATA; |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | if ((!ctx.defaultStatement().isEmpty()) |
| 193 | && (ctx.defaultStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 194 | yangConstruct = DEFAULT_DATA; |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | if (ctx.typeStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY) { |
| 199 | yangConstruct = TYPE_DATA; |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | if ((!ctx.descriptionStatement().isEmpty()) |
| 204 | && (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 205 | yangConstruct = DESCRIPTION_DATA; |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | if ((!ctx.referenceStatement().isEmpty()) |
| 210 | && (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 211 | yangConstruct = REFERENCE_DATA; |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | if ((!ctx.statusStatement().isEmpty()) |
| 216 | && (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 217 | yangConstruct = STATUS_DATA; |
| 218 | return false; |
| 219 | } |
| 220 | return true; |
| 221 | } |
| 222 | } |