Gaurav Agrawal
Committed by Gerrit Code Review

YANG Derived Data Type Support + Change in YANG Parser Grammar to

Support String values which are not in double quotes with UT case + 
Addition of Revision Date Validation with UT case + Removal of 
String.valueOf

Change-Id: Ic152265a35c0d2b0f84473a94ca67681cf8efca8
Showing 21 changed files with 435 additions and 59 deletions
......@@ -24,6 +24,7 @@ import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangListener;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.impl.listeners.DefaultListener;
import org.onosproject.yangutils.parser.impl.listeners.KeyListener;
import org.onosproject.yangutils.parser.impl.listeners.LeafListListener;
import org.onosproject.yangutils.parser.impl.listeners.LeafListener;
......@@ -49,6 +50,7 @@ import org.onosproject.yangutils.parser.impl.listeners.RevisionDateListener;
import org.onosproject.yangutils.parser.impl.listeners.RevisionListener;
import org.onosproject.yangutils.parser.impl.listeners.StatusListener;
import org.onosproject.yangutils.parser.impl.listeners.SubModuleListener;
import org.onosproject.yangutils.parser.impl.listeners.TypeDefListener;
import org.onosproject.yangutils.parser.impl.listeners.TypeListener;
import org.onosproject.yangutils.parser.impl.listeners.UnitsListener;
import org.onosproject.yangutils.parser.impl.listeners.VersionListener;
......@@ -519,12 +521,12 @@ public class TreeWalkListener implements GeneratedYangListener {
@Override
public void enterTypedefStatement(GeneratedYangParser.TypedefStatementContext ctx) {
//TODO: implement the method.
TypeDefListener.processTypeDefEntry(this, ctx);
}
@Override
public void exitTypedefStatement(GeneratedYangParser.TypedefStatementContext ctx) {
//TODO: implement the method.
TypeDefListener.processTypeDefExit(this, ctx);
}
@Override
......@@ -609,7 +611,7 @@ public class TreeWalkListener implements GeneratedYangListener {
@Override
public void enterDefaultStatement(GeneratedYangParser.DefaultStatementContext ctx) {
//TODO: implement the method.
DefaultListener.processDefaultEntry(this, ctx);
}
@Override
......
......@@ -78,8 +78,8 @@ public final class BaseFileListener {
checkStackIsNotEmpty(listener, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
// Data Model tree root node is set.
if (listener.getParsedDataStack().peek() instanceof YangModule
|| listener.getParsedDataStack().peek() instanceof YangSubModule) {
if ((listener.getParsedDataStack().peek() instanceof YangModule)
|| (listener.getParsedDataStack().peek() instanceof YangSubModule)) {
listener.setRootNode((YangNode) listener.getParsedDataStack().pop());
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, YANGBASE_DATA, "", EXIT));
......
......@@ -77,11 +77,11 @@ public final class BelongsToListener {
GeneratedYangParser.BelongstoStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.IDENTIFIER().getText(),
ENTRY);
YangBelongsTo belongstoNode = new YangBelongsTo();
belongstoNode.setBelongsToModuleName(String.valueOf(ctx.IDENTIFIER().getText()));
belongstoNode.setBelongsToModuleName(ctx.IDENTIFIER().getText());
// Push belongsto into the stack.
listener.getParsedDataStack().push(belongstoNode);
......@@ -98,7 +98,7 @@ public final class BelongsToListener {
GeneratedYangParser.BelongstoStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.IDENTIFIER().getText(),
EXIT);
Parsable tmpBelongstoNode = listener.getParsedDataStack().peek();
......@@ -107,7 +107,7 @@ public final class BelongsToListener {
// Check for stack to be empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA,
String.valueOf(ctx.IDENTIFIER().getText()), EXIT);
ctx.IDENTIFIER().getText(), EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getParsableDataType()) {
......@@ -118,12 +118,12 @@ public final class BelongsToListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BELONGS_TO_DATA,
String.valueOf(ctx.IDENTIFIER().getText()),
ctx.IDENTIFIER().getText(),
EXIT));
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BELONGS_TO_DATA,
String.valueOf(ctx.IDENTIFIER().getText()), EXIT));
ctx.IDENTIFIER().getText(), EXIT));
}
}
}
......
......@@ -92,24 +92,24 @@ public final class ContactListener {
public static void processContactEntry(TreeWalkListener listener, GeneratedYangParser.ContactStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTACT_DATA, String.valueOf(ctx.string().getText()), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTACT_DATA, ctx.string().getText(), ENTRY);
// Obtain the node of the stack.
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getParsableDataType()) {
case MODULE_DATA: {
YangModule module = (YangModule) tmpNode;
module.setContact(String.valueOf(ctx.string().getText()));
module.setContact(ctx.string().getText());
break;
}
case SUB_MODULE_DATA: {
YangSubModule subModule = (YangSubModule) tmpNode;
subModule.setContact(String.valueOf(ctx.string().getText()));
subModule.setContact(ctx.string().getText());
break;
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTACT_DATA,
String.valueOf(ctx.string().getText()), ENTRY));
ctx.string().getText(), ENTRY));
}
}
}
......
/*
* Copyright 2014-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;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
*
* typedef-stmt = typedef-keyword sep identifier-arg-str optsep
* "{" stmtsep
* ;; these stmts can appear in any order
* type-stmt stmtsep
* [units-stmt stmtsep]
* [default-stmt stmtsep]
* [status-stmt stmtsep]
* [description-stmt stmtsep]
* [reference-stmt stmtsep]
* "}"
* default-stmt = default-keyword sep string stmtend
*
* ANTLR grammar rule
* typedefStatement : TYPEDEF_KEYWORD IDENTIFIER LEFT_CURLY_BRACE
* (typeStatement | unitsStatement | defaultStatement | statusStatement
* | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
* defaultStatement : DEFAULT_KEYWORD string STMTEND;
*/
import org.onosproject.yangutils.datamodel.YangTypeDef;
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.parser.ParsableDataType.DEFAULT_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
public final class DefaultListener {
/**
* Creates a new default listener.
*/
private DefaultListener() {
}
/**
* It is called when parser enters grammar rule (default), 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 processDefaultEntry(TreeWalkListener listener,
GeneratedYangParser.DefaultStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, DEFAULT_DATA, ctx.string().getText(), ENTRY);
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getParsableDataType()) {
case TYPEDEF_DATA: {
YangTypeDef typeDef = (YangTypeDef) tmpNode;
typeDef.setDefaultValueInString(ctx.string().getText());
break;
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
DEFAULT_DATA, ctx.string().getText(), ENTRY));
}
}
}
......@@ -75,10 +75,10 @@ public final class ImportListener {
public static void processImportEntry(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, String.valueOf(ctx.IDENTIFIER().getText()), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.IDENTIFIER().getText(), ENTRY);
YangImport importNode = new YangImport();
importNode.setModuleName(String.valueOf(ctx.IDENTIFIER().getText()));
importNode.setModuleName(ctx.IDENTIFIER().getText());
// Push import node to the stack.
listener.getParsedDataStack().push(importNode);
......@@ -94,14 +94,14 @@ public final class ImportListener {
public static void processImportExit(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, String.valueOf(ctx.IDENTIFIER().getText()), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.IDENTIFIER().getText(), EXIT);
Parsable tmpImportNode = listener.getParsedDataStack().peek();
if (tmpImportNode instanceof YangImport) {
listener.getParsedDataStack().pop();
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.IDENTIFIER().getText(),
EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
......@@ -118,12 +118,12 @@ public final class ImportListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IMPORT_DATA,
String.valueOf(ctx.IDENTIFIER().getText()),
ctx.IDENTIFIER().getText(),
EXIT));
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, IMPORT_DATA,
String.valueOf(ctx.IDENTIFIER().getText()), EXIT));
ctx.IDENTIFIER().getText(), EXIT));
}
}
}
......
......@@ -74,11 +74,11 @@ public final class IncludeListener {
public static void processIncludeEntry(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.IDENTIFIER().getText(),
ENTRY);
YangInclude includeNode = new YangInclude();
includeNode.setSubModuleName(String.valueOf(ctx.IDENTIFIER().getText()));
includeNode.setSubModuleName(ctx.IDENTIFIER().getText());
listener.getParsedDataStack().push(includeNode);
}
......@@ -93,14 +93,14 @@ public final class IncludeListener {
public static void processIncludeExit(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, String.valueOf(ctx.IDENTIFIER().getText()), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.IDENTIFIER().getText(), EXIT);
Parsable tmpIncludeNode = listener.getParsedDataStack().peek();
if (tmpIncludeNode instanceof YangInclude) {
listener.getParsedDataStack().pop();
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.IDENTIFIER().getText(),
EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
......@@ -117,12 +117,12 @@ public final class IncludeListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, INCLUDE_DATA,
String.valueOf(ctx.IDENTIFIER().getText()),
ctx.IDENTIFIER().getText(),
EXIT));
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, INCLUDE_DATA,
String.valueOf(ctx.IDENTIFIER().getText()), EXIT));
ctx.IDENTIFIER().getText(), EXIT));
}
}
}
......
......@@ -69,7 +69,7 @@ public final class ModuleListener {
public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
// Check if stack is empty.
checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, String.valueOf(ctx.IDENTIFIER().getText()), ENTRY);
checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.IDENTIFIER().getText(), ENTRY);
YangModule yangModule = new YangModule();
yangModule.setName(ctx.IDENTIFIER().getText());
......@@ -87,11 +87,11 @@ public final class ModuleListener {
public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, String.valueOf(ctx.IDENTIFIER().getText()), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.IDENTIFIER().getText(), EXIT);
if (!(listener.getParsedDataStack().peek() instanceof YangModule)) {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA,
String.valueOf(ctx.IDENTIFIER().getText()), EXIT));
ctx.IDENTIFIER().getText(), EXIT));
}
}
}
......
......@@ -77,9 +77,9 @@ public final class NamespaceListener {
GeneratedYangParser.NamespaceStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, NAMESPACE_DATA, String.valueOf(ctx.string().getText()), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, NAMESPACE_DATA, ctx.string().getText(), ENTRY);
if (!validateUriValue(String.valueOf(ctx.string().getText()))) {
if (!validateUriValue(ctx.string().getText())) {
ParserException parserException = new ParserException("Invalid namespace URI");
parserException.setLine(ctx.string().STRING(0).getSymbol().getLine());
parserException.setCharPosition(ctx.string().STRING(0).getSymbol().getCharPositionInLine());
......@@ -92,13 +92,13 @@ public final class NamespaceListener {
case MODULE_DATA: {
YangModule module = (YangModule) tmpNode;
YangNameSpace uri = new YangNameSpace();
uri.setUri(String.valueOf(ctx.string().getText()));
uri.setUri(ctx.string().getText());
module.setNameSpace(uri);
break;
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, NAMESPACE_DATA,
String.valueOf(ctx.string().getText()), ENTRY));
ctx.string().getText(), ENTRY));
}
}
......
......@@ -95,7 +95,7 @@ public final class OrganizationListener {
GeneratedYangParser.OrganizationStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, ORGANIZATION_DATA, String.valueOf(ctx.string().getText()),
checkStackIsNotEmpty(listener, MISSING_HOLDER, ORGANIZATION_DATA, ctx.string().getText(),
ENTRY);
// Obtain the node of the stack.
......@@ -103,17 +103,17 @@ public final class OrganizationListener {
switch (tmpNode.getParsableDataType()) {
case MODULE_DATA: {
YangModule module = (YangModule) tmpNode;
module.setOrganization(String.valueOf(ctx.string().getText()));
module.setOrganization(ctx.string().getText());
break;
}
case SUB_MODULE_DATA: {
YangSubModule subModule = (YangSubModule) tmpNode;
subModule.setOrganization(String.valueOf(ctx.string().getText()));
subModule.setOrganization(ctx.string().getText());
break;
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ORGANIZATION_DATA,
String.valueOf(ctx.string().getText()), ENTRY));
ctx.string().getText(), ENTRY));
}
}
}
......
......@@ -76,7 +76,7 @@ public final class PrefixListener {
public static void processPrefixEntry(TreeWalkListener listener, GeneratedYangParser.PrefixStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, PREFIX_DATA, String.valueOf(ctx.IDENTIFIER().getText()), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, PREFIX_DATA, ctx.IDENTIFIER().getText(), ENTRY);
// Obtain the node of the stack.
Parsable tmpNode = listener.getParsedDataStack().peek();
......@@ -98,7 +98,7 @@ public final class PrefixListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, PREFIX_DATA,
String.valueOf(ctx.IDENTIFIER().getText()), ENTRY));
ctx.IDENTIFIER().getText(), ENTRY));
}
}
}
......
......@@ -23,6 +23,10 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.onosproject.yangutils.parser.ParsableDataType.REVISION_DATE_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
......@@ -82,26 +86,57 @@ public final class RevisionDateListener {
GeneratedYangParser.RevisionDateStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, String.valueOf(ctx.DATE_ARG().getText()),
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, ctx.DATE_ARG().getText(),
ENTRY);
if (!isDateValid(ctx.DATE_ARG().getText())) {
ParserException parserException = new ParserException("Input date is not correct");
parserException.setLine(ctx.DATE_ARG().getSymbol().getLine());
parserException.setCharPosition(ctx.DATE_ARG().getSymbol().getCharPositionInLine());
throw parserException;
}
// Obtain the node of the stack.
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getParsableDataType()) {
case IMPORT_DATA: {
YangImport importNode = (YangImport) tmpNode;
importNode.setRevision(String.valueOf(ctx.DATE_ARG().getText()));
importNode.setRevision(ctx.DATE_ARG().getText());
break;
}
case INCLUDE_DATA: {
YangInclude includeNode = (YangInclude) tmpNode;
includeNode.setRevision(String.valueOf(ctx.DATE_ARG().getText()));
includeNode.setRevision(ctx.DATE_ARG().getText());
break;
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATE_DATA,
String.valueOf(ctx.DATE_ARG().getText()), ENTRY));
ctx.DATE_ARG().getText(), ENTRY));
}
}
/**
* Validates the revision date.
*
* @param dateToValidate input revision date.
* @return validation result, true for success, false for failure.
*/
private static boolean isDateValid(String dateToValidate) {
if (dateToValidate == null) {
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setLenient(false);
try {
//if not valid, it will throw ParseException
Date date = sdf.parse(dateToValidate);
System.out.println(date);
} catch (ParseException e) {
return false;
}
return true;
}
// TODO Implement the DATE_ARG validation as per RFC 6020.
}
}
\ No newline at end of file
......
......@@ -82,7 +82,7 @@ public final class RevisionListener {
GeneratedYangParser.RevisionStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, String.valueOf(ctx.DATE_ARG().getText()), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), ENTRY);
// Validate for reverse chronological order of revision & for revision
// value.
......@@ -92,7 +92,7 @@ public final class RevisionListener {
}
YangRevision revisionNode = new YangRevision();
revisionNode.setRevDate(String.valueOf(ctx.DATE_ARG().getText()));
revisionNode.setRevDate(ctx.DATE_ARG().getText());
listener.getParsedDataStack().push(revisionNode);
}
......@@ -108,14 +108,14 @@ public final class RevisionListener {
ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, String.valueOf(ctx.DATE_ARG().getText()), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), EXIT);
Parsable tmpRevisionNode = listener.getParsedDataStack().peek();
if (tmpRevisionNode instanceof YangRevision) {
listener.getParsedDataStack().pop();
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, String.valueOf(ctx.DATE_ARG().getText()),
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(),
EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
......@@ -132,12 +132,12 @@ public final class RevisionListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATA,
String.valueOf(ctx.DATE_ARG().getText()),
ctx.DATE_ARG().getText(),
EXIT));
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, REVISION_DATA,
String.valueOf(ctx.DATE_ARG().getText()), EXIT));
ctx.DATE_ARG().getText(), EXIT));
}
}
......
......@@ -71,7 +71,7 @@ public final class SubModuleListener {
GeneratedYangParser.SubModuleStatementContext ctx) {
// Check if stack is empty.
checkStackIsEmpty(listener, INVALID_HOLDER, SUB_MODULE_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
checkStackIsEmpty(listener, INVALID_HOLDER, SUB_MODULE_DATA, ctx.IDENTIFIER().getText(),
ENTRY);
YangSubModule yangSubModule = new YangSubModule();
......@@ -91,12 +91,12 @@ public final class SubModuleListener {
GeneratedYangParser.SubModuleStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, SUB_MODULE_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
checkStackIsNotEmpty(listener, MISSING_HOLDER, SUB_MODULE_DATA, ctx.IDENTIFIER().getText(),
EXIT);
if (!(listener.getParsedDataStack().peek() instanceof YangSubModule)) {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, SUB_MODULE_DATA,
String.valueOf(ctx.IDENTIFIER().getText()), EXIT));
ctx.IDENTIFIER().getText(), EXIT));
}
}
}
......
/*
* Copyright 2014-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;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
*
* body-stmts = *((extension-stmt /
* feature-stmt /
* identity-stmt /
* typedef-stmt /
* grouping-stmt /
* data-def-stmt /
* augment-stmt /
* rpc-stmt /
* notification-stmt /
* deviation-stmt) stmtsep)
*
* typedef-stmt = typedef-keyword sep identifier-arg-str optsep
* "{" stmtsep
* ;; these stmts can appear in any order
* type-stmt stmtsep
* [units-stmt stmtsep]
* [default-stmt stmtsep]
* [status-stmt stmtsep]
* [description-stmt stmtsep]
* [reference-stmt stmtsep]
* "}"
*
* ANTLR grammar rule
* typedefStatement : TYPEDEF_KEYWORD IDENTIFIER LEFT_CURLY_BRACE
* (typeStatement | unitsStatement | defaultStatement | statusStatement
* | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
*/
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.YangSubModule;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.ParsableDataType;
import static org.onosproject.yangutils.parser.ParsableDataType.TYPEDEF_DATA;
import static org.onosproject.yangutils.parser.ParsableDataType.UNITS_DATA;
import static org.onosproject.yangutils.parser.ParsableDataType.DEFAULT_DATA;
import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA;
import static org.onosproject.yangutils.parser.ParsableDataType.DESCRIPTION_DATA;
import static org.onosproject.yangutils.parser.ParsableDataType.REFERENCE_DATA;
import static org.onosproject.yangutils.parser.ParsableDataType.STATUS_DATA;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
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.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CARDINALITY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
/**
* Implements listener based call back function corresponding to the "typedef"
* rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
*/
public final class TypeDefListener {
private static ParsableDataType yangConstruct;
/**
* Creates a new typedef listener.
*/
private TypeDefListener() {
}
/**
* It is called when parser enters grammar rule (typedef), 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 processTypeDefEntry(TreeWalkListener listener,
GeneratedYangParser.TypedefStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY);
boolean result = validateSubStatementsCardinality(ctx);
if (!result) {
throw new ParserException(constructListenerErrorMessage(INVALID_CARDINALITY, yangConstruct, "", ENTRY));
}
YangTypeDef typeDefNode = new YangTypeDef();
typeDefNode.setDerivedName(ctx.IDENTIFIER().getText());
Parsable curData = listener.getParsedDataStack().peek();
if (curData instanceof YangModule | curData instanceof YangSubModule | curData instanceof YangContainer
| curData instanceof YangList) {
/*
* TODO YangGrouping, YangRpc, YangInput, YangOutput, Notification.
*/
YangNode curNode = (YangNode) curData;
try {
curNode.addChild(typeDefNode);
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage()));
}
listener.getParsedDataStack().push(typeDefNode);
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY));
}
}
/**
* It is called when parser exits from grammar rule (typedef), 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 processTypeDefExit(TreeWalkListener listener,
GeneratedYangParser.TypedefStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.IDENTIFIER().getText(), EXIT);
if (listener.getParsedDataStack().peek() instanceof YangTypeDef) {
listener.getParsedDataStack().pop();
} else {
listener.getErrorInformation().setErrorFlag(true);
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, TYPEDEF_DATA,
ctx.IDENTIFIER().getText(), EXIT));
}
}
/**
* Validates the cardinality of typedef sub-statements as per grammar.
*
* @param ctx context object of the grammar rule.
* @return true/false validation success or failure.
*/
private static boolean validateSubStatementsCardinality(GeneratedYangParser.TypedefStatementContext ctx) {
if ((!ctx.unitsStatement().isEmpty())
&& (ctx.unitsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = UNITS_DATA;
return false;
}
if ((!ctx.defaultStatement().isEmpty())
&& (ctx.defaultStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = DEFAULT_DATA;
return false;
}
if (ctx.typeStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY) {
yangConstruct = TYPE_DATA;
return false;
}
if ((!ctx.descriptionStatement().isEmpty())
&& (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = DESCRIPTION_DATA;
return false;
}
if ((!ctx.referenceStatement().isEmpty())
&& (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = REFERENCE_DATA;
return false;
}
if ((!ctx.statusStatement().isEmpty())
&& (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = STATUS_DATA;
return false;
}
return true;
}
}
......@@ -85,7 +85,7 @@ public final class VersionListener {
GeneratedYangParser.YangVersionStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, VERSION_DATA, String.valueOf(ctx.INTEGER().getText()), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, VERSION_DATA, ctx.INTEGER().getText(), ENTRY);
Integer version = Integer.valueOf(ctx.INTEGER().getText());
if (!isVersionValid(version)) {
......@@ -110,7 +110,7 @@ public final class VersionListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, VERSION_DATA,
String.valueOf(ctx.INTEGER().getText()), ENTRY));
ctx.INTEGER().getText(), ENTRY));
}
}
......
......@@ -1220,4 +1220,6 @@ package org.onosproject.yangutils.parser.antlrgencode;
defaultStatement? configStatement? mandatoryStatement? minElementsStatement?
maxElementsStatement? RIGHT_CURLY_BRACE));
string : STRING (PLUS STRING)*;
string : STRING (PLUS STRING)*
| IDENTIFIER
| INTEGER;
......
......@@ -47,7 +47,7 @@ public class ContactListenerTest {
YangNode node = manager.getDataModel("src/test/resources/ContactValidEntry.yang");
// Checks for the version value in data model tree.
// Checks for the contact value in data model tree.
assertThat(((YangModule) node).getContact(), is("\"WG List: <mailto:spring@ietf.org>\nEditor: "
+ "Stephane Litkowski\n " + "<mailto:stephane.litkowski@orange.com>\""));
}
......@@ -63,6 +63,18 @@ public class ContactListenerTest {
}
/**
* Checks that contact can have a string value without double quotes.
*/
@Test
public void processContactWithoutQuotes() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/ContactWithoutQuotes.yang");
// Checks for the contact value in data model tree.
assertThat(((YangModule) node).getContact(), is("WG"));
}
/**
* Checks if contact is not empty.
*/
@Test(expected = ParserException.class)
......
......@@ -80,6 +80,15 @@ public class RevisionDateListenerTest {
}
/**
* Checks if revision date is correct.
*/
@Test(expected = ParserException.class)
public void processRevisionDateInvalid() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/RevisionDateInvalid.yang");
}
/**
* Checks if revision date listener updates the data model tree.
*/
@Test
......
module Test {
yang-version 1;
namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
prefix On;
organization "IETF SPRING Working Group";
contact WG;
}
module Test {
yang-version 1;
namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
prefix test;
import ietf {
prefix On2;
revision-date 2015-02-30;
}
include itut {
revision-date 2016-02-03;
}
include sdn {
revision-date 2014-02-03;
}
contact "Test";
organization "ONOS";
}