blob: 610951839ab1a6ddcad55608634f94a099d6eadd [file] [log] [blame]
Vidyashree Rama620b6e92016-03-28 11:59:27 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama620b6e92016-03-28 11:59:27 +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
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053019import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053020import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053021import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal63f26fb2016-04-05 15:13:44 +053022import org.onosproject.yangutils.datamodel.YangNotification;
23import org.onosproject.yangutils.datamodel.YangSubModule;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053024import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053025import org.onosproject.yangutils.datamodel.utils.Parsable;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053026import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
27import org.onosproject.yangutils.parser.exceptions.ParserException;
28import org.onosproject.yangutils.parser.impl.TreeWalkListener;
29
30import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053031import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DESCRIPTION_DATA;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053032import static org.onosproject.yangutils.datamodel.utils.YangConstructType.NOTIFICATION_DATA;
33import static org.onosproject.yangutils.datamodel.utils.YangConstructType.REFERENCE_DATA;
34import static org.onosproject.yangutils.datamodel.utils.YangConstructType.STATUS_DATA;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Bharat saraswal63f26fb2016-04-05 15:13:44 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
43import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
45import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
46import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053047import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangNotificationNode;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053048
49/*
50 * Reference: RFC6020 and YANG ANTLR Grammar
51 *
52 * ABNF grammar as per RFC6020
53 * notification-stmt = notification-keyword sep
54 * identifier-arg-str optsep
55 * (";" /
56 * "{" stmtsep
57 * ;; these stmts can appear in any order
58 * *(if-feature-stmt stmtsep)
59 * [status-stmt stmtsep]
60 * [description-stmt stmtsep]
61 * [reference-stmt stmtsep]
62 * *((typedef-stmt /
63 * grouping-stmt) stmtsep)
64 * *(data-def-stmt stmtsep)
65 * "}")
66 *
67 * ANTLR grammar rule
68 * notificationStatement : NOTIFICATION_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement
69 * | statusStatement | descriptionStatement | referenceStatement | typedefStatement
70 * | groupingStatement | dataDefStatement)* RIGHT_CURLY_BRACE);
71 */
72
73/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053074 * Represents listener based call back function corresponding to the "notification"
Vidyashree Rama620b6e92016-03-28 11:59:27 +053075 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
76 */
77public final class NotificationListener {
78
79 /**
80 * Creates a new notification listener.
81 */
82 private NotificationListener() {
83 }
84
85 /**
86 * It is called when parser receives an input matching the grammar rule
87 * (notification), performs validation and updates the data model tree.
88 *
89 * @param listener listener's object
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053090 * @param ctx context object of the grammar rule
Vidyashree Rama620b6e92016-03-28 11:59:27 +053091 */
92 public static void processNotificationEntry(TreeWalkListener listener,
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053093 GeneratedYangParser.NotificationStatementContext ctx) {
Vidyashree Rama620b6e92016-03-28 11:59:27 +053094
95 // Check for stack to be non empty.
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053096 checkStackIsNotEmpty(listener, MISSING_HOLDER, NOTIFICATION_DATA,
97 ctx.identifier().getText(), ENTRY);
Vidyashree Rama620b6e92016-03-28 11:59:27 +053098
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053099 String identifier = getValidIdentifier(ctx.identifier().getText(),
100 NOTIFICATION_DATA, ctx);
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530101
102 // Validate sub statement cardinality.
103 validateSubStatementsCardinality(ctx);
104
105 // Check for identifier collision
106 int line = ctx.getStart().getLine();
107 int charPositionInLine = ctx.getStart().getCharPositionInLine();
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530108 detectCollidingChildUtil(listener, line, charPositionInLine, identifier,
109 NOTIFICATION_DATA);
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530110
111 Parsable curData = listener.getParsedDataStack().peek();
112 if (curData instanceof YangModule || curData instanceof YangSubModule) {
113
114 YangNotification notification = getYangNotificationNode(JAVA_GENERATION);
115 notification.setName(identifier);
Bharat saraswale3175d32016-08-31 17:50:11 +0530116
117 notification.setLineNumber(ctx.getStart().getLine());
118 notification.setCharPosition(ctx.getStart().getCharPositionInLine());
119 notification.setFileName(listener.getFileName());
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530120 ((RpcNotificationContainer) curData).setNotificationPresenceFlag(true);
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530121 YangNode curNode = (YangNode) curData;
122 try {
123 curNode.addChild(notification);
124 } catch (DataModelException e) {
125 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
126 NOTIFICATION_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
127 }
128 listener.getParsedDataStack().push(notification);
129 } else {
130 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, NOTIFICATION_DATA,
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530131 ctx.identifier().getText(), ENTRY));
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530132 }
133 }
134
135 /**
136 * It is called when parser exits from grammar rule (notification), it perform
137 * validations and updates the data model tree.
138 *
139 * @param listener listener's object
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530140 * @param ctx context object of the grammar rule
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530141 */
142 public static void processNotificationExit(TreeWalkListener listener,
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530143 GeneratedYangParser.NotificationStatementContext ctx) {
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530144
145 // Check for stack to be non empty.
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530146 checkStackIsNotEmpty(listener, MISSING_HOLDER, NOTIFICATION_DATA,
147 ctx.identifier().getText(), EXIT);
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530148
149 if (listener.getParsedDataStack().peek() instanceof YangNotification) {
150 listener.getParsedDataStack().pop();
151 } else {
152 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, NOTIFICATION_DATA,
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530153 ctx.identifier().getText(), EXIT));
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530154 }
155 }
156
157 /**
158 * Validates the cardinality of notification sub-statements as per grammar.
159 *
160 * @param ctx context object of the grammar rule
161 */
162 private static void validateSubStatementsCardinality(GeneratedYangParser.NotificationStatementContext ctx) {
163
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530164 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA,
165 NOTIFICATION_DATA, ctx.identifier().getText());
166 validateCardinalityMaxOne(ctx.descriptionStatement(),
167 DESCRIPTION_DATA, NOTIFICATION_DATA,
168 ctx.identifier().getText());
169 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA,
170 NOTIFICATION_DATA,
171 ctx.identifier().getText());
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530172 }
173}