Specialize Util.writeShortest() for Uint{16,32} 07/86707/5
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Jan 2020 09:45:04 +0000 (10:45 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Jan 2020 21:42:22 +0000 (22:42 +0100)
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 <robert.varga@pantheon.tech>
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSIpv6FlowLabelHandler.java
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/FSTcpFlagsHandler.java
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/NumericTwoByteOperandParser.java
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/Util.java

index ea7aeb8fa428755fdf905be34cb879cc148bb0c0..e9f592407981e3ff2b2825aba6903a263e4ad958 100644 (file)
@@ -33,7 +33,7 @@ public final class FSIpv6FlowLabelHandler implements FlowspecTypeParser, Flowspe
         for (final Iterator<FlowLabel> 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);
index e84d00314739024488b30f13d75d4517501115f9..f54340d2f07eed8986ea65f8179095841182cd0b 100644 (file)
@@ -35,7 +35,7 @@ public final class FSTcpFlagsHandler implements FlowspecTypeParser, FlowspecType
         for (final Iterator<TcpFlags> 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);
index 1fa116a2868cae511e87dc9b979184420d2dd06e..93d33a6ce8196c56a6b2f8e485185c2458f32484 100644 (file)
@@ -41,7 +41,7 @@ public final class NumericTwoByteOperandParser extends AbstractNumericByteOperan
         for (final Iterator<T> 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);
         }
index 568563ec31e54fcb43858e0052055970f4d39df3..019b2ef9ee844e6b4f7f4ee798ad3974c05d9a0a 100644 (file)
@@ -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());
         }
     }
 }