Vidyashree Rama
Committed by Gerrit Code Review

Identifier issue fix for module, list, container, leaf, leaf-list, prefix + date…

… agr string for revision fix

Change-Id: Id9d596512f35b54f6359d361402bcd0180b71678
Showing 33 changed files with 495 additions and 240 deletions
......@@ -1163,6 +1163,26 @@ public class TreeWalkListener implements GeneratedYangListener {
}
@Override
public void enterIdentifier(GeneratedYangParser.IdentifierContext ctx) {
// TODO: implement the method.
}
@Override
public void exitIdentifier(GeneratedYangParser.IdentifierContext ctx) {
// TODO: implement the method.
}
@Override
public void enterDateArgumentString(GeneratedYangParser.DateArgumentStringContext ctx) {
// TODO: implement the method.
}
@Override
public void exitDateArgumentString(GeneratedYangParser.DateArgumentStringContext ctx) {
// TODO: implement the method.
}
@Override
public void visitTerminal(TerminalNode terminalNode) {
// TODO: implement the method.
}
......
......@@ -22,6 +22,8 @@ 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.impl.parserutils.ListenerUtil.getValidIdentifier;
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.constructListenerErrorMessage;
......@@ -50,7 +52,7 @@ import static org.onosproject.yangutils.utils.YangConstructType.BELONGS_TO_DATA;
* submodule_header_statement : yang_version_stmt? belongs_to_stmt
* | belongs_to_stmt yang_version_stmt?
* ;
* belongs_to_stmt : BELONGS_TO_KEYWORD IDENTIFIER LEFT_CURLY_BRACE belongs_to_stmt_body RIGHT_CURLY_BRACE;
* belongs_to_stmt : BELONGS_TO_KEYWORD identifier LEFT_CURLY_BRACE belongs_to_stmt_body RIGHT_CURLY_BRACE;
* belongs_to_stmt_body : prefix_stmt;
*/
......@@ -78,11 +80,13 @@ public final class BelongsToListener {
GeneratedYangParser.BelongstoStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.IDENTIFIER().getText(),
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.identifier().getText(),
ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), BELONGS_TO_DATA, ctx);
YangBelongsTo belongstoNode = new YangBelongsTo();
belongstoNode.setBelongsToModuleName(ctx.IDENTIFIER().getText());
belongstoNode.setBelongsToModuleName(identifier);
// Push belongsto into the stack.
listener.getParsedDataStack().push(belongstoNode);
......@@ -99,7 +103,7 @@ public final class BelongsToListener {
GeneratedYangParser.BelongstoStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.IDENTIFIER().getText(),
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA, ctx.identifier().getText(),
EXIT);
Parsable tmpBelongstoNode = listener.getParsedDataStack().peek();
......@@ -108,7 +112,7 @@ public final class BelongsToListener {
// Check for stack to be empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BELONGS_TO_DATA,
ctx.IDENTIFIER().getText(), EXIT);
ctx.identifier().getText(), EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getYangConstructType()) {
......@@ -119,12 +123,12 @@ public final class BelongsToListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BELONGS_TO_DATA,
ctx.IDENTIFIER().getText(),
ctx.identifier().getText(),
EXIT));
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BELONGS_TO_DATA,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
}
......
......@@ -32,7 +32,7 @@ package org.onosproject.yangutils.parser.impl.listeners;
* "}")
*
* ANTLR grammar rule
* bitStatement : BIT_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE bitBodyStatement RIGHT_CURLY_BRACE);
* bitStatement : BIT_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE bitBodyStatement RIGHT_CURLY_BRACE);
*
* bitBodyStatement : positionStatement? statusStatement? descriptionStatement? referenceStatement?
* | positionStatement? statusStatement? referenceStatement? descriptionStatement?
......@@ -65,10 +65,12 @@ import org.onosproject.yangutils.datamodel.YangBit;
import org.onosproject.yangutils.datamodel.YangBits;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import static org.onosproject.yangutils.utils.YangConstructType.BIT_DATA;
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.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.utils.YangConstructType.BIT_DATA;
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;
......@@ -102,10 +104,12 @@ public final class BitListener {
GeneratedYangParser.BitStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), BIT_DATA, ctx);
YangBit bitNode = new YangBit();
bitNode.setBitName(ctx.IDENTIFIER().getText());
bitNode.setBitName(identifier);
listener.getParsedDataStack().push(bitNode);
}
......@@ -120,14 +124,14 @@ public final class BitListener {
GeneratedYangParser.BitStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT);
Parsable tmpBitNode = listener.getParsedDataStack().peek();
if (tmpBitNode instanceof YangBit) {
listener.getParsedDataStack().pop();
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getYangConstructType()) {
......@@ -152,20 +156,20 @@ public final class BitListener {
yangBits.addBitInfo((YangBit) tmpBitNode);
} catch (DataModelException e) {
ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
INVALID_CONTENT, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT, e.getMessage()));
parserException.setLine(ctx.IDENTIFIER().getSymbol().getLine());
parserException.setCharPosition(ctx.IDENTIFIER().getSymbol().getCharPositionInLine());
INVALID_CONTENT, BIT_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
break;
}
default:
throw new ParserException(
constructListenerErrorMessage(INVALID_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT));
constructListenerErrorMessage(INVALID_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT));
}
} else {
throw new ParserException(
constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT));
constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT));
}
}
}
......
......@@ -25,6 +25,9 @@ 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 org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
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;
......@@ -34,7 +37,6 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinality;
import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
......@@ -66,7 +68,7 @@ import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
* "}")
*
* ANTLR grammar rule
* containerStatement : CONTAINER_KEYWORD IDENTIFIER
* containerStatement : CONTAINER_KEYWORD identifier
* (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
* presenceStatement | configStatement | statusStatement | descriptionStatement |
* referenceStatement | typedefStatement | groupingStatement
......@@ -96,19 +98,20 @@ public final class ContainerListener {
GeneratedYangParser.ContainerStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.IDENTIFIER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), CONTAINER_DATA, ctx);
// Validate sub statement cardinality.
validateSubStatementsCardinality(ctx);
// Check for identifier collision
int line = ctx.IDENTIFIER().getSymbol().getLine();
int charPositionInLine = ctx.IDENTIFIER().getSymbol().getCharPositionInLine();
String identifierName = ctx.IDENTIFIER().getText();
detectCollidingChildUtil(listener, line, charPositionInLine, identifierName, CONTAINER_DATA);
int line = ctx.getStart().getLine();
int charPositionInLine = ctx.getStart().getCharPositionInLine();
detectCollidingChildUtil(listener, line, charPositionInLine, identifier, CONTAINER_DATA);
YangContainer container = new YangContainer();
container.setName(ctx.IDENTIFIER().getText());
container.setName(identifier);
/*
* If "config" is not specified, the default is the same as the parent
......@@ -127,12 +130,12 @@ public final class ContainerListener {
curNode.addChild(container);
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
CONTAINER_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage()));
CONTAINER_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
}
listener.getParsedDataStack().push(container);
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA,
ctx.IDENTIFIER().getText(), ENTRY));
ctx.identifier().getText(), ENTRY));
}
}
......@@ -147,7 +150,7 @@ public final class ContainerListener {
GeneratedYangParser.ContainerStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.IDENTIFIER().getText(), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), EXIT);
if (listener.getParsedDataStack().peek() instanceof YangContainer) {
YangContainer yangContainer = (YangContainer) listener.getParsedDataStack().peek();
......@@ -155,12 +158,12 @@ public final class ContainerListener {
yangContainer.validateDataOnExit();
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
CONTAINER_DATA, ctx.IDENTIFIER().getText(), EXIT, e.getMessage()));
CONTAINER_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
}
listener.getParsedDataStack().pop();
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
......@@ -171,11 +174,11 @@ public final class ContainerListener {
*/
private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
validateCardinality(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
validateCardinality(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.identifier().getText());
validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA, ctx.identifier().getText());
validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
validateCardinality(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.identifier().getText());
// TODO when, grouping, typedef.
}
}
......
......@@ -23,6 +23,8 @@ 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.impl.parserutils.ListenerUtil.getValidIdentifier;
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.constructListenerErrorMessage;
......@@ -49,7 +51,7 @@ import static org.onosproject.yangutils.utils.YangConstructType.IMPORT_DATA;
* ANTLR grammar rule
* linkage_stmts : (import_stmt
* | include_stmt)*;
* import_stmt : IMPORT_KEYWORD IDENTIFIER LEFT_CURLY_BRACE import_stmt_body
* import_stmt : IMPORT_KEYWORD identifier LEFT_CURLY_BRACE import_stmt_body
* RIGHT_CURLY_BRACE;
* import_stmt_body : prefix_stmt revision_date_stmt?;
*/
......@@ -76,10 +78,12 @@ 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, ctx.IDENTIFIER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.identifier().getText(), ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), IMPORT_DATA, ctx);
YangImport importNode = new YangImport();
importNode.setModuleName(ctx.IDENTIFIER().getText());
importNode.setModuleName(identifier);
// Push import node to the stack.
listener.getParsedDataStack().push(importNode);
......@@ -95,14 +99,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, 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, ctx.IDENTIFIER().getText(),
checkStackIsNotEmpty(listener, MISSING_HOLDER, IMPORT_DATA, ctx.identifier().getText(),
EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
......@@ -119,12 +123,12 @@ public final class ImportListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IMPORT_DATA,
ctx.IDENTIFIER().getText(),
ctx.identifier().getText(),
EXIT));
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, IMPORT_DATA,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
}
......
......@@ -23,6 +23,8 @@ 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.impl.parserutils.ListenerUtil.getValidIdentifier;
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.constructListenerErrorMessage;
......@@ -49,7 +51,7 @@ import static org.onosproject.yangutils.utils.YangConstructType.INCLUDE_DATA;
* ANTLR grammar rule
* linkage_stmts : (import_stmt
* | include_stmt)*;
* include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE
* include_stmt : INCLUDE_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE
* revision_date_stmt? RIGHT_CURLY_BRACE);
*/
......@@ -75,11 +77,13 @@ 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, ctx.IDENTIFIER().getText(),
checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.identifier().getText(),
ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), INCLUDE_DATA, ctx);
YangInclude includeNode = new YangInclude();
includeNode.setSubModuleName(ctx.IDENTIFIER().getText());
includeNode.setSubModuleName(identifier);
listener.getParsedDataStack().push(includeNode);
}
......@@ -94,14 +98,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, 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, ctx.IDENTIFIER().getText(),
checkStackIsNotEmpty(listener, MISSING_HOLDER, INCLUDE_DATA, ctx.identifier().getText(),
EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
......@@ -118,12 +122,12 @@ public final class IncludeListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, INCLUDE_DATA,
ctx.IDENTIFIER().getText(),
ctx.identifier().getText(),
EXIT));
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, INCLUDE_DATA,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
}
......
......@@ -22,6 +22,8 @@ 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.impl.parserutils.ListenerUtil.getValidIdentifier;
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;
......@@ -64,7 +66,7 @@ import static org.onosproject.yangutils.utils.YangConstructType.UNITS_DATA;
* "}"
*
* ANTLR grammar rule
* leafListStatement : LEAF_LIST_KEYWORD IDENTIFIER LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement |
* leafListStatement : LEAF_LIST_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement |
* typeStatement | unitsStatement | mustStatement | configStatement | minElementsStatement | maxElementsStatement |
* orderedByStatement | statusStatement | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
*/
......@@ -92,19 +94,20 @@ public final class LeafListListener {
GeneratedYangParser.LeafListStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.IDENTIFIER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.identifier().getText(), ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), LEAF_LIST_DATA, ctx);
// Validate sub statement cardinality.
validateSubStatementsCardinality(ctx);
// Check for identifier collision
int line = ctx.IDENTIFIER().getSymbol().getLine();
int charPositionInLine = ctx.IDENTIFIER().getSymbol().getCharPositionInLine();
String identifierName = ctx.IDENTIFIER().getText();
detectCollidingChildUtil(listener, line, charPositionInLine, identifierName, LEAF_LIST_DATA);
int line = ctx.getStart().getLine();
int charPositionInLine = ctx.getStart().getCharPositionInLine();
detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LEAF_LIST_DATA);
YangLeafList leafList = new YangLeafList();
leafList.setLeafName(ctx.IDENTIFIER().getText());
leafList.setLeafName(identifier);
Parsable tmpData = listener.getParsedDataStack().peek();
YangLeavesHolder leaves;
......@@ -114,7 +117,7 @@ public final class LeafListListener {
leaves.addLeafList(leafList);
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_LIST_DATA,
ctx.IDENTIFIER().getText(), ENTRY));
ctx.identifier().getText(), ENTRY));
}
listener.getParsedDataStack().push(leafList);
}
......@@ -130,31 +133,31 @@ public final class LeafListListener {
GeneratedYangParser.LeafListStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.IDENTIFIER().getText(), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.identifier().getText(), EXIT);
if (listener.getParsedDataStack().peek() instanceof YangLeafList) {
listener.getParsedDataStack().pop();
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_LIST_DATA,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
/**
* Validates the cardinality of leaf-list sub-statements as per grammar.
*
* @param ctx context object of the grammar rule.
* @param ctx context object of the grammar rule
*/
private static void validateSubStatementsCardinality(GeneratedYangParser.LeafListStatementContext ctx) {
validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.unitsStatement(), UNITS_DATA, LEAF_LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.configStatement(), CONFIG_DATA, LEAF_LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LEAF_LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LEAF_LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, LEAF_LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.statusStatement(), STATUS_DATA, LEAF_LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.unitsStatement(), UNITS_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.configStatement(), CONFIG_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.statusStatement(), STATUS_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
//TODO ordered by
}
}
......
......@@ -26,6 +26,8 @@ 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.impl.parserutils.ListenerUtil.getValidIdentifier;
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;
......@@ -66,7 +68,7 @@ import static org.onosproject.yangutils.utils.YangConstructType.UNITS_DATA;
* "}"
*
* ANTLR grammar rule
* leafStatement : LEAF_KEYWORD IDENTIFIER LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | typeStatement |
* leafStatement : LEAF_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | typeStatement |
* unitsStatement | mustStatement | defaultStatement | configStatement | mandatoryStatement | statusStatement |
* descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
*/
......@@ -94,19 +96,20 @@ public final class LeafListener {
GeneratedYangParser.LeafStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.IDENTIFIER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), LEAF_DATA, ctx);
// Validate sub statement cardinality.
validateSubStatementsCardinality(ctx);
// Check for identifier collision
int line = ctx.IDENTIFIER().getSymbol().getLine();
int charPositionInLine = ctx.IDENTIFIER().getSymbol().getCharPositionInLine();
String identifierName = ctx.IDENTIFIER().getText();
detectCollidingChildUtil(listener, line, charPositionInLine, identifierName, LEAF_DATA);
int line = ctx.getStart().getLine();
int charPositionInLine = ctx.getStart().getCharPositionInLine();
detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LEAF_DATA);
YangLeaf leaf = new YangLeaf();
leaf.setLeafName(ctx.IDENTIFIER().getText());
leaf.setLeafName(identifier);
Parsable tmpData = listener.getParsedDataStack().peek();
YangLeavesHolder leaves;
......@@ -116,7 +119,7 @@ public final class LeafListener {
leaves.addLeaf(leaf);
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_DATA,
ctx.IDENTIFIER().getText(), ENTRY));
ctx.identifier().getText(), ENTRY));
}
listener.getParsedDataStack().push(leaf);
......@@ -126,37 +129,37 @@ public final class LeafListener {
* It is called when parser exits from grammar rule (leaf), performs
* validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
* @param listener listener's object
* @param ctx context object of the grammar rule
*/
public static void processLeafExit(TreeWalkListener listener,
GeneratedYangParser.LeafStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.IDENTIFIER().getText(), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), EXIT);
if (listener.getParsedDataStack().peek() instanceof YangLeaf) {
listener.getParsedDataStack().pop();
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_DATA,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
/**
* Validates the cardinality of leaf sub-statements as per grammar.
*
* @param ctx context object of the grammar rule.
* @param ctx context object of the grammar rule
*/
private static void validateSubStatementsCardinality(GeneratedYangParser.LeafStatementContext ctx) {
validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.unitsStatement(), UNITS_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.configStatement(), CONFIG_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.mandatoryStatement(), MANDATORY_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.statusStatement(), STATUS_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_DATA, ctx.identifier().getText());
validateCardinality(ctx.unitsStatement(), UNITS_DATA, LEAF_DATA, ctx.identifier().getText());
validateCardinality(ctx.configStatement(), CONFIG_DATA, LEAF_DATA, ctx.identifier().getText());
validateCardinality(ctx.mandatoryStatement(), MANDATORY_DATA, LEAF_DATA, ctx.identifier().getText());
validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_DATA, ctx.identifier().getText());
validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, LEAF_DATA, ctx.identifier().getText());
validateCardinality(ctx.statusStatement(), STATUS_DATA, LEAF_DATA, ctx.identifier().getText());
//TODO when.
}
}
......
......@@ -27,6 +27,7 @@ import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
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;
......@@ -74,7 +75,7 @@ import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
* "}"
*
* ANTLR grammar rule
* listStatement : LIST_KEYWORD IDENTIFIER LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
* listStatement : LIST_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
* keyStatement | uniqueStatement | configStatement | minElementsStatement | maxElementsStatement |
* orderedByStatement | statusStatement | descriptionStatement | referenceStatement | typedefStatement |
* groupingStatement| dataDefStatement)* RIGHT_CURLY_BRACE;
......@@ -104,19 +105,20 @@ public final class ListListener {
YangNode curNode;
checkStackIsNotEmpty(listener, MISSING_HOLDER, LIST_DATA, ctx.IDENTIFIER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, LIST_DATA, ctx.identifier().getText(), ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), LIST_DATA, ctx);
// Validate sub statement cardinality.
validateSubStatementsCardinality(ctx);
// Check for identifier collision
int line = ctx.IDENTIFIER().getSymbol().getLine();
int charPositionInLine = ctx.IDENTIFIER().getSymbol().getCharPositionInLine();
String identifierName = ctx.IDENTIFIER().getText();
detectCollidingChildUtil(listener, line, charPositionInLine, identifierName, LIST_DATA);
int line = ctx.getStart().getLine();
int charPositionInLine = ctx.getStart().getCharPositionInLine();
detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LIST_DATA);
YangList yangList = new YangList();
yangList.setName(ctx.IDENTIFIER().getText());
yangList.setName(identifier);
/*
* If "config" is not specified, the default is the same as the parent
......@@ -135,12 +137,12 @@ public final class ListListener {
curNode.addChild(yangList);
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
LIST_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage()));
LIST_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
}
listener.getParsedDataStack().push(yangList);
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LIST_DATA,
ctx.IDENTIFIER().getText(), ENTRY));
ctx.identifier().getText(), ENTRY));
}
}
......@@ -154,7 +156,7 @@ public final class ListListener {
public static void processListExit(TreeWalkListener listener,
GeneratedYangParser.ListStatementContext ctx) {
checkStackIsNotEmpty(listener, MISSING_HOLDER, LIST_DATA, ctx.IDENTIFIER().getText(), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, LIST_DATA, ctx.identifier().getText(), EXIT);
if (listener.getParsedDataStack().peek() instanceof YangList) {
YangList yangList = (YangList) listener.getParsedDataStack().peek();
......@@ -162,12 +164,12 @@ public final class ListListener {
yangList.validateDataOnExit();
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
LIST_DATA, ctx.IDENTIFIER().getText(), EXIT, e.getMessage()));
LIST_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
}
listener.getParsedDataStack().pop();
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LIST_DATA,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
......@@ -178,14 +180,14 @@ public final class ListListener {
*/
private static void validateSubStatementsCardinality(GeneratedYangParser.ListStatementContext ctx) {
validateCardinality(ctx.keyStatement(), KEY_DATA, LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.configStatement(), CONFIG_DATA, LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.statusStatement(), STATUS_DATA, LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinalityNonNull(ctx.dataDefStatement(), DATA_DEF_DATA, LIST_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.keyStatement(), KEY_DATA, LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.configStatement(), CONFIG_DATA, LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, LIST_DATA, ctx.identifier().getText());
validateCardinality(ctx.statusStatement(), STATUS_DATA, LIST_DATA, ctx.identifier().getText());
validateCardinalityNonNull(ctx.dataDefStatement(), DATA_DEF_DATA, LIST_DATA, ctx.identifier().getText());
//TODO when, typedef, grouping, unique
}
}
......
......@@ -20,6 +20,8 @@ import org.onosproject.yangutils.datamodel.YangModule;
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.impl.parserutils.ListenerUtil.getValidIdentifier;
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.constructListenerErrorMessage;
......@@ -45,7 +47,7 @@ import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA;
* "}" optsep
*
* ANTLR grammar rule
* module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
* module_stmt : MODULE_KEYWORD identifier LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE;
*/
/**
......@@ -70,10 +72,12 @@ public final class ModuleListener {
public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
// Check if stack is empty.
checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.IDENTIFIER().getText(), ENTRY);
checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.identifier().getText(), ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), MODULE_DATA, ctx);
YangModule yangModule = new YangModule();
yangModule.setName(ctx.IDENTIFIER().getText());
yangModule.setName(identifier);
if (ctx.moduleBody(0).moduleHeaderStatement().yangVersionStatement() == null) {
yangModule.setVersion((byte) 1);
......@@ -92,11 +96,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, 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,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
}
......
......@@ -30,7 +30,7 @@ package org.onosproject.yangutils.parser.impl.listeners;
* zero-integer-value = 1*DIGIT
*
* ANTLR grammar rule
* positionStatement : POSITION_KEYWORD INTEGER STMTEND;
* positionStatement : POSITION_KEYWORD string STMTEND;
*/
import org.onosproject.yangutils.datamodel.YangBit;
......@@ -44,6 +44,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLoc
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.utils.YangConstructType.POSITION_DATA;
......@@ -73,25 +74,27 @@ public final class PositionListener {
GeneratedYangParser.PositionStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.string().getText(), ENTRY);
String position = removeQuotesAndHandleConcat(ctx.string().getText());
// Obtain the node of the stack.
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getYangConstructType()) {
case BIT_DATA: {
YangBit bitNode = (YangBit) tmpNode;
if (!isBitPositionValid(listener, ctx)) {
if (!isBitPositionValid(listener, ctx, position)) {
ParserException parserException = new ParserException(errMsg);
parserException.setLine(ctx.INTEGER().getSymbol().getLine());
parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
bitNode.setPosition(Integer.valueOf(ctx.INTEGER().getText()));
bitNode.setPosition(Integer.valueOf(position));
break;
}
default:
throw new ParserException(
constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY));
constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.string().getText(), ENTRY));
}
}
......@@ -103,13 +106,13 @@ public final class PositionListener {
* @return validation result
*/
private static boolean isBitPositionValid(TreeWalkListener listener,
GeneratedYangParser.PositionStatementContext ctx) {
GeneratedYangParser.PositionStatementContext ctx, String position) {
Parsable bitNode = listener.getParsedDataStack().pop();
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.string().getText(), ENTRY);
if (Integer.valueOf(ctx.INTEGER().getText()) < 0) {
if (Integer.valueOf(position) < 0) {
errMsg = "YANG file error: Negative value of position is invalid.";
listener.getParsedDataStack().push(bitNode);
return false;
......@@ -120,7 +123,7 @@ public final class PositionListener {
case BITS_DATA: {
YangBits yangBits = (YangBits) tmpNode;
for (YangBit curBit : yangBits.getBitSet()) {
if (Integer.valueOf(ctx.INTEGER().getText()) == curBit.getPosition()) {
if (Integer.valueOf(position) == curBit.getPosition()) {
errMsg = "YANG file error: Duplicate value of position is invalid.";
listener.getParsedDataStack().push(bitNode);
return false;
......@@ -132,7 +135,7 @@ public final class PositionListener {
default:
listener.getParsedDataStack().push(bitNode);
throw new ParserException(
constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY));
constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.string().getText(), ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -24,6 +24,7 @@ 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.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.utils.YangConstructType.PREFIX_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
......@@ -51,7 +52,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidati
* | prefix_stmt namespace_stmt yang_version_stmt?
* | prefix_stmt yang_version_stmt? namespace_stmt
* ;
* prefix_stmt : PREFIX_KEYWORD IDENTIFIER STMTEND;
* prefix_stmt : PREFIX_KEYWORD identifier STMTEND;
*/
/**
......@@ -76,29 +77,31 @@ 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, ctx.IDENTIFIER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, PREFIX_DATA, ctx.identifier().getText(), ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), PREFIX_DATA, ctx);
// Obtain the node of the stack.
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getYangConstructType()) {
case MODULE_DATA: {
YangModule module = (YangModule) tmpNode;
module.setPrefix(ctx.IDENTIFIER().getText());
module.setPrefix(identifier);
break;
}
case IMPORT_DATA: {
YangImport importNode = (YangImport) tmpNode;
importNode.setPrefixId(ctx.IDENTIFIER().getText());
importNode.setPrefixId(identifier);
break;
}
case BELONGS_TO_DATA: {
YangBelongsTo belongstoNode = (YangBelongsTo) tmpNode;
belongstoNode.setPrefix(ctx.IDENTIFIER().getText());
belongstoNode.setPrefix(identifier);
break;
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, PREFIX_DATA,
ctx.IDENTIFIER().getText(), ENTRY));
ctx.identifier().getText(), ENTRY));
}
}
}
......
......@@ -16,10 +16,6 @@
package org.onosproject.yangutils.parser.impl.listeners;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.onosproject.yangutils.datamodel.YangImport;
import org.onosproject.yangutils.datamodel.YangInclude;
import org.onosproject.yangutils.parser.Parsable;
......@@ -27,6 +23,8 @@ 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.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.isDateValid;
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.INVALID_HOLDER;
......@@ -86,13 +84,14 @@ public final class RevisionDateListener {
GeneratedYangParser.RevisionDateStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, ctx.DATE_ARG().getText(),
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, ctx.dateArgumentString().getText(),
ENTRY);
if (!isDateValid(ctx.DATE_ARG().getText())) {
String date = removeQuotesAndHandleConcat(ctx.dateArgumentString().getText());
if (!isDateValid(date)) {
ParserException parserException = new ParserException("YANG file error: Input date is not correct");
parserException.setLine(ctx.DATE_ARG().getSymbol().getLine());
parserException.setCharPosition(ctx.DATE_ARG().getSymbol().getCharPositionInLine());
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
......@@ -101,42 +100,17 @@ public final class RevisionDateListener {
switch (tmpNode.getYangConstructType()) {
case IMPORT_DATA: {
YangImport importNode = (YangImport) tmpNode;
importNode.setRevision(ctx.DATE_ARG().getText());
importNode.setRevision(date);
break;
}
case INCLUDE_DATA: {
YangInclude includeNode = (YangInclude) tmpNode;
includeNode.setRevision(ctx.DATE_ARG().getText());
includeNode.setRevision(date);
break;
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATE_DATA,
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;
ctx.dateArgumentString().getText(), ENTRY));
}
return true;
}
}
\ No newline at end of file
......
......@@ -24,6 +24,8 @@ 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.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.isDateValid;
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.ListenerErrorType.MISSING_CURRENT_HOLDER;
......@@ -33,10 +35,6 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLoc
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
*
......@@ -88,7 +86,7 @@ public final class RevisionListener {
GeneratedYangParser.RevisionStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(), ENTRY);
// Validate for reverse chronological order of revision & for revision
// value.
......@@ -97,15 +95,16 @@ public final class RevisionListener {
// TODO to be implemented.
}
if (!isDateValid(ctx.DATE_ARG().getText())) {
String date = removeQuotesAndHandleConcat(ctx.dateArgumentString().getText());
if (!isDateValid(date)) {
ParserException parserException = new ParserException("YANG file error: Input date is not correct");
parserException.setLine(ctx.DATE_ARG().getSymbol().getLine());
parserException.setCharPosition(ctx.DATE_ARG().getSymbol().getCharPositionInLine());
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
YangRevision revisionNode = new YangRevision();
revisionNode.setRevDate(ctx.DATE_ARG().getText());
revisionNode.setRevDate(date);
listener.getParsedDataStack().push(revisionNode);
}
......@@ -121,14 +120,14 @@ public final class RevisionListener {
ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().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, ctx.DATE_ARG().getText(),
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(),
EXIT);
Parsable tmpNode = listener.getParsedDataStack().peek();
......@@ -145,12 +144,12 @@ public final class RevisionListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATA,
ctx.DATE_ARG().getText(),
ctx.dateArgumentString().getText(),
EXIT));
}
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, REVISION_DATA,
ctx.DATE_ARG().getText(), EXIT));
ctx.dateArgumentString().getText(), EXIT));
}
}
......@@ -166,29 +165,4 @@ public final class RevisionListener {
// TODO to be implemented
return true;
}
/**
* 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;
}
}
......
......@@ -20,6 +20,8 @@ import org.onosproject.yangutils.datamodel.YangSubModule;
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.impl.parserutils.ListenerUtil.getValidIdentifier;
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.constructListenerErrorMessage;
......@@ -45,7 +47,7 @@ import static org.onosproject.yangutils.utils.YangConstructType.SUB_MODULE_DATA;
* "}" optsep
*
* ANTLR grammar rule
* submodule_stmt : SUBMODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE submodule_body* RIGHT_CURLY_BRACE;
* submodule_stmt : SUBMODULE_KEYWORD identifier LEFT_CURLY_BRACE submodule_body* RIGHT_CURLY_BRACE;
* submodule_body : submodule_header_statement linkage_stmts meta_stmts revision_stmts body_stmts;
*/
......@@ -72,11 +74,13 @@ public final class SubModuleListener {
GeneratedYangParser.SubModuleStatementContext ctx) {
// Check if stack is empty.
checkStackIsEmpty(listener, INVALID_HOLDER, SUB_MODULE_DATA, ctx.IDENTIFIER().getText(),
checkStackIsEmpty(listener, INVALID_HOLDER, SUB_MODULE_DATA, ctx.identifier().getText(),
ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), SUB_MODULE_DATA, ctx);
YangSubModule yangSubModule = new YangSubModule();
yangSubModule.setName(ctx.IDENTIFIER().getText());
yangSubModule.setName(identifier);
if (ctx.submoduleBody(0).submoduleHeaderStatement().yangVersionStatement() == null) {
yangSubModule.setVersion((byte) 1);
......@@ -96,12 +100,12 @@ public final class SubModuleListener {
GeneratedYangParser.SubModuleStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, SUB_MODULE_DATA, 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,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
}
......
......@@ -42,7 +42,7 @@ package org.onosproject.yangutils.parser.impl.listeners;
* "}"
*
* ANTLR grammar rule
* typedefStatement : TYPEDEF_KEYWORD IDENTIFIER LEFT_CURLY_BRACE
* typedefStatement : TYPEDEF_KEYWORD identifier LEFT_CURLY_BRACE
* (typeStatement | unitsStatement | defaultStatement | statusStatement
* | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
*/
......@@ -62,6 +62,7 @@ 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.impl.parserutils.ListenerUtil.getValidIdentifier;
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;
......@@ -105,7 +106,9 @@ public final class TypeDefListener {
GeneratedYangParser.TypedefStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.identifier().getText(), ENTRY);
String identifier = getValidIdentifier(ctx.identifier().getText(), TYPEDEF_DATA, ctx);
// Validate sub statement cardinality.
validateSubStatementsCardinality(ctx);
......@@ -116,7 +119,7 @@ public final class TypeDefListener {
*/
YangType<YangDerivedType> derivedType = new YangType<YangDerivedType>();
derivedType.setDataType(YangDataTypes.DERIVED);
derivedType.setDataTypeName(ctx.IDENTIFIER().getText());
derivedType.setDataTypeName(identifier);
YangTypeDef typeDefNode = new YangTypeDef();
typeDefNode.setDerivedType(derivedType);
......@@ -133,12 +136,12 @@ public final class TypeDefListener {
curNode.addChild(typeDefNode);
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage()));
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));
TYPEDEF_DATA, ctx.identifier().getText(), ENTRY));
}
}
......@@ -153,7 +156,7 @@ public final class TypeDefListener {
GeneratedYangParser.TypedefStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.IDENTIFIER().getText(), EXIT);
checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.identifier().getText(), EXIT);
if (listener.getParsedDataStack().peek() instanceof YangTypeDef) {
YangTypeDef typeDefNode = (YangTypeDef) listener.getParsedDataStack().peek();
......@@ -161,13 +164,13 @@ public final class TypeDefListener {
typeDefNode.validateDataOnExit();
} catch (DataModelException e) {
throw new ParserException(constructListenerErrorMessage(INVALID_CONTENT, TYPEDEF_DATA,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
listener.getParsedDataStack().pop();
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, TYPEDEF_DATA,
ctx.IDENTIFIER().getText(), EXIT));
ctx.identifier().getText(), EXIT));
}
}
......@@ -178,11 +181,11 @@ public final class TypeDefListener {
*/
private static void validateSubStatementsCardinality(GeneratedYangParser.TypedefStatementContext ctx) {
validateCardinality(ctx.unitsStatement(), UNITS_DATA, TYPEDEF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.defaultStatement(), DEFAULT_DATA, TYPEDEF_DATA, ctx.IDENTIFIER().getText());
validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, TYPEDEF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, TYPEDEF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, TYPEDEF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.statusStatement(), STATUS_DATA, TYPEDEF_DATA, ctx.IDENTIFIER().getText());
validateCardinality(ctx.unitsStatement(), UNITS_DATA, TYPEDEF_DATA, ctx.identifier().getText());
validateCardinality(ctx.defaultStatement(), DEFAULT_DATA, TYPEDEF_DATA, ctx.identifier().getText());
validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, TYPEDEF_DATA, ctx.identifier().getText());
validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, TYPEDEF_DATA, ctx.identifier().getText());
validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, TYPEDEF_DATA, ctx.identifier().getText());
validateCardinality(ctx.statusStatement(), STATUS_DATA, TYPEDEF_DATA, ctx.identifier().getText());
}
}
......
......@@ -23,6 +23,7 @@ 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.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
import static org.onosproject.yangutils.utils.YangConstructType.VERSION_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
......@@ -59,7 +60,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidati
* submodule_header_statement : yang_version_stmt? belongs_to_stmt
* | belongs_to_stmt yang_version_stmt?
* ;
* yang_version_stmt : YANG_VERSION_KEYWORD INTEGER STMTEND;
* yang_version_stmt : YANG_VERSION_KEYWORD string STMTEND;
*/
/**
......@@ -85,13 +86,13 @@ public final class VersionListener {
GeneratedYangParser.YangVersionStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, VERSION_DATA, ctx.INTEGER().getText(), ENTRY);
checkStackIsNotEmpty(listener, MISSING_HOLDER, VERSION_DATA, ctx.string().getText(), ENTRY);
Integer version = Integer.valueOf(ctx.INTEGER().getText());
if (!isVersionValid(version)) {
String version = removeQuotesAndHandleConcat(ctx.string().getText());
if (!isVersionValid(Integer.valueOf(version))) {
ParserException parserException = new ParserException("YANG file error: Input version not supported");
parserException.setLine(ctx.INTEGER().getSymbol().getLine());
parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
......@@ -110,7 +111,7 @@ public final class VersionListener {
}
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, VERSION_DATA,
ctx.INTEGER().getText(), ENTRY));
ctx.string().getText(), ENTRY));
}
}
......
/*
* 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.parserutils;
import org.antlr.v4.runtime.ParserRuleContext;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;
import org.onosproject.yangutils.utils.YangConstructType;
import org.onosproject.yangutils.parser.exceptions.ParserException;
/**
* It's a utility for listener.
*/
public final class ListenerUtil {
private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*");
private static final String PLUS = "+";
private static final int IDENTIFIER_LENGTH = 64;
/**
* Creates a new listener util.
*/
private ListenerUtil() {
}
/**
* Removes doubles quotes and concatenates if string has plus symbol.
*
* @param yangStringData string from yang file
* @return concatenated string after removing double quotes
*/
public static String removeQuotesAndHandleConcat(String yangStringData) {
yangStringData = yangStringData.replace("\"", "");
String[] tmpData = yangStringData.split(Pattern.quote(PLUS));
StringBuilder builder = new StringBuilder();
for (String yangString : tmpData) {
builder.append(yangString);
}
return builder.toString();
}
/**
* Validates identifier and returns concatenated string if string contains plus symbol.
*
* @param identifier string from yang file
* @param yangConstruct yang construct for creating error message
* @param ctx yang construct's context to get the line number and character position
* @return concatenated string after removing double quotes
*/
public static String getValidIdentifier(String identifier, YangConstructType yangConstruct, ParserRuleContext ctx) {
String identifierString = removeQuotesAndHandleConcat(identifier);
ParserException parserException;
if (identifierString.length() > IDENTIFIER_LENGTH) {
parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is " +
"greater than 64 characters.");
} else if (!IDENTIFIER_PATTERN.matcher(identifierString).matches()) {
parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is not " +
"valid.");
} else {
return identifierString;
}
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
/**
* Validates the revision date.
*
* @param dateToValidate input revision date
* @return validation result, true for success, false for failure
*/
public static boolean isDateValid(String dateToValidate) {
if (dateToValidate == null || !dateToValidate.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}")) {
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setLenient(false);
try {
//if not valid, it will throw ParseException
sdf.parse(dateToValidate);
} catch (ParseException e) {
return false;
}
return true;
}
}
\ No newline at end of file
......@@ -226,7 +226,7 @@ public class ContainerListenerTest {
@Test
public void processContainerInvalidIdentifier() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input '1valid' expecting IDENTIFIER");
thrown.expectMessage("YANG file error : container name 1valid is not valid.");
YangNode node = manager.getDataModel("src/test/resources/ContainerInvalidIdentifier.yang");
}
}
......
......@@ -85,7 +85,7 @@ public class LeafListListenerTest {
@Test
public void processLeafListInvalidIdentifier() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input '1invalid-interval' expecting IDENTIFIER");
thrown.expectMessage("YANG file error : leaf-list name 1invalid-interval is not valid.");
YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidIdentifier.yang");
}
......
......@@ -85,7 +85,7 @@ public class LeafListenerTest {
@Test
public void processLeafInvalidIdentifier() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input '1invalid-interval' expecting IDENTIFIER");
thrown.expectMessage("YANG file error : leaf name 1invalid-interval is not valid.");
YangNode node = manager.getDataModel("src/test/resources/LeafInvalidIdentifier.yang");
}
......
......@@ -205,7 +205,7 @@ public class ListListenerTest {
@Test
public void processListInvalidIdentifier() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input '1valid' expecting IDENTIFIER");
thrown.expectMessage("YANG file error : list name 1valid is not valid.");
YangNode node = manager.getDataModel("src/test/resources/ListInvalidIdentifier.yang");
}
}
\ No newline at end of file
......
......@@ -16,7 +16,9 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeType;
......@@ -35,6 +37,9 @@ public class ModuleListenerTest {
private final YangUtilsParserManager manager = new YangUtilsParserManager();
@Rule
public ExpectedException thrown = ExpectedException.none();
/**
* Checks if module listener updates the data model root node.
*/
......@@ -62,4 +67,15 @@ public class ModuleListenerTest {
YangNode node = manager.getDataModel("src/test/resources/ModuleWithInvalidIdentifier.yang");
}
/**
* Checks whether exception is thrown when module length is greater than 64 characters.
*/
@Test
public void processModuleInvalidIdentifierLength() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : module name Testttttttttttttttttttttttttttttttttttttttttttttttttttt" +
"tttttttttt is greater than 64 characters.");
YangNode node = manager.getDataModel("src/test/resources/ModuleInvalidIdentifierLength.yang");
}
}
\ No newline at end of file
......
......@@ -80,6 +80,45 @@ public class PositionListenerTest {
}
/**
* Checks position value with double quotes.
*/
@Test
public void processPositionWithDoubleQuotes() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/PositionWithDoubleQuotes.yang");
// Check whether the data model tree returned is of type module.
assertThat((node instanceof YangModule), is(true));
// Check whether the node type is set properly to module.
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getLeafName(), is("mybits"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
is("mybits"));
Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
for (YangBit tmp : bitSet) {
if (tmp.getBitName().equals("disable-nagle")) {
assertThat(tmp.getPosition(), is(0));
} else if (tmp.getBitName().equals("auto-sense-speed")) {
assertThat(tmp.getPosition(), is(1));
} else if (tmp.getBitName().equals("Ten-Mb-only")) {
assertThat(tmp.getPosition(), is(2));
}
}
}
/**
* Checks explicit value and auto generated value.
*/
@Test
......
......@@ -56,6 +56,16 @@ public class PrefixListenerTest {
}
/**
* Checks prefix value with double quotes.
*/
@Test
public void processPrefixWithDoubleQuotes() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/PrefixWithDoubleQuotes.yang");
assertThat(((YangModule) node).getPrefix(), is("On"));
}
/**
* Checks that prefix should be present just once.
*/
@Test(expected = ParserException.class)
......
......@@ -53,21 +53,29 @@ public class RevisionDateListenerTest {
}
/**
* Checks revision date should not be in quotes inside include.
* Checks revision date in quotes inside include.
*/
@Test(expected = ParserException.class)
@Test
public void processRevisionDateInQuotesAtInclude() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/RevisionDateInQuotesAtInclude.yang");
// Checks for the version value in data model tree.
assertThat(((YangModule) node).getImportList().get(0).getRevision(), is("2015-02-03"));
assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is("2014-02-03"));
}
/**
* Checks revision date should not be in quotes inside import.
* Checks revision date in quotes inside import.
*/
@Test(expected = ParserException.class)
@Test
public void processRevisionDateInQuotesAtImport() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/RevisionDateInQuotesAtImport.yang");
// Checks for the version value in data model tree.
assertThat(((YangModule) node).getImportList().get(0).getRevision(), is("2015-02-03"));
assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is("2014-02-03"));
}
/**
......
......@@ -56,6 +56,18 @@ public class VersionListenerTest {
}
/**
* Checks version in double quotes.
*/
@Test
public void processValidVersionWithDoubleQuotes() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/ValidVersionWithDoubleQuotes.yang");
// Checks for the version value in data model tree.
assertThat(((YangModule) node).getVersion(), is((byte) 1));
}
/**
* Checks if version which is optional paramater is not present.
*/
@Test
......
module Testttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt {
yang-version 1;
namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
prefix On;
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf mybits {
type bits {
bit disable-nagle {
position "0";
}
bit auto-sense-speed {
position "1";
}
bit Ten-Mb-only {
position "2";
}
}
}
}
module Test {
yang-version 1;
namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
prefix "On";
import ietf {
prefix On2;
revision-date 2015-02-03;
}
include itut {
revision-date 2016-02-03;
}
include sdn {
revision-date 2014-02-03;
}
contact "Test";
organization "ONOS";
}
module Test {
yang-version "1";
namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
prefix On;
}