[ONOS-4938] Defect fix: range interval

Change-Id: I3229d35fb7c80da3bcf150d52ed7e7eba72bb4c8
diff --git a/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java b/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java
index ae9b68e..29322e6 100644
--- a/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java
+++ b/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java
@@ -165,7 +165,7 @@
 
         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)");
         }
diff --git a/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangType.java b/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangType.java
index 6815a3a..45f0df6 100644
--- a/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangType.java
+++ b/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangType.java
@@ -289,7 +289,10 @@
                 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 @@
                         && ((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 @@
 
                 if (!isValidated) {
                     throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
-                                                        "union");
+                                                        getDataType());
                 }
                 break;
             }
@@ -339,14 +342,14 @@
                 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 @@
                 break;
             }
             case EMPTY: {
-                throw new DataTypeException("YANG file error : Input value \"" + value
-                                                    + "\" is not a allowed for a data type " + "empty");
+                if (value.length() > 0) {
+                    throw new DataTypeException("YANG file error : Input value \"" + value
+                                                        + "\" is not allowed for a data type " + getDataType());
+                }
+                break;
             }
             case UNION: {
                 ListIterator<YangType<?>> listIterator = ((YangUnion) getDataTypeExtendedInfo()).getTypeList()
@@ -380,7 +386,7 @@
 
                 if (!isValidated) {
                     throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
-                                                        "union");
+                                                        getDataType());
                 }
                 break;
             }
@@ -399,7 +405,7 @@
                         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 @@
                         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 @@
                 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());
             }
         }
     }
diff --git a/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/utils/builtindatatype/YangUint64.java b/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/utils/builtindatatype/YangUint64.java
index 49a5153..16576c9 100644
--- a/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/utils/builtindatatype/YangUint64.java
+++ b/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/utils/builtindatatype/YangUint64.java
@@ -18,7 +18,6 @@
 
 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 @@
     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,11 +65,13 @@
             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 {
-            throw new DataTypeException("YANG file error : Input value \"" + valueInString + "\" is not a valid " +
-                    "uint64.");
+            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) {
diff --git a/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java b/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java
index 81e973f..b65b948 100644
--- a/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java
+++ b/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java
@@ -180,6 +180,17 @@
     }
 
     /**
+     * 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 @@
     @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 @@
     @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 @@
     @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 @@
     @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 @@
     @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 @@
     @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 @@
     @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");
     }
diff --git a/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/RestrictionResolutionTest.java b/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/RestrictionResolutionTest.java
index c0f67b7..6b64e5b 100644
--- a/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/RestrictionResolutionTest.java
+++ b/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/RestrictionResolutionTest.java
@@ -19,6 +19,7 @@
 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.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 @@
     }
 
     /**
+     * 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
diff --git a/utils/yangutils/plugin/src/test/resources/RangeRestrictionInRefTypedef.yang b/utils/yangutils/plugin/src/test/resources/RangeRestrictionInRefTypedef.yang
new file mode 100644
index 0000000..14382b5
--- /dev/null
+++ b/utils/yangutils/plugin/src/test/resources/RangeRestrictionInRefTypedef.yang
@@ -0,0 +1,33 @@
+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
diff --git a/utils/yangutils/plugin/src/test/resources/RangeRestrictionInvalidInRefTypedef.yang b/utils/yangutils/plugin/src/test/resources/RangeRestrictionInvalidInRefTypedef.yang
new file mode 100644
index 0000000..9e93f1e
--- /dev/null
+++ b/utils/yangutils/plugin/src/test/resources/RangeRestrictionInvalidInRefTypedef.yang
@@ -0,0 +1,33 @@
+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
diff --git a/utils/yangutils/plugin/src/test/resources/default/DefaultInvalidValueWithRangeInTypedef.yang b/utils/yangutils/plugin/src/test/resources/default/DefaultInvalidValueWithRangeInTypedef.yang
new file mode 100644
index 0000000..c4ca002
--- /dev/null
+++ b/utils/yangutils/plugin/src/test/resources/default/DefaultInvalidValueWithRangeInTypedef.yang
@@ -0,0 +1,11 @@
+module Test {
+    yang-version 1;
+    namespace http://huawei.com;
+    prefix Ant;
+    typedef hello {
+        type int32 {
+            range "1..4 | 10..20";
+        }
+        default "0";
+    }
+}