From: Martin Bobak Date: Mon, 8 Dec 2014 13:35:00 +0000 (+0100) Subject: Bug 2280 - MatchConvertorImpl bugs found during test creation X-Git-Tag: release/lithium~717 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=b23ac3b9145122bfa72ae76c41574c0e7a74634b;p=openflowplugin.git Bug 2280 - MatchConvertorImpl bugs found during test creation - changed PbbIsid mask byte[] legnth to 3 Change-Id: I7f6eb71bc9a2ea03778835f8232f8f5057a93296 Signed-off-by: Martin Bobak --- diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl.java index 3b73311e9a..ef88ccfaf8 100644 --- a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl.java +++ b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl.java @@ -971,7 +971,7 @@ public class MatchConvertorImpl implements MatchConvertor> { pbbBuilder.setPbbIsid(isidMatchEntry.getIsid()); MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class); if (maskMatchEntry != null) { - pbbBuilder.setPbbMask(ByteUtil.bytesToUnsignedInt(maskMatchEntry.getMask())); + pbbBuilder.setPbbMask(ByteUtil.bytesToUnsignedMedium(maskMatchEntry.getMask())); } protocolMatchFieldsBuilder.setPbb(pbbBuilder.build()); matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build()); @@ -1011,7 +1011,7 @@ public class MatchConvertorImpl implements MatchConvertor> { matchEntriesBuilder.addAugmentation(IsidMatchEntry.class, isidBuilder.build()); if (pbb.getPbbMask() != null) { hasmask = true; - addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedIntToBytes(pbb.getPbbMask())); + addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedMediumToBytes(pbb.getPbbMask())); } matchEntriesBuilder.setHasMask(hasmask); return matchEntriesBuilder.build(); diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtil.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtil.java index bf2631fc02..671c081393 100644 --- a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtil.java +++ b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtil.java @@ -8,7 +8,6 @@ package org.opendaylight.openflowplugin.openflow.md.util; import com.google.common.base.Preconditions; - import java.math.BigInteger; import java.util.Arrays; @@ -62,7 +61,7 @@ public abstract class ByteUtil { * @return a long representing the unsigned int */ public static final long bytesToUnsignedInt(final byte[] bytes) { - Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly two bytes long."); + Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long."); long unsignedInt = 0; unsignedInt |= bytes[0] & 0xFF; unsignedInt <<= 8; @@ -74,6 +73,23 @@ public abstract class ByteUtil { return unsignedInt; } + /** + * Converts a 3 byte array of unsigned bytes to unsigned int + * + * @param bytes an array of 4 unsigned bytes + * @return a long representing the unsigned int + */ + public static final long bytesToUnsignedMedium(final byte[] bytes) { + Preconditions.checkArgument(bytes.length == 3, "Input byte array must be exactly three bytes long."); + long unsignedMedium = 0; + unsignedMedium |= bytes[0] & 0xFF; + unsignedMedium <<= 8; + unsignedMedium |= bytes[1] & 0xFF; + unsignedMedium <<= 8; + unsignedMedium |= bytes[2] & 0xFF; + return unsignedMedium; + } + /** * Converts a 2 byte array of unsigned bytes to unsigned short * @@ -104,6 +120,20 @@ public abstract class ByteUtil { return bytes; } + /** + * Converts unsigned integer to a 3 byte array of unsigned bytes + * + * @param unsignedInt representing the unsigned integer + * @return bytes an array of 3 unsigned bytes + */ + public static byte[] unsignedMediumToBytes(final Long unsignedInt) { + byte[] bytes = new byte[3]; + bytes[2] = (byte) (unsignedInt & 0xFF); + bytes[1] = (byte) ((unsignedInt >> 8) & 0xFF); + bytes[0] = (byte) ((unsignedInt >> 16) & 0xFF); + return bytes; + } + /** * Converts unsigned short to a 2 byte array of unsigned bytes * diff --git a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl2Test.java b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl2Test.java index d7692f0dd4..767c61a476 100644 --- a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl2Test.java +++ b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl2Test.java @@ -652,9 +652,8 @@ public class MatchConvertorImpl2Test { checkEntryHeader(entry, PbbIsid.class, true); Assert.assertEquals("Wrong pbb isid", 20, entry.getAugmentation(IsidMatchEntry.class) .getIsid().intValue()); - // FIXME - fix mask computation (length should be 3 instead of 4) -// Assert.assertArrayEquals("Wrong pbb isid mask", new byte[]{0, 0, 8}, -// entry.getAugmentation(MaskMatchEntry.class).getMask()); + Assert.assertArrayEquals("Wrong pbb isid mask", new byte[]{0, 0, 8}, + entry.getAugmentation(MaskMatchEntry.class).getMask()); entry = entries.get(7); checkEntryHeader(entry, TunnelId.class, true); Assert.assertArrayEquals("Wrong tunnel id", new byte[]{0, 0, 0, 0, 0, 0, 0, 21}, diff --git a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImplV13Test.java b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImplV13Test.java index 65622c485f..b62ed381c0 100644 --- a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImplV13Test.java +++ b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImplV13Test.java @@ -497,18 +497,17 @@ public class MatchConvertorImplV13Test { maskBuilder.setMask(new byte[]{(byte) 255, (byte) 255, (byte) 240, 0}); entriesBuilder.addAugmentation(MaskMatchEntry.class, maskBuilder.build()); entries.add(entriesBuilder.build()); - // FIXME - PbbIsid should check for mask length of 3 instead of 2 -// entriesBuilder = new MatchEntriesBuilder(); -// entriesBuilder.setOxmClass(OpenflowBasicClass.class); -// entriesBuilder.setOxmMatchField(PbbIsid.class); -// entriesBuilder.setHasMask(true); -// IsidMatchEntryBuilder isidBuilder = new IsidMatchEntryBuilder(); -// isidBuilder.setIsid(23L); -// entriesBuilder.addAugmentation(IsidMatchEntry.class, isidBuilder.build()); -// maskBuilder = new MaskMatchEntryBuilder(); -// maskBuilder.setMask(new byte[]{0, 0, 7}); -// entriesBuilder.addAugmentation(MaskMatchEntry.class, maskBuilder.build()); -// entries.add(entriesBuilder.build()); + entriesBuilder = new MatchEntriesBuilder(); + entriesBuilder.setOxmClass(OpenflowBasicClass.class); + entriesBuilder.setOxmMatchField(PbbIsid.class); + entriesBuilder.setHasMask(true); + IsidMatchEntryBuilder isidBuilder = new IsidMatchEntryBuilder(); + isidBuilder.setIsid(23L); + entriesBuilder.addAugmentation(IsidMatchEntry.class, isidBuilder.build()); + maskBuilder = new MaskMatchEntryBuilder(); + maskBuilder.setMask(new byte[]{0, 0, 7}); + entriesBuilder.addAugmentation(MaskMatchEntry.class, maskBuilder.build()); + entries.add(entriesBuilder.build()); entriesBuilder = new MatchEntriesBuilder(); entriesBuilder.setOxmClass(OpenflowBasicClass.class); entriesBuilder.setOxmMatchField(TunnelId.class); diff --git a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtilTest.java b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtilTest.java index e0e3590d23..8fdd1a25e8 100644 --- a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtilTest.java +++ b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtilTest.java @@ -7,13 +7,12 @@ package org.opendaylight.openflowplugin.openflow.md.util; -import org.junit.Test; - -import java.math.BigInteger; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.math.BigInteger; +import org.junit.Test; + /** * Created by Martin Bobak mbobak@cisco.com on 6/30/14. */ @@ -27,11 +26,19 @@ public class ByteUtilTest { private static final byte[] testBytes00 = {0, 0, 0, 0}; private static final byte[] testBytesFF = {(byte) 255, (byte) 255, (byte) 255, (byte) 255}; + private static final byte[] test3Bytes = {100, 101, 102}; + private static final byte[] test3Bytes00 = {0, 0, 0}; + private static final byte[] test3BytesFF = {(byte) 255, (byte) 255, (byte) 255}; + private static final BigInteger bigInteger = new BigInteger("1684367103"); private static final BigInteger bigIntFF = new BigInteger("4294967295"); + + private static final Integer mediumInteger = new Integer("6579558"); + private static final Integer mediumIntegerFF = new Integer("16777215"); private static final int int00 = 0; private static final int shortByteLength = 2; + private static final int mediumByteLength = 3; private static final int intByteLength = 4; @Test @@ -81,6 +88,18 @@ public class ByteUtilTest { assertEquals(bigIntFF.shiftRight(16).intValue(), unsigned); } + @Test + public void testBytesToUnsignedMedium() { + long unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes); + assertEquals(mediumInteger.longValue(), unsigned); + + unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes00); + assertEquals(0, unsigned); + + unsigned = ByteUtil.bytesToUnsignedMedium(test3BytesFF); + assertEquals(mediumIntegerFF.longValue(), unsigned); + } + @Test(expected = IllegalArgumentException.class) public void exceptionTestBytesToUnsignedShort() { ByteUtil.bytesToUnsignedShort(testBytes); @@ -123,4 +142,21 @@ public class ByteUtilTest { bytes = ByteUtil.unsignedShortToBytes(intValue); assertTrue(bytes.length == shortByteLength); } + + @Test + public void testUnsignedMediumToBytes() { + long intValue = 255; + byte[] bytes = ByteUtil.unsignedMediumToBytes(intValue); + + assertTrue(bytes.length == mediumByteLength); + + intValue += 256; + bytes = ByteUtil.unsignedMediumToBytes(intValue); + assertTrue(bytes.length == mediumByteLength); + + intValue += 256; + bytes = ByteUtil.unsignedMediumToBytes(intValue); + assertTrue(bytes.length == mediumByteLength); + } + }