From e7971f6ae3cfc260e14cd26df98d6eeed30665f8 Mon Sep 17 00:00:00 2001 From: Michal Rehak Date: Mon, 26 May 2014 14:50:31 +0200 Subject: [PATCH] BUG-770: NumberFormatException for datapathId - 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 --- .../openflow/md/lldp/LLDPSpeaker.java | 10 ++- .../md/util/InventoryDataServiceUtil.java | 13 +++- .../md/util/InventoryDataServiceUtilTest.java | 74 +++++++++++++++++++ 3 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/InventoryDataServiceUtilTest.java diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/lldp/LLDPSpeaker.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/lldp/LLDPSpeaker.java index b5abbb859e..2799229542 100644 --- a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/lldp/LLDPSpeaker.java +++ b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/lldp/LLDPSpeaker.java @@ -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() { diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/InventoryDataServiceUtil.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/InventoryDataServiceUtil.java index 81a00302c0..51708714be 100644 --- a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/InventoryDataServiceUtil.java +++ b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/InventoryDataServiceUtil.java @@ -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 index 0000000000..eadcff9181 --- /dev/null +++ b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/InventoryDataServiceUtilTest.java @@ -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); + } + } + +} -- 2.36.6