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