Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangSubModule; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 23 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 24 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 25 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.utils.YangConstructType.SUB_MODULE_DATA; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 35 | * |
| 36 | * ABNF grammar as per RFC6020 |
| 37 | * submodule-stmt = optsep submodule-keyword sep identifier-arg-str |
| 38 | * optsep |
| 39 | * "{" stmtsep |
| 40 | * submodule-header-stmts |
| 41 | * linkage-stmts |
| 42 | * meta-stmts |
| 43 | * revision-stmts |
| 44 | * body-stmts |
| 45 | * "}" optsep |
| 46 | * |
| 47 | * ANTLR grammar rule |
| 48 | * submodule_stmt : SUBMODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE submodule_body* RIGHT_CURLY_BRACE; |
| 49 | * submodule_body : submodule_header_statement linkage_stmts meta_stmts revision_stmts body_stmts; |
| 50 | */ |
| 51 | |
| 52 | /** |
| 53 | * Implements listener based call back function corresponding to the "submodule" |
| 54 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 55 | */ |
| 56 | public final class SubModuleListener { |
| 57 | |
| 58 | /** |
| 59 | * Creates a new sub module listener. |
| 60 | */ |
| 61 | private SubModuleListener() { |
| 62 | } |
| 63 | |
| 64 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 65 | * It is called when parser receives an input matching the grammar rule (sub |
| 66 | * module), perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 67 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 68 | * @param listener Listener's object |
| 69 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 70 | */ |
| 71 | public static void processSubModuleEntry(TreeWalkListener listener, |
| 72 | GeneratedYangParser.SubModuleStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 73 | |
| 74 | // Check if stack is empty. |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 75 | checkStackIsEmpty(listener, INVALID_HOLDER, SUB_MODULE_DATA, ctx.IDENTIFIER().getText(), |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 76 | ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 77 | |
| 78 | YangSubModule yangSubModule = new YangSubModule(); |
| 79 | yangSubModule.setName(ctx.IDENTIFIER().getText()); |
| 80 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 81 | if (ctx.submoduleBody(0).submoduleHeaderStatement().yangVersionStatement() == null) { |
| 82 | yangSubModule.setVersion((byte) 1); |
| 83 | } |
| 84 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 85 | listener.getParsedDataStack().push(yangSubModule); |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * It is called when parser exits from grammar rule (submodule), it perform |
| 90 | * validations and update the data model tree. |
| 91 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 92 | * @param listener Listener's object |
| 93 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 94 | */ |
| 95 | public static void processSubModuleExit(TreeWalkListener listener, |
| 96 | GeneratedYangParser.SubModuleStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 97 | |
| 98 | // Check for stack to be non empty. |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 99 | checkStackIsNotEmpty(listener, MISSING_HOLDER, SUB_MODULE_DATA, ctx.IDENTIFIER().getText(), |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 100 | EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 101 | |
| 102 | if (!(listener.getParsedDataStack().peek() instanceof YangSubModule)) { |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 103 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SUB_MODULE_DATA, |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 104 | ctx.IDENTIFIER().getText(), EXIT)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 105 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 106 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 107 | } |