[ONOS-4348] Yang Bits, Binary and Decimal64

Change-Id: I8e4e54a19a8f9634cbc56a07579a1730174f53f6
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BitListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BitListenerTest.java
index 4b2b185..ef25c97 100644
--- a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BitListenerTest.java
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BitListenerTest.java
@@ -35,7 +35,7 @@
 import java.io.IOException;
 import java.util.List;
 import java.util.ListIterator;
-import java.util.Set;
+import java.util.Map;
 
 /**
  * Test cases for bit listener.
@@ -71,14 +71,38 @@
         assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
                 is("mybits"));
 
-        Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().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));
-            } else if (tmp.getBitName().equals("Ten-Mb-only")) {
-                assertThat(tmp.getPosition(), is(2));
+        // Check bit name map
+        Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
+        assertThat(bitNameMap.size(), is(3));
+        for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
+            String bitName = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (bitName.equals("disable-nagle")) {
+                assertThat(yangBit.getPosition(), is(0));
+            } else if (bitName.equals("auto-sense-speed")) {
+                assertThat(yangBit.getPosition(), is(1));
+            } else if (bitName.equals("Ten-Mb-only")) {
+                assertThat(yangBit.getPosition(), is(2));
+            } else {
+                throw new IOException("Invalid bit name: " + bitName);
+            }
+        }
+
+        // Check bit position map
+        Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
+                                                                                 .getBitPositionMap();
+        assertThat(bitPositionMap.size(), is(3));
+        for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
+            int position = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (position == 0) {
+                assertThat(yangBit.getBitName(), is("disable-nagle"));
+            } else if (position == 1) {
+                assertThat(yangBit.getBitName(), is("auto-sense-speed"));
+            } else if (position == 2) {
+                assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
+            } else {
+                throw new IOException("Invalid bit position: " + position);
             }
         }
     }
@@ -107,12 +131,38 @@
         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));
+
+        // Check bit name map
+        Map<String, YangBit> bitNameMap = ((YangBits) type.getDataTypeExtendedInfo()).getBitNameMap();
+        assertThat(bitNameMap.size(), is(3));
+        for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
+            String bitName = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (bitName.equals("disable-nagle")) {
+                assertThat(yangBit.getPosition(), is(0));
+            } else if (bitName.equals("auto-sense-speed")) {
+                assertThat(yangBit.getPosition(), is(1));
+            } else if (bitName.equals("Mb-only")) {
+                assertThat(yangBit.getPosition(), is(2));
+            } else {
+                throw new IOException("Invalid bit name: " + bitName);
+            }
+        }
+
+        // Check bit position map
+        Map<Integer, YangBit> bitPositionMap = ((YangBits) type.getDataTypeExtendedInfo()).getBitPositionMap();
+        assertThat(bitPositionMap.size(), is(3));
+        for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
+            int position = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (position == 0) {
+                assertThat(yangBit.getBitName(), is("disable-nagle"));
+            } else if (position == 1) {
+                assertThat(yangBit.getBitName(), is("auto-sense-speed"));
+            } else if (position == 2) {
+                assertThat(yangBit.getBitName(), is("Mb-only"));
+            } else {
+                throw new IOException("Invalid bit position: " + position);
             }
         }
     }
@@ -151,12 +201,38 @@
 
         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));
+
+        // Check bit name map
+        Map<String, YangBit> bitNameMap = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitNameMap();
+        assertThat(bitNameMap.size(), is(3));
+        for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
+            String bitName = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (bitName.equals("disable-nagle")) {
+                assertThat(yangBit.getPosition(), is(0));
+            } else if (bitName.equals("auto-sense-speed")) {
+                assertThat(yangBit.getPosition(), is(1));
+            } else if (bitName.equals("Mb-only")) {
+                assertThat(yangBit.getPosition(), is(2));
+            } else {
+                throw new IOException("Invalid bit name: " + bitName);
+            }
+        }
+
+        // Check bit position map
+        Map<Integer, YangBit> bitPositionMap = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitPositionMap();
+        assertThat(bitPositionMap.size(), is(3));
+        for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
+            int position = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (position == 0) {
+                assertThat(yangBit.getBitName(), is("disable-nagle"));
+            } else if (position == 1) {
+                assertThat(yangBit.getBitName(), is("auto-sense-speed"));
+            } else if (position == 2) {
+                assertThat(yangBit.getBitName(), is("Mb-only"));
+            } else {
+                throw new IOException("Invalid bit position: " + position);
             }
         }
     }
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/Decimal64ListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/Decimal64ListenerTest.java
new file mode 100644
index 0000000..ec5b3fc
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/Decimal64ListenerTest.java
@@ -0,0 +1,371 @@
+/*
+ * 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.listeners;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangDecimal64;
+import org.onosproject.yangutils.datamodel.YangDerivedInfo;
+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.YangRangeRestriction;
+import org.onosproject.yangutils.datamodel.YangRangeInterval;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.ListIterator;
+
+/**
+ * Test cases for decimal64 listener.
+ */
+public class Decimal64ListenerTest {
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+
+    private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+    /**
+     * Checks decimal64 statement with fraction-digits.
+     */
+    @Test
+    public void processDecimal64TypeStatement() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeStatement.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("validDecimal"));
+        assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
+        assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
+        assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(),
+                is(2));
+    }
+
+    /**
+     * Checks decimal64 statement with range statement.
+     */
+    @Test
+    public void processDecimal64TypeWithRangeStatement() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeWithRangeStatement.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("validDecimal"));
+        assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
+        assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
+        assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(),
+                   is(8));
+
+        YangRangeRestriction rangeRestriction = ((YangDecimal64<YangRangeRestriction>) leafInfo.getDataType()
+                .getDataTypeExtendedInfo())
+                .getRangeRestrictedExtendedInfo();
+
+        ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+                .listIterator();
+        YangRangeInterval rangeInterval = rangeListIterator.next();
+        assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(-92233720368.54775808));
+        assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(92233720368.54775807));
+    }
+
+    /**
+     * Successful validation of decimal64 statement.
+     */
+    @Test
+    public void processDecimal64ValueSuccessfulValidation() throws IOException, ParserException, DataModelException {
+
+        YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeValidation.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();
+        YangDecimal64 decimal64 = (YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+        assertThat(leafInfo.getName(), is("validDecimal"));
+        assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
+        assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
+        assertThat(decimal64.getFractionDigit(), is(18));
+
+        decimal64.setValue(new BigDecimal(-9.223372036854775808));
+        decimal64.validateValue();
+        decimal64.setValue(new BigDecimal(9.223372036854775807));
+        decimal64.validateValue();
+    }
+
+    /**
+     * Failure validation of decimal64 statement.
+     */
+    @Test
+    public void processDecimal64ValueFailureValidation() throws IOException, ParserException, DataModelException {
+        thrown.expect(DataModelException.class);
+        thrown.expectMessage("YANG file error : decimal64 validation failed.");
+
+        YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeValidation.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();
+        YangDecimal64 decimal64 = (YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+        assertThat(leafInfo.getName(), is("validDecimal"));
+        assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
+        assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
+        assertThat(decimal64.getFractionDigit(), is(18));
+
+        decimal64.setValue(new BigDecimal(-92233720368547758.08));
+        // validation should fail
+        decimal64.validateValue();
+    }
+
+    /**
+     * Validation of invalid maximum value limit of fraction-digits.
+     */
+    @Test
+    public void processDecimal64InvalidMaxFraction() throws IOException, ParserException, DataModelException {
+        thrown.expect(ParserException.class);
+        thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
+
+        manager.getDataModel("src/test/resources/Decimal64TypeInvalidMaxValueFraction.yang");
+    }
+
+    /**
+     * Validation of invalid (0) minimum value limit of fraction-digits.
+     */
+    @Test
+    public void processDecimal64InvalidMinFraction1() throws IOException, ParserException, DataModelException {
+        thrown.expect(ParserException.class);
+        thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
+
+        manager.getDataModel("src/test/resources/Decimal64TypeInvalidMinValueFraction1.yang");
+    }
+
+    /**
+     * Validation of invalid (-1) minimum value limit of fraction-digits.
+     */
+    @Test
+    public void processDecimal64InvalidMinFraction2() throws IOException, ParserException, DataModelException {
+        thrown.expect(ParserException.class);
+        thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
+
+        manager.getDataModel("src/test/resources/Decimal64TypeInvalidMinValueFraction2.yang");
+    }
+
+    /**
+     * Validation of decimal64 range statement.
+     */
+    @Test
+    public void processDecimal64TypeWithMultiValueRangeStatement() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeWithMultiValueRangeStmnt.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("validDecimal"));
+        assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
+        assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
+        assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(),
+                   is(18));
+
+        YangRangeRestriction rangeRestriction = ((YangDecimal64<YangRangeRestriction>) leafInfo.getDataType()
+                .getDataTypeExtendedInfo())
+                .getRangeRestrictedExtendedInfo();
+
+        ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+                .listIterator();
+        // range "1 .. 3.14 | 10 | 20..max";
+        // check first range 1 .. 3.14
+        YangRangeInterval rangeInterval = rangeListIterator.next();
+        assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(-9.22));
+        assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(7.22));
+        // check second range 10
+        rangeInterval = rangeListIterator.next();
+        assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(8.0));
+        assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(8.0));
+        // check third range 20..max
+        rangeInterval = rangeListIterator.next();
+        assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(9.0));
+        assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(9.223372036854776));
+    }
+
+    /**
+     * Validation of decimal64 with invalid range.
+     */
+    @Test
+    public void processDecimal64InvalidRange() throws IOException, ParserException, DataModelException {
+        thrown.expect(ParserException.class);
+        thrown.expectMessage("YANG file error : decimal64 validation failed.");
+
+        manager.getDataModel("src/test/resources/Decimal64TypeInvalidRangeStmnt.yang");
+    }
+
+    /**
+     * Validation of decimal64 without fraction-digits. Fraction-digits must be present for decimal64.
+     */
+    @Test
+    public void processDecimal64WithoutFraction() throws IOException, ParserException, DataModelException {
+        thrown.expect(ParserException.class);
+        thrown.expectMessage("YANG file error : a type decimal64 must have fraction-digits statement.");
+
+        manager.getDataModel("src/test/resources/Decimal64TypeWithoutFraction.yang");
+    }
+
+    /**
+     * Checks decimal64 with typedef statement.
+     */
+    @Test
+    public void processDecimal64TypedefStatement() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/Decimal64TypedefStatement.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("setFourDecimal"));
+        assertThat(leafInfo.getDataType().getDataTypeName(), is("validDecimal"));
+        YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
+        YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
+        YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
+        assertThat(typedef.getName(), is("validDecimal"));
+
+        assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
+        YangType type = typedef.getTypeList().iterator().next();
+        assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
+        assertThat(type.getDataTypeName(), is("decimal64"));
+        YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) typedef.getTypeList().iterator().next();
+        YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
+        assertThat(decimal64.getFractionDigit(), is(4));
+    }
+
+    /**
+     * Checks decimal64 with multiple typedef statement.
+     */
+    @Test
+    public void processDecimal64MultiTypedefStatement() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/Decimal64MultiTypedefStatement.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();
+
+        // check leaf type
+        assertThat(leafInfo.getName(), is("setFourDecimal"));
+        assertThat(leafInfo.getDataType().getDataTypeName(), is("validDecimal"));
+        YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
+        assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
+        YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
+
+        // check previous typedef
+        YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
+        assertThat(prevTypedef.getName(), is("validDecimal"));
+        YangType type = prevTypedef.getTypeList().iterator().next();
+        assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
+        derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
+
+        // check top typedef
+        YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
+        assertThat(topTypedef.getName(), is("topDecimal"));
+        type = topTypedef.getTypeList().iterator().next();
+        assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
+        assertThat(type.getDataTypeName(), is("decimal64"));
+        YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
+        YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
+        assertThat(decimal64.getFractionDigit(), is(4));
+    }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LengthRestrictionListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LengthRestrictionListenerTest.java
index 1f65023..a67aa53 100644
--- a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LengthRestrictionListenerTest.java
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LengthRestrictionListenerTest.java
@@ -138,6 +138,37 @@
     }
 
     /**
+     * Checks valid length statement as sub-statement of binary statement.
+     */
+    @Test
+    public void processValidBinaryLengthStatement() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/ValidBinaryLengthStatement.yang");
+
+        assertThat((node instanceof YangModule), is(true));
+        assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+        YangModule yangNode = (YangModule) node;
+        assertThat(yangNode.getName(), is("Test"));
+
+        ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+        YangLeaf leafInfo = leafIterator.next();
+
+        assertThat(leafInfo.getName(), is("message"));
+        assertThat(leafInfo.getDataType().getDataTypeName(), is("binary"));
+        assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BINARY));
+        YangRangeRestriction lengthRestriction = (YangRangeRestriction) leafInfo
+                                                  .getDataType().getDataTypeExtendedInfo();
+
+        ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+                .listIterator();
+
+        YangRangeInterval rangeInterval = lengthListIterator.next();
+
+        assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(4)));
+        assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(4)));
+    }
+
+    /**
      * Checks length statement with invalid type.
      */
     @Test
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PositionListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PositionListenerTest.java
index 699f530..7b59894 100644
--- a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PositionListenerTest.java
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PositionListenerTest.java
@@ -31,7 +31,7 @@
 
 import java.io.IOException;
 import java.util.ListIterator;
-import java.util.Set;
+import java.util.Map;
 
 /**
  * Test cases for position listener.
@@ -67,14 +67,38 @@
         assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
                 is("mybits"));
 
-        Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().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));
-            } else if (tmp.getBitName().equals("Ten-Mb-only")) {
-                assertThat(tmp.getPosition(), is(2));
+        // Check bit name map
+        Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
+        assertThat(bitNameMap.size(), is(3));
+        for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
+            String bitName = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (bitName.equals("disable-nagle")) {
+                assertThat(yangBit.getPosition(), is(0));
+            } else if (bitName.equals("auto-sense-speed")) {
+                assertThat(yangBit.getPosition(), is(1));
+            } else if (bitName.equals("Ten-Mb-only")) {
+                assertThat(yangBit.getPosition(), is(2));
+            } else {
+                throw new IOException("Invalid bit name: " + bitName);
+            }
+        }
+
+        // Check bit position map
+        Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
+                                                                                 .getBitPositionMap();
+        assertThat(bitPositionMap.size(), is(3));
+        for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
+            int position = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (position == 0) {
+                assertThat(yangBit.getBitName(), is("disable-nagle"));
+            } else if (position == 1) {
+                assertThat(yangBit.getBitName(), is("auto-sense-speed"));
+            } else if (position == 2) {
+                assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
+            } else {
+                throw new IOException("Invalid bit position: " + position);
             }
         }
     }
@@ -106,14 +130,38 @@
         assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
                 is("mybits"));
 
-        Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().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));
-            } else if (tmp.getBitName().equals("Ten-Mb-only")) {
-                assertThat(tmp.getPosition(), is(2));
+        // Check bit name map
+        Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
+        assertThat(bitNameMap.size(), is(3));
+        for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
+            String bitName = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (bitName.equals("disable-nagle")) {
+                assertThat(yangBit.getPosition(), is(0));
+            } else if (bitName.equals("auto-sense-speed")) {
+                assertThat(yangBit.getPosition(), is(1));
+            } else if (bitName.equals("Ten-Mb-only")) {
+                assertThat(yangBit.getPosition(), is(2));
+            } else {
+                throw new IOException("Invalid bit name: " + bitName);
+            }
+        }
+
+        // Check bit position map
+        Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
+                                                                                 .getBitPositionMap();
+        assertThat(bitPositionMap.size(), is(3));
+        for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
+            int position = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (position == 0) {
+                assertThat(yangBit.getBitName(), is("disable-nagle"));
+            } else if (position == 1) {
+                assertThat(yangBit.getBitName(), is("auto-sense-speed"));
+            } else if (position == 2) {
+                assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
+            } else {
+                throw new IOException("Invalid bit position: " + position);
             }
         }
     }
@@ -145,14 +193,38 @@
         assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
                 is("mybits"));
 
-        Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().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));
-            } else if (tmp.getBitName().equals("Ten-Mb-only")) {
-                assertThat(tmp.getPosition(), is(2));
+        // Check bit name map
+        Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap();
+        assertThat(bitNameMap.size(), is(3));
+        for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) {
+            String bitName = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (bitName.equals("disable-nagle")) {
+                assertThat(yangBit.getPosition(), is(0));
+            } else if (bitName.equals("auto-sense-speed")) {
+                assertThat(yangBit.getPosition(), is(1));
+            } else if (bitName.equals("Ten-Mb-only")) {
+                assertThat(yangBit.getPosition(), is(2));
+            } else {
+                throw new IOException("Invalid bit name: " + bitName);
+            }
+        }
+
+        // Check bit position map
+        Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo())
+                                                                                 .getBitPositionMap();
+        assertThat(bitPositionMap.size(), is(3));
+        for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) {
+            int position = element.getKey();
+            YangBit yangBit = element.getValue();
+            if (position == 0) {
+                assertThat(yangBit.getBitName(), is("disable-nagle"));
+            } else if (position == 1) {
+                assertThat(yangBit.getBitName(), is("auto-sense-speed"));
+            } else if (position == 2) {
+                assertThat(yangBit.getBitName(), is("Ten-Mb-only"));
+            } else {
+                throw new IOException("Invalid bit position: " + position);
             }
         }
     }