Mahesh Poojary S
Committed by Patrick Liu

[ONOS-4938] Defect fix: range interval

Change-Id: I3229d35fb7c80da3bcf150d52ed7e7eba72bb4c8
......@@ -165,7 +165,7 @@ public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
T curMaxvalue = getMaxRestrictedvalue();
if (newInterval.getStartValue().compareTo(curMaxvalue) != 1) {
if (newInterval.getStartValue().compareTo(curMaxvalue) < 1) {
throw new DataModelException(
"New added range interval is lesser than the old interval(s)");
}
......
......@@ -289,7 +289,10 @@ public class YangType<T>
if (getDataTypeExtendedInfo() == null) {
getDataObjectFromString(value, getDataType());
} else {
((YangRangeRestriction) getDataTypeExtendedInfo()).isValidValueString(value);
if (!((YangRangeRestriction) getDataTypeExtendedInfo()).isValidValueString(value)) {
throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
getDataType());
}
}
break;
}
......@@ -308,14 +311,14 @@ public class YangType<T>
&& ((YangStringRestriction) getDataTypeExtendedInfo())
.isValidStringOnPatternRestriction(value))) {
throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
"string");
getDataType());
}
break;
}
case BOOLEAN:
if (!(value.equals(DataModelUtils.TRUE) || value.equals(DataModelUtils.FALSE))) {
throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
"boolean");
getDataType());
}
break;
case ENUMERATION: {
......@@ -331,7 +334,7 @@ public class YangType<T>
if (!isValidated) {
throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
"union");
getDataType());
}
break;
}
......@@ -339,14 +342,14 @@ public class YangType<T>
YangBits bits = (YangBits) getDataTypeExtendedInfo();
if (bits.fromString(value) == null) {
throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
"bits");
getDataType());
}
break;
}
case BINARY: {
if (!isValidBinary(value, (YangRangeRestriction) getDataTypeExtendedInfo())) {
throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
"binary");
getDataType());
}
break;
}
......@@ -360,8 +363,11 @@ public class YangType<T>
break;
}
case EMPTY: {
if (value.length() > 0) {
throw new DataTypeException("YANG file error : Input value \"" + value
+ "\" is not a allowed for a data type " + "empty");
+ "\" is not allowed for a data type " + getDataType());
}
break;
}
case UNION: {
ListIterator<YangType<?>> listIterator = ((YangUnion) getDataTypeExtendedInfo()).getTypeList()
......@@ -380,7 +386,7 @@ public class YangType<T>
if (!isValidated) {
throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
"union");
getDataType());
}
break;
}
......@@ -399,7 +405,7 @@ public class YangType<T>
if (!((YangRangeRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo())
.getResolvedExtendedInfo()).isValidValueString(value)) {
throw new DataTypeException("YANG file error : Input value \"" + value
+ "\" is not a valid " + dataType.toString());
+ "\" is not a valid " + dataType);
}
}
} else if (dataType == YangDataTypes.STRING) {
......@@ -410,20 +416,20 @@ public class YangType<T>
if (!(stringRestriction.isValidStringOnLengthRestriction(value) &&
stringRestriction.isValidStringOnPatternRestriction(value))) {
throw new DataTypeException("YANG file error : Input value \"" + value
+ "\" is not a valid " + dataType.toString());
+ "\" is not a valid " + dataType);
}
}
} else if (dataType == YangDataTypes.BITS) {
YangBits bits = (YangBits) getDataTypeExtendedInfo();
if (bits.fromString(value) == null) {
throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
"bits");
dataType);
}
} else if (dataType == YangDataTypes.BINARY) {
if (!isValidBinary(value, (YangRangeRestriction) ((YangDerivedInfo)
getDataTypeExtendedInfo()).getResolvedExtendedInfo())) {
throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
dataType.toString());
dataType);
}
} else if (dataType == YangDataTypes.DECIMAL64) {
YangDerivedInfo derivedInfo = (YangDerivedInfo) getDataTypeExtendedInfo();
......@@ -438,8 +444,8 @@ public class YangType<T>
break;
}
default: {
throw new DataTypeException("YANG file error : Input value \"" + value + "\" is for unsupported " +
"data type.");
throw new DataTypeException("YANG file error : Input value \"" + value + "\" received for " +
"unsupported data type " + getDataType());
}
}
}
......
......@@ -18,7 +18,6 @@ package org.onosproject.yangutils.datamodel.utils.builtindatatype;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.regex.Pattern;
/**
* Handles the YANG's Uint16 data type processing.
......@@ -40,11 +39,6 @@ public class YangUint64 implements YangBuiltInDataTypeInfo<YangUint64>, Serializ
private static final String MAX_KEYWORD = "max";
/**
* YANG's Integer value pattern.
*/
private static final Pattern NON_NEGATIVE_INTEGER_PATTERN = Pattern.compile("[0-9]+");
/**
* Valid minimum value of YANG's Uint64.
*/
public static final BigInteger MIN_VALUE = BigInteger.valueOf(0);
......@@ -71,12 +65,14 @@ public class YangUint64 implements YangBuiltInDataTypeInfo<YangUint64>, Serializ
value = MIN_VALUE;
} else if (valueInString.matches(MAX_KEYWORD)) {
value = MAX_VALUE;
} else if (NON_NEGATIVE_INTEGER_PATTERN.matcher(valueInString).matches()) {
value = new BigInteger(valueInString);
} else {
try {
value = new BigInteger(valueInString);
} catch (Exception e) {
throw new DataTypeException("YANG file error : Input value \"" + valueInString + "\" is not a valid " +
"uint64.");
}
}
if (value.compareTo(MIN_VALUE) < 0) {
throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
......
......@@ -180,6 +180,17 @@ public class DefaultListenerTest {
}
/**
* Validates default invalid value in typedef.
*/
@Test
public void processDefaultInvalidValueInTypedef() throws IOException, ParserException {
thrown.expect(DataTypeException.class);
thrown.expectMessage("YANG file error : Input value \"0\" is not a valid INT32");
manager.getDataModel("src/test/resources/default/DefaultInvalidValueWithRangeInTypedef.yang");
}
/**
* Validates default value decimal64 in leaf.
*/
@Test
......@@ -250,7 +261,7 @@ public class DefaultListenerTest {
@Test
public void processDefaultInvalidValueStringInLeaf() throws IOException, ParserException {
thrown.expect(DataTypeException.class);
thrown.expectMessage("YANG file error : Input value \"2bB2bB\" is not a valid string");
thrown.expectMessage("YANG file error : Input value \"2bB2bB\" is not a valid STRING");
manager.getDataModel("src/test/resources/default/DefaultInvalidValueStringInLeaf.yang");
}
......@@ -288,7 +299,7 @@ public class DefaultListenerTest {
@Test
public void processDefaultInvalidValueBooleanInLeaf() throws IOException, ParserException {
thrown.expect(DataTypeException.class);
thrown.expectMessage("YANG file error : Input value \"yes\" is not a valid boolean");
thrown.expectMessage("YANG file error : Input value \"yes\" is not a valid BOOLEAN");
manager.getDataModel("src/test/resources/default/DefaultInvalidValueBooleanInLeaf.yang");
}
......@@ -326,7 +337,7 @@ public class DefaultListenerTest {
@Test
public void processDefaultInvalidValueEnumberationInLeaf() throws IOException, ParserException {
thrown.expect(DataTypeException.class);
thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid union");
thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid ENUMERATION");
manager.getDataModel("src/test/resources/default/DefaultInvalidValueEnumerationInLeaf.yang");
}
......@@ -364,7 +375,7 @@ public class DefaultListenerTest {
@Test
public void processDefaultInvalidValueBitsInLeaf() throws IOException, ParserException {
thrown.expect(DataTypeException.class);
thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid bits");
thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid BITS");
manager.getDataModel("src/test/resources/default/DefaultInvalidValueBitsInLeaf.yang");
}
......@@ -402,7 +413,7 @@ public class DefaultListenerTest {
@Test
public void processDefaultInvlaidValueBinaryInLeaf() throws IOException, ParserException {
thrown.expect(DataTypeException.class);
thrown.expectMessage("YANG file error : Input value \"000\" is not a valid binary");
thrown.expectMessage("YANG file error : Input value \"000\" is not a valid BINARY");
manager.getDataModel("src/test/resources/default/DefaultInvalidValueBinaryInLeaf.yang");
}
......@@ -413,7 +424,7 @@ public class DefaultListenerTest {
@Test
public void processDefaultValueEmptyInLeaf() throws IOException, ParserException {
thrown.expect(DataTypeException.class);
thrown.expectMessage("YANG file error : Input value \"something\" is not a allowed for a data type empty");
thrown.expectMessage("YANG file error : Input value \"something\" is not allowed for a data type EMPTY");
manager.getDataModel("src/test/resources/default/DefaultValueEmptyInLeaf.yang");
}
......@@ -451,7 +462,7 @@ public class DefaultListenerTest {
@Test
public void processDefaultInvalidValueUnionInLeaf() throws IOException, ParserException {
thrown.expect(DataTypeException.class);
thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid union");
thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid UNION");
manager.getDataModel("src/test/resources/default/DefaultInvalidValueUnionInLeaf.yang");
}
......
......@@ -19,6 +19,7 @@ package org.onosproject.yangutils.plugin.manager;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ListIterator;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangLeaf;
......@@ -28,8 +29,11 @@ import org.onosproject.yangutils.datamodel.YangPatternRestriction;
import org.onosproject.yangutils.datamodel.YangRangeInterval;
import org.onosproject.yangutils.datamodel.YangRangeRestriction;
import org.onosproject.yangutils.datamodel.YangStringRestriction;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt16;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt32;
import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint64;
import org.onosproject.yangutils.linker.exceptions.LinkerException;
......@@ -292,6 +296,79 @@ public final class RestrictionResolutionTest {
}
/**
* Checks range restriction in referred typedef.
*/
@Test
public void processRangeRestrictionInRefTypedef()
throws IOException, ParserException, DataModelException {
YangNode node = manager.getDataModel("src/test/resources/RangeRestrictionInRefTypedef.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(MODULE_NODE));
// Check whether the module name is set correctly.
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
// check top typedef
YangTypeDef topTypedef = (YangTypeDef) yangNode.getChild();
assertThat(topTypedef.getName(), is("Num3"));
YangType type = topTypedef.getTypeList().iterator().next();
assertThat(type.getDataType(), is(YangDataTypes.INT16));
assertThat(type.getDataTypeName(), is("int16"));
// Check for the restriction value.
YangRangeRestriction rangeRestriction = (YangRangeRestriction) type.getDataTypeExtendedInfo();
ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
.listIterator();
YangRangeInterval rangeInterval1 = rangeListIterator.next();
assertThat((int) ((YangInt16) rangeInterval1.getStartValue()).getValue(), is(-32000));
assertThat((int) ((YangInt16) rangeInterval1.getEndValue()).getValue(), is(4));
YangRangeInterval rangeInterval2 = rangeListIterator.next();
assertThat((int) ((YangInt16) rangeInterval2.getStartValue()).getValue(), is(32767));
assertThat((int) ((YangInt16) rangeInterval2.getEndValue()).getValue(), is(32767));
// check referred typedef
YangTypeDef refTypedef = (YangTypeDef) topTypedef.getNextSibling();
assertThat(refTypedef.getName(), is("Num6"));
YangType refType = refTypedef.getTypeList().iterator().next();
assertThat(refType.getDataType(), is(YangDataTypes.DERIVED));
assertThat(refType.getDataTypeName(), is("Num3"));
YangDerivedInfo<YangRangeRestriction> derivedInfo =
(YangDerivedInfo<YangRangeRestriction>) refType.getDataTypeExtendedInfo();
// Check for the restriction value.
rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
rangeListIterator = rangeRestriction.getAscendingRangeIntervals().listIterator();
rangeInterval1 = rangeListIterator.next();
assertThat((int) ((YangInt16) rangeInterval1.getStartValue()).getValue(), is(-3));
assertThat((int) ((YangInt16) rangeInterval1.getEndValue()).getValue(), is(-3));
rangeInterval2 = rangeListIterator.next();
assertThat((int) ((YangInt16) rangeInterval2.getStartValue()).getValue(), is(-2));
assertThat((int) ((YangInt16) rangeInterval2.getEndValue()).getValue(), is(2));
YangRangeInterval rangeInterval3 = rangeListIterator.next();
assertThat((int) ((YangInt16) rangeInterval3.getStartValue()).getValue(), is(3));
assertThat((int) ((YangInt16) rangeInterval3.getEndValue()).getValue(), is(3));
}
/**
* Checks invalid range restriction in referred typedef.
*/
@Test(expected = LinkerException.class)
public void processInvalidRangeRestrictionInRefTypedef()
throws IOException, ParserException, DataModelException {
manager.getDataModel("src/test/resources/RangeRestrictionInvalidInRefTypedef.yang");
}
/**
* Checks range restriction in referred type.
*/
@Test
......
module Test {
namespace "urn:ietf:params:xml:ns:yang:yt3";
prefix "yt3";
organization
"YANG Language Design Team";
contact
"Andy Bierman";
description
"YANG test module 3.";
revision 2007-12-04 {
description "Initial revision.";
}
typedef Num3 {
units seconds;
type int16 {
range "-32000 .. 4 | max";
}
description "test 3";
}
typedef Num6 {
description "test 6";
type Num3 {
range "-3 | -2 .. +2 | 3";
}
default 0;
}
}
\ No newline at end of file
module Test {
namespace "urn:ietf:params:xml:ns:yang:yt3";
prefix "yt3";
organization
"YANG Language Design Team";
contact
"Andy Bierman";
description
"YANG test module 3.";
revision 2007-12-04 {
description "Initial revision.";
}
typedef Num3 {
units seconds;
type int16 {
range "-3 | -2 .. +2 | 3";
}
description "test 3";
}
typedef Num6 {
description "test 6";
type Num3 {
range "-32000 .. 4 | max" ;
}
default 0;
}
}
\ No newline at end of file
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
typedef hello {
type int32 {
range "1..4 | 10..20";
}
default "0";
}
}