blob: bb1ba85abe6a2d38f356c2353f33ad82dc767e63 [file] [log] [blame]
Vidyashree Rama6a72b792016-03-29 12:00:42 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama6a72b792016-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.YangInput;
20import org.onosproject.yangutils.datamodel.YangNode;
21import org.onosproject.yangutils.datamodel.YangRpc;
22import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
23import org.onosproject.yangutils.parser.Parsable;
24import 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;
29import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangInputNode;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal56527662016-04-20 15:49:17 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
39import 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/**
Bharat saraswald9822e92016-04-05 15:13:44 +053063 * Represents listener based call back function corresponding to the "input"
Vidyashree Rama6a72b792016-03-29 12:00:42 +053064 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
65 */
66public final class InputListener {
67
Gaurav Agrawal56527662016-04-20 15:49:17 +053068 private static final String INPUT_KEYWORD = "_input";
Vidyashree Rama6a72b792016-03-29 12:00:42 +053069
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
Gaurav Agrawal56527662016-04-20 15:49:17 +053081 * @param ctx context object of the grammar rule
Vidyashree Rama6a72b792016-03-29 12:00:42 +053082 */
83 public static void processInputEntry(TreeWalkListener listener,
Gaurav Agrawal56527662016-04-20 15:49:17 +053084 GeneratedYangParser.InputStatementContext ctx) {
Vidyashree Rama6a72b792016-03-29 12:00:42 +053085
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
Gaurav Agrawal56527662016-04-20 15:49:17 +0530113 * @param ctx context object of the grammar rule
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530114 */
115 public static void processInputExit(TreeWalkListener listener,
Gaurav Agrawal56527662016-04-20 15:49:17 +0530116 GeneratedYangParser.InputStatementContext ctx) {
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530117
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}