Vidyashree Rama
Committed by Gerrit Code Review

yang Notification Listener

Change-Id: I1073be0342bee083495e2c103bad8351f492441c
...@@ -25,6 +25,7 @@ import org.onosproject.yangutils.datamodel.YangModule; ...@@ -25,6 +25,7 @@ import org.onosproject.yangutils.datamodel.YangModule;
25 import org.onosproject.yangutils.datamodel.YangSubModule; 25 import org.onosproject.yangutils.datamodel.YangSubModule;
26 import org.onosproject.yangutils.datamodel.YangTypeDef; 26 import org.onosproject.yangutils.datamodel.YangTypeDef;
27 import org.onosproject.yangutils.datamodel.YangUses; 27 import org.onosproject.yangutils.datamodel.YangUses;
28 +import org.onosproject.yangutils.datamodel.YangNotification;
28 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment; 29 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment;
29 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaCase; 30 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaCase;
30 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaChoice; 31 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaChoice;
...@@ -35,6 +36,7 @@ import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule; ...@@ -35,6 +36,7 @@ import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
35 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule; 36 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
36 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef; 37 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef;
37 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUses; 38 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUses;
39 +import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaNotification;
38 40
39 /** 41 /**
40 * Factory to create data model objects based on the target file type. 42 * Factory to create data model objects based on the target file type.
...@@ -51,8 +53,8 @@ public final class YangDataModelFactory { ...@@ -51,8 +53,8 @@ public final class YangDataModelFactory {
51 * Based on the target language generate the inherited data model node. 53 * Based on the target language generate the inherited data model node.
52 * 54 *
53 * @param targetLanguage target language in which YANG mapping needs to be 55 * @param targetLanguage target language in which YANG mapping needs to be
54 - * generated. 56 + * generated
55 - * @return the corresponding inherited node based on the target language. 57 + * @return the corresponding inherited node based on the target language
56 */ 58 */
57 public static YangModule getYangModuleNode(GeneratedLanguage targetLanguage) { 59 public static YangModule getYangModuleNode(GeneratedLanguage targetLanguage) {
58 switch (targetLanguage) { 60 switch (targetLanguage) {
...@@ -69,8 +71,8 @@ public final class YangDataModelFactory { ...@@ -69,8 +71,8 @@ public final class YangDataModelFactory {
69 * Based on the target language generate the inherited data model node. 71 * Based on the target language generate the inherited data model node.
70 * 72 *
71 * @param targetLanguage target language in which YANG mapping needs to be 73 * @param targetLanguage target language in which YANG mapping needs to be
72 - * generated. 74 + * generated
73 - * @return the corresponding inherited node based on the target language. 75 + * @return the corresponding inherited node based on the target language
74 */ 76 */
75 public static YangAugment getYangAugmentNode(GeneratedLanguage targetLanguage) { 77 public static YangAugment getYangAugmentNode(GeneratedLanguage targetLanguage) {
76 switch (targetLanguage) { 78 switch (targetLanguage) {
...@@ -87,8 +89,8 @@ public final class YangDataModelFactory { ...@@ -87,8 +89,8 @@ public final class YangDataModelFactory {
87 * Based on the target language generate the inherited data model node. 89 * Based on the target language generate the inherited data model node.
88 * 90 *
89 * @param targetLanguage target language in which YANG mapping needs to be 91 * @param targetLanguage target language in which YANG mapping needs to be
90 - * generated. 92 + * generated
91 - * @return the corresponding inherited node based on the target language. 93 + * @return the corresponding inherited node based on the target language
92 */ 94 */
93 public static YangCase getYangCaseNode(GeneratedLanguage targetLanguage) { 95 public static YangCase getYangCaseNode(GeneratedLanguage targetLanguage) {
94 switch (targetLanguage) { 96 switch (targetLanguage) {
...@@ -105,8 +107,8 @@ public final class YangDataModelFactory { ...@@ -105,8 +107,8 @@ public final class YangDataModelFactory {
105 * Based on the target language generate the inherited data model node. 107 * Based on the target language generate the inherited data model node.
106 * 108 *
107 * @param targetLanguage target language in which YANG mapping needs to be 109 * @param targetLanguage target language in which YANG mapping needs to be
108 - * generated. 110 + * generated
109 - * @return the corresponding inherited node based on the target language. 111 + * @return the corresponding inherited node based on the target language
110 */ 112 */
111 public static YangChoice getYangChoiceNode(GeneratedLanguage targetLanguage) { 113 public static YangChoice getYangChoiceNode(GeneratedLanguage targetLanguage) {
112 switch (targetLanguage) { 114 switch (targetLanguage) {
...@@ -123,8 +125,8 @@ public final class YangDataModelFactory { ...@@ -123,8 +125,8 @@ public final class YangDataModelFactory {
123 * Based on the target language generate the inherited data model node. 125 * Based on the target language generate the inherited data model node.
124 * 126 *
125 * @param targetLanguage target language in which YANG mapping needs to be 127 * @param targetLanguage target language in which YANG mapping needs to be
126 - * generated. 128 + * generated
127 - * @return the corresponding inherited node based on the target language. 129 + * @return the corresponding inherited node based on the target language
128 */ 130 */
129 public static YangContainer getYangContainerNode(GeneratedLanguage targetLanguage) { 131 public static YangContainer getYangContainerNode(GeneratedLanguage targetLanguage) {
130 switch (targetLanguage) { 132 switch (targetLanguage) {
...@@ -141,8 +143,8 @@ public final class YangDataModelFactory { ...@@ -141,8 +143,8 @@ public final class YangDataModelFactory {
141 * Based on the target language generate the inherited data model node. 143 * Based on the target language generate the inherited data model node.
142 * 144 *
143 * @param targetLanguage target language in which YANG mapping needs to be 145 * @param targetLanguage target language in which YANG mapping needs to be
144 - * generated. 146 + * generated
145 - * @return the corresponding inherited node based on the target language. 147 + * @return the corresponding inherited node based on the target language
146 */ 148 */
147 public static YangGrouping getYangGroupingNode(GeneratedLanguage targetLanguage) { 149 public static YangGrouping getYangGroupingNode(GeneratedLanguage targetLanguage) {
148 switch (targetLanguage) { 150 switch (targetLanguage) {
...@@ -159,8 +161,8 @@ public final class YangDataModelFactory { ...@@ -159,8 +161,8 @@ public final class YangDataModelFactory {
159 * Based on the target language generate the inherited data model node. 161 * Based on the target language generate the inherited data model node.
160 * 162 *
161 * @param targetLanguage target language in which YANG mapping needs to be 163 * @param targetLanguage target language in which YANG mapping needs to be
162 - * generated. 164 + * generated
163 - * @return the corresponding inherited node based on the target language. 165 + * @return the corresponding inherited node based on the target language
164 */ 166 */
165 public static YangList getYangListNode(GeneratedLanguage targetLanguage) { 167 public static YangList getYangListNode(GeneratedLanguage targetLanguage) {
166 switch (targetLanguage) { 168 switch (targetLanguage) {
...@@ -177,8 +179,8 @@ public final class YangDataModelFactory { ...@@ -177,8 +179,8 @@ public final class YangDataModelFactory {
177 * Based on the target language generate the inherited data model node. 179 * Based on the target language generate the inherited data model node.
178 * 180 *
179 * @param targetLanguage target language in which YANG mapping needs to be 181 * @param targetLanguage target language in which YANG mapping needs to be
180 - * generated. 182 + * generated
181 - * @return the corresponding inherited node based on the target language. 183 + * @return the corresponding inherited node based on the target language
182 */ 184 */
183 public static YangSubModule getYangSubModuleNode(GeneratedLanguage targetLanguage) { 185 public static YangSubModule getYangSubModuleNode(GeneratedLanguage targetLanguage) {
184 switch (targetLanguage) { 186 switch (targetLanguage) {
...@@ -195,8 +197,8 @@ public final class YangDataModelFactory { ...@@ -195,8 +197,8 @@ public final class YangDataModelFactory {
195 * Based on the target language generate the inherited data model node. 197 * Based on the target language generate the inherited data model node.
196 * 198 *
197 * @param targetLanguage target language in which YANG mapping needs to be 199 * @param targetLanguage target language in which YANG mapping needs to be
198 - * generated. 200 + * generated
199 - * @return the corresponding inherited node based on the target language. 201 + * @return the corresponding inherited node based on the target language
200 */ 202 */
201 public static YangTypeDef getYangTypeDefNode(GeneratedLanguage targetLanguage) { 203 public static YangTypeDef getYangTypeDefNode(GeneratedLanguage targetLanguage) {
202 switch (targetLanguage) { 204 switch (targetLanguage) {
...@@ -213,8 +215,8 @@ public final class YangDataModelFactory { ...@@ -213,8 +215,8 @@ public final class YangDataModelFactory {
213 * Based on the target language generate the inherited data model node. 215 * Based on the target language generate the inherited data model node.
214 * 216 *
215 * @param targetLanguage target language in which YANG mapping needs to be 217 * @param targetLanguage target language in which YANG mapping needs to be
216 - * generated. 218 + * generated
217 - * @return the corresponding inherited node based on the target language. 219 + * @return the corresponding inherited node based on the target language
218 */ 220 */
219 public static YangUses getYangUsesNode(GeneratedLanguage targetLanguage) { 221 public static YangUses getYangUsesNode(GeneratedLanguage targetLanguage) {
220 switch (targetLanguage) { 222 switch (targetLanguage) {
...@@ -226,4 +228,22 @@ public final class YangDataModelFactory { ...@@ -226,4 +228,22 @@ public final class YangDataModelFactory {
226 } 228 }
227 } 229 }
228 } 230 }
231 +
232 + /**
233 + * Based on the target language generate the inherited data model node.
234 + *
235 + * @param targetLanguage target language in which YANG mapping needs to be
236 + * generated
237 + * @return the corresponding inherited node based on the target language
238 + */
239 + public static YangNotification getYangNotificationNode(GeneratedLanguage targetLanguage) {
240 + switch (targetLanguage) {
241 + case JAVA_GENERATION: {
242 + return new YangJavaNotification();
243 + }
244 + default: {
245 + throw new RuntimeException("Only YANG to Java is supported.");
246 + }
247 + }
248 + }
229 } 249 }
......
...@@ -49,6 +49,7 @@ import org.onosproject.yangutils.parser.impl.listeners.MandatoryListener; ...@@ -49,6 +49,7 @@ import org.onosproject.yangutils.parser.impl.listeners.MandatoryListener;
49 import org.onosproject.yangutils.parser.impl.listeners.MaxElementsListener; 49 import org.onosproject.yangutils.parser.impl.listeners.MaxElementsListener;
50 import org.onosproject.yangutils.parser.impl.listeners.MinElementsListener; 50 import org.onosproject.yangutils.parser.impl.listeners.MinElementsListener;
51 import org.onosproject.yangutils.parser.impl.listeners.ModuleListener; 51 import org.onosproject.yangutils.parser.impl.listeners.ModuleListener;
52 +import org.onosproject.yangutils.parser.impl.listeners.NotificationListener;
52 import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener; 53 import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener;
53 import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener; 54 import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener;
54 import org.onosproject.yangutils.parser.impl.listeners.PositionListener; 55 import org.onosproject.yangutils.parser.impl.listeners.PositionListener;
...@@ -1090,12 +1091,12 @@ public class TreeWalkListener implements GeneratedYangListener { ...@@ -1090,12 +1091,12 @@ public class TreeWalkListener implements GeneratedYangListener {
1090 1091
1091 @Override 1092 @Override
1092 public void enterNotificationStatement(GeneratedYangParser.NotificationStatementContext ctx) { 1093 public void enterNotificationStatement(GeneratedYangParser.NotificationStatementContext ctx) {
1093 - // TODO: implement the method. 1094 + NotificationListener.processNotificationEntry(this, ctx);
1094 } 1095 }
1095 1096
1096 @Override 1097 @Override
1097 public void exitNotificationStatement(GeneratedYangParser.NotificationStatementContext ctx) { 1098 public void exitNotificationStatement(GeneratedYangParser.NotificationStatementContext ctx) {
1098 - // TODO: implement the method. 1099 + NotificationListener.processNotificationExit(this, ctx);
1099 } 1100 }
1100 1101
1101 @Override 1102 @Override
......
...@@ -21,6 +21,10 @@ import org.onosproject.yangutils.datamodel.YangContainer; ...@@ -21,6 +21,10 @@ import org.onosproject.yangutils.datamodel.YangContainer;
21 import org.onosproject.yangutils.datamodel.YangList; 21 import org.onosproject.yangutils.datamodel.YangList;
22 import org.onosproject.yangutils.datamodel.YangModule; 22 import org.onosproject.yangutils.datamodel.YangModule;
23 import org.onosproject.yangutils.datamodel.YangNode; 23 import org.onosproject.yangutils.datamodel.YangNode;
24 +import org.onosproject.yangutils.datamodel.YangNotification;
25 +import org.onosproject.yangutils.datamodel.YangInput;
26 +import org.onosproject.yangutils.datamodel.YangOutput;
27 +import org.onosproject.yangutils.datamodel.YangAugment;
24 import org.onosproject.yangutils.datamodel.exceptions.DataModelException; 28 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
25 import org.onosproject.yangutils.parser.Parsable; 29 import org.onosproject.yangutils.parser.Parsable;
26 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 30 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
...@@ -127,7 +131,10 @@ public final class ContainerListener { ...@@ -127,7 +131,10 @@ public final class ContainerListener {
127 131
128 Parsable curData = listener.getParsedDataStack().peek(); 132 Parsable curData = listener.getParsedDataStack().peek();
129 if (curData instanceof YangModule || curData instanceof YangContainer 133 if (curData instanceof YangModule || curData instanceof YangContainer
130 - || curData instanceof YangList || curData instanceof YangCase) { 134 + || curData instanceof YangList || curData instanceof YangCase
135 + || curData instanceof YangNotification
136 + || curData instanceof YangInput || curData instanceof YangOutput
137 + || curData instanceof YangAugment) {
131 YangNode curNode = (YangNode) curData; 138 YangNode curNode = (YangNode) curData;
132 try { 139 try {
133 curNode.addChild(container); 140 curNode.addChild(container);
......
...@@ -21,6 +21,10 @@ import org.onosproject.yangutils.datamodel.YangContainer; ...@@ -21,6 +21,10 @@ import org.onosproject.yangutils.datamodel.YangContainer;
21 import org.onosproject.yangutils.datamodel.YangList; 21 import org.onosproject.yangutils.datamodel.YangList;
22 import org.onosproject.yangutils.datamodel.YangModule; 22 import org.onosproject.yangutils.datamodel.YangModule;
23 import org.onosproject.yangutils.datamodel.YangNode; 23 import org.onosproject.yangutils.datamodel.YangNode;
24 +import org.onosproject.yangutils.datamodel.YangNotification;
25 +import org.onosproject.yangutils.datamodel.YangInput;
26 +import org.onosproject.yangutils.datamodel.YangOutput;
27 +import org.onosproject.yangutils.datamodel.YangAugment;
24 import org.onosproject.yangutils.datamodel.exceptions.DataModelException; 28 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
25 import org.onosproject.yangutils.parser.Parsable; 29 import org.onosproject.yangutils.parser.Parsable;
26 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 30 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
...@@ -134,7 +138,9 @@ public final class ListListener { ...@@ -134,7 +138,9 @@ public final class ListListener {
134 138
135 Parsable curData = listener.getParsedDataStack().peek(); 139 Parsable curData = listener.getParsedDataStack().peek();
136 if (curData instanceof YangModule || curData instanceof YangContainer 140 if (curData instanceof YangModule || curData instanceof YangContainer
137 - || curData instanceof YangList || curData instanceof YangCase) { 141 + || curData instanceof YangList || curData instanceof YangCase
142 + || curData instanceof YangNotification || curData instanceof YangInput
143 + || curData instanceof YangOutput || curData instanceof YangAugment) {
138 curNode = (YangNode) curData; 144 curNode = (YangNode) curData;
139 try { 145 try {
140 curNode.addChild(yangList); 146 curNode.addChild(yangList);
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
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 +
17 +package org.onosproject.yangutils.parser.impl.listeners;
18 +
19 +import org.onosproject.yangutils.datamodel.YangNotification;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangSubModule;
22 +import org.onosproject.yangutils.datamodel.YangNode;
23 +import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
24 +import org.onosproject.yangutils.parser.Parsable;
25 +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
26 +import org.onosproject.yangutils.parser.exceptions.ParserException;
27 +import org.onosproject.yangutils.parser.impl.TreeWalkListener;
28 +
29 +import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
30 +import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangNotificationNode;
31 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
32 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
33 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
34 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
35 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
36 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*;
37 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
38 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
39 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
40 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds;
41 +import static org.onosproject.yangutils.utils.YangConstructType.NOTIFICATION_DATA;
42 +import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
43 +import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
44 +import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
45 +import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA;
46 +import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA;
47 +
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 +/**
73 + * Implements listener based call back function corresponding to the "notification"
74 + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
75 + */
76 +public 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());
159 + validateMutuallyExclusiveChilds(ctx.typedefStatement(), TYPEDEF_DATA, ctx.groupingStatement(), GROUPING_DATA,
160 + NOTIFICATION_DATA, ctx.identifier().getText());
161 + }
162 +}
...@@ -25,6 +25,10 @@ import org.onosproject.yangutils.datamodel.YangNode; ...@@ -25,6 +25,10 @@ import org.onosproject.yangutils.datamodel.YangNode;
25 import org.onosproject.yangutils.datamodel.YangSubModule; 25 import org.onosproject.yangutils.datamodel.YangSubModule;
26 import org.onosproject.yangutils.datamodel.YangType; 26 import org.onosproject.yangutils.datamodel.YangType;
27 import org.onosproject.yangutils.datamodel.YangTypeDef; 27 import org.onosproject.yangutils.datamodel.YangTypeDef;
28 +import org.onosproject.yangutils.datamodel.YangInput;
29 +import org.onosproject.yangutils.datamodel.YangOutput;
30 +import org.onosproject.yangutils.datamodel.YangNotification;
31 +import org.onosproject.yangutils.datamodel.YangRpc;
28 import org.onosproject.yangutils.datamodel.exceptions.DataModelException; 32 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
29 import org.onosproject.yangutils.parser.Parsable; 33 import org.onosproject.yangutils.parser.Parsable;
30 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 34 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
...@@ -129,9 +133,11 @@ public final class TypeDefListener { ...@@ -129,9 +133,11 @@ public final class TypeDefListener {
129 Parsable curData = listener.getParsedDataStack().peek(); 133 Parsable curData = listener.getParsedDataStack().peek();
130 134
131 if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangContainer 135 if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangContainer
132 - || curData instanceof YangList) { 136 + || curData instanceof YangList || curData instanceof YangNotification || curData instanceof YangRpc
137 + || curData instanceof YangInput || curData instanceof YangOutput) {
138 +
133 /* 139 /*
134 - * TODO YangGrouping, YangRpc, YangInput, YangOutput, Notification. 140 + * TODO YangGrouping.
135 */ 141 */
136 YangNode curNode = (YangNode) curData; 142 YangNode curNode = (YangNode) curData;
137 try { 143 try {
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
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 +
17 +package org.onosproject.yangutils.translator.tojava.javamodel;
18 +
19 +import java.io.IOException;
20 +import org.onosproject.yangutils.datamodel.YangNotification;
21 +import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
22 +import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
23 +import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
24 +import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
25 +import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
26 +import org.onosproject.yangutils.translator.tojava.JavaImportData;
27 +import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
28 +
29 +import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
30 +import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
31 +import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
32 +import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
33 +import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
34 +import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
35 +
36 +/**
37 + * Notification information extended to support java code generation.
38 + */
39 +public class YangJavaNotification extends YangNotification
40 + implements JavaCodeGenerator, HasJavaFileInfo,
41 + HasJavaImportData, HasTempJavaCodeFragmentFiles {
42 +
43 + /**
44 + * Contains information of the java file being generated.
45 + */
46 + private JavaFileInfo javaFileInfo;
47 +
48 + /**
49 + * Contains information of the imports to be inserted in the java file
50 + * generated.
51 + */
52 + private JavaImportData javaImportData;
53 +
54 + /**
55 + * File handle to maintain temporary java code fragments as per the code
56 + * snippet types.
57 + */
58 + private TempJavaCodeFragmentFiles tempFileHandle;
59 +
60 + /**
61 + * Creates an instance of java Notification.
62 + */
63 + public YangJavaNotification() {
64 + super();
65 + setJavaFileInfo(new JavaFileInfo());
66 + setJavaImportData(new JavaImportData());
67 + getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
68 + }
69 +
70 + /**
71 + * Returns the generated java file information.
72 + *
73 + * @return generated java file information
74 + */
75 + @Override
76 + public JavaFileInfo getJavaFileInfo() {
77 +
78 + if (javaFileInfo == null) {
79 + throw new RuntimeException("Missing java info in java datamodel node");
80 + }
81 + return javaFileInfo;
82 + }
83 +
84 + /**
85 + * Set the java file info object.
86 + *
87 + * @param javaInfo java file info object
88 + */
89 + @Override
90 + public void setJavaFileInfo(JavaFileInfo javaInfo) {
91 +
92 + javaFileInfo = javaInfo;
93 + }
94 +
95 + /**
96 + * Returns the data of java imports to be included in generated file.
97 + *
98 + * @return data of java imports to be included in generated file
99 + */
100 + @Override
101 + public JavaImportData getJavaImportData() {
102 +
103 + return javaImportData;
104 + }
105 +
106 + /**
107 + * Set the data of java imports to be included in generated file.
108 + *
109 + * @param javaImportData data of java imports to be included in generated
110 + * file
111 + */
112 + @Override
113 + public void setJavaImportData(JavaImportData javaImportData) {
114 +
115 + this.javaImportData = javaImportData;
116 + }
117 +
118 + /**
119 + * Returns the temporary file handle.
120 + *
121 + * @return temporary file handle
122 + */
123 + @Override
124 + public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
125 +
126 + if (tempFileHandle == null) {
127 + throw new RuntimeException("Missing temporary file handle for" +
128 + "current node " + getJavaFileInfo().getJavaName());
129 + }
130 + return tempFileHandle;
131 + }
132 +
133 + /**
134 + * Set temporary file handle.
135 + *
136 + * @param fileHandle temporary file handle
137 + */
138 + @Override
139 + public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
140 +
141 + tempFileHandle = fileHandle;
142 + }
143 +
144 + /**
145 + * Prepare the information for java code generation corresponding to YANG
146 + * notification info.
147 + *
148 + * @param codeGenDir code generation directory
149 + * @throws IOException IO operation fail
150 + */
151 + @Override
152 + public void generateCodeEntry(String codeGenDir) throws IOException {
153 +
154 + getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
155 + getJavaFileInfo().setPackage(getCurNodePackage(this));
156 + getJavaFileInfo().setPackageFilePath(
157 + getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
158 + getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
159 +
160 + String absolutePath = getAbsolutePackagePath(
161 + getJavaFileInfo().getBaseCodeGenPath(),
162 + getJavaFileInfo().getPackageFilePath());
163 +
164 + setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
165 + getJavaFileInfo().getGeneratedFileTypes(), absolutePath,
166 + getJavaFileInfo().getJavaName()));
167 +
168 + getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
169 +
170 + getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false);
171 + }
172 +
173 + /**
174 + * Create a java file using the YANG notification info.
175 + */
176 + @Override
177 + public void generateCodeExit() {
178 + // TODO Auto-generated method stub
179 +
180 + }
181 +}
...@@ -1132,9 +1132,9 @@ package org.onosproject.yangutils.parser.antlrgencode; ...@@ -1132,9 +1132,9 @@ package org.onosproject.yangutils.parser.antlrgencode;
1132 * "}") 1132 * "}")
1133 * TODO : 0..1 occurance to be checked in listener 1133 * TODO : 0..1 occurance to be checked in listener
1134 */ 1134 */
1135 - notificationStatement : NOTIFICATION_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement | statusStatement 1135 + notificationStatement : NOTIFICATION_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement
1136 - | descriptionStatement | referenceStatement | typedefStatement | groupingStatement | dataDefStatement)* 1136 + | statusStatement | descriptionStatement | referenceStatement | typedefStatement
1137 - RIGHT_CURLY_BRACE); 1137 + | groupingStatement | dataDefStatement)* RIGHT_CURLY_BRACE);
1138 1138
1139 /** 1139 /**
1140 * deviation-stmt = deviation-keyword sep 1140 * deviation-stmt = deviation-keyword sep
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
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 +
17 +package org.onosproject.yangutils.parser.impl.listeners;
18 +
19 +import org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangNode;
21 +import org.onosproject.yangutils.datamodel.YangModule;
22 +import org.onosproject.yangutils.datamodel.YangNotification;
23 +import org.onosproject.yangutils.datamodel.YangStatusType;
24 +import org.onosproject.yangutils.datamodel.YangNodeType;
25 +import org.onosproject.yangutils.datamodel.YangTypeDef;
26 +import org.onosproject.yangutils.datamodel.YangLeaf;
27 +import org.onosproject.yangutils.datamodel.YangDataTypes;
28 +import org.onosproject.yangutils.parser.exceptions.ParserException;
29 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
30 +
31 +import java.io.IOException;
32 +import java.util.ListIterator;
33 +
34 +import static org.hamcrest.core.Is.is;
35 +import static org.junit.Assert.assertThat;
36 +
37 +/**
38 + * Test cases for testing notification listener functionality.
39 + */
40 +public class NotificationListenerTest {
41 +
42 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
43 +
44 + /**
45 + * Checks valid notification statement.
46 + */
47 + @Test
48 + public void processValidNotificationStatement() throws IOException, ParserException {
49 +
50 + YangNode node = manager.getDataModel("src/test/resources/ValidNotificationStatement.yang");
51 +
52 + assertThat((node instanceof YangModule), is(true));
53 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
54 + YangModule yangNode = (YangModule) node;
55 + assertThat(yangNode.getName(), is("rock"));
56 +
57 + YangNotification yangNotification = (YangNotification) yangNode.getChild();
58 + assertThat(yangNotification.getName(), is("link-failure"));
59 + assertThat(yangNotification.getDescription(), is("\"A link failure has been detected\""));
60 + assertThat(yangNotification.getStatus(), is(YangStatusType.DEPRECATED));
61 + assertThat(yangNotification.getReference(), is("\"reference\""));
62 +
63 + YangTypeDef typeDef = (YangTypeDef) yangNotification.getChild();
64 + assertThat(typeDef.getName(), is("my-type"));
65 + assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED));
66 + assertThat(typeDef.getDerivedType().getDataTypeExtendedInfo()
67 + .getBaseType().getDataType(), is(YangDataTypes.INT32));
68 +
69 + ListIterator<YangLeaf> leafIterator = yangNotification.getListOfLeaf().listIterator();
70 + YangLeaf leafInfo = leafIterator.next();
71 +
72 + assertThat(leafInfo.getLeafName(), is("if-name"));
73 + assertThat(leafInfo.getDataType().getDataTypeName(), is("leafref"));
74 + }
75 +}
1 +module rock {
2 + namespace "http://example.net/rock";
3 + prefix "rock";
4 +
5 + notification link-failure {
6 + description "A link failure has been detected";
7 + status deprecated;
8 + reference "reference";
9 + typedef my-type {
10 + status deprecated;
11 + type int32;
12 + }
13 + leaf if-name {
14 + type leafref {
15 + path "/interface/name";
16 + }
17 + }
18 + leaf if-admin-status {
19 + type admin-status;
20 + }
21 + leaf if-oper-status {
22 + type oper-status;
23 + }
24 + }
25 +}