blob: 58daefb10994faccfcf43eb40ed21a01572eed55 [file] [log] [blame]
Vidyashree Rama506cbe12016-03-28 11:59:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama506cbe12016-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
Vidyashree Rama506cbe12016-03-28 11:59:27 +053019import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053020import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswald9822e92016-04-05 15:13:44 +053021import org.onosproject.yangutils.datamodel.YangNotification;
22import org.onosproject.yangutils.datamodel.YangSubModule;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053023import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
24import org.onosproject.yangutils.parser.Parsable;
25import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
26import org.onosproject.yangutils.parser.exceptions.ParserException;
27import org.onosproject.yangutils.parser.impl.TreeWalkListener;
28
29import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
30import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangNotificationNode;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Bharat saraswald9822e92016-04-05 15:13:44 +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 Rama506cbe12016-03-28 11:59:27 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
43import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053044import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053045import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA;
Bharat saraswald9822e92016-04-05 15:13:44 +053046import static org.onosproject.yangutils.utils.YangConstructType.NOTIFICATION_DATA;
47import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
48import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
49import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053050
51/*
52 * Reference: RFC6020 and YANG ANTLR Grammar
53 *
54 * ABNF grammar as per RFC6020
55 * notification-stmt = notification-keyword sep
56 * identifier-arg-str optsep
57 * (";" /
58 * "{" stmtsep
59 * ;; these stmts can appear in any order
60 * *(if-feature-stmt stmtsep)
61 * [status-stmt stmtsep]
62 * [description-stmt stmtsep]
63 * [reference-stmt stmtsep]
64 * *((typedef-stmt /
65 * grouping-stmt) stmtsep)
66 * *(data-def-stmt stmtsep)
67 * "}")
68 *
69 * ANTLR grammar rule
70 * notificationStatement : NOTIFICATION_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement
71 * | statusStatement | descriptionStatement | referenceStatement | typedefStatement
72 * | groupingStatement | dataDefStatement)* RIGHT_CURLY_BRACE);
73 */
74
75/**
Bharat saraswald9822e92016-04-05 15:13:44 +053076 * Represents listener based call back function corresponding to the "notification"
Vidyashree Rama506cbe12016-03-28 11:59:27 +053077 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
78 */
79public final class NotificationListener {
80
81 /**
82 * Creates a new notification listener.
83 */
84 private NotificationListener() {
85 }
86
87 /**
88 * It is called when parser receives an input matching the grammar rule
89 * (notification), performs validation and updates the data model tree.
90 *
91 * @param listener listener's object
92 * @param ctx context object of the grammar rule
93 */
94 public static void processNotificationEntry(TreeWalkListener listener,
95 GeneratedYangParser.NotificationStatementContext ctx) {
96
97 // Check for stack to be non empty.
98 checkStackIsNotEmpty(listener, MISSING_HOLDER, NOTIFICATION_DATA, ctx.identifier().getText(), ENTRY);
99
100 String identifier = getValidIdentifier(ctx.identifier().getText(), NOTIFICATION_DATA, ctx);
101
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();
108 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, NOTIFICATION_DATA);
109
110 Parsable curData = listener.getParsedDataStack().peek();
111 if (curData instanceof YangModule || curData instanceof YangSubModule) {
112
113 YangNotification notification = getYangNotificationNode(JAVA_GENERATION);
114 notification.setName(identifier);
115 YangNode curNode = (YangNode) curData;
116 try {
117 curNode.addChild(notification);
118 } catch (DataModelException e) {
119 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
120 NOTIFICATION_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
121 }
122 listener.getParsedDataStack().push(notification);
123 } else {
124 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, NOTIFICATION_DATA,
125 ctx.identifier().getText(), ENTRY));
126 }
127 }
128
129 /**
130 * It is called when parser exits from grammar rule (notification), it perform
131 * validations and updates the data model tree.
132 *
133 * @param listener listener's object
134 * @param ctx context object of the grammar rule
135 */
136 public static void processNotificationExit(TreeWalkListener listener,
137 GeneratedYangParser.NotificationStatementContext ctx) {
138
139 // Check for stack to be non empty.
140 checkStackIsNotEmpty(listener, MISSING_HOLDER, NOTIFICATION_DATA, ctx.identifier().getText(), EXIT);
141
142 if (listener.getParsedDataStack().peek() instanceof YangNotification) {
143 listener.getParsedDataStack().pop();
144 } else {
145 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, NOTIFICATION_DATA,
146 ctx.identifier().getText(), EXIT));
147 }
148 }
149
150 /**
151 * Validates the cardinality of notification sub-statements as per grammar.
152 *
153 * @param ctx context object of the grammar rule
154 */
155 private static void validateSubStatementsCardinality(GeneratedYangParser.NotificationStatementContext ctx) {
156
157 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, NOTIFICATION_DATA, ctx.identifier().getText());
158 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, NOTIFICATION_DATA,
159 ctx.identifier().getText());
160 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, NOTIFICATION_DATA,
161 ctx.identifier().getText());
162 validateMutuallyExclusiveChilds(ctx.typedefStatement(), TYPEDEF_DATA, ctx.groupingStatement(), GROUPING_DATA,
163 NOTIFICATION_DATA, ctx.identifier().getText());
164 }
165}