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.YangNode; |
| 20 | import org.onosproject.yangutils.datamodel.YangOutput; |
| 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.getYangOutputNode; |
| 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; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 39 | import static org.onosproject.yangutils.utils.YangConstructType.OUTPUT_DATA; |
| 40 | |
| 41 | /* |
| 42 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 43 | * |
| 44 | * ABNF grammar as per RFC6020 |
| 45 | * |
| 46 | * output-stmt = output-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 | * outputStatement : OUTPUT_KEYWORD LEFT_CURLY_BRACE outputStatementBody RIGHT_CURLY_BRACE; |
| 55 | |
| 56 | * outputStatementBody : typedefStatement* dataDefStatement+ |
| 57 | * | dataDefStatement+ typedefStatement* |
| 58 | * | groupingStatement* dataDefStatement+ |
| 59 | * | dataDefStatement+ groupingStatement*; |
| 60 | */ |
| 61 | |
| 62 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 63 | * Represents listener based call back function corresponding to the "output" |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 64 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 65 | */ |
| 66 | public final class OutputListener { |
| 67 | |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 68 | private static final String OUTPUT_KEYWORD = "_output"; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Creates a new output listener. |
| 72 | */ |
| 73 | private OutputListener() { |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * It is called when parser receives an input matching the grammar rule |
| 78 | * (output), performs validation and updates the data model tree. |
| 79 | * |
| 80 | * @param listener listener's object |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 81 | * @param ctx context object of the grammar rule |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 82 | */ |
| 83 | public static void processOutputEntry(TreeWalkListener listener, |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 84 | GeneratedYangParser.OutputStatementContext ctx) { |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 85 | |
| 86 | // Check for stack to be non empty. |
| 87 | checkStackIsNotEmpty(listener, MISSING_HOLDER, OUTPUT_DATA, "", ENTRY); |
| 88 | |
| 89 | Parsable curData = listener.getParsedDataStack().peek(); |
| 90 | if (curData instanceof YangRpc) { |
| 91 | |
| 92 | YangOutput yangOutput = getYangOutputNode(JAVA_GENERATION); |
| 93 | yangOutput.setName(((YangRpc) curData).getName() + OUTPUT_KEYWORD); |
| 94 | YangNode curNode = (YangNode) curData; |
| 95 | try { |
| 96 | curNode.addChild(yangOutput); |
| 97 | } catch (DataModelException e) { |
| 98 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 99 | OUTPUT_DATA, "", ENTRY, e.getMessage())); |
| 100 | } |
| 101 | listener.getParsedDataStack().push(yangOutput); |
| 102 | } else { |
| 103 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, OUTPUT_DATA, |
| 104 | "", ENTRY)); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * It is called when parser exits from grammar rule (output), it perform |
| 110 | * validations and updates the data model tree. |
| 111 | * |
| 112 | * @param listener listener's object |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 113 | * @param ctx context object of the grammar rule |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 114 | */ |
| 115 | public static void processOutputExit(TreeWalkListener listener, |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 116 | GeneratedYangParser.OutputStatementContext ctx) { |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 117 | |
| 118 | // Check for stack to be non empty. |
| 119 | checkStackIsNotEmpty(listener, MISSING_HOLDER, OUTPUT_DATA, "", EXIT); |
| 120 | |
| 121 | if (!(listener.getParsedDataStack().peek() instanceof YangOutput)) { |
| 122 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, OUTPUT_DATA, |
| 123 | "", EXIT)); |
| 124 | } |
| 125 | listener.getParsedDataStack().pop(); |
| 126 | } |
| 127 | } |