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.YangBelongsTo; |
| 20 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 21 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 25 | |
| 26 | import static org.onosproject.yangutils.parser.ParsableDataType.BELONGS_TO_DATA; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
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-header-stmts = |
| 38 | * ;; these stmts can appear in any order |
| 39 | * [yang-version-stmt stmtsep] |
| 40 | * belongs-to-stmt stmtsep |
| 41 | * |
| 42 | * belongs-to-stmt = belongs-to-keyword sep identifier-arg-str |
| 43 | * optsep |
| 44 | * "{" stmtsep |
| 45 | * prefix-stmt stmtsep |
| 46 | * "}" |
| 47 | * |
| 48 | * ANTLR grammar rule |
| 49 | * submodule_header_statement : yang_version_stmt? belongs_to_stmt |
| 50 | * | belongs_to_stmt yang_version_stmt? |
| 51 | * ; |
| 52 | * belongs_to_stmt : BELONGS_TO_KEYWORD IDENTIFIER LEFT_CURLY_BRACE belongs_to_stmt_body RIGHT_CURLY_BRACE; |
| 53 | * belongs_to_stmt_body : prefix_stmt; |
| 54 | */ |
| 55 | |
| 56 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 57 | * Implements listener based call back function corresponding to the |
| 58 | * "belongs to" rule defined in ANTLR grammar file for corresponding ABNF rule |
| 59 | * in RFC 6020. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 60 | */ |
| 61 | public final class BelongsToListener { |
| 62 | |
| 63 | /** |
| 64 | * Creates a new belongto listener. |
| 65 | */ |
| 66 | private BelongsToListener() { |
| 67 | } |
| 68 | |
| 69 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 70 | * It is called when parser receives an input matching the grammar rule |
| 71 | * (belongsto), perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 72 | * |
| 73 | * @param listener Listener's object. |
| 74 | * @param ctx context object of the grammar rule. |
| 75 | */ |
| 76 | public static void processBelongsToEntry(TreeWalkListener listener, |
| 77 | GeneratedYangParser.BelongstoStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 78 | |
| 79 | // Check for stack to be non empty. |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 80 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.IDENTIFIER().getText(), |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 81 | ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 82 | |
| 83 | YangBelongsTo belongstoNode = new YangBelongsTo(); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 84 | belongstoNode.setBelongsToModuleName(ctx.IDENTIFIER().getText()); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 85 | |
| 86 | // Push belongsto into the stack. |
| 87 | listener.getParsedDataStack().push(belongstoNode); |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * It is called when parser exits from grammar rule (belongsto), it perform |
| 92 | * validations and update the data model tree. |
| 93 | * |
| 94 | * @param listener Listener's object. |
| 95 | * @param ctx context object of the grammar rule. |
| 96 | */ |
| 97 | public static void processBelongsToExit(TreeWalkListener listener, |
| 98 | GeneratedYangParser.BelongstoStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 99 | |
| 100 | // Check for stack to be non empty. |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 101 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.IDENTIFIER().getText(), |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 102 | EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 103 | |
| 104 | Parsable tmpBelongstoNode = listener.getParsedDataStack().peek(); |
| 105 | if (tmpBelongstoNode instanceof YangBelongsTo) { |
| 106 | listener.getParsedDataStack().pop(); |
| 107 | |
| 108 | // Check for stack to be empty. |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 109 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 110 | ctx.IDENTIFIER().getText(), EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 111 | |
| 112 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 113 | switch (tmpNode.getParsableDataType()) { |
| 114 | case SUB_MODULE_DATA: { |
| 115 | YangSubModule subModule = (YangSubModule) tmpNode; |
| 116 | subModule.setBelongsTo((YangBelongsTo) tmpBelongstoNode); |
| 117 | break; |
| 118 | } |
| 119 | default: |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 120 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BELONGS_TO_DATA, |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 121 | ctx.IDENTIFIER().getText(), |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 122 | EXIT)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 123 | } |
| 124 | } else { |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 125 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BELONGS_TO_DATA, |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 126 | ctx.IDENTIFIER().getText(), EXIT)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 127 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 128 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 129 | } |