Vidyashree Rama
Committed by Ray Milkey

[ONOS-4563][ONOS-4564][ONOS-4551][ONOS-4538]defect fix

Change-Id: Ia3fe844e1e846d2e1d2c4359eefc815e7767aef9
Showing 24 changed files with 361 additions and 31 deletions
......@@ -50,7 +50,7 @@ import org.onosproject.yangutils.utils.YangConstructType;
/**
* Represents the ENUM data type information.
*/
public class YangEnum implements YangCommonInfo, Parsable {
public class YangEnum implements YangCommonInfo, Parsable, Comparable<YangEnum> {
/**
* Named value for the ENUM.
......@@ -226,4 +226,12 @@ public class YangEnum implements YangCommonInfo, Parsable {
public void validateDataOnExit() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
@Override
public int compareTo(YangEnum otherEnum) {
if (this.namedValue.equals(otherEnum.getNamedValue())) {
return 0;
}
return new Integer(this.value).compareTo(otherEnum.getValue());
}
}
......
......@@ -16,8 +16,8 @@
package org.onosproject.yangutils.datamodel;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
......@@ -34,7 +34,7 @@ import org.onosproject.yangutils.utils.YangConstructType;
public class YangEnumeration extends YangNode implements Parsable, CollisionDetector {
// Enumeration info set.
private Set<YangEnum> enumSet;
private SortedSet<YangEnum> enumSet;
// Enumeration name.
private String name;
......@@ -44,7 +44,7 @@ public class YangEnumeration extends YangNode implements Parsable, CollisionDete
*/
public YangEnumeration() {
super(YangNodeType.ENUMERATION_NODE);
setEnumSet(new HashSet<YangEnum>());
setEnumSet(new TreeSet<YangEnum>());
}
/**
......@@ -52,7 +52,7 @@ public class YangEnumeration extends YangNode implements Parsable, CollisionDete
*
* @return the ENUM set
*/
public Set<YangEnum> getEnumSet() {
public SortedSet<YangEnum> getEnumSet() {
return enumSet;
}
......@@ -61,7 +61,7 @@ public class YangEnumeration extends YangNode implements Parsable, CollisionDete
*
* @param enumSet the ENUM set to set
*/
private void setEnumSet(Set<YangEnum> enumSet) {
private void setEnumSet(SortedSet<YangEnum> enumSet) {
this.enumSet = enumSet;
}
......
......@@ -21,6 +21,8 @@ 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;
/*-
* Reference RFC 6020.
*
......@@ -54,7 +56,7 @@ import org.onosproject.yangutils.utils.YangConstructType;
/**
* Represents data model node to maintain information defined in YANG typedef.
*/
public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable, YangTypeHolder {
public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable, YangTypeHolder, CollisionDetector {
/**
* Default value in string, needs to be converted to the target object,
......@@ -272,4 +274,18 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable, Y
public List<YangType<?>> getTypeList() {
return typeList;
}
@Override
public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
// Asks helper to detect colliding child.
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 typedef \""
+ getName() + "\"");
}
}
}
......
......@@ -34,6 +34,8 @@ import static org.onosproject.yangutils.linker.impl.ResolvableStatus.INTRA_FILE_
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.LINKED;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.RESOLVED;
import static org.onosproject.yangutils.linker.impl.ResolvableStatus.UNRESOLVED;
import static org.onosproject.yangutils.utils.UtilConstants.TYPEDEF_LINKER_ERROR;
import static org.onosproject.yangutils.utils.UtilConstants.GROUPING_LINKER_ERROR;
/**
* Represents resolution object which will be resolved by linker.
......@@ -180,9 +182,14 @@ public class YangResolutionInfo<T> implements LocationInfo {
if (resolvable.getResolvableStatus() == UNRESOLVED) {
// If current entity is still not resolved, then linking/resolution has failed.
String errorInfo;
if (resolvable instanceof YangType) {
errorInfo = TYPEDEF_LINKER_ERROR;
} else {
errorInfo = GROUPING_LINKER_ERROR;
}
DataModelException dataModelException =
new DataModelException("YANG file error: Unable to find base "
+ "typedef/grouping for given type/uses");
new DataModelException(errorInfo);
dataModelException.setLine(getLineNumber());
dataModelException.setCharPosition(getCharPosition());
throw dataModelException;
......
......@@ -45,6 +45,8 @@ import org.onosproject.yangutils.datamodel.YangBits;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUnion;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
......@@ -101,6 +103,12 @@ public final class BitsListener {
case LEAF_LIST_DATA:
bitsNode.setBitsName(((YangLeafList) tmpData).getName());
break;
case TYPEDEF_DATA:
bitsNode.setBitsName(((YangTypeDef) tmpData).getName());
break;
case UNION_DATA:
bitsNode.setBitsName(((YangUnion) tmpData).getName());
break;
// TODO typedef, union, deviate.
default:
throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
......
......@@ -106,7 +106,16 @@ public final class MaxElementsListener {
if (value.equals(UNBOUNDED_KEYWORD)) {
maxElementsValue = Integer.MAX_VALUE;
} else if (value.matches(POSITIVE_INTEGER_PATTERN)) {
maxElementsValue = Integer.parseInt(value);
try {
maxElementsValue = Integer.parseInt(value);
} catch (NumberFormatException e) {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(MAX_ELEMENT_DATA) + " value " + value + " is not " +
"valid.");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
} else {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(MAX_ELEMENT_DATA) + " value " + value + " is not " +
......
......@@ -35,6 +35,7 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangTypeDefNode;
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;
......@@ -117,6 +118,11 @@ public final class TypeDefListener {
// Validate sub statement cardinality.
validateSubStatementsCardinality(ctx);
// Check for identifier collision
int line = ctx.getStart().getLine();
int charPositionInLine = ctx.getStart().getCharPositionInLine();
detectCollidingChildUtil(listener, line, charPositionInLine, identifier, TYPEDEF_DATA);
/*
* Create a derived type information, the base type must be set in type
* listener.
......
......@@ -54,6 +54,7 @@ public final class ListenerUtil {
private static final String DATE_PATTERN = "[0-9]{4}-([0-9]{2}|[0-9])-([0-9]{2}|[0-9])";
private static final String NON_NEGATIVE_INTEGER_PATTERN = "[0-9]+";
private static final Pattern INTEGER_PATTERN = Pattern.compile("[-][0-9]+|[0-9]+");
private static final String XML = "xml";
private static final String ONE = "1";
private static final int IDENTIFIER_LENGTH = 64;
private static final String DATE_FORMAT = "yyyy-MM-dd";
......@@ -102,6 +103,10 @@ public final class ListenerUtil {
parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is not " +
"valid.");
} else if (identifierString.toLowerCase().startsWith(XML)) {
parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(yangConstruct) + " identifier " + identifierString +
" must not start with (('X'|'x') ('M'|'m') ('L'|'l')).");
} else {
return identifierString;
}
......@@ -175,7 +180,18 @@ public final class ListenerUtil {
throw parserException;
}
return Integer.parseInt(value);
int valueInInteger;
try {
valueInInteger = Integer.parseInt(value);
} catch (NumberFormatException e) {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " +
"valid.");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
return valueInInteger;
}
/**
......
......@@ -251,8 +251,8 @@ public final class JavaCodeGeneratorUtil {
*/
private static void close(YangNode node)
throws IOException {
if (((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles() != null) {
if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
.getTempJavaCodeFragmentFiles() != null) {
((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
}
}
......
......@@ -1337,19 +1337,6 @@ public class TempJavaFragmentFiles {
*/
if ((fileType & INTERFACE_MASK) != 0 || (fileType &
BUILDER_INTERFACE_MASK) != 0) {
/*
* Adds import for case.
*/
if (curNode instanceof YangCase) {
List<String> importData =
((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
.getBeanTempFiles().getJavaImportData().getImports();
for (String importInfo : importData) {
if (!imports.contains(importInfo)) {
imports.add(importInfo);
}
}
}
/*
* Create interface file.
......
......@@ -42,6 +42,7 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION_HOLDER;
......@@ -279,7 +280,7 @@ public final class YangJavaModelUtils {
if (javaCodeGeneratorInfo instanceof YangCase) {
YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo();
String parentName = ((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName();
String parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName());
String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
parentsInfo.setClassInfo(parentName);
parentsInfo.setPkgInfo(parentPkg);
......
......@@ -1122,6 +1122,18 @@ public final class UtilConstants {
" for YANG utils road map.";
/**
* Static attribute for typedef linker error information.
*/
public static final String TYPEDEF_LINKER_ERROR = "YANG file error: Unable to find base "
+ "typedef for given type";
/**
* Static attribute for grouping linker error information.
*/
public static final String GROUPING_LINKER_ERROR = "YANG file error: Unable to find base "
+ "grouping for given uses";
/**
* Creates an instance of util constants.
*/
private UtilConstants() {
......
......@@ -564,7 +564,7 @@ public class IntraFileUsesLinkingTest {
thrown.expect(ParserException.class);
thrown.expectMessage(
"YANG file error: Unable to find base typedef/grouping for given type/uses");
"YANG file error: Unable to find base grouping for given uses");
YangNode node = manager
.getDataModel("src/test/resources/SelfResolutionNestedGroupingWithUnresolvedUses.yang");
......
......@@ -26,10 +26,14 @@ import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeType;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUnion;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import java.io.IOException;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
......@@ -80,6 +84,84 @@ public class BitListenerTest {
}
/**
* Checks bit statement with typedef.
*/
@Test
public void processBitTypedefStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/BitTypedefStatement.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"));
YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
assertThat(typedef.getName(), is("type15"));
YangType type = typedef.getTypeList().iterator().next();
assertThat(type.getDataType(), is(YangDataTypes.BITS));
assertThat(type.getDataTypeName(), is("bits"));
Set<YangBit> bitSet = ((YangBits) type.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));
}
}
}
/**
* Checks bit statement with union.
*/
@Test
public void processBitUnionStatement() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/BitUnionStatement.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.getName(), is("type15"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UNION));
assertThat(leafInfo.getDataType().getDataTypeName(), is("union"));
YangUnion yangUnion = (YangUnion) leafInfo.getDataType().getDataTypeExtendedInfo();
List<YangType<?>> typeList = yangUnion.getTypeList();
ListIterator<YangType<?>> typeListIterator = typeList.listIterator();
YangType<?> yangType = typeListIterator.next();
assertThat(yangType.getDataType(), is(YangDataTypes.BITS));
assertThat(yangType.getDataTypeName(), is("bits"));
Set<YangBit> bitSet = ((YangBits) yangType.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));
}
}
}
/**
* Checks if enum with same name is not allowed.
*/
@Test(expected = ParserException.class)
......
......@@ -33,8 +33,9 @@ import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import java.io.IOException;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Set;
import java.util.SortedSet;
/**
* Test cases for enum listener.
......@@ -73,7 +74,7 @@ public class EnumListenerTest {
assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
is("speed_enum"));
Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
SortedSet<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
for (YangEnum tmp : enumSet) {
if (tmp.getNamedValue().equals("10m")) {
assertThat(tmp.getValue(), is(0));
......@@ -114,4 +115,34 @@ public class EnumListenerTest {
+ "with the current highest value");
YangNode node = manager.getDataModel("src/test/resources/EnumMaxNextValue.yang");
}
/**
* Checks enum values stored are sorted.
*/
@Test
public void processEnumSorted() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/EnumSorted.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.getName(), is("ifType"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION));
assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
is("ifType_enum"));
SortedSet<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
Iterator<YangEnum> enumIterator = enumSet.iterator();
assertThat(enumIterator.next().getNamedValue(), is("five"));
}
}
......
......@@ -176,4 +176,15 @@ public class MaxElementsListenerTest {
assertThat(leafListInfo.getName(), is("invalid-interval"));
assertThat(leafListInfo.getMaxElelements(), is(2147483647));
}
/**
* Checks whether exception is thrown when invalid min-elements value is
* given as input.
*/
@Test
public void processMaxElementsMaxValue() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : max-elements value 77777777777777777777777 is not valid.");
YangNode node = manager.getDataModel("src/test/resources/MaxElementsMaxValue.yang");
}
}
......
......@@ -116,6 +116,17 @@ public class MinElementsListenerTest {
}
/**
* Checks whether exception is thrown when invalid min-elements value is
* given as input.
*/
@Test
public void processMinElementsMaxValue() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : min-elements value 77777777777777777777777 is not valid.");
YangNode node = manager.getDataModel("src/test/resources/MinElementsMaxValue.yang");
}
/**
* Checks whether exception is thrown when min-elements statement without
* statement end is given as input.
*/
......
/*
* 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.parser.impl.parseutils;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
/**
* Test case for testing listener util.
*/
public class ListenerUtilTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private final YangUtilsParserManager manager = new YangUtilsParserManager();
/**
* Checks whether exception is thrown when identifier starts with xml.
*/
@Test
public void validateIdentifierStartsWithXml() throws IOException {
thrown.expect(ParserException.class);
thrown.expectMessage("YANG file error : module identifier xMlTest must not start" +
" with (('X'|'x') ('M'|'m') ('L'|'l'))");
manager.getDataModel("src/test/resources/InValidIdentifierXML.yang");
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
typedef type15 {
type bits {
bit disable-nagle {
position 0;
}
bit auto-sense-speed {
position 1;
}
bit Mb-only {
position 2;
}
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf type15 {
type union {
type bits {
bit disable-nagle {
position 0;
}
bit auto-sense-speed {
position 1;
}
bit Mb-only {
position 2;
}
}
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf ifType {
type enumeration {
enum four{
value 7;
}
enum seven {
value 2147483647;
}
enum five {
value 5;
}
}
}
}
module xMlTest {
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-list invalid-interval {
type "uint16";
max-elements 77777777777777777777777;
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf-list invalid-interval {
type "uint16";
min-elements 77777777777777777777777;
}
}