blob: 70c135f6c21ef1011563b0b7d4171fcff1843194 [file] [log] [blame]
Vidyashree Rama09e1ca52016-03-29 12:00:42 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama09e1ca52016-03-29 12:00:42 +05303 *
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
17package org.onosproject.yangutils.parser.impl.listeners;
18
19import org.onosproject.yangutils.datamodel.YangNode;
20import org.onosproject.yangutils.datamodel.YangOutput;
21import org.onosproject.yangutils.datamodel.YangRpc;
22import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053023import org.onosproject.yangutils.datamodel.utils.Parsable;
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
25import org.onosproject.yangutils.parser.exceptions.ParserException;
26import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27
28import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053029import static org.onosproject.yangutils.datamodel.utils.YangConstructType.OUTPUT_DATA;
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053030import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
33 .constructExtendedListenerErrorMessage;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
35 .constructListenerErrorMessage;
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053041import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangOutputNode;
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053042
43/*
44 * Reference: RFC6020 and YANG ANTLR Grammar
45 *
46 * ABNF grammar as per RFC6020
47 *
48 * output-stmt = output-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 * outputStatement : OUTPUT_KEYWORD LEFT_CURLY_BRACE outputStatementBody RIGHT_CURLY_BRACE;
57
58 * outputStatementBody : typedefStatement* dataDefStatement+
59 * | dataDefStatement+ typedefStatement*
60 * | groupingStatement* dataDefStatement+
61 * | dataDefStatement+ groupingStatement*;
62 */
63
64/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053065 * Represents listener based call back function corresponding to the "output"
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053066 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
67 */
68public final class OutputListener {
69
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053070 private static final String OUTPUT_KEYWORD = "_output";
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053071
72 /**
73 * Creates a new output listener.
74 */
75 private OutputListener() {
76 }
77
78 /**
79 * It is called when parser receives an input matching the grammar rule
80 * (output), performs validation and updates the data model tree.
81 *
82 * @param listener listener's object
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053083 * @param ctx context object of the grammar rule
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053084 */
85 public static void processOutputEntry(TreeWalkListener listener,
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053086 GeneratedYangParser.OutputStatementContext ctx) {
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053087
88 // Check for stack to be non empty.
89 checkStackIsNotEmpty(listener, MISSING_HOLDER, OUTPUT_DATA, "", ENTRY);
90
91 Parsable curData = listener.getParsedDataStack().peek();
92 if (curData instanceof YangRpc) {
93
94 YangOutput yangOutput = getYangOutputNode(JAVA_GENERATION);
95 yangOutput.setName(((YangRpc) curData).getName() + OUTPUT_KEYWORD);
Bharat saraswale3175d32016-08-31 17:50:11 +053096
97 yangOutput.setLineNumber(ctx.getStart().getLine());
98 yangOutput.setCharPosition(ctx.getStart().getCharPositionInLine());
99 yangOutput.setFileName(listener.getFileName());
Vidyashree Rama09e1ca52016-03-29 12:00:42 +0530100 YangNode curNode = (YangNode) curData;
101 try {
102 curNode.addChild(yangOutput);
103 } catch (DataModelException e) {
104 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
105 OUTPUT_DATA, "", ENTRY, e.getMessage()));
106 }
107 listener.getParsedDataStack().push(yangOutput);
108 } else {
109 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, OUTPUT_DATA,
110 "", ENTRY));
111 }
112 }
113
114 /**
115 * It is called when parser exits from grammar rule (output), it perform
116 * validations and updates the data model tree.
117 *
118 * @param listener listener's object
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530119 * @param ctx context object of the grammar rule
Vidyashree Rama09e1ca52016-03-29 12:00:42 +0530120 */
121 public static void processOutputExit(TreeWalkListener listener,
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530122 GeneratedYangParser.OutputStatementContext ctx) {
Vidyashree Rama09e1ca52016-03-29 12:00:42 +0530123
124 // Check for stack to be non empty.
125 checkStackIsNotEmpty(listener, MISSING_HOLDER, OUTPUT_DATA, "", EXIT);
126
127 if (!(listener.getParsedDataStack().peek() instanceof YangOutput)) {
128 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, OUTPUT_DATA,
129 "", EXIT));
130 }
131 listener.getParsedDataStack().pop();
132 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530133}