janani b
Committed by Gerrit Code Review

YANG uses and UT

Change-Id: Id3ec5cfed2b8e2a7d2d580786c70b5804f03ecfa
Showing 30 changed files with 486 additions and 116 deletions
......@@ -16,6 +16,7 @@
package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.translator.tojava.TraversalType;
import static org.onosproject.yangutils.translator.tojava.TraversalType.CHILD;
......@@ -281,9 +282,11 @@ public abstract class YangNode
if (nextNodeToClone == null) {
throw new DataModelException("Internal error: Cloning failed, source tree null pointer reached");
}
if (curTraversal == CHILD) {
if (curTraversal != PARENT) {
newNode = nextNodeToClone.clone();
detectCollisionWhileCloning(clonedTreeCurNode, newNode, curTraversal);
}
if (curTraversal == CHILD) {
/**
* add the new node to the cloned tree.
......@@ -295,7 +298,6 @@ public abstract class YangNode
*/
clonedTreeCurNode = newNode;
} else if (curTraversal == SIBILING) {
newNode = nextNodeToClone.clone();
clonedTreeCurNode.addNextSibling(newNode);
clonedTreeCurNode = newNode;
......@@ -328,6 +330,38 @@ public abstract class YangNode
}
/**
* Detects collision when the grouping is deep copied to the uses's parent.
*
* @param currentNode parent/previous sibling node for the new node
* @param newNode node which has to be added
* @param addAs traversal type of the node
* @throws DataModelException data model error
*/
private static void detectCollisionWhileCloning(YangNode currentNode, YangNode newNode, TraversalType addAs)
throws DataModelException {
if ((!(currentNode instanceof CollisionDetector))
|| (!(newNode instanceof Parsable))) {
throw new DataModelException("Node in data model tree does not support collision detection");
}
CollisionDetector collisionDetector = (CollisionDetector) currentNode;
Parsable parsable = (Parsable) newNode;
if (addAs == TraversalType.CHILD) {
collisionDetector.detectCollidingChild(newNode.getName(), parsable.getYangConstructType());
} else if (addAs == TraversalType.SIBILING) {
currentNode = currentNode.getParent();
if (!(currentNode instanceof CollisionDetector)) {
throw new DataModelException("Node in data model tree does not support collision detection");
}
collisionDetector = (CollisionDetector) currentNode;
collisionDetector.detectCollidingChild(newNode.getName(), parsable.getYangConstructType());
} else {
throw new DataModelException("Errored tree cloning");
}
}
/**
* Add a new next sibling.
*
* @param newSibling new sibling to be added
......
......@@ -19,6 +19,7 @@ import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
/*-
......@@ -56,7 +57,7 @@ import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSy
*/
public class YangUses
extends YangNode
implements YangCommonInfo, Parsable, Resolvable {
implements YangCommonInfo, Parsable, Resolvable, CollisionDetector {
/**
* YANG node identifier.
......@@ -267,18 +268,23 @@ public class YangUses
}
YangNode usesParentNode = getParentNodeInGenCode(this);
if (!(usesParentNode instanceof YangLeavesHolder)) {
if ((!(usesParentNode instanceof YangLeavesHolder))
|| (!(usesParentNode instanceof CollisionDetector))) {
throw new DataModelException("YANG uses holder construct is wrong");
}
YangLeavesHolder usesParentLeavesHolder = (YangLeavesHolder) usesParentNode;
if (referredGrouping.getListOfLeaf() != null) {
for (YangLeaf leaf : referredGrouping.getListOfLeaf()) {
((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leaf.getLeafName(),
YangConstructType.LEAF_DATA);
usesParentLeavesHolder.addLeaf(leaf);
}
}
if (referredGrouping.getListOfLeafList() != null) {
for (YangLeafList leafList : referredGrouping.getListOfLeafList()) {
((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leafList.getLeafName(),
YangConstructType.LEAF_LIST_DATA);
usesParentLeavesHolder.addLeafList(leafList);
}
}
......@@ -295,4 +301,19 @@ public class YangUses
public void setResolvableStatus(ResolvableStatus resolvableStatus) {
this.resolvableStatus = resolvableStatus;
}
@Override
public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
detectCollidingChildUtil(identifierName, dataType, this);
}
@Override
public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
if (getName().equals(identifierName)) {
throw new DataModelException("YANG file error: Duplicate input identifier detected, same as uses \""
+ getName() + "\"");
}
}
}
......
......@@ -27,9 +27,9 @@ import org.onosproject.yangutils.datamodel.YangLeavesHolder;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangResolutionInfo;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
/**
* Represents utilities for data model tree.
*/
......@@ -53,21 +53,44 @@ public final class DataModelUtils {
public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
throws DataModelException {
if (dataType == YangConstructType.LEAF_DATA) {
YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
if (leavesHolder.getListOfLeaf() != null) {
detectCollidingLeaf(leavesHolder, identifierName);
}
}
if (dataType == YangConstructType.LEAF_LIST_DATA) {
if (((YangLeavesHolder) node).getListOfLeafList() != null) {
if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
detectCollidingForUsesGrouping(identifierName, dataType, node);
} else {
if (node instanceof YangLeavesHolder) {
YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
detectCollidingLeafList(leavesHolder, identifierName);
detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
}
node = node.getChild();
while (node != null) {
Parsable parsable = (Parsable) node;
if (node instanceof CollisionDetector
&& (parsable.getYangConstructType() != YangConstructType.USES_DATA)
&& (parsable.getYangConstructType() != YangConstructType.GROUPING_DATA)) {
((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
}
node = node.getNextSibling();
}
}
}
/**
* Detects colliding of uses and grouping only with uses and grouping respectively.
*
* @param identifierName name for which collision detection is to be
* checked
* @param dataType type of YANG node asking for detecting collision
* @param node node instance of calling node
* @throws DataModelException a violation of data model rules
*/
public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node)
throws DataModelException {
node = node.getChild();
while (node != null) {
if (node instanceof CollisionDetector) {
Parsable parsable = (Parsable) node;
if (node instanceof CollisionDetector
&& (parsable.getYangConstructType() == dataType)) {
((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
}
node = node.getNextSibling();
......@@ -77,15 +100,18 @@ public final class DataModelUtils {
/**
* Detects the colliding identifier name in a given leaf node.
*
* @param leavesHolder leaves node against which collision to be checked
* @param listOfLeaf List of leaves to detect collision
* @param identifierName name for which collision detection is to be
* checked
* @throws DataModelException a violation of data model rules
*/
private static void detectCollidingLeaf(YangLeavesHolder leavesHolder, String identifierName)
private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
throws DataModelException {
for (YangLeaf leaf : leavesHolder.getListOfLeaf()) {
if (listOfLeaf == null) {
return;
}
for (YangLeaf leaf : listOfLeaf) {
if (leaf.getLeafName().equals(identifierName)) {
throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
+ leaf.getLeafName() + "\"");
......@@ -96,15 +122,18 @@ public final class DataModelUtils {
/**
* Detects the colliding identifier name in a given leaf-list node.
*
* @param leavesHolder leaves node against which collision to be checked
* @param listOfLeafList list of leaf-lists to detect collision
* @param identifierName name for which collision detection is to be
* checked
* @throws DataModelException a violation of data model rules
*/
private static void detectCollidingLeafList(YangLeavesHolder leavesHolder, String identifierName)
private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
throws DataModelException {
for (YangLeafList leafList : leavesHolder.getListOfLeafList()) {
if (listOfLeafList == null) {
return;
}
for (YangLeafList leafList : listOfLeafList) {
if (leafList.getLeafName().equals(identifierName)) {
throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
"list \"" + leafList.getLeafName() + "\"");
......@@ -122,8 +151,6 @@ public final class DataModelUtils {
public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
throws DataModelException {
/* get the module node to add maintain the list of nested reference */
YangNode curNode = resolutionInfo.getEntityToResolveInfo()
.getHolderOfEntityToResolve();
......@@ -142,6 +169,13 @@ public final class DataModelUtils {
resolutionNode.addToResolutionList(resolutionInfo);
}
/**
* Evaluates whether the prefix in uses/type is valid.
*
* @param entityPrefix prefix in the current module/sub-module
* @param resolutionNode uses/type node which has the prefix with it
* @return whether prefix is valid or not
*/
private static boolean isPrefixValid(String entityPrefix, HasResolutionInfo resolutionNode) {
if (entityPrefix == null) {
return true;
......
......@@ -1565,22 +1565,6 @@ public interface GeneratedYangListener extends ParseTreeListener {
/**
* Enter a parse tree produced by GeneratedYangParser for grammar rule
* inputStatementBody.
*
* @param currentContext current context in the parsed tree
*/
void enterInputStatementBody(GeneratedYangParser.InputStatementBodyContext currentContext);
/**
* Exit a parse tree produced by GeneratedYangParser for grammar rule
* inputStatementBody.
*
* @param currentContext current context in the parsed tree
*/
void exitInputStatementBody(GeneratedYangParser.InputStatementBodyContext currentContext);
/**
* Enter a parse tree produced by GeneratedYangParser for grammar rule
* outputStatement.
*
* @param currentContext current context in the parsed tree
......@@ -1597,22 +1581,6 @@ public interface GeneratedYangListener extends ParseTreeListener {
/**
* Enter a parse tree produced by GeneratedYangParser for grammar rule
* outputStatementBody.
*
* @param currentContext current context in the parsed tree
*/
void enterOutputStatementBody(GeneratedYangParser.OutputStatementBodyContext currentContext);
/**
* Exit a parse tree produced by GeneratedYangParser for grammar rule
* outputStatementBody.
*
* @param currentContext current context in the parsed tree
*/
void exitOutputStatementBody(GeneratedYangParser.OutputStatementBodyContext currentContext);
/**
* Enter a parse tree produced by GeneratedYangParser for grammar rule
* notificationStatement.
*
* @param currentContext current context in the parsed tree
......
......@@ -52,8 +52,8 @@ import org.onosproject.yangutils.parser.impl.listeners.MandatoryListener;
import org.onosproject.yangutils.parser.impl.listeners.MaxElementsListener;
import org.onosproject.yangutils.parser.impl.listeners.MinElementsListener;
import org.onosproject.yangutils.parser.impl.listeners.ModuleListener;
import org.onosproject.yangutils.parser.impl.listeners.NotificationListener;
import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener;
import org.onosproject.yangutils.parser.impl.listeners.NotificationListener;
import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener;
import org.onosproject.yangutils.parser.impl.listeners.OutputListener;
import org.onosproject.yangutils.parser.impl.listeners.PatternRestrictionListener;
......@@ -1087,14 +1087,6 @@ public class TreeWalkListener implements GeneratedYangListener {
}
@Override
public void enterInputStatementBody(GeneratedYangParser.InputStatementBodyContext ctx) {
}
@Override
public void exitInputStatementBody(GeneratedYangParser.InputStatementBodyContext ctx) {
}
@Override
public void enterOutputStatement(GeneratedYangParser.OutputStatementContext ctx) {
OutputListener.processOutputEntry(this, ctx);
}
......@@ -1105,14 +1097,6 @@ public class TreeWalkListener implements GeneratedYangListener {
}
@Override
public void enterOutputStatementBody(GeneratedYangParser.OutputStatementBodyContext ctx) {
}
@Override
public void exitOutputStatementBody(GeneratedYangParser.OutputStatementBodyContext ctx) {
}
@Override
public void enterNotificationStatement(GeneratedYangParser.NotificationStatementContext ctx) {
NotificationListener.processNotificationEntry(this, ctx);
}
......
......@@ -46,12 +46,10 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds;
import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
......@@ -168,7 +166,5 @@ public final class GroupingListener {
validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, GROUPING_DATA,
ctx.identifier().getText());
validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, GROUPING_DATA, ctx.identifier().getText());
validateMutuallyExclusiveChilds(ctx.typedefStatement(), TYPEDEF_DATA, ctx.groupingStatement(), GROUPING_DATA,
GROUPING_DATA, ctx.identifier().getText());
}
}
......
......@@ -36,6 +36,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityNonZero;
import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.INPUT_DATA;
/*
......@@ -86,6 +88,9 @@ public final class InputListener {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, INPUT_DATA, "", ENTRY);
validateCardinalityNonZero(ctx.dataDefStatement(), DATA_DEF_DATA,
INPUT_DATA, "", ctx);
Parsable curData = listener.getParsedDataStack().peek();
if (curData instanceof YangRpc) {
......
......@@ -19,6 +19,7 @@ package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangAugment;
import org.onosproject.yangutils.datamodel.YangCase;
import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangGrouping;
import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.datamodel.YangList;
import org.onosproject.yangutils.datamodel.YangModule;
......@@ -140,7 +141,7 @@ public final class ListListener {
if (curData instanceof YangModule || curData instanceof YangContainer
|| curData instanceof YangList || curData instanceof YangCase
|| curData instanceof YangNotification || curData instanceof YangInput
|| curData instanceof YangOutput || curData instanceof YangAugment) {
|| curData instanceof YangOutput || curData instanceof YangAugment || curData instanceof YangGrouping) {
curNode = (YangNode) curData;
try {
curNode.addChild(yangList);
......
......@@ -40,13 +40,10 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds;
import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.NOTIFICATION_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
......@@ -159,7 +156,5 @@ public final class NotificationListener {
ctx.identifier().getText());
validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, NOTIFICATION_DATA,
ctx.identifier().getText());
validateMutuallyExclusiveChilds(ctx.typedefStatement(), TYPEDEF_DATA, ctx.groupingStatement(), GROUPING_DATA,
NOTIFICATION_DATA, ctx.identifier().getText());
}
}
......
......@@ -36,6 +36,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityNonZero;
import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.OUTPUT_DATA;
/*
......@@ -86,6 +88,9 @@ public final class OutputListener {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, OUTPUT_DATA, "", ENTRY);
validateCardinalityNonZero(ctx.dataDefStatement(), DATA_DEF_DATA,
OUTPUT_DATA, "", ctx);
Parsable curData = listener.getParsedDataStack().peek();
if (curData instanceof YangRpc) {
......
......@@ -37,12 +37,9 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds;
import static org.onosproject.yangutils.utils.YangConstructType.RPC_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.INPUT_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.OUTPUT_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
......@@ -157,8 +154,6 @@ public final class RpcListener {
validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, RPC_DATA, ctx.identifier().getText());
validateCardinalityMaxOne(ctx.inputStatement(), INPUT_DATA, RPC_DATA, ctx.identifier().getText());
validateCardinalityMaxOne(ctx.outputStatement(), OUTPUT_DATA, RPC_DATA, ctx.identifier().getText());
validateMutuallyExclusiveChilds(ctx.typedefStatement(), TYPEDEF_DATA, ctx.groupingStatement(), GROUPING_DATA,
RPC_DATA, ctx.identifier().getText());
}
}
......
......@@ -17,6 +17,7 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangGrouping;
import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.datamodel.YangList;
import org.onosproject.yangutils.datamodel.YangModule;
......@@ -127,11 +128,8 @@ public final class TypeDefListener {
if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangContainer
|| curData instanceof YangList || curData instanceof YangNotification || curData instanceof YangRpc
|| curData instanceof YangInput || curData instanceof YangOutput) {
|| curData instanceof YangInput || curData instanceof YangOutput || curData instanceof YangGrouping) {
/*
* TODO YangGrouping.
*/
YangNode curNode = (YangNode) curData;
try {
curNode.addChild(typeDefNode);
......
......@@ -23,8 +23,10 @@ import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.datamodel.YangList;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.datamodel.YangOutput;
import org.onosproject.yangutils.datamodel.YangResolutionInfo;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
......@@ -33,16 +35,18 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangUsesNode;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
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 static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
......@@ -116,6 +120,11 @@ public final class UsesListener {
// Validate sub statement cardinality.
validateSubStatementsCardinality(ctx);
// Check for identifier collision
int line = ctx.getStart().getLine();
int charPositionInLine = ctx.getStart().getCharPositionInLine();
detectCollidingChildUtil(listener, line, charPositionInLine, ctx.string().getText(), USES_DATA);
Parsable curData = listener.getParsedDataStack().peek();
if (curData instanceof YangModule || curData instanceof YangSubModule
......@@ -126,8 +135,8 @@ public final class UsesListener {
|| curData instanceof YangNotification) {
YangUses usesNode = getYangUsesNode(JAVA_GENERATION);
usesNode.setName(ctx.string().getText());
YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), USES_DATA, ctx);
usesNode.setNodeIdentifier(nodeIdentifier);
YangNode curNode = (YangNode) curData;
try {
......@@ -156,12 +165,29 @@ public final class UsesListener {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, USES_DATA, ctx.string().getText(), EXIT);
if (listener.getParsedDataStack().peek() instanceof YangUses) {
listener.getParsedDataStack().pop();
} else {
throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, USES_DATA,
Parsable parsableUses = listener.getParsedDataStack().pop();
if (!(parsableUses instanceof YangUses)) {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, USES_DATA,
ctx.string().getText(), EXIT));
}
YangUses uses = (YangUses) parsableUses;
int errorLine = ctx.getStart().getLine();
int errorPosition = ctx.getStart().getCharPositionInLine();
// Parent YANG node of uses to be added in resolution information.
Parsable parentNode = listener.getParsedDataStack().peek();
// Verify parent node of leaf
if (!(parentNode instanceof YangNode)) {
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, USES_DATA,
ctx.string().getText(), EXIT));
}
// Add resolution information to the list
YangResolutionInfo resolutionInfo = new YangResolutionInfo<YangUses>(uses,
(YangNode) parentNode, errorLine,
errorPosition);
addToResolutionList(resolutionInfo, ctx);
}
// TODO linker to handle collision scenarios like leaf obtained by uses, conflicts with some existing leaf.
......@@ -175,5 +201,23 @@ public final class UsesListener {
validateCardinalityMaxOne(ctx.whenStatement(), WHEN_DATA, USES_DATA, ctx.string().getText());
validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, USES_DATA, ctx.string().getText());
validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, USES_DATA, ctx.string().getText());
validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, USES_DATA, ctx.string().getText()); }
validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, USES_DATA, ctx.string().getText());
}
/**
* Add to resolution list.
*
* @param resolutionInfo resolution information.
* @param ctx context object of the grammar rule
*/
private static void addToResolutionList(YangResolutionInfo<YangUses> resolutionInfo,
GeneratedYangParser.UsesStatementContext ctx) {
try {
addResolutionInfo(resolutionInfo);
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
USES_DATA, ctx.string().getText(), EXIT, e.getMessage()));
}
}
}
......
......@@ -1098,12 +1098,7 @@ package org.onosproject.yangutils.parser.antlrgencode;
* 1*(data-def-stmt stmtsep)
* "}"
*/
inputStatement : INPUT_KEYWORD LEFT_CURLY_BRACE inputStatementBody RIGHT_CURLY_BRACE;
inputStatementBody : typedefStatement* dataDefStatement+
| dataDefStatement+ typedefStatement*
| groupingStatement* dataDefStatement+
| dataDefStatement+ groupingStatement*;
inputStatement : INPUT_KEYWORD LEFT_CURLY_BRACE (typedefStatement | groupingStatement | dataDefStatement)* RIGHT_CURLY_BRACE;
/**
* output-stmt = output-keyword optsep
......@@ -1114,12 +1109,7 @@ package org.onosproject.yangutils.parser.antlrgencode;
* 1*(data-def-stmt stmtsep)
* "}"
*/
outputStatement : OUTPUT_KEYWORD LEFT_CURLY_BRACE outputStatementBody RIGHT_CURLY_BRACE;
outputStatementBody : typedefStatement* dataDefStatement+
| dataDefStatement+ typedefStatement*
| groupingStatement* dataDefStatement+
| dataDefStatement+ groupingStatement*;
outputStatement : OUTPUT_KEYWORD LEFT_CURLY_BRACE (typedefStatement | groupingStatement | dataDefStatement)* RIGHT_CURLY_BRACE;
/**
* notification-stmt = notification-keyword sep
......
......@@ -19,6 +19,7 @@ package org.onosproject.yangutils.parser.impl.listeners;
import java.io.IOException;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangGrouping;
import org.onosproject.yangutils.datamodel.YangList;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangNode;
......@@ -56,7 +57,10 @@ public class UsesListenerTest {
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
YangUses yangUses = (YangUses) yangNode.getChild();
YangGrouping yangGrouping = (YangGrouping) yangNode.getChild();
assertThat(yangGrouping.getName(), is("endpoint"));
YangUses yangUses = (YangUses) yangGrouping.getNextSibling();
assertThat(yangUses.getName(), is("endpoint"));
}
......@@ -78,7 +82,10 @@ public class UsesListenerTest {
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
YangContainer yangContainer = (YangContainer) yangNode.getChild();
YangGrouping yangGrouping = (YangGrouping) yangNode.getChild();
assertThat(yangGrouping.getName(), is("endpoint"));
YangContainer yangContainer = (YangContainer) yangGrouping.getNextSibling();
assertThat(yangContainer.getName(), is("valid"));
YangUses yangUses = (YangUses) yangContainer.getChild();
......@@ -108,7 +115,10 @@ public class UsesListenerTest {
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
YangList yangList = (YangList) yangNode.getChild();
YangGrouping yangGrouping = (YangGrouping) yangNode.getChild();
assertThat(yangGrouping.getName(), is("endpoint"));
YangList yangList = (YangList) yangGrouping.getNextSibling();
assertThat(yangList.getName(), is("valid"));
YangUses yangUses = (YangUses) yangList.getChild();
......
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
import ietf-yang-types {
prefix "P";
}
grouping create {
uses P:valid;
}
container test{
uses create;
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
import ietf-yang-types {
prefix "P";
}
grouping Percentage {
leaf hello{
type String;
}
}
container ospf {
list valid {
key "invalid";
leaf invalid{
type String;
}
uses Ant:FirstClass;
grouping FirstClass {
uses P:PassingClass;
}
}
grouping PassingClass {
uses Ant:Percentage;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
grouping Percentage {
leaf hello{
type String;
}
}
container ospf {
list valid {
key "invalid";
leaf invalid{
type String;
}
uses Ant:FirstClass;
grouping FirstClass {
uses PassingClass;
}
}
grouping PassingClass {
uses Ant:Percentage;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
container valid {
grouping endpoint {
leaf zip-code {
type string;
}
uses failure;
container hold {
leaf newone {
type string;
}
}
uses failure;
}
grouping failure {
leaf test {
type string;
}
}
}
}
module rock {
namespace "http://example.net/rock";
prefix "rock";
rpc rock-the-house {
description "description";
status current;
reference "reference";
grouping hello {
list valid {
key invalid-interval;
reference "RFC 6020";
leaf invalid-interval {
type "uint16";
units "seconds";
status current;
reference "RFC 6020";
}
}
}
input {
leaf zip-code {
type string;
}
uses hello;
}
output {
leaf status {
type string;
}
uses hello;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
container valid {
grouping endpoint {
leaf zip-code {
type string;
}
uses endpoint;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
container valid {
grouping endpoint {
leaf zip-code {
type string;
}
uses first;
container design {
uses second;
container correct {
leaf newone {
type string;
}
uses third;
}
}
uses fourth;
uses fifth;
}
uses endpoint;
grouping first {
list valid {
key invalid-interval;
reference "RFC 6020";
leaf invalid-interval {
type "uint16";
units "seconds";
status current;
reference "RFC 6020";
}
}
}
grouping second {
leaf ink {
type int32;
}
}
grouping third {
container value {
leaf zip-code {
type string;
}
}
}
grouping fourth {
typedef my-type {
status deprecated;
type int32;
}
leaf correct {
type my-type;
}
}
grouping fifth {
leaf abc {
type string;
}
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
container test{
leaf leaf2{
type String;
}
grouping treat {
grouping create {
grouping mass {
}
}
}
}
uses treat;
}
module rock {
namespace "http://example.net/rock";
prefix "rock";
rpc rock-the-house {
description "description";
status current;
reference "reference";
input {
leaf zip-code {
type string;
}
grouping creative {
leaf carry {
type string;
}
}
}
output {
leaf status {
type string;
}
grouping creative {
list valid {
key invalid-interval;
leaf invalid-interval {
type "uint16";
}
}
}
typedef my-type {
status deprecated;
type int32;
}
uses creative;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
uses hello;
grouping hello {
leaf hello{
type String;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
uses treat;
grouping treat {
leaf treat{
type String;
}
container test{
leaf leaf2{
type String;
}
}
}
}
......@@ -2,6 +2,8 @@ module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
grouping endpoint {
}
container valid {
uses endpoint {
description "grouping under test";
......
......@@ -4,7 +4,9 @@ module Test {
prefix Ant;
import ietf-yang-types {
prefix "P";
}
}
grouping endpoint {
}
list valid {
key address;
leaf address {
......
......@@ -2,5 +2,7 @@ module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
grouping endpoint {
}
uses endpoint;
}
......