Vidyashree Rama
Committed by Gerrit Code Review

[ONOS-4762][ONOS-4601]Grammar for meta data + Union defect fix

Change-Id: I8f78127e5b292cca6a79b32d496c2602c9105acd
Showing 21 changed files with 156 additions and 162 deletions
......@@ -1376,6 +1376,99 @@ public class TreeWalkListener implements GeneratedYangListener {
}
@Override
public void enterCompilerAnnotationStatement(GeneratedYangParser.CompilerAnnotationStatementContext ctx) {
// TODO: implement the method.
}
@Override
public void exitCompilerAnnotationStatement(GeneratedYangParser.CompilerAnnotationStatementContext ctx) {
// TODO: implement the method.
}
@Override
public void enterAnnotationStatement(GeneratedYangParser.AnnotationStatementContext ctx) {
// TODO: implement the method.
}
@Override
public void exitAnnotationStatement(GeneratedYangParser.AnnotationStatementContext ctx) {
// TODO: implement the method.
}
@Override
public void enterAnnotationType(GeneratedYangParser.AnnotationTypeContext ctx) {
// TODO: implement the method.
}
@Override
public void exitAnnotationType(GeneratedYangParser.AnnotationTypeContext ctx) {
// TODO: implement the method.
}
@Override
public void enterAnnotationParameterSpecification(GeneratedYangParser.AnnotationParameterSpecificationContext
ctx) {
// TODO: implement the method.
}
@Override
public void exitAnnotationParameterSpecification(GeneratedYangParser.AnnotationParameterSpecificationContext ctx) {
// TODO: implement the method.
}
@Override
public void enterAnnotationParameterSpecificationArg(GeneratedYangParser.AnnotationParameterSpecificationArgContext
ctx) {
// TODO: implement the method.
}
@Override
public void exitAnnotationParameterSpecificationArg(GeneratedYangParser.AnnotationParameterSpecificationArgContext
ctx) {
// TODO: implement the method.
}
@Override
public void enterAnnotationParaInstance(GeneratedYangParser.AnnotationParaInstanceContext ctx) {
// TODO: implement the method.
}
@Override
public void exitAnnotationParaInstance(GeneratedYangParser.AnnotationParaInstanceContext ctx) {
// TODO: implement the method.
}
@Override
public void enterAnnotationParaTypeIdentifier(GeneratedYangParser.AnnotationParaTypeIdentifierContext ctx) {
// TODO: implement the method.
}
@Override
public void exitAnnotationParaTypeIdentifier(GeneratedYangParser.AnnotationParaTypeIdentifierContext ctx) {
// TODO: implement the method.
}
@Override
public void enterAnnotationParaTypeValue(GeneratedYangParser.AnnotationParaTypeValueContext ctx) {
// TODO: implement the method.
}
@Override
public void exitAnnotationParaTypeValue(GeneratedYangParser.AnnotationParaTypeValueContext ctx) {
// TODO: implement the method.
}
@Override
public void enterAnnotationIdentifier(GeneratedYangParser.AnnotationIdentifierContext ctx) {
// TODO: implement the method.
}
@Override
public void exitAnnotationIdentifier(GeneratedYangParser.AnnotationIdentifierContext ctx) {
// TODO: implement the method.
}
@Override
public void enterVersion(GeneratedYangParser.VersionContext ctx) {
// do nothing.
}
......
......@@ -16,7 +16,6 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
......@@ -27,6 +26,7 @@ import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUnion;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.utils.Parsable;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
......@@ -94,6 +94,8 @@ public final class TypeListener {
// Obtain the YANG data type.
YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText());
validateTypeSubStatementCardinality(ctx, yangDataTypes);
// Create YANG type object and fill the values.
YangType<?> type = getYangType(JAVA_GENERATION);
type.setNodeIdentifier(nodeIdentifier);
......@@ -265,4 +267,37 @@ public final class TypeListener {
TYPE_DATA, ctx.string().getText(), ENTRY, e.getMessage()));
}
}
/**
* Validates type body statements cardinality.
*
* @param ctx context object of the grammar rule
* @param yangDataType yang data type
*/
private static void validateTypeSubStatementCardinality(GeneratedYangParser.TypeStatementContext ctx,
YangDataTypes yangDataType) {
if (ctx.typeBodyStatements() == null || ctx.typeBodyStatements().isEmpty()) {
ParserException parserException;
switch (yangDataType) {
case UNION:
parserException = new ParserException("YANG file error : a type union" +
" must have atleast one type statement.");
break;
case ENUMERATION:
parserException = new ParserException("YANG file error : a type enumeration" +
" must have atleast one enum statement.");
break;
case BITS:
parserException = new ParserException("YANG file error : a type bits" +
" must have atleast one bit statement.");
break;
// TODO : decimal64, identity ref, leafref
default:
return;
}
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
}
}
......
......@@ -100,6 +100,7 @@ lexer grammar YangLexer;
TRUE_KEYWORD : 'true';
UNBOUNDED_KEYWORD : 'unbounded';
USER_KEYWORD : 'user';
COMPILER_ANNOTATION_KEYWORD : 'compiler-annotation';
// Lexer tokens to be skipped
COMMENT
......@@ -116,11 +117,18 @@ lexer grammar YangLexer;
DATE_ARG : DIGIT DIGIT DIGIT DIGIT '-' DIGIT DIGIT '-' DIGIT DIGIT;
LEFT_CURLY_BRACE : '{';
RIGHT_CURLY_BRACE : '}';
LEFT_ROUND_BRACE : '(';
RIGHT_ROUND_BRACE : ')';
ANNOTATION_START : '@';
ANNOTATION_IDENTIFIER : ('@')(ALPHA | '_')
(ALPHA | DIGIT | '_' | '-' | '.')*;
IDENTIFIER : (ALPHA | '_')
(ALPHA | DIGIT | '_' | '-' | '.')*;
STMTEND : ';';
DQUOTE : '"';
COLON : ':';
COMMA : ',';
EQUAL : '=';
PLUS : '+';
MINUS: '-';
......
......@@ -129,19 +129,6 @@ public class ConfigListenerTest {
}
/**
* Checks config statement as sub-statement of module.
*/
@Test
public void processModuleSubStatementConfig() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input 'config' expecting {'anyxml', 'augment', 'choice', 'contact', "
+ "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import',"
+ " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference',"
+ " 'revision', 'rpc', 'typedef', 'uses', '}'}");
YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementConfig.yang");
}
/**
* Checks config statement as sub-statement of container.
*/
@Test
......
......@@ -91,13 +91,8 @@ public class LeafListListenerTest {
/**
* Checks whether exception is thrown when leaf-list keyword is incorrect.
*/
@Test
@Test(expected = ParserException.class)
public void processLeafListInvalidStatement() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input 'leaflist' expecting {'anyxml', 'augment', 'choice', 'contact', "
+ "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import',"
+ " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference',"
+ " 'revision', 'rpc', 'typedef', 'uses', '}'}");
YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidStatement.yang");
}
......
......@@ -90,19 +90,6 @@ public class LeafListenerTest {
}
/**
* Checks whether exception is thrown when leaf keyword is incorrect.
*/
@Test
public void processLeafInvalidStatement() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input 'leafs' expecting {'anyxml', 'augment', 'choice', 'contact', "
+ "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import',"
+ " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference',"
+ " 'revision', 'rpc', 'typedef', 'uses', '}'}");
YangNode node = manager.getDataModel("src/test/resources/LeafInvalidStatement.yang");
}
/**
* Checks whether exception is thrown when leaf keyword without Left brace
* as per grammar.
*/
......
......@@ -140,18 +140,4 @@ public class MandatoryListenerTest {
thrown.expectMessage("missing ';' at '}'");
YangNode node = manager.getDataModel("src/test/resources/MandatoryWithoutStatementEnd.yang");
}
/**
* Checks mandatory statement as sub-statement of module and expects
* exception.
*/
@Test
public void processModuleSubStatementMandatory() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input 'mandatory' expecting {'anyxml', 'augment', 'choice', 'contact',"
+ " 'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import',"
+ " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference',"
+ " 'revision', 'rpc', 'typedef', 'uses', '}'}");
YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementMandatory.yang");
}
}
......
......@@ -92,19 +92,6 @@ public class MaxElementsListenerTest {
}
/**
* Checks whether exception is thrown when invalid max-elements keyword is
* given as input.
*/
@Test
public void processMaxElementsInvalidStatement() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("extraneous input 'max-element' expecting {'config', 'description', 'if-feature',"
+ " 'max-elements', 'min-elements', 'must', 'ordered-by', 'reference', 'status', 'type', 'units', "
+ "'when', '}'}");
YangNode node = manager.getDataModel("src/test/resources/MaxElementsInvalidStatement.yang");
}
/**
* Checks whether exception is thrown when max-elements statement without
* statement end is given as input.
*/
......
......@@ -92,19 +92,6 @@ public class MinElementsListenerTest {
}
/**
* Checks whether exception is thrown when invalid min-elements keyword is
* given as input.
*/
@Test
public void processMinElementsInvalidKeyword() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("extraneous input 'min-element' expecting {'config', 'description', 'if-feature',"
+ " 'max-elements', 'min-elements', 'must', 'ordered-by', 'reference', 'status', 'type', 'units',"
+ " 'when', '}'}");
YangNode node = manager.getDataModel("src/test/resources/MinElementsInvalidKeyword.yang");
}
/**
* Checks whether exception is thrown when invalid min-elements value is
* given as input.
*/
......
......@@ -145,19 +145,6 @@ public class StatusListenerTest {
}
/**
* Checks status statement as sub-statement of module.
*/
@Test
public void processModuleSubStatementStatus() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input 'status' expecting {'anyxml', 'augment', 'choice', 'contact', "
+ "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import',"
+ " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference', "
+ "'revision', 'rpc', 'typedef', 'uses', '}'}");
YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementStatus.yang");
}
/**
* Checks status statement as sub-statement of container.
*/
@Test
......
......@@ -138,4 +138,12 @@ public class UnionListenerTest {
YangNode node = manager.getDataModel("src/test/resources/UnionWithEmptyType.yang");
}
/**
* Checks whether type union has atleast one type statement.
*/
@Test (expected = ParserException.class)
public void processUnionWithoutChild() throws IOException, ParserException {
manager.getDataModel("src/test/resources/UnionWithoutChild.yang");
}
}
......
......@@ -71,19 +71,6 @@ public class UnitsListenerTest {
}
/**
* Checks invalid units statement as sub-statement of module.
*/
@Test
public void processModuleSubStatementUnits() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input 'type' expecting {'anyxml', 'augment', 'choice', 'contact', "
+ "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity',"
+ " 'import', 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', "
+ "'reference', 'revision', 'rpc', 'typedef', 'uses', '}'}");
YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementUnits.yang");
}
/**
* Checks invalid units statement(without statement end).
*/
@Test
......
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leafs invalid-interval {
type "uint16";
units "seconds";
description "Interval before a route is declared invalid";
config true;
mandatory true;
status current;
reference "RFC 6020";
}
}
\ No newline at end of file
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type "uint16";
units "seconds";
max-element 3;
description "Interval before a route is declared invalid;
config true;
status current;
reference "RFC 6020";
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type "uint16";
units "seconds";
min-element 3;
description "Interval before a route is declared invalid";
config true;
status current;
reference "RFC 6020";
}
}
\ No newline at end of file
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
mandatory false;
}
\ No newline at end of file
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
status current;
}
\ No newline at end of file
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
type "uint16";
units "seconds";
}
\ No newline at end of file
......@@ -2,5 +2,13 @@ module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
config invalid;
}
\ No newline at end of file
list valid {
key "invalid-interval";
leaf invalid-interval {
type union {
type "union";
}
}
}
}
......