BUG-770: NumberFormatException for datapathId 99/7399/2
authorMichal Rehak <mirehak@cisco.com>
Mon, 26 May 2014 12:50:31 +0000 (14:50 +0200)
committerPrasanna Huddar <prasanna.huddar@ericsson.com>
Mon, 26 May 2014 12:59:30 +0000 (12:59 +0000)
- changed long to bigInteger in order to avoid negative Long
- added unit tests for touched util-methods

Change-Id: Ibdebd3965baf552bd92702af0399961b70defe56
Signed-off-by: Michal Rehak <mirehak@cisco.com>
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/lldp/LLDPSpeaker.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/InventoryDataServiceUtil.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/InventoryDataServiceUtilTest.java [new file with mode: 0644]

index b5abbb859e1a7349157e6adde5b7b49f2363aa8f..2799229542189ba640ea06d716710062b01ed0eb 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.lldp;
 
+import java.math.BigInteger;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -14,7 +15,6 @@ import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.ConcurrentHashMap;
 
-import org.apache.commons.lang3.StringUtils;
 import org.opendaylight.controller.sal.packet.Ethernet;
 import org.opendaylight.controller.sal.packet.LLDP;
 import org.opendaylight.controller.sal.packet.LLDPTLV;
@@ -101,7 +101,9 @@ public class LLDPSpeaker {
         ttlTlv.setType(LLDPTLV.TLVType.TTL.getValue()).setLength((short) ttl.length).setValue(ttl);
                
         // Create LLDP ChassisID TLV
-        byte[] cidValue = LLDPTLV.createChassisIDTLVValue(colonize(StringUtils.leftPad(Long.toHexString(InventoryDataServiceUtil.dataPathIdFromNodeId(nodeId)),16,"0")));
+        BigInteger dataPathId = InventoryDataServiceUtil.dataPathIdFromNodeId(nodeId);
+        byte[] cidValue = LLDPTLV.createChassisIDTLVValue(
+                colonize(InventoryDataServiceUtil.bigIntegerToPaddedHex(dataPathId)));
         LLDPTLV chassisIdTlv = new LLDPTLV();
         chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue());
         chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue()).setLength((short) cidValue.length)
@@ -150,8 +152,8 @@ public class LLDPSpeaker {
         }
         return null;
        }
-       
-       private class LLDPSpeakerTask extends TimerTask {
+
+    private class LLDPSpeakerTask extends TimerTask {
 
         @Override
         public void run() {
index 81a00302c0fbca7a4eea5166d4d61cff933203e3..51708714be0edaf8307669e76c8edb25ff14174b 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.util;
 
+import org.apache.commons.lang3.StringUtils;
 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
@@ -130,9 +131,9 @@ public abstract class InventoryDataServiceUtil {
         return new NodeId(OF_URI_PREFIX + current);
     }
     
-    public static Long dataPathIdFromNodeId(NodeId nodeId) {
+    public static BigInteger dataPathIdFromNodeId(NodeId nodeId) {
         String dpids = nodeId.getValue().replace(OF_URI_PREFIX, "");
-        Long dpid = Long.decode(dpids);
+        BigInteger dpid = new BigInteger(dpids);
         return dpid;
     }
 
@@ -200,4 +201,12 @@ public abstract class InventoryDataServiceUtil {
         builder.setId(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathId,portNo));
         return builder;
     }
+
+    /**
+     * @param dataPathId
+     * @return string of size 16, padded with '0'
+     */
+    public static String bigIntegerToPaddedHex(BigInteger dataPathId) {
+        return StringUtils.leftPad(dataPathId.toString(16),16,"0");
+    }
 }
diff --git a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/InventoryDataServiceUtilTest.java b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/InventoryDataServiceUtilTest.java
new file mode 100644 (file)
index 0000000..eadcff9
--- /dev/null
@@ -0,0 +1,74 @@
+/**
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * 
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.openflowplugin.openflow.md.util;
+
+import java.math.BigInteger;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+
+/**
+ * 
+ */
+public class InventoryDataServiceUtilTest {
+
+    /**
+     * Test method for {@link InventoryDataServiceUtil#dataPathIdFromNodeId(NodeId)}.
+     */
+    @Test
+    public void testDataPathIdFromNodeId() {
+        String string = "openflow:";
+        NodeId[] nodeIds = new NodeId[] {
+                // 0x00000000 000004d2
+                new NodeId(string + "1234"),
+                // 0x8db2089e 01391a86
+                new NodeId(string + "10210232779920710278"),
+                // 0xffffffff ffffffff
+                new NodeId(string + "18446744073709551615"),
+        };
+        
+        long[] expectedDPIDs = new long[] {
+                1234L,
+                -8236511293788841338L,
+                -1L
+        };
+        
+        for (int i = 0; i < nodeIds.length; i++) {
+            BigInteger datapathId = InventoryDataServiceUtil.dataPathIdFromNodeId(nodeIds[i] );
+            Assert.assertEquals(expectedDPIDs[i], datapathId.longValue());
+        }
+    }
+    
+    /**
+     * Test method for {@link InventoryDataServiceUtil#bigIntegerToPaddedHex(BigInteger)}.
+     */
+    @Test
+    public void testLongToPaddedHex() {
+        BigInteger[] dpids = new BigInteger[] {
+                // 0x00000000 000004d2
+                new BigInteger("1234"),
+                // 0x8db2089e 01391a86
+                new BigInteger("10210232779920710278"),
+                // 0xffffffff ffffffff
+                new BigInteger("18446744073709551615"),
+        };
+        
+        String[] expectedPaddedHexes = new String[] {
+                "00000000000004d2",
+                "8db2089e01391a86",
+                "ffffffffffffffff"
+        };
+        
+        for (int i = 0; i < dpids.length; i++) {
+            String datapathIdHex = InventoryDataServiceUtil.bigIntegerToPaddedHex(dpids[i]);
+            Assert.assertEquals(expectedPaddedHexes[i], datapathIdHex);
+        }
+    }
+
+}