blob: 4ed74938f3c38813cedbe04e6d79268303f28265 [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;
Bharat saraswal96dfef02016-06-16 00:29:12 +053024import org.onosproject.yangutils.datamodel.utils.Parsable;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053025import 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;
Bharat saraswal96dfef02016-06-16 00:29:12 +053030import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DESCRIPTION_DATA;
Bharat saraswal96dfef02016-06-16 00:29:12 +053031import static org.onosproject.yangutils.datamodel.utils.YangConstructType.NOTIFICATION_DATA;
32import static org.onosproject.yangutils.datamodel.utils.YangConstructType.REFERENCE_DATA;
33import static org.onosproject.yangutils.datamodel.utils.YangConstructType.STATUS_DATA;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Bharat saraswald9822e92016-04-05 15:13:44 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
44import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
45import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Bharat saraswal96dfef02016-06-16 00:29:12 +053046import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangNotificationNode;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053047
48/*
49 * Reference: RFC6020 and YANG ANTLR Grammar
50 *
51 * ABNF grammar as per RFC6020
52 * notification-stmt = notification-keyword sep
53 * identifier-arg-str optsep
54 * (";" /
55 * "{" stmtsep
56 * ;; these stmts can appear in any order
57 * *(if-feature-stmt stmtsep)
58 * [status-stmt stmtsep]
59 * [description-stmt stmtsep]
60 * [reference-stmt stmtsep]
61 * *((typedef-stmt /
62 * grouping-stmt) stmtsep)
63 * *(data-def-stmt stmtsep)
64 * "}")
65 *
66 * ANTLR grammar rule
67 * notificationStatement : NOTIFICATION_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement
68 * | statusStatement | descriptionStatement | referenceStatement | typedefStatement
69 * | groupingStatement | dataDefStatement)* RIGHT_CURLY_BRACE);
70 */
71
72/**
Bharat saraswald9822e92016-04-05 15:13:44 +053073 * Represents listener based call back function corresponding to the "notification"
Vidyashree Rama506cbe12016-03-28 11:59:27 +053074 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
75 */
76public final class NotificationListener {
77
78 /**
79 * Creates a new notification listener.
80 */
81 private NotificationListener() {
82 }
83
84 /**
85 * It is called when parser receives an input matching the grammar rule
86 * (notification), performs validation and updates the data model tree.
87 *
88 * @param listener listener's object
89 * @param ctx context object of the grammar rule
90 */
91 public static void processNotificationEntry(TreeWalkListener listener,
92 GeneratedYangParser.NotificationStatementContext ctx) {
93
94 // Check for stack to be non empty.
95 checkStackIsNotEmpty(listener, MISSING_HOLDER, NOTIFICATION_DATA, ctx.identifier().getText(), ENTRY);
96
97 String identifier = getValidIdentifier(ctx.identifier().getText(), NOTIFICATION_DATA, ctx);
98
99 // Validate sub statement cardinality.
100 validateSubStatementsCardinality(ctx);
101
102 // Check for identifier collision
103 int line = ctx.getStart().getLine();
104 int charPositionInLine = ctx.getStart().getCharPositionInLine();
105 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, NOTIFICATION_DATA);
106
107 Parsable curData = listener.getParsedDataStack().peek();
108 if (curData instanceof YangModule || curData instanceof YangSubModule) {
109
110 YangNotification notification = getYangNotificationNode(JAVA_GENERATION);
111 notification.setName(identifier);
112 YangNode curNode = (YangNode) curData;
113 try {
114 curNode.addChild(notification);
115 } catch (DataModelException e) {
116 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
117 NOTIFICATION_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
118 }
119 listener.getParsedDataStack().push(notification);
120 } else {
121 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, NOTIFICATION_DATA,
122 ctx.identifier().getText(), ENTRY));
123 }
124 }
125
126 /**
127 * It is called when parser exits from grammar rule (notification), it perform
128 * validations and updates the data model tree.
129 *
130 * @param listener listener's object
131 * @param ctx context object of the grammar rule
132 */
133 public static void processNotificationExit(TreeWalkListener listener,
134 GeneratedYangParser.NotificationStatementContext ctx) {
135
136 // Check for stack to be non empty.
137 checkStackIsNotEmpty(listener, MISSING_HOLDER, NOTIFICATION_DATA, ctx.identifier().getText(), EXIT);
138
139 if (listener.getParsedDataStack().peek() instanceof YangNotification) {
140 listener.getParsedDataStack().pop();
141 } else {
142 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, NOTIFICATION_DATA,
143 ctx.identifier().getText(), EXIT));
144 }
145 }
146
147 /**
148 * Validates the cardinality of notification sub-statements as per grammar.
149 *
150 * @param ctx context object of the grammar rule
151 */
152 private static void validateSubStatementsCardinality(GeneratedYangParser.NotificationStatementContext ctx) {
153
154 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, NOTIFICATION_DATA, ctx.identifier().getText());
155 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, NOTIFICATION_DATA,
156 ctx.identifier().getText());
157 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, NOTIFICATION_DATA,
158 ctx.identifier().getText());
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530159 }
160}