Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +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 | |
| 19 | import org.onosproject.yangutils.datamodel.YangInput; |
| 20 | import org.onosproject.yangutils.datamodel.YangNode; |
| 21 | import org.onosproject.yangutils.datamodel.YangRpc; |
| 22 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 26 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 27 | |
| 28 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.INPUT_DATA; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction |
| 33 | .constructExtendedListenerErrorMessage; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction |
| 35 | .constructListenerErrorMessage; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangInputNode; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 45 | * |
| 46 | * ABNF grammar as per RFC6020 |
| 47 | * |
| 48 | * input-stmt = input-keyword optsep |
| 49 | * "{" stmtsep |
| 50 | * ;; these stmts can appear in any order |
| 51 | * *((typedef-stmt / |
| 52 | * grouping-stmt) stmtsep) |
| 53 | * 1*(data-def-stmt stmtsep) |
| 54 | * "}" |
| 55 | * |
| 56 | * inputStatement : INPUT_KEYWORD LEFT_CURLY_BRACE inputStatementBody RIGHT_CURLY_BRACE; |
| 57 | |
| 58 | * inputStatementBody : typedefStatement* dataDefStatement+ |
| 59 | * | dataDefStatement+ typedefStatement* |
| 60 | * | groupingStatement* dataDefStatement+ |
| 61 | * | dataDefStatement+ groupingStatement*; |
| 62 | */ |
| 63 | |
| 64 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 65 | * Represents listener based call back function corresponding to the "input" |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 66 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 67 | */ |
| 68 | public final class InputListener { |
| 69 | |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 70 | private static final String INPUT_KEYWORD = "_input"; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Creates a new input listener. |
| 74 | */ |
| 75 | private InputListener() { |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * It is called when parser receives an input matching the grammar rule |
| 80 | * (input), performs validation and updates the data model tree. |
| 81 | * |
| 82 | * @param listener listener's object |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 83 | * @param ctx context object of the grammar rule |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 84 | */ |
| 85 | public static void processInputEntry(TreeWalkListener listener, |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 86 | GeneratedYangParser.InputStatementContext ctx) { |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 87 | |
| 88 | // Check for stack to be non empty. |
| 89 | checkStackIsNotEmpty(listener, MISSING_HOLDER, INPUT_DATA, "", ENTRY); |
| 90 | |
| 91 | Parsable curData = listener.getParsedDataStack().peek(); |
| 92 | if (curData instanceof YangRpc) { |
| 93 | |
| 94 | YangInput yangInput = getYangInputNode(JAVA_GENERATION); |
| 95 | yangInput.setName(((YangRpc) curData).getName() + INPUT_KEYWORD); |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 96 | |
| 97 | yangInput.setLineNumber(ctx.getStart().getLine()); |
| 98 | yangInput.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 99 | yangInput.setFileName(listener.getFileName()); |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 100 | YangNode curNode = (YangNode) curData; |
| 101 | try { |
| 102 | curNode.addChild(yangInput); |
| 103 | } catch (DataModelException e) { |
| 104 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 105 | INPUT_DATA, "", ENTRY, e.getMessage())); |
| 106 | } |
| 107 | listener.getParsedDataStack().push(yangInput); |
| 108 | } else { |
| 109 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, INPUT_DATA, |
| 110 | "", ENTRY)); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * It is called when parser exits from grammar rule (input), it perform |
| 116 | * validations and updates the data model tree. |
| 117 | * |
| 118 | * @param listener listener's object |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 119 | * @param ctx context object of the grammar rule |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 120 | */ |
| 121 | public static void processInputExit(TreeWalkListener listener, |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 122 | GeneratedYangParser.InputStatementContext ctx) { |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 123 | |
| 124 | // Check for stack to be non empty. |
| 125 | checkStackIsNotEmpty(listener, MISSING_HOLDER, INPUT_DATA, "", EXIT); |
| 126 | |
| 127 | if (!(listener.getParsedDataStack().peek() instanceof YangInput)) { |
| 128 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, INPUT_DATA, |
| 129 | "", EXIT)); |
| 130 | } |
| 131 | listener.getParsedDataStack().pop(); |
| 132 | } |
| 133 | } |