From 3b658310b793d4882ce59198a6d2563ce6ba114d Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 5 Jan 2020 10:45:04 +0100 Subject: [PATCH] Specialize Util.writeShortest() for Uint{16,32} This adds type safety to the picture, so that we end up doing minimal correct checks on the value supplied. It also allows us to ditch superfluous use of ByteBufWriteUtils. Change-Id: Iddf77acd16115ba6b342279f18e6bd1b5411de1c Signed-off-by: Robert Varga --- .../bgp/flowspec/FSIpv6FlowLabelHandler.java | 2 +- .../bgp/flowspec/FSTcpFlagsHandler.java | 2 +- .../handlers/NumericTwoByteOperandParser.java | 2 +- .../protocol/bgp/flowspec/handlers/Util.java | 42 ++++++++++++------- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSIpv6FlowLabelHandler.java b/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSIpv6FlowLabelHandler.java index ea7aeb8fa4..e9f5924079 100644 --- a/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSIpv6FlowLabelHandler.java +++ b/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSIpv6FlowLabelHandler.java @@ -33,7 +33,7 @@ public final class FSIpv6FlowLabelHandler implements FlowspecTypeParser, Flowspe for (final Iterator it = list.iterator(); it.hasNext(); ) { final FlowLabel label = it.next(); final ByteBuf protoBuf = Unpooled.buffer(); - Util.writeShortest(label.getValue().intValue(), protoBuf); + Util.writeShortest(label.getValue(), protoBuf); NumericOneByteOperandParser.INSTANCE.serialize(label.getOp(), protoBuf.readableBytes(), !it.hasNext(), nlriByteBuf); nlriByteBuf.writeBytes(protoBuf); diff --git a/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSTcpFlagsHandler.java b/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSTcpFlagsHandler.java index e84d003147..f54340d2f0 100644 --- a/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSTcpFlagsHandler.java +++ b/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSTcpFlagsHandler.java @@ -35,7 +35,7 @@ public final class FSTcpFlagsHandler implements FlowspecTypeParser, FlowspecType for (final Iterator it = flags.iterator(); it.hasNext(); ) { final TcpFlags flag = it.next(); final ByteBuf flagsBuf = Unpooled.buffer(); - Util.writeShortest(flag.getValue().toJava(), flagsBuf); + Util.writeShortest(flag.getValue(), flagsBuf); BitmaskOperandParser.INSTANCE.serialize(flag.getOp(), flagsBuf.readableBytes(), !it.hasNext(), nlriByteBuf); nlriByteBuf.writeBytes(flagsBuf); diff --git a/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/NumericTwoByteOperandParser.java b/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/NumericTwoByteOperandParser.java index 1fa116a286..93d33a6ce8 100644 --- a/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/NumericTwoByteOperandParser.java +++ b/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/NumericTwoByteOperandParser.java @@ -41,7 +41,7 @@ public final class NumericTwoByteOperandParser extends AbstractNumericByteOperan for (final Iterator it = list.iterator(); it.hasNext(); ) { final T operand = it.next(); final ByteBuf protoBuf = Unpooled.buffer(); - Util.writeShortest(operand.getValue().toJava(), protoBuf); + Util.writeShortest(operand.getValue(), protoBuf); super.serialize(operand.getOp(), protoBuf.readableBytes(), !it.hasNext(), nlriByteBuf); nlriByteBuf.writeBytes(protoBuf); } diff --git a/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/Util.java b/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/Util.java index 568563ec31..019b2ef9ee 100644 --- a/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/Util.java +++ b/bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/Util.java @@ -7,11 +7,10 @@ */ package org.opendaylight.protocol.bgp.flowspec.handlers; -import com.google.common.primitives.UnsignedBytes; -import com.google.common.primitives.UnsignedInts; import io.netty.buffer.ByteBuf; -import org.opendaylight.protocol.util.ByteBufWriteUtil; import org.opendaylight.protocol.util.Values; +import org.opendaylight.yangtools.yang.common.Uint16; +import org.opendaylight.yangtools.yang.common.Uint32; public final class Util { private Util() { @@ -19,21 +18,36 @@ public final class Util { } /** - * Given the integer values, this method instead of writing the value - * in 4B field, compresses the value to lowest required byte field - * depending on the value. + * Given an uint16, this method instead of writing the value in 2B field, compresses the value to lowest required + * byte field depending on the value. * - * @param value integer to be written + * @param value uint16 to be written * @param buffer ByteBuf where the value will be written */ - public static void writeShortest(final int value, final ByteBuf buffer) { - if (value <= Values.UNSIGNED_BYTE_MAX_VALUE) { - buffer.writeByte(UnsignedBytes.checkedCast(value)); - } else if (value <= Values.UNSIGNED_SHORT_MAX_VALUE) { - ByteBufWriteUtil.writeUnsignedShort(value, buffer); + public static void writeShortest(final Uint16 value, final ByteBuf buffer) { + final int unsigned = value.toJava(); + if (unsigned <= Values.UNSIGNED_BYTE_MAX_VALUE) { + buffer.writeByte(unsigned); } else { - //value <= Values.UNSIGNED_INT_MAX_VALUE - ByteBufWriteUtil.writeUnsignedInt(UnsignedInts.toLong(value), buffer); + buffer.writeShort(unsigned); + } + } + + /** + * Given an uint32, this method instead of writing the value in 4B field, compresses the value to lowest required + * byte field depending on the value. + * + * @param value uint32 to be written + * @param buffer ByteBuf where the value will be written + */ + public static void writeShortest(final Uint32 value, final ByteBuf buffer) { + final long unsigned = value.toJava(); + if (unsigned <= Values.UNSIGNED_BYTE_MAX_VALUE) { + buffer.writeByte((int) unsigned); + } else if (unsigned <= Values.UNSIGNED_SHORT_MAX_VALUE) { + buffer.writeShort((int) unsigned); + } else { + buffer.writeInt(value.intValue()); } } } -- 2.36.6