Vidyashree Rama
Committed by Gerrit Code Review

yang Notification Listener

Change-Id: I1073be0342bee083495e2c103bad8351f492441c
......@@ -25,6 +25,7 @@ import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaCase;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaChoice;
......@@ -35,6 +36,7 @@ import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUses;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaNotification;
/**
* Factory to create data model objects based on the target file type.
......@@ -51,8 +53,8 @@ public final class YangDataModelFactory {
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangModule getYangModuleNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
......@@ -69,8 +71,8 @@ public final class YangDataModelFactory {
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangAugment getYangAugmentNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
......@@ -87,8 +89,8 @@ public final class YangDataModelFactory {
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangCase getYangCaseNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
......@@ -105,8 +107,8 @@ public final class YangDataModelFactory {
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangChoice getYangChoiceNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
......@@ -123,8 +125,8 @@ public final class YangDataModelFactory {
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangContainer getYangContainerNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
......@@ -141,8 +143,8 @@ public final class YangDataModelFactory {
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangGrouping getYangGroupingNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
......@@ -159,8 +161,8 @@ public final class YangDataModelFactory {
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangList getYangListNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
......@@ -177,8 +179,8 @@ public final class YangDataModelFactory {
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangSubModule getYangSubModuleNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
......@@ -195,8 +197,8 @@ public final class YangDataModelFactory {
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangTypeDef getYangTypeDefNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
......@@ -213,8 +215,8 @@ public final class YangDataModelFactory {
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangUses getYangUsesNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
......@@ -226,4 +228,22 @@ public final class YangDataModelFactory {
}
}
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangNotification getYangNotificationNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaNotification();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
}
......
......@@ -49,6 +49,7 @@ import org.onosproject.yangutils.parser.impl.listeners.MandatoryListener;
import org.onosproject.yangutils.parser.impl.listeners.MaxElementsListener;
import org.onosproject.yangutils.parser.impl.listeners.MinElementsListener;
import org.onosproject.yangutils.parser.impl.listeners.ModuleListener;
import org.onosproject.yangutils.parser.impl.listeners.NotificationListener;
import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener;
import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener;
import org.onosproject.yangutils.parser.impl.listeners.PositionListener;
......@@ -1090,12 +1091,12 @@ public class TreeWalkListener implements GeneratedYangListener {
@Override
public void enterNotificationStatement(GeneratedYangParser.NotificationStatementContext ctx) {
// TODO: implement the method.
NotificationListener.processNotificationEntry(this, ctx);
}
@Override
public void exitNotificationStatement(GeneratedYangParser.NotificationStatementContext ctx) {
// TODO: implement the method.
NotificationListener.processNotificationExit(this, ctx);
}
@Override
......
......@@ -21,6 +21,10 @@ import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangList;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.datamodel.YangOutput;
import org.onosproject.yangutils.datamodel.YangAugment;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
......@@ -127,7 +131,10 @@ public final class ContainerListener {
Parsable curData = listener.getParsedDataStack().peek();
if (curData instanceof YangModule || curData instanceof YangContainer
|| curData instanceof YangList || curData instanceof YangCase) {
|| curData instanceof YangList || curData instanceof YangCase
|| curData instanceof YangNotification
|| curData instanceof YangInput || curData instanceof YangOutput
|| curData instanceof YangAugment) {
YangNode curNode = (YangNode) curData;
try {
curNode.addChild(container);
......
......@@ -21,6 +21,10 @@ import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangList;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.datamodel.YangOutput;
import org.onosproject.yangutils.datamodel.YangAugment;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
......@@ -134,7 +138,9 @@ public final class ListListener {
Parsable curData = listener.getParsedDataStack().peek();
if (curData instanceof YangModule || curData instanceof YangContainer
|| curData instanceof YangList || curData instanceof YangCase) {
|| curData instanceof YangList || curData instanceof YangCase
|| curData instanceof YangNotification || curData instanceof YangInput
|| curData instanceof YangOutput || curData instanceof YangAugment) {
curNode = (YangNode) curData;
try {
curNode.addChild(yangList);
......
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangNotificationNode;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds;
import static org.onosproject.yangutils.utils.YangConstructType.NOTIFICATION_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
*
* ABNF grammar as per RFC6020
* notification-stmt = notification-keyword sep
* identifier-arg-str optsep
* (";" /
* "{" stmtsep
* ;; these stmts can appear in any order
* *(if-feature-stmt stmtsep)
* [status-stmt stmtsep]
* [description-stmt stmtsep]
* [reference-stmt stmtsep]
* *((typedef-stmt /
* grouping-stmt) stmtsep)
* *(data-def-stmt stmtsep)
* "}")
*
* ANTLR grammar rule
* notificationStatement : NOTIFICATION_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement
* | statusStatement | descriptionStatement | referenceStatement | typedefStatement
* | groupingStatement | dataDefStatement)* RIGHT_CURLY_BRACE);
*/
/**
* Implements listener based call back function corresponding to the "notification"
* rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
*/
public final class NotificationListener {
/**
* Creates a new notification listener.
*/
private NotificationListener() {
}
/**
* It is called when parser receives an input matching the grammar rule
* (notification), performs validation and updates the data model tree.
*
* @param listener listener's object
* @param ctx context object of the grammar rule
*/
public static void processNotificationEntry(TreeWalkListener listener,
GeneratedYangParser.NotificationStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, NOTIFICATION_DATA, ctx.identifier().getText(), ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), NOTIFICATION_DATA, ctx);
// Validate sub statement cardinality.
validateSubStatementsCardinality(ctx);
// Check for identifier collision
int line = ctx.getStart().getLine();
int charPositionInLine = ctx.getStart().getCharPositionInLine();
detectCollidingChildUtil(listener, line, charPositionInLine, identifier, NOTIFICATION_DATA);
Parsable curData = listener.getParsedDataStack().peek();
if (curData instanceof YangModule || curData instanceof YangSubModule) {
YangNotification notification = getYangNotificationNode(JAVA_GENERATION);
notification.setName(identifier);
YangNode curNode = (YangNode) curData;
try {
curNode.addChild(notification);
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
NOTIFICATION_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
}
listener.getParsedDataStack().push(notification);
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, NOTIFICATION_DATA,
ctx.identifier().getText(), ENTRY));
}
}
/**
* It is called when parser exits from grammar rule (notification), it perform
* validations and updates the data model tree.
*
* @param listener listener's object
* @param ctx context object of the grammar rule
*/
public static void processNotificationExit(TreeWalkListener listener,
GeneratedYangParser.NotificationStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, NOTIFICATION_DATA, ctx.identifier().getText(), EXIT);
if (listener.getParsedDataStack().peek() instanceof YangNotification) {
listener.getParsedDataStack().pop();
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, NOTIFICATION_DATA,
ctx.identifier().getText(), EXIT));
}
}
/**
* Validates the cardinality of notification sub-statements as per grammar.
*
* @param ctx context object of the grammar rule
*/
private static void validateSubStatementsCardinality(GeneratedYangParser.NotificationStatementContext ctx) {
validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, NOTIFICATION_DATA, ctx.identifier().getText());
validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, NOTIFICATION_DATA,
ctx.identifier().getText());
validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, NOTIFICATION_DATA,
ctx.identifier().getText());
validateMutuallyExclusiveChilds(ctx.typedefStatement(), TYPEDEF_DATA, ctx.groupingStatement(), GROUPING_DATA,
NOTIFICATION_DATA, ctx.identifier().getText());
}
}
......@@ -25,6 +25,10 @@ import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.datamodel.YangOutput;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.datamodel.YangRpc;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
......@@ -129,9 +133,11 @@ public final class TypeDefListener {
Parsable curData = listener.getParsedDataStack().peek();
if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangContainer
|| curData instanceof YangList) {
|| curData instanceof YangList || curData instanceof YangNotification || curData instanceof YangRpc
|| curData instanceof YangInput || curData instanceof YangOutput) {
/*
* TODO YangGrouping, YangRpc, YangInput, YangOutput, Notification.
* TODO YangGrouping.
*/
YangNode curNode = (YangNode) curData;
try {
......
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
/**
* Notification information extended to support java code generation.
*/
public class YangJavaNotification extends YangNotification
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Creates an instance of java Notification.
*/
public YangJavaNotification() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Returns the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Returns the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@Override
public void setJavaImportData(JavaImportData javaImportData) {
this.javaImportData = javaImportData;
}
/**
* Returns the temporary file handle.
*
* @return temporary file handle
*/
@Override
public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
if (tempFileHandle == null) {
throw new RuntimeException("Missing temporary file handle for" +
"current node " + getJavaFileInfo().getJavaName());
}
return tempFileHandle;
}
/**
* Set temporary file handle.
*
* @param fileHandle temporary file handle
*/
@Override
public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
tempFileHandle = fileHandle;
}
/**
* Prepare the information for java code generation corresponding to YANG
* notification info.
*
* @param codeGenDir code generation directory
* @throws IOException IO operation fail
*/
@Override
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
String absolutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absolutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false);
}
/**
* Create a java file using the YANG notification info.
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
}
}
......@@ -1132,9 +1132,9 @@ package org.onosproject.yangutils.parser.antlrgencode;
* "}")
* TODO : 0..1 occurance to be checked in listener
*/
notificationStatement : NOTIFICATION_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement | statusStatement
| descriptionStatement | referenceStatement | typedefStatement | groupingStatement | dataDefStatement)*
RIGHT_CURLY_BRACE);
notificationStatement : NOTIFICATION_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement
| statusStatement | descriptionStatement | referenceStatement | typedefStatement
| groupingStatement | dataDefStatement)* RIGHT_CURLY_BRACE);
/**
* deviation-stmt = deviation-keyword sep
......
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.parser.impl.listeners;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.datamodel.YangStatusType;
import org.onosproject.yangutils.datamodel.YangNodeType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import java.io.IOException;
import java.util.ListIterator;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
/**
* Test cases for testing notification listener functionality.
*/
public class NotificationListenerTest {
private final YangUtilsParserManager manager = new YangUtilsParserManager();
/**
* Checks valid notification statement.
*/
@Test
public void processValidNotificationStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/ValidNotificationStatement.yang");
assertThat((node instanceof YangModule), is(true));
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("rock"));
YangNotification yangNotification = (YangNotification) yangNode.getChild();
assertThat(yangNotification.getName(), is("link-failure"));
assertThat(yangNotification.getDescription(), is("\"A link failure has been detected\""));
assertThat(yangNotification.getStatus(), is(YangStatusType.DEPRECATED));
assertThat(yangNotification.getReference(), is("\"reference\""));
YangTypeDef typeDef = (YangTypeDef) yangNotification.getChild();
assertThat(typeDef.getName(), is("my-type"));
assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED));
assertThat(typeDef.getDerivedType().getDataTypeExtendedInfo()
.getBaseType().getDataType(), is(YangDataTypes.INT32));
ListIterator<YangLeaf> leafIterator = yangNotification.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getLeafName(), is("if-name"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("leafref"));
}
}
module rock {
namespace "http://example.net/rock";
prefix "rock";
notification link-failure {
description "A link failure has been detected";
status deprecated;
reference "reference";
typedef my-type {
status deprecated;
type int32;
}
leaf if-name {
type leafref {
path "/interface/name";
}
}
leaf if-admin-status {
type admin-status;
}
leaf if-oper-status {
type oper-status;
}
}
}