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