Vidyashree Rama
Committed by Gerrit Code Review

[ONOS-4636]grouping and uses

Change-Id: Ic410d03a838003ad23b2b0e8874b91503da84153
/*
* Copyright 2016-present 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.datamodel;
/**
* Represents data model tree traversal types.
*/
public enum TraversalType {
/**
* Start of traversal at the tree root.
*/
ROOT,
/**
* Child node traversal.
*/
CHILD,
/**
* Sibling node traversal.
*/
SIBILING,
/**
* Parent node traversal.
*/
PARENT
}
......@@ -17,6 +17,13 @@ package org.onosproject.yangutils.datamodel;
import java.io.Serializable;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.utils.Parsable;
import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.cloneLeaves;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.updateClonedLeavesUnionEnumRef;
/**
* Represents base class of a node in data model tree.
......@@ -226,4 +233,189 @@ public abstract class YangNode
newChild.setPreviousSibling(curNode);
}
}
/**
* Clones the current node contents and create a new node.
*
* @return cloned node
* @throws CloneNotSupportedException clone is not supported by the referred
* node
*/
public YangNode clone()
throws CloneNotSupportedException {
YangNode clonedNode = (YangNode) super.clone();
if (clonedNode instanceof YangLeavesHolder) {
try {
cloneLeaves((YangLeavesHolder) clonedNode);
} catch (DataModelException e) {
throw new CloneNotSupportedException(e.getMessage());
}
}
clonedNode.setParent(null);
clonedNode.setChild(null);
clonedNode.setNextSibling(null);
clonedNode.setPreviousSibling(null);
return clonedNode;
}
/**
* Clones the subtree from the specified source node to the mentioned target
* node. The source and target root node cloning is carried out by the
* caller.
*
* @param srcRootNode source node for sub tree cloning
* @param dstRootNode destination node where the sub tree needs to be cloned
* @throws DataModelException data model error
*/
public static void cloneSubTree(YangNode srcRootNode, YangNode dstRootNode)
throws DataModelException {
YangNode nextNodeToClone = srcRootNode;
TraversalType curTraversal;
YangNode clonedTreeCurNode = dstRootNode;
YangNode newNode = null;
nextNodeToClone = nextNodeToClone.getChild();
if (nextNodeToClone == null) {
return;
} else {
/**
* Root level cloning is taken care in the caller.
*/
curTraversal = CHILD;
}
/**
* Caller ensures the cloning of the root nodes
*/
try {
while (nextNodeToClone != srcRootNode) {
if (nextNodeToClone == null) {
throw new DataModelException("Internal error: Cloning failed, source tree null pointer reached");
}
if (curTraversal != PARENT) {
newNode = nextNodeToClone.clone();
detectCollisionWhileCloning(clonedTreeCurNode, newNode, curTraversal);
}
if (curTraversal == CHILD) {
/**
* add the new node to the cloned tree.
*/
clonedTreeCurNode.addChild(newNode);
/**
* update the cloned tree's traversal current node as the
* new node.
*/
clonedTreeCurNode = newNode;
} else if (curTraversal == SIBILING) {
clonedTreeCurNode.addNextSibling(newNode);
clonedTreeCurNode = newNode;
} else if (curTraversal == PARENT) {
if (clonedTreeCurNode instanceof YangLeavesHolder) {
updateClonedLeavesUnionEnumRef((YangLeavesHolder) clonedTreeCurNode);
}
clonedTreeCurNode = clonedTreeCurNode.getParent();
}
if (curTraversal != PARENT && nextNodeToClone.getChild() != null) {
curTraversal = CHILD;
/**
* update the traversal's current node.
*/
nextNodeToClone = nextNodeToClone.getChild();
} else if (nextNodeToClone.getNextSibling() != null) {
curTraversal = SIBILING;
nextNodeToClone = nextNodeToClone.getNextSibling();
} else {
curTraversal = PARENT;
nextNodeToClone = nextNodeToClone.getParent();
}
}
} catch (CloneNotSupportedException e) {
throw new DataModelException("Failed to clone the tree");
}
}
/**
* 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");
}
}
/**
* Adds a new next sibling.
*
* @param newSibling new sibling to be added
* @throws DataModelException data model error
*/
private void addNextSibling(YangNode newSibling)
throws DataModelException {
if (newSibling.getNodeType() == null) {
throw new DataModelException("Cloned abstract node cannot be inserted into a tree");
}
if (newSibling.getParent() == null) {
/**
* Since the siblings needs to have a common parent, set the parent
* as the current node's parent
*/
newSibling.setParent(getParent());
} else {
throw new DataModelException("Node is already part of a tree, and cannot be added as a sibling");
}
if (newSibling.getPreviousSibling() == null) {
newSibling.setPreviousSibling(this);
setNextSibling(newSibling);
} else {
throw new DataModelException("New sibling to be added is not atomic, it already has a previous sibling");
}
if (newSibling.getChild() != null) {
throw new DataModelException("Sibling to be added is not atomic, it already has a child");
}
if (newSibling.getNextSibling() != null) {
throw new DataModelException("Sibling to be added is not atomic, it already has a next sibling");
}
}
}
......
......@@ -25,6 +25,7 @@ import org.onosproject.yangutils.datamodel.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.updateClonedLeavesUnionEnumRef;
/*-
* Reference RFC 6020.
......@@ -329,42 +330,45 @@ public class YangUses
}
YangLeavesHolder usesParentLeavesHolder = (YangLeavesHolder) usesParentNode;
if (referredGrouping.getListOfLeaf() != null
&& referredGrouping.getListOfLeaf().size() != 0) {
addLeavesOfGrouping(
cloneLeavesList(referredGrouping.getListOfLeaf(),
usesParentLeavesHolder));
}
if (referredGrouping.getListOfLeaf() != null) {
for (YangLeaf leaf : referredGrouping.getListOfLeaf()) {
YangLeaf clonedLeaf = null;
try {
((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leaf.getName(),
YangConstructType.LEAF_DATA);
clonedLeaf = leaf.clone();
if (referredGrouping.getListOfLeafList() != null
&& referredGrouping.getListOfLeafList().size() != 0) {
addListOfLeafListOfGrouping(
cloneListOfLeafList(referredGrouping.getListOfLeafList(),
usesParentLeavesHolder));
} catch (CloneNotSupportedException | DataModelException e) {
throw new DataModelException(e.getMessage());
}
YangNode childInGrouping = referredGrouping.getChild();
clonedLeaf.setContainedIn(usesParentLeavesHolder);
usesParentLeavesHolder.addLeaf(clonedLeaf);
}
}
if (referredGrouping.getListOfLeafList() != null) {
for (YangLeafList leafList : referredGrouping.getListOfLeafList()) {
YangLeafList clonedLeafList = null;
try {
((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leafList.getName(),
YangConstructType.LEAF_LIST_DATA);
clonedLeafList = leafList.clone();
while (childInGrouping != null) {
if (childInGrouping instanceof YangEnumeration
|| childInGrouping instanceof YangUnion
|| childInGrouping instanceof YangTypeDef) {
} catch (CloneNotSupportedException | DataModelException e) {
throw new DataModelException(e.getMessage());
}
/*
* No need to copy the leaves, union / enum class, as these will
* be generated in the scope of grouping
*/
childInGrouping = childInGrouping.getNextSibling();
continue;
} else if (childInGrouping instanceof YangUses) {
addResolvedUsesInfoOfGrouping((YangUses) childInGrouping,
usesParentLeavesHolder);
} else {
addNodeOfGrouping(childInGrouping);
clonedLeafList.setContainedIn(usesParentLeavesHolder);
usesParentLeavesHolder.addLeafList(clonedLeafList);
}
}
childInGrouping = childInGrouping.getNextSibling();
try {
YangNode.cloneSubTree(referredGrouping, usesParentNode);
} catch (DataModelException e) {
throw new DataModelException(e.getMessage());
}
updateClonedLeavesUnionEnumRef(usesParentLeavesHolder);
}
/**
......
......@@ -20,6 +20,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
......@@ -28,6 +29,7 @@ import org.onosproject.yangutils.datamodel.ResolvableType;
import org.onosproject.yangutils.datamodel.YangIfFeature;
import org.onosproject.yangutils.datamodel.YangAugment;
import org.onosproject.yangutils.datamodel.YangBase;
import org.onosproject.yangutils.datamodel.YangEnumeration;
import org.onosproject.yangutils.datamodel.YangIdentityRef;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
......@@ -38,8 +40,10 @@ import org.onosproject.yangutils.datamodel.YangReferenceResolver;
import org.onosproject.yangutils.datamodel.YangResolutionInfo;
import org.onosproject.yangutils.datamodel.YangRpc;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangUnion;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
/**
* Represents utilities for data model tree.
......@@ -307,4 +311,104 @@ public final class DataModelUtils {
}
return nodes;
}
/**
* Clones the list of leaves and list of leaf list in the leaves holder.
*
* @param leavesHolder YANG node potentially containing leaves or leaf lists
* @throws CloneNotSupportedException clone is not supported
* @throws DataModelException data model error
*/
public static void cloneLeaves(YangLeavesHolder leavesHolder)
throws CloneNotSupportedException, DataModelException {
List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf();
if (currentListOfLeaves != null) {
List<YangLeaf> clonedLeavesList = new LinkedList<YangLeaf>();
for (YangLeaf leaf : currentListOfLeaves) {
YangLeaf clonedLeaf = leaf.clone();
clonedLeaf.setContainedIn(leavesHolder);
clonedLeavesList.add(clonedLeaf);
}
leavesHolder.setListOfLeaf(clonedLeavesList);
}
List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList();
if (currentListOfLeafList != null) {
List<YangLeafList> clonedListOfLeafList = new LinkedList<YangLeafList>();
for (YangLeafList leafList : currentListOfLeafList) {
YangLeafList clonedLeafList = leafList.clone();
clonedLeafList.setContainedIn(leavesHolder);
clonedListOfLeafList.add(clonedLeafList);
}
leavesHolder.setListOfLeafList(clonedListOfLeafList);
}
}
/**
* Clones the union or enum leaves. If there is any cloned leaves whose type is union/enum then the corresponding
* type info needs to be updated to the cloned new type node.
*
* @param leavesHolder cloned leaves holder, for whom the leaves reference needs to be updated
*/
public static void updateClonedLeavesUnionEnumRef(YangLeavesHolder leavesHolder) throws DataModelException {
List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf();
if (currentListOfLeaves != null) {
for (YangLeaf leaf : currentListOfLeaves) {
if (leaf.getDataType().getDataType() == YangDataTypes.ENUMERATION
|| leaf.getDataType().getDataType() == YangDataTypes.UNION) {
try {
updateClonedTypeRef(leaf.getDataType(), leavesHolder);
} catch (DataModelException e) {
throw e;
}
}
}
}
List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList();
if (currentListOfLeafList != null) {
for (YangLeafList leafList : currentListOfLeafList) {
if (leafList.getDataType().getDataType() == YangDataTypes.ENUMERATION
|| leafList.getDataType().getDataType() == YangDataTypes.UNION) {
try {
updateClonedTypeRef(leafList.getDataType(), leavesHolder);
} catch (DataModelException e) {
throw e;
}
}
}
}
}
/**
* Updates the types extended info pointer to point to the cloned type node.
*
* @param dataType data type, whose extended info needs to be pointed to the cloned type
* @param leavesHolder the leaves holder having the cloned type
*/
private static void updateClonedTypeRef(YangType dataType, YangLeavesHolder leavesHolder)
throws DataModelException {
if (!(leavesHolder instanceof YangNode)) {
throw new DataModelException("Data model error: cloned leaves holder is not a node");
}
YangNode potentialTypeNode = ((YangNode) leavesHolder).getChild();
while (potentialTypeNode != null) {
String dataTypeName = null;
if (dataType.getDataType() == YangDataTypes.ENUMERATION) {
YangEnumeration enumNode = (YangEnumeration) dataType.getDataTypeExtendedInfo();
dataTypeName = enumNode.getName();
} else if (dataType.getDataType() == YangDataTypes.UNION) {
YangUnion unionNode = (YangUnion) dataType.getDataTypeExtendedInfo();
dataTypeName = unionNode.getName();
}
if (potentialTypeNode.getName().contentEquals(dataTypeName)) {
dataType.setDataTypeExtendedInfo((Object) potentialTypeNode);
return;
}
potentialTypeNode = potentialTypeNode.getNextSibling();
}
throw new DataModelException("Data model error: cloned leaves type is not found");
}
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.translator.exception;
/**
* Represents custom translator exception for translator's operations.
*/
public class InvalidNodeForTranslatorException extends RuntimeException {
private static final long serialVersionUID = 20160311L;
private String fileName;
/**
* Create a new exception.
*/
public InvalidNodeForTranslatorException() {
super();
}
/**
* Creates a new exception with given message.
*
* @param message the detail of exception in string
*/
public InvalidNodeForTranslatorException(String message) {
super(message);
}
/**
* Creates a new exception from given message and cause.
*
* @param message the detail of exception in string
* @param cause underlying cause of the error
*/
public InvalidNodeForTranslatorException(final String message, final Throwable cause) {
super(message, cause);
}
/**
* Creates a new exception from cause.
*
* @param cause underlying cause of the error
*/
public InvalidNodeForTranslatorException(final Throwable cause) {
super(cause);
}
/**
* Returns generated file name for the exception.
*
* @return generated file name for the exception
*/
public String getFileName() {
return this.fileName;
}
/**
* Sets file name in translator exception.
*
* @param fileName generated file name
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
......@@ -17,17 +17,16 @@
package org.onosproject.yangutils.translator.tojava;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.TraversalType;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
import static org.onosproject.yangutils.translator.tojava.TraversalType.CHILD;
import static org.onosproject.yangutils.translator.tojava.TraversalType.PARENT;
import static org.onosproject.yangutils.translator.tojava.TraversalType.ROOT;
import static org.onosproject.yangutils.translator.tojava.TraversalType.SIBILING;
import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
/**
* Representation of java code generator based on application schema.
......@@ -82,10 +81,10 @@ public final class JavaCodeGeneratorUtil {
if (!(codeGenNode instanceof JavaCodeGenerator)) {
throw new TranslatorException("Unsupported node to generate code");
}
if (codeGenNode instanceof YangTypeDef) {
YangTypeDef typeDef = (YangTypeDef) codeGenNode;
if (typeDef.getTypeDefBaseType().getDataType() == YangDataTypes.LEAFREF
|| typeDef.getTypeDefBaseType().getDataType() == YangDataTypes.IDENTITYREF) {
setCurNode(codeGenNode);
try {
generateCodeEntry(codeGenNode, yangPlugin);
} catch (InvalidNodeForTranslatorException e) {
if (codeGenNode.getNextSibling() != null) {
curTraversal = SIBILING;
codeGenNode = codeGenNode.getNextSibling();
......@@ -94,11 +93,6 @@ public final class JavaCodeGeneratorUtil {
codeGenNode = codeGenNode.getParent();
}
continue;
}
}
setCurNode(codeGenNode);
try {
generateCodeEntry(codeGenNode, yangPlugin);
} catch (Exception e) {
throw new TranslatorException(e.getMessage());
}
......
......@@ -15,9 +15,8 @@
*/
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangGrouping;
import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
......@@ -25,8 +24,6 @@ import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.updatePackageInfo;
/**
* Represents grouping information extended to support java code generation.
*/
......@@ -102,11 +99,7 @@ public class YangJavaGrouping
@Override
public void generateCodeEntry(YangPluginConfig yangPlugin)
throws TranslatorException {
try {
updatePackageInfo(this, yangPlugin);
} catch (IOException e) {
throw new TranslatorException(e.getCause());
}
throw new InvalidNodeForTranslatorException();
}
@Override
......
......@@ -15,14 +15,8 @@
*/
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangGrouping;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
......@@ -30,10 +24,6 @@ import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeAsAttributeInTargetTempFile;
import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.updatePackageInfo;
/**
* Represents uses information extended to support java code generation.
*/
......@@ -108,42 +98,7 @@ public class YangJavaUses
@Override
public void generateCodeEntry(YangPluginConfig yangPlugin)
throws TranslatorException {
try {
updatePackageInfo(this, yangPlugin);
if (!(getParentNodeInGenCode(this) instanceof JavaCodeGeneratorInfo)) {
throw new TranslatorException("invalid container of uses");
}
JavaCodeGeneratorInfo javaCodeGeneratorInfo = (JavaCodeGeneratorInfo) getParentNodeInGenCode(this);
if (javaCodeGeneratorInfo instanceof YangGrouping) {
/*
* Do nothing, since it will taken care in the groupings uses.
*/
return;
}
for (List<YangLeaf> leavesList : getUsesResolvedLeavesList()) {
// add the resolved leaves to the parent as an attribute
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
.getBeanTempFiles().addLeavesInfoToTempFiles(leavesList, yangPlugin);
}
for (List<YangLeafList> listOfLeafLists : getUsesResolvedListOfLeafList()) {
// add the resolved leaf-list to the parent as an attribute
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
.getBeanTempFiles().addLeafListInfoToTempFiles(listOfLeafLists, yangPlugin);
}
for (YangNode usesResolvedNode : getUsesResolvedNodeList()) {
// add the resolved nodes to the parent as an attribute
addCurNodeAsAttributeInTargetTempFile(usesResolvedNode, yangPlugin,
getParentNodeInGenCode(this));
}
} catch (IOException e) {
throw new TranslatorException(e.getCause());
}
throw new InvalidNodeForTranslatorException();
}
@Override
......
......@@ -17,14 +17,13 @@
package org.onosproject.yangutils.plugin.manager;
import java.io.IOException;
import java.util.List;
import java.util.ListIterator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangGrouping;
import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangList;
import org.onosproject.yangutils.datamodel.YangModule;
......@@ -33,6 +32,7 @@ import org.onosproject.yangutils.datamodel.YangNodeType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
......@@ -70,8 +70,13 @@ public class IntraFileUsesLinkingTest {
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator;
YangLeaf leafInfo;
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under module.
assertThat(leafInfo.getName(), is("hello"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
// Check whether grouping is the sibling of module's child.
assertThat((yangNode.getChild().getNextSibling() instanceof YangGrouping), is(true));
......@@ -89,20 +94,9 @@ public class IntraFileUsesLinkingTest {
assertThat((yangNode.getChild() instanceof YangUses), is(true));
YangUses uses = (YangUses) yangNode.getChild();
// Check whether uses get resolved.
// Check whether uses get resolved
assertThat(uses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
ListIterator<List<YangLeaf>> leafIterator1 = uses.getUsesResolvedLeavesList().listIterator();
List<YangLeaf> leafInfo1 = leafIterator1.next();
ListIterator<YangLeaf> leafIterator2 = leafInfo1.listIterator();
YangLeaf leafInfo2 = leafIterator2.next();
// Check whether the information in the leaf is correct under module.
assertThat(leafInfo2.getName(), is("hello"));
assertThat(leafInfo2.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo2.getDataType().getDataType(), is(YangDataTypes.STRING));
}
/**
......@@ -129,6 +123,27 @@ public class IntraFileUsesLinkingTest {
ListIterator<YangLeaf> leafIterator;
YangLeaf leafInfo;
ListIterator<YangLeaf> leafIterator1 = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo1 = leafIterator1.next();
// Check whether the information in the leaf is correct under module.
assertThat(leafInfo1.getName(), is("treat"));
assertThat(leafInfo1.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo1.getDataType().getDataType(), is(YangDataTypes.STRING));
YangContainer container = (YangContainer) yangNode.getChild().getNextSibling().getNextSibling();
// Check whether the container name is set correctly which is under module.
assertThat(container.getName(), is("test"));
leafIterator = container.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under container which is under module.
assertThat(leafInfo.getName(), is("leaf2"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
// Check whether grouping is the sibling of module's child.
assertThat((yangNode.getChild().getNextSibling() instanceof YangGrouping), is(true));
......@@ -143,7 +158,7 @@ public class IntraFileUsesLinkingTest {
// Check whether container is the child of grouping.
assertThat((grouping.getChild() instanceof YangContainer), is(true));
YangContainer container = (YangContainer) grouping.getChild();
container = (YangContainer) grouping.getChild();
// Check whether the container name is set correctly which is under grouping.
assertThat(container.getName(), is("test"));
......@@ -164,32 +179,6 @@ public class IntraFileUsesLinkingTest {
assertThat(uses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
ListIterator<List<YangLeaf>> leafIterator1 = uses.getUsesResolvedLeavesList().listIterator();
List<YangLeaf> leafInfo1 = leafIterator1.next();
ListIterator<YangLeaf> leafIterator2 = leafInfo1.listIterator();
YangLeaf leafInfo2 = leafIterator2.next();
// Check whether the information in the leaf is correct under module.
assertThat(leafInfo2.getName(), is("treat"));
assertThat(leafInfo2.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo2.getDataType().getDataType(), is(YangDataTypes.STRING));
ListIterator<YangNode> usesChildren = uses.getUsesResolvedNodeList().listIterator();
YangNode usesChild = usesChildren.next();
// Check whether container is the child of module.
assertThat((usesChild instanceof YangContainer), is(true));
container = (YangContainer) usesChild;
// Check whether the container name is set correctly which is under module.
assertThat(container.getName(), is("test"));
leafIterator = container.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under container which is under module.
assertThat(leafInfo.getName(), is("leaf2"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
}
/**
......@@ -245,18 +234,12 @@ public class IntraFileUsesLinkingTest {
YangUses uses = (YangUses) yangNode.getChild().getChild().getNextSibling().getChild();
// Check whether uses get resolved.
assertThat(uses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
ListIterator<YangNode> usesChildren = uses.getUsesResolvedNodeList().listIterator();
YangNode usesChild = usesChildren.next();
// Check whether list is the sibling of uses which has been deep copied from grouping.
assertThat((usesChild instanceof YangList), is(true));
assertThat(uses.getResolvableStatus(), is(ResolvableStatus.RESOLVED));
YangList yangList = (YangList) usesChild;
YangInput inputNode = ((YangInput) yangNode.getChild().getChild().getNextSibling());
assertThat((inputNode.getChild() instanceof YangUses), is(true));
// Check whether the list name is set correctly.
YangList yangList = ((YangList) inputNode.getChild().getNextSibling());
assertThat(yangList.getName(), is("valid"));
leafIterator = yangList.getListOfLeaf().listIterator();
......@@ -268,36 +251,6 @@ public class IntraFileUsesLinkingTest {
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
assertThat(leafInfo.getUnits(), is("\"seconds\""));
assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
// Check whether uses is output's child.
assertThat((yangNode.getChild().getChild().getNextSibling().getNextSibling().getChild() instanceof YangUses),
is(true));
YangUses usesInOuput = (YangUses) yangNode.getChild().getChild().getNextSibling().getNextSibling().getChild();
// Check whether uses get resolved.
assertThat(usesInOuput.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
ListIterator<YangNode> usesInOuputChildren = usesInOuput.getUsesResolvedNodeList().listIterator();
YangNode usesInOuputChild = usesInOuputChildren.next();
// Check whether list is the sibling of uses which has been deep copied from grouping.
assertThat((usesInOuputChild instanceof YangList), is(true));
YangList yangListInOutput = (YangList) usesInOuputChild;
// Check whether the list name is set correctly.
assertThat(yangListInOutput.getName(), is("valid"));
leafIterator = yangListInOutput.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under list which is deep copied.
assertThat(leafInfo.getName(), is("invalid-interval"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
assertThat(leafInfo.getUnits(), is("\"seconds\""));
assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
}
/**
......@@ -350,18 +303,16 @@ public class IntraFileUsesLinkingTest {
YangUses firstUses = (YangUses) grouping.getChild();
// Check whether uses get resolved.
assertThat(firstUses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
assertThat(firstUses.getResolvableStatus(), is(ResolvableStatus.RESOLVED));
ListIterator<YangNode> firstUsesChildren = firstUses.getUsesResolvedNodeList().listIterator();
YangNode firstUsesChild = firstUsesChildren.next();
// Check whether list is the sibling of uses.
assertThat((firstUsesChild instanceof YangList), is(true));
YangList yangList = (YangList) firstUsesChild;
assertThat(yangList.getName(), is("valid"));
// Validate first uses child is cloned properly
assertThat((firstUses.getNextSibling().getNextSibling()
.getNextSibling().getNextSibling() instanceof YangList), is(true));
YangList firstUsesChild = ((YangList) firstUses.getNextSibling().getNextSibling().getNextSibling()
.getNextSibling());
assertThat(firstUsesChild.getName(), is("valid"));
leafIterator = yangList.getListOfLeaf().listIterator();
leafIterator = firstUsesChild.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under list which has been deep copied from grouping.
......@@ -371,134 +322,45 @@ public class IntraFileUsesLinkingTest {
assertThat(leafInfo.getUnits(), is("\"seconds\""));
assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
// Check whether container is the sibling of uses.
//validate uses second
assertThat((firstUses.getNextSibling() instanceof YangContainer), is(true));
YangContainer yangContainer = (YangContainer) firstUses.getNextSibling();
// Check whether the container name is set correctly.
assertThat(yangContainer.getName(), is("design"));
// Check whether uses is design-container's child.
assertThat((yangContainer.getChild() instanceof YangUses), is(true));
YangUses secondUses = (YangUses) yangContainer.getChild();
// Check whether uses get resolved.
assertThat(secondUses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
ListIterator<List<YangLeaf>> leafIterator1 = secondUses.getUsesResolvedLeavesList().listIterator();
List<YangLeaf> leafInfo1 = leafIterator1.next();
ListIterator<YangLeaf> leafIterator2 = leafInfo1.listIterator();
YangLeaf leafInfo2 = leafIterator2.next();
// Check whether the information in the leaf is correct under design-container.
assertThat(leafInfo2.getName(), is("ink"));
assertThat(leafInfo2.getDataType().getDataTypeName(), is("int32"));
assertThat(leafInfo2.getDataType().getDataType(), is(YangDataTypes.INT32));
// Check whether container is the sibling of uses.
assertThat((secondUses.getNextSibling() instanceof YangContainer), is(true));
YangContainer yangContainer2 = (YangContainer) secondUses.getNextSibling();
assertThat(yangContainer2.getName(), is("correct"));
leafIterator = yangContainer2.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under correct-container.
assertThat(leafInfo.getName(), is("newone"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
// Check whether uses is correct container's child.
assertThat((yangContainer2.getChild() instanceof YangUses), is(true));
YangUses thirdUses = (YangUses) yangContainer2.getChild();
// Check whether uses get resolved.
assertThat(thirdUses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
ListIterator<YangNode> thirdUsesChildren = thirdUses.getUsesResolvedNodeList().listIterator();
YangNode thirdUsesChild = thirdUsesChildren.next();
// Check whether container is the child of uses.
assertThat((thirdUsesChild instanceof YangContainer), is(true));
YangContainer yangContainer3 = (YangContainer) thirdUsesChild;
assertThat(yangContainer3.getName(), is("value"));
leafIterator = yangContainer3.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
// Check whether the information in the leaf is correct under container
// which has been deep copied from grouping.
assertThat(leafInfo.getName(), is("zip-code"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
// Check whether uses is the sibling of container-design.
assertThat((yangContainer.getNextSibling() instanceof YangUses), is(true));
YangUses fourthUses = (YangUses) yangContainer.getNextSibling();
assertThat(fourthUses.getName(), is("fourth"));
// Check whether uses get resolved.
assertThat(fourthUses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
ListIterator<List<YangLeaf>> fourthUsesChildren = fourthUses.getUsesResolvedLeavesList().listIterator();
List<YangLeaf> fourthUsesChild = fourthUsesChildren.next();
ListIterator<YangLeaf> fourthUsesChildren1 = fourthUsesChild.listIterator();
YangLeaf fourthUsesChild1 = fourthUsesChildren1.next();
// Check whether the information in the leaf is correct under correct-container.
assertThat(fourthUsesChild1.getName(), is("correct"));
assertThat(fourthUsesChild1.getDataType().getDataTypeName(), is("my-type"));
assertThat(fourthUsesChild1.getDataType().getDataType(), is(YangDataTypes.DERIVED));
// Check whether uses is the sibling of previous uses.
assertThat((fourthUses.getNextSibling() instanceof YangUses), is(true));
YangUses fifthUses = (YangUses) fourthUses.getNextSibling();
assertThat(fifthUses.getName(), is("fifth"));
// Check whether uses get resolved.
assertThat(fifthUses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
ListIterator<List<YangLeaf>> fifthUsesChildren = fifthUses.getUsesResolvedLeavesList().listIterator();
List<YangLeaf> fifthUsesChild = fifthUsesChildren.next();
ListIterator<YangLeaf> fifthUsesChildren1 = fifthUsesChild.listIterator();
YangLeaf fifthUsesChild1 = fifthUsesChildren1.next();
//Check whether the information in the leaf is correct under correct-container.
assertThat(fifthUsesChild1.getName(), is("abc"));
assertThat(fifthUsesChild1.getDataType().getDataTypeName(), is("string"));
assertThat(fifthUsesChild1.getDataType().getDataType(), is(YangDataTypes.STRING));
//Check whether uses is endpoint-grouping's sibling.
assertThat((grouping.getNextSibling() instanceof YangUses), is(true));
YangUses endpointUses = (YangUses) grouping.getNextSibling();
// Check whether uses get resolved.
assertThat(endpointUses.getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
assertThat(endpointUses.getName(), is("endpoint"));
ListIterator<YangNode> endpointUsesUsesChildren = endpointUses.getUsesResolvedNodeList().listIterator();
YangNode endpointUsesUsesChild = endpointUsesUsesChildren.next();
// Check whether list is the sibling of uses.
assertThat((endpointUsesUsesChild instanceof YangList), is(true));
YangList yangList1 = (YangList) firstUsesChild;
assertThat(yangList1.getName(), is("valid"));
YangContainer container = (YangContainer) firstUses.getNextSibling();
assertThat(container.getName(), is("design"));
assertThat((container.getChild() instanceof YangUses), is(true));
assertThat((container.getListOfLeaf().iterator().next().getName()), is("ink"));
//validate uses third
assertThat((container.getChild().getNextSibling() instanceof YangContainer), is(true));
YangContainer container2 = ((YangContainer) container.getChild().getNextSibling());
assertThat(container2.getName(), is("correct"));
assertThat((container2.getChild() instanceof YangUses), is(true));
assertThat((container2.getChild().getNextSibling() instanceof YangContainer), is(true));
YangContainer thirdUsesChild = ((YangContainer) container2.getChild().getNextSibling());
assertThat(thirdUsesChild.getListOfLeaf().iterator().next().getName(), is("zip-code"));
//validate fourth uses
assertThat((firstUses.getNextSibling().getNextSibling() instanceof YangUses), is(true));
YangUses fourthUses = ((YangUses) firstUses.getNextSibling().getNextSibling());
assertThat((fourthUses.getNextSibling().getNextSibling().getNextSibling() instanceof YangTypeDef),
is(true));
assertThat(fourthUses.getNextSibling().getNextSibling().getNextSibling().getName(), is("my-type"));
leafIterator = yangList1.getListOfLeaf().listIterator();
leafInfo = leafIterator.next();
//validate fifth uses
assertThat((firstUses.getNextSibling().getNextSibling().getNextSibling() instanceof YangUses),
is(true));
// Check whether the information in the leaf is correct under list which has been deep copied from grouping.
assertThat(leafInfo.getName(), is("invalid-interval"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
assertThat(leafInfo.getUnits(), is("\"seconds\""));
assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
//validate end point uses
assertThat(grouping.getNextSibling() instanceof YangUses, is(true));
assertThat(grouping.getNextSibling().getNextSibling().getNextSibling().getNextSibling()
.getNextSibling().getNextSibling().getNextSibling().getNextSibling() instanceof YangContainer,
is(true));
container = (YangContainer) grouping.getNextSibling().getNextSibling().getNextSibling().getNextSibling()
.getNextSibling().getNextSibling().getNextSibling().getNextSibling();
assertThat(container.getName(), is("design"));
container2 = (YangContainer) container.getChild().getNextSibling();
assertThat(container2.getName(), is("correct"));
assertThat(container2.getChild().getNextSibling().getName(), is("value"));
}
/**
......
......@@ -26,6 +26,7 @@ import org.onosproject.yangutils.datamodel.YangAugment;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangReferenceResolver;
import org.onosproject.yangutils.datamodel.YangResolutionInfo;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
import org.onosproject.yangutils.linker.impl.YangLinkerManager;
import org.onosproject.yangutils.linker.impl.YangXpathLinker;
import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
......@@ -229,7 +230,7 @@ public class YangXpathLinkerTest {
*
* @throws IOException when fails to do IO operations
*/
@Test
@Test(expected = LinkerException.class)
public void processIntraFileLinkingInUsesSingleLevel() throws IOException {
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(INTRA_FILE_PATH + "IntraSingleUses/"));
......@@ -259,7 +260,7 @@ public class YangXpathLinkerTest {
*
* @throws IOException when fails to do IO operations
*/
@Test
@Test(expected = LinkerException.class)
public void processIntraFileLinkingInUsesMultiLevel() throws IOException {
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(INTRA_FILE_PATH + "IntraMultiUses/"));
......@@ -568,7 +569,7 @@ public class YangXpathLinkerTest {
*
* @throws IOException when fails to do IO operations
*/
@Test
@Test(expected = LinkerException.class)
public void processInterFileLinkingInUsesMultiLevel() throws IOException {
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(INTER_FILE_PATH + "InterMultiUses/"));
......