From d3e6c92c1f8bf92d60c4a0ec2ee8fc734806ff5d Mon Sep 17 00:00:00 2001 From: Michal Rehak Date: Wed, 21 May 2014 10:16:43 +0200 Subject: [PATCH] BUG-770: NumberFormatException for input string on switch OFPT_HELLO DatapathId is of type uint64 and only lower 6 bytes are containign MAC address. If the input exceeds highest positive integer (2**63-1), then parsing to long failed. Now it is parsed into BigInteger and only lower 8 bytes are later on used as long. - moved unit test to test folder Change-Id: Ie9fe78cde9fb4232209382396c2278dc029d8dca Signed-off-by: Michal Rehak --- .../sal/compatibility/NodeMapping.java | 5 +- .../compatibility/test/NodeMappingTest.java | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 opendaylight/md-sal/compatibility/sal-compatibility/src/test/java/org/opendaylight/controller/sal/compatibility/test/NodeMappingTest.java diff --git a/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java b/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java index 9276238e01..2c95252ac7 100644 --- a/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java +++ b/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.sal.compatibility; +import java.math.BigInteger; import java.util.Date; import java.util.HashSet; import java.util.List; @@ -334,8 +335,8 @@ public final class NodeMapping { public static MacAddress toADMacAddress(final NodeId id) { final String nodeId = id.getValue().replaceAll("openflow:", ""); - long lNodeId = Long.parseLong(nodeId); - lNodeId = Long.valueOf(lNodeId).longValue(); + BigInteger nodeIdRaw = new BigInteger(nodeId); + long lNodeId = nodeIdRaw.longValue(); byte[] bytesFromDpid = ToSalConversionsUtils.bytesFromDpid(lNodeId); return new MacAddress(bytesFromDpid); } diff --git a/opendaylight/md-sal/compatibility/sal-compatibility/src/test/java/org/opendaylight/controller/sal/compatibility/test/NodeMappingTest.java b/opendaylight/md-sal/compatibility/sal-compatibility/src/test/java/org/opendaylight/controller/sal/compatibility/test/NodeMappingTest.java new file mode 100644 index 0000000000..b9a2f5bff0 --- /dev/null +++ b/opendaylight/md-sal/compatibility/sal-compatibility/src/test/java/org/opendaylight/controller/sal/compatibility/test/NodeMappingTest.java @@ -0,0 +1,58 @@ +/** + * 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.controller.sal.compatibility.test; + +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.controller.sal.compatibility.NodeMapping; +import org.opendaylight.controller.sal.core.MacAddress; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; + +/** + * test of {@link NodeMapping} utility class + */ +public class NodeMappingTest { + + /** + * Test method for + * {@link org.opendaylight.controller.sal.compatibility.NodeMapping#toADMacAddress(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId)} + * . + */ + @Test + public void testToADMacAddress() { + NodeId[] nodeIds = new NodeId[] { + // 0x0000|0000 0000002a (answer to the ultimate question of life, universe and everything) + new NodeId("42"), + // 0x7fff|ffff ffffffff (max long -> 2**63 - 1) + new NodeId("9223372036854775807"), + // 0x7fff|7fff ffffffff + new NodeId("9223231299366420479"), + // 0x8fff|7fff ffffffff (more than biggest positive long) + new NodeId("10376152803973267455"), + // 0xca13|764a e9ace65a (BUG-770) + new NodeId("14561112084339025498") + }; + + byte[][] expectedMacs = new byte[][] { + {0, 0, 0, 0, 0, 0x2a}, + {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}, + {(byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}, + {(byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}, + {(byte) 0x76, (byte) 0x4a, (byte) 0xe9, (byte) 0xac, (byte) 0xe6, (byte) 0x5a} + }; + + Assert.assertEquals(expectedMacs.length, nodeIds.length); + + for (int i = 0; i < expectedMacs.length; i++) { + NodeId nodeId = nodeIds[i]; + MacAddress mac = NodeMapping.toADMacAddress(nodeId); + Assert.assertArrayEquals(expectedMacs[i], mac.getMacAddress()); + } + } + +} -- 2.36.6