Reduce use of ByteBugWriteUtil in BMP 19/86719/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Jan 2020 14:14:12 +0000 (15:14 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Jan 2020 14:14:56 +0000 (15:14 +0100)
Use either direct ByteBuf interaction or ByteBufUtils methods to
provide better speed & simplicity.

Change-Id: Iff91ab0286c06668dd551adcabaaf2046b8467f4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bmp/bmp-parser-impl/src/main/java/org/opendaylight/protocol/bmp/parser/message/PeerDownHandler.java
bmp/bmp-parser-impl/src/main/java/org/opendaylight/protocol/bmp/parser/message/PeerUpHandler.java
bmp/bmp-parser-impl/src/main/java/org/opendaylight/protocol/bmp/parser/message/StatisticsReportHandler.java
bmp/bmp-parser-impl/src/main/java/org/opendaylight/protocol/bmp/parser/tlv/StatType009TlvHandler.java
bmp/bmp-parser-impl/src/main/java/org/opendaylight/protocol/bmp/parser/tlv/StatType010TlvHandler.java
bmp/bmp-spi/src/main/java/org/opendaylight/protocol/bmp/spi/parser/AbstractBmpPerPeerMessageParser.java
bmp/bmp-spi/src/main/java/org/opendaylight/protocol/bmp/spi/parser/TlvUtil.java
bmp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/SimpleBmpMessageRegistryTest.java

index fd361c80911bd797e6013f31b4d6ec1010d6eb7e..e84d6e1a24a4cd79b5947db49e970ffafa17db4b 100644 (file)
@@ -22,7 +22,6 @@ import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
 import org.opendaylight.protocol.bmp.spi.parser.AbstractBmpPerPeerMessageParser;
 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.NotifyBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.NotifyMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.PeerDownNotification;
@@ -50,22 +49,22 @@ public class PeerDownHandler extends AbstractBmpPerPeerMessageParser<PeerDownNot
         final PeerDownNotification peerDown = (PeerDownNotification) message;
         if (peerDown.isLocalSystemClosed()) {
             if (peerDown.getData() instanceof FsmEventCode) {
-                ByteBufWriteUtil.writeUnsignedByte(REASON_TWO.getValue(), buffer);
-                ByteBufWriteUtil.writeUnsignedShort(((FsmEventCode) peerDown.getData()).getFsmEventCode(), buffer);
+                buffer.writeByte(REASON_TWO.getValue());
+                ByteBufUtils.writeOrZero(buffer, ((FsmEventCode) peerDown.getData()).getFsmEventCode());
             } else if (peerDown.getData()
                     instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329
                     .peer.down.data.Notification) {
-                ByteBufWriteUtil.writeUnsignedByte(REASON_ONE.getValue(), buffer);
+                buffer.writeByte(REASON_ONE.getValue());
                 serializePDU(peerDown.getData(), buffer);
             }
         } else {
             if (peerDown.getData()
                     instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329
                     .peer.down.data.Notification) {
-                ByteBufWriteUtil.writeUnsignedByte(REASON_THREE.getValue(), buffer);
+                buffer.writeByte(REASON_THREE.getValue());
                 serializePDU(peerDown.getData(), buffer);
             } else {
-                ByteBufWriteUtil.writeUnsignedByte(REASON_FOUR.getValue(), buffer);
+                buffer.writeByte(REASON_FOUR.getValue());
             }
         }
     }
index 8c600bf53b225ad1b82425c7349788cbf8e82019..2fadb1aaa3167b5e6297256e5822f9cc6c22ea04 100644 (file)
@@ -62,8 +62,8 @@ public class PeerUpHandler extends AbstractBmpPerPeerMessageParser<InformationBu
         } else {
             ByteBufWriteUtil.writeIpv6Address(peerUp.getLocalAddress().getIpv6Address(), buffer);
         }
-        ByteBufWriteUtil.writeUnsignedShort(peerUp.getLocalPort().getValue(), buffer);
-        ByteBufWriteUtil.writeUnsignedShort(peerUp.getRemotePort().getValue(), buffer);
+        ByteBufUtils.write(buffer, peerUp.getLocalPort().getValue());
+        ByteBufUtils.write(buffer, peerUp.getRemotePort().getValue());
 
         this.msgRegistry.serializeMessage(new OpenBuilder(peerUp.getSentOpen()).build(), buffer);
         this.msgRegistry.serializeMessage(new OpenBuilder(peerUp.getReceivedOpen()).build(), buffer);
@@ -71,8 +71,8 @@ public class PeerUpHandler extends AbstractBmpPerPeerMessageParser<InformationBu
     }
 
     private void serializeTlvs(final Information tlvs, final ByteBuf output) {
-        if (tlvs != null && tlvs.getStringInformation() != null) {
-            for (final StringInformation stringInfo : tlvs.getStringInformation()) {
+        if (tlvs != null) {
+            for (final StringInformation stringInfo : tlvs.nonnullStringInformation()) {
                 if (stringInfo.getStringTlv() != null) {
                     serializeTlv(stringInfo.getStringTlv(), output);
                 }
index ba9282873e54571ad04aa6c4f420ec5dc1c1850c..c6eeb69da8d45e49ce362eecfd27de681de4f945 100644 (file)
@@ -7,8 +7,6 @@
  */
 package org.opendaylight.protocol.bmp.parser.message;
 
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
-
 import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
@@ -89,7 +87,7 @@ public class StatisticsReportHandler extends AbstractBmpPerPeerMessageParser<Tlv
         serializeStatTlv(tlvs.getPrefixesTreatedAsWithdrawTlv(), tlvsBuffer, counter);
         serializeStatTlv(tlvs.getDuplicateUpdatesTlv(), tlvsBuffer, counter);
 
-        writeUnsignedInt(counter.longValue(), output);
+        output.writeInt(counter.get());
         output.writeBytes(tlvsBuffer);
     }
 
index 0217b89a592ddd3adb95d8eea414d38856387475..1ccd70e8d45a4cf2ed4ef83c0e5f6e3d1d686b04 100644 (file)
@@ -18,7 +18,6 @@ import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvParser;
 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvSerializer;
 import org.opendaylight.protocol.bmp.spi.parser.TlvUtil;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Gauge64;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.Tlv;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.stat.tlvs.PerAfiSafiAdjRibInTlv;
@@ -31,7 +30,7 @@ public class StatType009TlvHandler implements BmpTlvParser, BmpTlvSerializer {
     private final AddressFamilyRegistry afiRegistry;
     private final SubsequentAddressFamilyRegistry safiRegistry;
 
-    public StatType009TlvHandler(final AddressFamilyRegistry afiReg, SubsequentAddressFamilyRegistry safiReg) {
+    public StatType009TlvHandler(final AddressFamilyRegistry afiReg, final SubsequentAddressFamilyRegistry safiReg) {
         this.afiRegistry = requireNonNull(afiReg, "AddressFamily cannot be null");
         this.safiRegistry = requireNonNull(safiReg, "SubsequentAddressFamily cannot be null");
     }
@@ -39,12 +38,13 @@ public class StatType009TlvHandler implements BmpTlvParser, BmpTlvSerializer {
     @Override
     public void serializeTlv(final Tlv tlv, final ByteBuf output) {
         checkArgument(tlv instanceof PerAfiSafiAdjRibInTlv, "PerAfiSafiAdjRibInTlv is mandatory.");
+        final PerAfiSafiAdjRibInTlv perAfiSafi = (PerAfiSafiAdjRibInTlv) tlv;
+
         final ByteBuf buffer = Unpooled.buffer();
-        ByteBufWriteUtil.writeUnsignedShort(this.afiRegistry.numberForClass(((PerAfiSafiAdjRibInTlv) tlv)
-                .getAfi()), buffer);
-        ByteBufWriteUtil.writeUnsignedByte(this.safiRegistry.numberForClass(((PerAfiSafiAdjRibInTlv) tlv)
-                .getSafi()).shortValue(), buffer);
-        ByteBufWriteUtil.writeUnsignedLong(((PerAfiSafiAdjRibInTlv) tlv).getCount().getValue(), buffer);
+        final Integer afiInt = this.afiRegistry.numberForClass(perAfiSafi.getAfi());
+        buffer.writeShort(afiInt != null ? afiInt : 0);
+        buffer.writeByte(this.safiRegistry.numberForClass(perAfiSafi.getSafi()));
+        ByteBufUtils.write(buffer, perAfiSafi.getCount().getValue());
         TlvUtil.formatTlv(TYPE, buffer, output);
     }
 
index 0efcedc67bea2f9f0321d8992821cd58af1ba12a..d7ed1aaeacecdb198c25ef319612f388ca5fe729 100644 (file)
@@ -9,9 +9,6 @@ package org.opendaylight.protocol.bmp.parser.tlv;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedLong;
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
 
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
@@ -33,7 +30,7 @@ public class StatType010TlvHandler implements BmpTlvParser, BmpTlvSerializer {
     private final AddressFamilyRegistry afiRegistry;
     private final SubsequentAddressFamilyRegistry safiRegistry;
 
-    public StatType010TlvHandler(final AddressFamilyRegistry afiReg, SubsequentAddressFamilyRegistry safiReg) {
+    public StatType010TlvHandler(final AddressFamilyRegistry afiReg, final SubsequentAddressFamilyRegistry safiReg) {
         this.afiRegistry = requireNonNull(afiReg, "AddressFamily cannot be null");
         this.safiRegistry = requireNonNull(safiReg, "SubsequentAddressFamily cannot be null");
     }
@@ -41,10 +38,13 @@ public class StatType010TlvHandler implements BmpTlvParser, BmpTlvSerializer {
     @Override
     public void serializeTlv(final Tlv tlv, final ByteBuf output) {
         checkArgument(tlv instanceof PerAfiSafiLocRibTlv, "PerAfiSafiLocRibInTlv is mandatory.");
+        final PerAfiSafiLocRibTlv perAfiSafi = (PerAfiSafiLocRibTlv) tlv;
+
         final ByteBuf buffer = Unpooled.buffer();
-        writeUnsignedShort(this.afiRegistry.numberForClass(((PerAfiSafiLocRibTlv) tlv).getAfi()), buffer);
-        writeUnsignedByte(this.safiRegistry.numberForClass(((PerAfiSafiLocRibTlv) tlv).getSafi()).shortValue(), buffer);
-        writeUnsignedLong(((PerAfiSafiLocRibTlv) tlv).getCount().getValue(), buffer);
+        final Integer afiInt = this.afiRegistry.numberForClass(perAfiSafi.getAfi());
+        buffer.writeShort(afiInt != null ? afiInt : 0);
+        buffer.writeByte(this.safiRegistry.numberForClass(perAfiSafi.getSafi()));
+        ByteBufUtils.write(buffer, perAfiSafi.getCount().getValue());
         TlvUtil.formatTlv(TYPE, buffer, output);
     }
 
index 0169f86d2c13760acd90bdfe1f84d2db6fd0dad0..652fb2acf1f215edfe6098e0c3d8f5208397321f 100644 (file)
@@ -5,12 +5,11 @@
  * 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.protocol.bmp.spi.parser;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
@@ -32,7 +31,6 @@ import org.opendaylight.yangtools.yang.binding.Notification;
 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 
 public abstract class AbstractBmpPerPeerMessageParser<T extends Builder<?>> extends AbstractBmpMessageWithTlvParser<T> {
-
     private static final int L_FLAG_POS = 1;
     private static final int V_FLAG_POS = 0;
     private static final int FLAGS_SIZE = 8;
@@ -63,7 +61,7 @@ public abstract class AbstractBmpPerPeerMessageParser<T extends Builder<?>> exte
     }
 
     protected static PeerHeader parsePerPeerHeader(final ByteBuf bytes) {
-        Preconditions.checkArgument(bytes.readableBytes() >= PER_PEER_HEADER_SIZE);
+        checkArgument(bytes.readableBytes() >= PER_PEER_HEADER_SIZE);
         final PeerHeaderBuilder phBuilder = new PeerHeaderBuilder();
         final PeerType peerType = PeerType.forValue(bytes.readByte());
         phBuilder.setType(peerType);
@@ -98,7 +96,7 @@ public abstract class AbstractBmpPerPeerMessageParser<T extends Builder<?>> exte
     }
 
     protected void serializePerPeerHeader(final PeerHeader peerHeader, final ByteBuf output) {
-        Preconditions.checkArgument(peerHeader != null, "Per-peer header cannot be null.");
+        checkArgument(peerHeader != null, "Per-peer header cannot be null.");
         final PeerType peerType = peerHeader.getType();
         output.writeByte(peerType.getIntValue());
         final BitArray flags = new BitArray(FLAGS_SIZE);
@@ -124,15 +122,18 @@ public abstract class AbstractBmpPerPeerMessageParser<T extends Builder<?>> exte
         } else {
             ByteBufWriteUtil.writeIpv6Address(peerHeader.getAddress().getIpv6Address(), output);
         }
-        ByteBufWriteUtil.writeUnsignedInt(peerHeader.getAs().getValue(), output);
+        ByteBufUtils.write(output, peerHeader.getAs().getValue());
         ByteBufWriteUtil.writeIpv4Address(peerHeader.getBgpId(), output);
-        if (peerHeader.getTimestampSec() != null) {
-            ByteBufWriteUtil.writeUnsignedInt(peerHeader.getTimestampSec().getValue(), output);
+
+        final Timestamp stampSec = peerHeader.getTimestampSec();
+        if (stampSec != null) {
+            ByteBufUtils.write(output, stampSec.getValue());
         } else {
             output.writeInt(0);
         }
-        if (peerHeader.getTimestampMicro() != null) {
-            ByteBufWriteUtil.writeUnsignedInt(peerHeader.getTimestampMicro().getValue(), output);
+        final Timestamp stampMicro = peerHeader.getTimestampMicro();
+        if (stampMicro != null) {
+            ByteBufUtils.write(output, stampMicro.getValue());
         } else {
             output.writeInt(0);
         }
index de85e65520de9e57240f7592b08b4638a236a2d2..d9e1dd24d0d3ed94735755f7794c1496da88ee02 100644 (file)
@@ -10,9 +10,9 @@ package org.opendaylight.protocol.bmp.spi.parser;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import java.nio.charset.StandardCharsets;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Gauge64;
+import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 
 public final class TlvUtil {
     private TlvUtil() {
@@ -26,20 +26,20 @@ public final class TlvUtil {
 
     public static void formatTlvShort16(final int type, final int value, final ByteBuf out) {
         formatTlvHeader(type, Short.BYTES, out);
-        ByteBufWriteUtil.writeUnsignedShort(value, out);
+        out.writeShort(value);
     }
 
     public static void formatTlvCounter32(final int type, final Counter32 value, final ByteBuf out) {
-        if (value != null && value.getValue() != null) {
+        if (value != null) {
             formatTlvHeader(type, Integer.BYTES, out);
-            ByteBufWriteUtil.writeUnsignedInt(value.getValue(), out);
+            ByteBufUtils.write(out, value.getValue());
         }
     }
 
     public static void formatTlvGauge64(final int type, final Gauge64 value, final ByteBuf out) {
-        if (value != null && value.getValue() != null) {
+        if (value != null) {
             formatTlvHeader(type, Long.BYTES, out);
-            ByteBufWriteUtil.writeUnsignedLong(value.getValue(), out);
+            ByteBufUtils.write(out, value.getValue());
         }
     }
 
@@ -53,11 +53,10 @@ public final class TlvUtil {
         if (value != null) {
             TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.US_ASCII), out);
         }
-
     }
 
     private static void formatTlvHeader(final int type, final int length, final ByteBuf output) {
-        ByteBufWriteUtil.writeUnsignedShort(type, output);
-        ByteBufWriteUtil.writeUnsignedShort(length, output);
+        output.writeShort(type);
+        output.writeShort(length);
     }
 }
index 336e0bdc6ff5601bcbac75121b7383c01e526d8d..d5a81c2d9f560169023a6b101ce92ff4ba5295a1 100644 (file)
@@ -5,7 +5,6 @@
  * 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.protocol.bmp.spi.registry;
 
 import static org.junit.Assert.assertArrayEquals;
@@ -17,7 +16,6 @@ import org.junit.Test;
 import org.opendaylight.protocol.bmp.spi.parser.AbstractBmpMessageParser;
 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
 import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
 public class SimpleBmpMessageRegistryTest {
@@ -73,10 +71,9 @@ public class SimpleBmpMessageRegistryTest {
     }
 
     private static final class BmpTestParser extends AbstractBmpMessageParser {
-
         @Override
         public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
-            ByteBufWriteUtil.writeUnsignedInt(((BmpTestMessage) message).getValue(), buffer);
+            buffer.writeInt((int) ((BmpTestMessage) message).getValue());
         }
 
         @Override
@@ -91,7 +88,6 @@ public class SimpleBmpMessageRegistryTest {
     }
 
     private static final class BmpTestMessage implements Notification {
-
         private final long value;
 
         BmpTestMessage(final long value) {
@@ -111,7 +107,5 @@ public class SimpleBmpMessageRegistryTest {
         public String toString() {
             return "BmpTestMessage [value=" + this.value + "]";
         }
-
     }
-
 }