From ef2228423763ece2385b7cdc34e446f3fee748b8 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 5 Jan 2020 12:12:11 +0100 Subject: [PATCH] Minimize use of ByteBufWriteUtil in bgp-parser-spi Most of the call sites are easily reformulated in terms of either raw ByteBuf access or ByteBufUtils. Change-Id: Ie500b13578ed9be0b94f48d335889e2121d8e034 Signed-off-by: Robert Varga --- .../protocol/bgp/parser/spi/PathIdUtil.java | 10 ++++------ .../spi/extended/community/FourOctAsCommonECUtil.java | 11 ++++++----- .../spi/pojo/SimpleExtendedCommunityRegistry.java | 6 ++---- .../protocol/bgp/parser/spi/PathIdUtilTest.java | 6 ++---- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtil.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtil.java index 3afaf6d126..8351604d36 100644 --- a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtil.java +++ b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtil.java @@ -10,7 +10,6 @@ package org.opendaylight.protocol.bgp.parser.spi; import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; import java.util.Optional; -import org.opendaylight.protocol.util.ByteBufWriteUtil; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId; import org.opendaylight.yangtools.util.ImmutableOffsetMapTemplate; import org.opendaylight.yangtools.yang.common.QName; @@ -33,17 +32,16 @@ public final class PathIdUtil { } /** - * Writes path-id value into the buffer when - * the path-id is not null or does not equal to zero. + * Writes path-id value into the buffer when the path-id is not null or does not equal to zero. * * @param pathId The NLRI Path Identifier. * @param buffer The ByteBuf where path-id value can be written. */ public static void writePathId(final PathId pathId, final ByteBuf buffer) { if (pathId != null) { - final Uint32 value = pathId.getValue(); - if (value.toJava() != 0) { - ByteBufWriteUtil.writeUnsignedInt(value, buffer); + final int value = pathId.getValue().intValue(); + if (value != 0) { + buffer.writeInt(value); } } } diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/FourOctAsCommonECUtil.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/FourOctAsCommonECUtil.java index 9e2fefdbc6..ba909a2a91 100644 --- a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/FourOctAsCommonECUtil.java +++ b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/FourOctAsCommonECUtil.java @@ -8,7 +8,6 @@ package org.opendaylight.protocol.bgp.parser.spi.extended.community; import io.netty.buffer.ByteBuf; -import org.opendaylight.protocol.util.ByteBufWriteUtil; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommon; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.as._4.spec.common.As4SpecificCommonBuilder; @@ -20,12 +19,14 @@ public final class FourOctAsCommonECUtil { } public static As4SpecificCommon parseCommon(final ByteBuf body) { - return new As4SpecificCommonBuilder().setAsNumber(new AsNumber(ByteBufUtils.readUint32(body))) - .setLocalAdministrator(ByteBufUtils.readUint16(body)).build(); + return new As4SpecificCommonBuilder() + .setAsNumber(new AsNumber(ByteBufUtils.readUint32(body))) + .setLocalAdministrator(ByteBufUtils.readUint16(body)) + .build(); } public static void serializeCommon(final As4SpecificCommon extComm, final ByteBuf body) { - body.writeInt(extComm.getAsNumber().getValue().intValue()); - ByteBufWriteUtil.writeUnsignedShort(extComm.getLocalAdministrator(), body); + ByteBufUtils.write(body, extComm.getAsNumber().getValue()); + ByteBufUtils.writeOrZero(body, extComm.getLocalAdministrator()); } } diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleExtendedCommunityRegistry.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleExtendedCommunityRegistry.java index eb8a87a918..6a7deaf8b5 100644 --- a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleExtendedCommunityRegistry.java +++ b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleExtendedCommunityRegistry.java @@ -19,7 +19,6 @@ import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommu import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityRegistry; import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer; import org.opendaylight.protocol.concepts.HandlerRegistry; -import org.opendaylight.protocol.util.ByteBufWriteUtil; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder; import org.opendaylight.yangtools.concepts.Registration; @@ -64,9 +63,8 @@ final class SimpleExtendedCommunityRegistry implements ExtendedCommunityRegistry if (serializer == null) { return; } - ByteBufWriteUtil.writeUnsignedByte(Shorts.checkedCast(serializer.getType(extendedCommunity.isTransitive())), - byteAggregator); - ByteBufWriteUtil.writeUnsignedByte(Shorts.checkedCast(serializer.getSubType()), byteAggregator); + byteAggregator.writeByte(Shorts.checkedCast(serializer.getType(extendedCommunity.isTransitive()))); + byteAggregator.writeByte(Shorts.checkedCast(serializer.getSubType())); serializer.serializeExtendedCommunity(extendedCommunity.getExtendedCommunity(), byteAggregator); } diff --git a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtilTest.java b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtilTest.java index f0afbd3062..de683aa4f6 100644 --- a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtilTest.java +++ b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtilTest.java @@ -14,7 +14,6 @@ import io.netty.buffer.Unpooled; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.opendaylight.protocol.util.ByteBufWriteUtil; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.Uint32; @@ -52,10 +51,9 @@ public class PathIdUtilTest { @Test public void testReadPathId() { - final long expected = 10L; - ByteBufWriteUtil.writeUnsignedInt(expected, this.buffer); + this.buffer.writeInt(10); final PathId pathId = PathIdUtil.readPathId(this.buffer); - Assert.assertEquals(expected, pathId.getValue().longValue()); + Assert.assertEquals(Uint32.TEN, pathId.getValue()); } @Test -- 2.36.6