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; |
| 23 | import org.onosproject.yangutils.parser.Parsable; |
| 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; |
| 29 | import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangInputNode; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
| 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 39 | import static org.onosproject.yangutils.utils.YangConstructType.INPUT_DATA; |
| 40 | |
| 41 | /* |
| 42 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 43 | * |
| 44 | * ABNF grammar as per RFC6020 |
| 45 | * |
| 46 | * input-stmt = input-keyword optsep |
| 47 | * "{" stmtsep |
| 48 | * ;; these stmts can appear in any order |
| 49 | * *((typedef-stmt / |
| 50 | * grouping-stmt) stmtsep) |
| 51 | * 1*(data-def-stmt stmtsep) |
| 52 | * "}" |
| 53 | * |
| 54 | * inputStatement : INPUT_KEYWORD LEFT_CURLY_BRACE inputStatementBody RIGHT_CURLY_BRACE; |
| 55 | |
| 56 | * inputStatementBody : typedefStatement* dataDefStatement+ |
| 57 | * | dataDefStatement+ typedefStatement* |
| 58 | * | groupingStatement* dataDefStatement+ |
| 59 | * | dataDefStatement+ groupingStatement*; |
| 60 | */ |
| 61 | |
| 62 | /** |
| 63 | * Implements listener based call back function corresponding to the "input" |
| 64 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 65 | */ |
| 66 | public final class InputListener { |
| 67 | |
| 68 | private static final String INPUT_KEYWORD = "Input"; |
| 69 | |
| 70 | /** |
| 71 | * Creates a new input listener. |
| 72 | */ |
| 73 | private InputListener() { |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * It is called when parser receives an input matching the grammar rule |
| 78 | * (input), performs validation and updates the data model tree. |
| 79 | * |
| 80 | * @param listener listener's object |
| 81 | * @param ctx context object of the grammar rule |
| 82 | */ |
| 83 | public static void processInputEntry(TreeWalkListener listener, |
| 84 | GeneratedYangParser.InputStatementContext ctx) { |
| 85 | |
| 86 | // Check for stack to be non empty. |
| 87 | checkStackIsNotEmpty(listener, MISSING_HOLDER, INPUT_DATA, "", ENTRY); |
| 88 | |
| 89 | Parsable curData = listener.getParsedDataStack().peek(); |
| 90 | if (curData instanceof YangRpc) { |
| 91 | |
| 92 | YangInput yangInput = getYangInputNode(JAVA_GENERATION); |
| 93 | yangInput.setName(((YangRpc) curData).getName() + INPUT_KEYWORD); |
| 94 | YangNode curNode = (YangNode) curData; |
| 95 | try { |
| 96 | curNode.addChild(yangInput); |
| 97 | } catch (DataModelException e) { |
| 98 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 99 | INPUT_DATA, "", ENTRY, e.getMessage())); |
| 100 | } |
| 101 | listener.getParsedDataStack().push(yangInput); |
| 102 | } else { |
| 103 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, INPUT_DATA, |
| 104 | "", ENTRY)); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * It is called when parser exits from grammar rule (input), it perform |
| 110 | * validations and updates the data model tree. |
| 111 | * |
| 112 | * @param listener listener's object |
| 113 | * @param ctx context object of the grammar rule |
| 114 | */ |
| 115 | public static void processInputExit(TreeWalkListener listener, |
| 116 | GeneratedYangParser.InputStatementContext ctx) { |
| 117 | |
| 118 | // Check for stack to be non empty. |
| 119 | checkStackIsNotEmpty(listener, MISSING_HOLDER, INPUT_DATA, "", EXIT); |
| 120 | |
| 121 | if (!(listener.getParsedDataStack().peek() instanceof YangInput)) { |
| 122 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, INPUT_DATA, |
| 123 | "", EXIT)); |
| 124 | } |
| 125 | listener.getParsedDataStack().pop(); |
| 126 | } |
| 127 | } |