From 8f4d8c2b4cb95d53c00ff70d12c1fc251f9992aa Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 4 Jan 2020 15:43:27 +0100 Subject: [PATCH] Reduce ByteBuf.writeZero() usage For 1/2/3/4/8-byte output we can use faster methods which put the appropriately-sized 0 constant into the buffer. Use that. Change-Id: I2eba6124d1730a7d52a3e992df5542aa080377db Signed-off-by: Robert Varga --- .../bgp/concepts/IpAddressUtil.java | 6 +- .../bgp/evpn/impl/esi/types/ASGenParser.java | 2 +- .../evpn/impl/esi/types/AbstractEsiType.java | 1 - .../bgp/evpn/impl/esi/types/LacpParser.java | 2 +- .../bgp/evpn/impl/esi/types/LanParser.java | 2 +- .../evpn/impl/esi/types/RouterIdParser.java | 2 +- .../bgp/evpn/impl/nlri/AbstractEvpnNlri.java | 1 - .../bgp/evpn/impl/nlri/MACIpAdvRParser.java | 4 +- .../mcast/nlri/L3vpnMcastNlriSerializer.java | 3 +- .../impl/message/BGPUpdateMessageParser.java | 8 +- .../open/GracefulCapabilityHandler.java | 2 +- .../open/LlGracefulCapabilityHandler.java | 2 +- .../parser/spi/pojo/SimpleNlriRegistry.java | 3 +- .../AbstractBmpPerPeerMessageParser.java | 4 +- .../te/lsp/P2MPTeLspCapabilityParser.java | 4 +- .../impl/te/AdminStatusObjectParser.java | 2 +- .../parser/impl/te/FlowSpecObjectParser.java | 10 +-- .../InformationalFastRerouteObjectParser.java | 2 +- .../parser/impl/te/MetricObjectParser.java | 2 +- .../impl/te/ProtectionCommonParser.java | 9 +- .../impl/te/SenderTspecObjectParser.java | 11 +-- .../protocol/util/ByteBufWriteUtil.java | 82 ++++--------------- .../protocol/util/ByteBufWriteUtilTest.java | 2 +- 23 files changed, 54 insertions(+), 112 deletions(-) diff --git a/bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/IpAddressUtil.java b/bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/IpAddressUtil.java index 81c36eb0db..0c067c9377 100644 --- a/bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/IpAddressUtil.java +++ b/bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/IpAddressUtil.java @@ -25,8 +25,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes; * @author Claudio D. Gasparini */ public final class IpAddressUtil { - private static final int ZERO_BYTE = 1; - private IpAddressUtil() { throw new UnsupportedOperationException(); } @@ -78,7 +76,7 @@ public final class IpAddressUtil { body.writeByte(Ipv6Util.IPV6_BITS_LENGTH); body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address())); } else { - body.writeZero(ZERO_BYTE); + body.writeByte(0); } return body; } @@ -96,7 +94,7 @@ public final class IpAddressUtil { } else if (address.getIpv6Address() != null) { body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address())); } else { - body.writeZero(ZERO_BYTE); + body.writeByte(0); } return body; } diff --git a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/ASGenParser.java b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/ASGenParser.java index da03b1a9b4..1af115b6aa 100644 --- a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/ASGenParser.java +++ b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/ASGenParser.java @@ -31,7 +31,7 @@ final class ASGenParser extends AbstractEsiType { final AsGenerated asGen = ((AsGeneratedCase) esi).getAsGenerated(); writeUnsignedInt(asGen.getAs().getValue(), body); writeUnsignedInt(asGen.getLocalDiscriminator(), body); - return body.writeZero(ZERO_BYTE); + return body.writeByte(0); } @Override diff --git a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/AbstractEsiType.java b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/AbstractEsiType.java index 942d95e9fe..980ba90f02 100644 --- a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/AbstractEsiType.java +++ b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/AbstractEsiType.java @@ -17,7 +17,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn abstract class AbstractEsiType implements EsiParser, EsiSerializer { private static final int BODY_LENGTH = 9; - static final int ZERO_BYTE = 1; static final int MAC_ADDRESS_LENGTH = 6; @Override diff --git a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/LacpParser.java b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/LacpParser.java index a2bdc2e5a7..cf48c37bf6 100644 --- a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/LacpParser.java +++ b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/LacpParser.java @@ -33,7 +33,7 @@ final class LacpParser extends AbstractEsiType { final LacpAutoGenerated lacp = ((LacpAutoGeneratedCase) esi).getLacpAutoGenerated(); body.writeBytes(IetfYangUtil.INSTANCE.bytesFor(lacp.getCeLacpMacAddress())); ByteBufWriteUtil.writeUnsignedShort(lacp.getCeLacpPortKey(), body); - return body.writeZero(ZERO_BYTE); + return body.writeByte(0); } @Override diff --git a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/LanParser.java b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/LanParser.java index 0065a97ccd..255553da0b 100644 --- a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/LanParser.java +++ b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/LanParser.java @@ -33,7 +33,7 @@ final class LanParser extends AbstractEsiType { final LanAutoGenerated lan = ((LanAutoGeneratedCase) esi).getLanAutoGenerated(); body.writeBytes(IetfYangUtil.INSTANCE.bytesFor(lan.getRootBridgeMacAddress())); ByteBufWriteUtil.writeUnsignedShort(lan.getRootBridgePriority(), body); - return body.writeZero(ZERO_BYTE); + return body.writeByte(0); } @Override diff --git a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/RouterIdParser.java b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/RouterIdParser.java index 6ec1aa5037..34651725c9 100644 --- a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/RouterIdParser.java +++ b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/esi/types/RouterIdParser.java @@ -32,7 +32,7 @@ final class RouterIdParser extends AbstractEsiType { final RouterIdGenerated routerID = ((RouterIdGeneratedCase) esi).getRouterIdGenerated(); ByteBufWriteUtil.writeIpv4Address(routerID.getRouterId(), body); ByteBufWriteUtil.writeUnsignedInt(routerID.getLocalDiscriminator(), body); - return body.writeZero(ZERO_BYTE); + return body.writeByte(0); } @Override diff --git a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/AbstractEvpnNlri.java b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/AbstractEvpnNlri.java index e78ca3a720..b035d9ab33 100644 --- a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/AbstractEvpnNlri.java +++ b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/AbstractEvpnNlri.java @@ -24,7 +24,6 @@ abstract class AbstractEvpnNlri implements EvpnParser, EvpnSerializer { static final int MAC_ADDRESS_LENGTH = 6; static final int ESI_SIZE = 10; private static final NodeIdentifier ESI_NID = NodeIdentifier.create(Esi.QNAME); - static final int ZERO_BYTE = 1; @Override public final ByteBuf serializeEvpn(final EvpnChoice evpn, final ByteBuf common) { diff --git a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/MACIpAdvRParser.java b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/MACIpAdvRParser.java index cea7f0d05b..74f185a8ff 100644 --- a/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/MACIpAdvRParser.java +++ b/bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/MACIpAdvRParser.java @@ -133,10 +133,10 @@ final class MACIpAdvRParser extends AbstractEvpnNlri { body.writeByte(Ipv6Util.IPV6_BITS_LENGTH); body.writeBytes(Ipv6Util.bytesForAddress(ipAddress.getIpv6Address())); } else { - body.writeZero(ZERO_BYTE); + body.writeByte(0); } } else { - body.writeZero(ZERO_BYTE); + body.writeByte(0); } return body; } diff --git a/bgp/extensions/l3vpn/src/main/java/org/opendaylight/protocol/bgp/l3vpn/mcast/nlri/L3vpnMcastNlriSerializer.java b/bgp/extensions/l3vpn/src/main/java/org/opendaylight/protocol/bgp/l3vpn/mcast/nlri/L3vpnMcastNlriSerializer.java index 691f47189e..9507b2caef 100644 --- a/bgp/extensions/l3vpn/src/main/java/org/opendaylight/protocol/bgp/l3vpn/mcast/nlri/L3vpnMcastNlriSerializer.java +++ b/bgp/extensions/l3vpn/src/main/java/org/opendaylight/protocol/bgp/l3vpn/mcast/nlri/L3vpnMcastNlriSerializer.java @@ -67,8 +67,9 @@ public final class L3vpnMcastNlriSerializer { output.writeByte(IPV6_BITS_LENGTH); ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv6Prefix(), prefixBuf); } + // FIXME: remove this funky loop while (prefixBuf.readableBytes() % 8 != 0) { - prefixBuf.writeZero(1); + prefixBuf.writeByte(0); } output.writeBytes(prefixBuf); } diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPUpdateMessageParser.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPUpdateMessageParser.java index 978851173f..7e4154eec7 100755 --- a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPUpdateMessageParser.java +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPUpdateMessageParser.java @@ -70,10 +70,6 @@ public final class BGPUpdateMessageParser implements MessageParser, MessageSeria public static final int TYPE = 2; - private static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2; - - private static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2; - private final AttributeRegistry attrReg; private final NlriRegistry nlriReg; @@ -97,7 +93,7 @@ public final class BGPUpdateMessageParser implements MessageParser, MessageSeria messageBody.writeShort(withdrawnRoutesBuf.writerIndex()); messageBody.writeBytes(withdrawnRoutesBuf); } else { - messageBody.writeZero(WITHDRAWN_ROUTES_LENGTH_SIZE); + messageBody.writeShort(0); } if (update.getAttributes() != null) { final ByteBuf pathAttributesBuf = Unpooled.buffer(); @@ -105,7 +101,7 @@ public final class BGPUpdateMessageParser implements MessageParser, MessageSeria messageBody.writeShort(pathAttributesBuf.writerIndex()); messageBody.writeBytes(pathAttributesBuf); } else { - messageBody.writeZero(TOTAL_PATH_ATTR_LENGTH_SIZE); + messageBody.writeShort(0); } final List nlris = update.getNlri(); if (nlris != null) { diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/GracefulCapabilityHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/GracefulCapabilityHandler.java index 85dd22c193..fa5979f9be 100644 --- a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/GracefulCapabilityHandler.java +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/GracefulCapabilityHandler.java @@ -86,7 +86,7 @@ public final class GracefulCapabilityHandler implements CapabilityParser, Capabi if (t.getAfiFlags() != null && t.getAfiFlags().isForwardingState()) { bytes.writeByte(AFI_FLAG_FORWARDING_STATE); } else { - bytes.writeZero(1); + bytes.writeByte(0); } } } diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/LlGracefulCapabilityHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/LlGracefulCapabilityHandler.java index 0aab83aca2..098d940e96 100644 --- a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/LlGracefulCapabilityHandler.java +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/LlGracefulCapabilityHandler.java @@ -129,7 +129,7 @@ public final class LlGracefulCapabilityHandler implements CapabilityParser, Capa if (table.getAfiFlags() != null && table.getAfiFlags().isForwardingState()) { buffer.writeByte(AFI_FLAG_FORWARDING_STATE); } else { - buffer.writeZero(1); + buffer.writeByte(0); } final Uint24 staleTime = table.getLongLivedStaleTime(); final int timeval = staleTime != null ? staleTime.getValue().intValue() : 0; diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleNlriRegistry.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleNlriRegistry.java index d215c60553..bb4967aa70 100644 --- a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleNlriRegistry.java +++ b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleNlriRegistry.java @@ -46,7 +46,6 @@ import org.slf4j.LoggerFactory; final class SimpleNlriRegistry implements NlriRegistry { private static final int RESERVED = 1; - private static final int NEXT_HOP_LENGHT = 1; private static final String PARSER_NOT_FOUND = "Nlri parser not found for table type {}"; private static final Logger LOG = LoggerFactory.getLogger(SimpleNlriRegistry.class); @@ -186,7 +185,7 @@ final class SimpleNlriRegistry implements NlriRegistry { byteAggregator.writeBytes(nextHopBuffer); } else { - byteAggregator.writeZero(NEXT_HOP_LENGHT); + byteAggregator.writeByte(0); } byteAggregator.writeZero(RESERVED); } diff --git a/bmp/bmp-spi/src/main/java/org/opendaylight/protocol/bmp/spi/parser/AbstractBmpPerPeerMessageParser.java b/bmp/bmp-spi/src/main/java/org/opendaylight/protocol/bmp/spi/parser/AbstractBmpPerPeerMessageParser.java index ba6593a6c4..cfaf7e7c19 100644 --- a/bmp/bmp-spi/src/main/java/org/opendaylight/protocol/bmp/spi/parser/AbstractBmpPerPeerMessageParser.java +++ b/bmp/bmp-spi/src/main/java/org/opendaylight/protocol/bmp/spi/parser/AbstractBmpPerPeerMessageParser.java @@ -129,12 +129,12 @@ public abstract class AbstractBmpPerPeerMessageParser> exte if (peerHeader.getTimestampSec() != null) { ByteBufWriteUtil.writeUnsignedInt(peerHeader.getTimestampSec().getValue(), output); } else { - output.writeZero(ByteBufWriteUtil.INT_BYTES_LENGTH); + output.writeInt(0); } if (peerHeader.getTimestampMicro() != null) { ByteBufWriteUtil.writeUnsignedInt(peerHeader.getTimestampMicro().getValue(), output); } else { - output.writeZero(ByteBufWriteUtil.INT_BYTES_LENGTH); + output.writeInt(0); } } diff --git a/pcep/ietf-p2mp-te-lsp/src/main/java/org/opendaylight/protocol/pcep/p2mp/te/lsp/P2MPTeLspCapabilityParser.java b/pcep/ietf-p2mp-te-lsp/src/main/java/org/opendaylight/protocol/pcep/p2mp/te/lsp/P2MPTeLspCapabilityParser.java index 15d6882c5e..6298034ff5 100644 --- a/pcep/ietf-p2mp-te-lsp/src/main/java/org/opendaylight/protocol/pcep/p2mp/te/lsp/P2MPTeLspCapabilityParser.java +++ b/pcep/ietf-p2mp-te-lsp/src/main/java/org/opendaylight/protocol/pcep/p2mp/te/lsp/P2MPTeLspCapabilityParser.java @@ -23,13 +23,13 @@ public final class P2MPTeLspCapabilityParser implements TlvParser, TlvSerializer public static final int TYPE = 6; private static final int CONTENT_LENGTH = 2; @VisibleForTesting - static P2mpPceCapability P2MP_CAPABILITY = new P2mpPceCapabilityBuilder().build(); + static final P2mpPceCapability P2MP_CAPABILITY = new P2mpPceCapabilityBuilder().build(); @Override public void serializeTlv(final Tlv tlv, final ByteBuf buffer) { Preconditions.checkArgument(tlv instanceof P2mpPceCapability, "P2mpPceCapability is mandatory."); final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH); - body.writeZero(2); + body.writeShort(0); TlvUtil.formatTlv(TYPE, body, buffer); } diff --git a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/AdminStatusObjectParser.java b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/AdminStatusObjectParser.java index e6e4818db2..686fdcfd4e 100644 --- a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/AdminStatusObjectParser.java +++ b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/AdminStatusObjectParser.java @@ -48,7 +48,7 @@ public final class AdminStatusObjectParser extends AbstractRSVPObjectParser { final BitArray reflect = new BitArray(FLAGS_SIZE); reflect.set(REFLECT, addObject.isReflect()); reflect.toByteBuf(output); - output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH); + output.writeShort(0); final BitArray flags = new BitArray(FLAGS_SIZE); flags.set(TESTING, addObject.isTesting()); flags.set(DOWN, addObject.isAdministrativelyDown()); diff --git a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/FlowSpecObjectParser.java b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/FlowSpecObjectParser.java index 32a06aa5bf..bfd47c47c4 100644 --- a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/FlowSpecObjectParser.java +++ b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/FlowSpecObjectParser.java @@ -71,15 +71,15 @@ public final class FlowSpecObjectParser extends AbstractRSVPObjectParser { final int sHeader = flowObj.getServiceHeader().getIntValue(); if (sHeader == 2) { serializeAttributeHeader(BODY_SIZE_REQUESTING, CLASS_NUM, CTYPE, output); - output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH); + output.writeShort(0); output.writeShort(REQUESTING_OVERALL_LENGTH); } else { serializeAttributeHeader(BODY_SIZE_CONTROLLED, CLASS_NUM, CTYPE, output); - output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH); + output.writeShort(0); output.writeShort(CONTROLLER_OVERALL_LENGHT); } output.writeByte(sHeader); - output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH); + output.writeByte(0); if (sHeader == 2) { output.writeShort(SERVICE_LENGHT); } else { @@ -87,7 +87,7 @@ public final class FlowSpecObjectParser extends AbstractRSVPObjectParser { } output.writeByte(TOKEN_BUCKET_TSPEC); - output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH); + output.writeByte(0); output.writeShort(PARAMETER_127_LENGTH); final TspecObject tSpec = flowObj.getTspecObject(); writeFloat32(tSpec.getTokenBucketRate(), output); @@ -100,7 +100,7 @@ public final class FlowSpecObjectParser extends AbstractRSVPObjectParser { return; } output.writeByte(GUARANTEED_SERVICE_RSPEC); - output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH); + output.writeByte(0); output.writeShort(PARAMETER_130_LENGTH); writeFloat32(flowObj.getRate(), output); writeUnsignedInt(flowObj.getSlackTerm(), output); diff --git a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/InformationalFastRerouteObjectParser.java b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/InformationalFastRerouteObjectParser.java index 3984aaadf5..ecc2761b87 100644 --- a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/InformationalFastRerouteObjectParser.java +++ b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/InformationalFastRerouteObjectParser.java @@ -53,7 +53,7 @@ public final class InformationalFastRerouteObjectParser extends AbstractRSVPObje byteAggregator.writeByte(fastRerouteObject.getSetupPriority().toJava()); byteAggregator.writeByte(fastRerouteObject.getHoldPriority().toJava()); byteAggregator.writeByte(fastRerouteObject.getHopLimit().toJava()); - byteAggregator.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH); + byteAggregator.writeByte(0); byteAggregator.writeBytes(Unpooled.wrappedBuffer(fastRerouteObject.getBandwidth().getValue())); writeAttributeFilter(fastRerouteObject.getIncludeAny(), byteAggregator); writeAttributeFilter(fastRerouteObject.getExcludeAny(), byteAggregator); diff --git a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/MetricObjectParser.java b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/MetricObjectParser.java index 39d2abf9b7..80c885d9d2 100644 --- a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/MetricObjectParser.java +++ b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/MetricObjectParser.java @@ -47,7 +47,7 @@ public final class MetricObjectParser extends AbstractRSVPObjectParser { checkArgument(teLspObject instanceof MetricObject, "BandwidthObject is mandatory."); final MetricObject metric = (MetricObject) teLspObject; serializeAttributeHeader(BODY_SIZE, CLASS_NUM, CTYPE, output); - output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH); + output.writeShort(0); final BitArray reflect = new BitArray(FLAGS_SIZE); reflect.set(BOUND, metric.isBound()); reflect.set(COMPUTED, metric.isComputed()); diff --git a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/ProtectionCommonParser.java b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/ProtectionCommonParser.java index 1b115fc0fe..d73c50918d 100644 --- a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/ProtectionCommonParser.java +++ b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/ProtectionCommonParser.java @@ -20,7 +20,6 @@ import org.slf4j.LoggerFactory; public class ProtectionCommonParser { - protected static final int BYTE_SIZE = 1; protected static final short PROTECTION_SUBOBJECT_TYPE_1 = 1; protected static final short PROTECTION_SUBOBJECT_TYPE_2 = 2; protected static final int CONTENT_LENGTH_C2 = 8; @@ -41,7 +40,7 @@ public class ProtectionCommonParser { final BitArray flagBitArray = new BitArray(FLAGS_SIZE); flagBitArray.set(SECONDARY, protObj.isSecondary()); flagBitArray.toByteBuf(output); - output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH); + output.writeShort(0); output.writeByte(protObj.getLinkFlags().getIntValue()); } @@ -53,14 +52,14 @@ public class ProtectionCommonParser { flagBitArray.set(OPERATIONAL, protObj.isOperational()); flagBitArray.toByteBuf(output); output.writeByte(protObj.getLspFlag().getIntValue()); - output.writeZero(BYTE_SIZE); + output.writeByte(0); output.writeByte(protObj.getLinkFlags().getIntValue()); final BitArray flagInPlaceBitArray = new BitArray(FLAGS_SIZE); flagInPlaceBitArray.set(IN_PLACE, protObj.isInPlace()); flagInPlaceBitArray.set(REQUIRED, protObj.isRequired()); flagInPlaceBitArray.toByteBuf(output); output.writeByte(protObj.getSegFlag().getIntValue()); - output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH); + output.writeShort(0); } protected static ProtectionSubobject parseCommonProtectionBodyType2(final ByteBuf byteBuf) throws @@ -106,7 +105,7 @@ public class ProtectionCommonParser { protected static void serializeBody(final short ctype, final ProtectionSubobject protObj, final ByteBuf output) { - output.writeZero(BYTE_SIZE); + output.writeByte(0); output.writeByte(ctype); switch (ctype) { case PROTECTION_SUBOBJECT_TYPE_1: diff --git a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/SenderTspecObjectParser.java b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/SenderTspecObjectParser.java index 9642c7e2fd..2b47938921 100644 --- a/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/SenderTspecObjectParser.java +++ b/rsvp/impl/src/main/java/org/opendaylight/protocol/rsvp/parser/impl/te/SenderTspecObjectParser.java @@ -26,7 +26,7 @@ public class SenderTspecObjectParser extends AbstractRSVPObjectParser { public static final short CLASS_NUM = 12; public static final short CTYPE = 2; private static final int BODY_SIZE = 32; - private static final int SERVICE_LENGHT = 6; + private static final int SERVICE_LENGTH = 6; @Override protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException { @@ -51,13 +51,14 @@ public class SenderTspecObjectParser extends AbstractRSVPObjectParser { checkArgument(teLspObject instanceof TspecObject, "SenderTspecObject is mandatory."); final TspecObject tspecObj = (TspecObject) teLspObject; serializeAttributeHeader(BODY_SIZE, CLASS_NUM, CTYPE, output); - output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH); + output.writeShort(0); output.writeShort(OVERALL_LENGTH); + // FIXME: this is weird. Use explicit 1? output.writeByte(ByteBufWriteUtil.ONE_BYTE_LENGTH); - output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH); - output.writeShort(SERVICE_LENGHT); + output.writeByte(0); + output.writeShort(SERVICE_LENGTH); output.writeByte(TOKEN_BUCKET_TSPEC); - output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH); + output.writeByte(0); output.writeShort(PARAMETER_127_LENGTH); writeFloat32(tspecObj.getTokenBucketRate(), output); writeFloat32(tspecObj.getTokenBucketSize(), output); diff --git a/util/src/main/java/org/opendaylight/protocol/util/ByteBufWriteUtil.java b/util/src/main/java/org/opendaylight/protocol/util/ByteBufWriteUtil.java index 6a205886dd..991cca391a 100644 --- a/util/src/main/java/org/opendaylight/protocol/util/ByteBufWriteUtil.java +++ b/util/src/main/java/org/opendaylight/protocol/util/ByteBufWriteUtil.java @@ -30,8 +30,6 @@ public final class ByteBufWriteUtil { public static final int SHORT_BYTES_LENGTH = Short.SIZE / Byte.SIZE; - public static final int MEDIUM_BYTES_LENGTH = 3; - public static final int INT_BYTES_LENGTH = Integer.SIZE / Byte.SIZE; public static final int LONG_BYTES_LENGTH = Long.SIZE / Byte.SIZE; @@ -59,11 +57,7 @@ public final class ByteBufWriteUtil { * ByteBuf, where value or zeros are written. */ public static void writeInt(final Integer value, final ByteBuf output) { - if (value != null) { - output.writeInt(value); - } else { - output.writeZero(INT_BYTES_LENGTH); - } + output.writeInt(value != null ? value : 0); } /** @@ -77,11 +71,7 @@ public final class ByteBufWriteUtil { * ByteBuf, where value or zeros are written. */ public static void writeMedium(final Integer value, final ByteBuf output) { - if (value != null) { - output.writeMedium(value); - } else { - output.writeZero(MEDIUM_BYTES_LENGTH); - } + output.writeMedium(value != null ? value : 0); } /** @@ -95,11 +85,7 @@ public final class ByteBufWriteUtil { * ByteBuf, where value or zeros are written. */ public static void writeShort(final Short value, final ByteBuf output) { - if (value != null) { - output.writeShort(value); - } else { - output.writeZero(SHORT_BYTES_LENGTH); - } + output.writeShort(value != null ? value : 0); } /** @@ -113,11 +99,7 @@ public final class ByteBufWriteUtil { * ByteBuf, where value or zeros are written. */ public static void writeLong(final Long value, final ByteBuf output) { - if (value != null) { - output.writeLong(value); - } else { - output.writeZero(LONG_BYTES_LENGTH); - } + output.writeLong(value != null ? value : 0L); } /** @@ -133,7 +115,7 @@ public final class ByteBufWriteUtil { if (value != null) { output.writeBoolean(value); } else { - output.writeZero(ONE_BYTE_LENGTH); + output.writeByte(0); } } @@ -150,11 +132,7 @@ public final class ByteBufWriteUtil { */ @Deprecated(forRemoval = true) public static void writeUnsignedByte(final Short value, final ByteBuf output) { - if (value != null) { - output.writeByte(value); - } else { - output.writeZero(ONE_BYTE_LENGTH); - } + output.writeByte(value != null ? value : 0); } /** @@ -168,11 +146,7 @@ public final class ByteBufWriteUtil { * ByteBuf, where value or zeros are written. */ public static void writeUnsignedByte(final Uint8 value, final ByteBuf output) { - if (value != null) { - output.writeByte(value.byteValue()); - } else { - output.writeZero(ONE_BYTE_LENGTH); - } + output.writeByte(value != null ? value.byteValue() : 0); } /** @@ -188,11 +162,7 @@ public final class ByteBufWriteUtil { */ @Deprecated(forRemoval = true) public static void writeUnsignedShort(final Integer value, final ByteBuf output) { - if (value != null) { - output.writeShort(value.shortValue()); - } else { - output.writeZero(SHORT_BYTES_LENGTH); - } + output.writeShort(value != null ? value.shortValue() : 0); } /** @@ -206,11 +176,7 @@ public final class ByteBufWriteUtil { * ByteBuf, where value or zeros are written. */ public static void writeUnsignedShort(final Uint16 value, final ByteBuf output) { - if (value != null) { - output.writeShort(value.shortValue()); - } else { - output.writeZero(SHORT_BYTES_LENGTH); - } + output.writeShort(value != null ? value.shortValue() : 0); } /** @@ -226,11 +192,7 @@ public final class ByteBufWriteUtil { */ @Deprecated(forRemoval = true) public static void writeUnsignedInt(final Long value, final ByteBuf output) { - if (value != null) { - output.writeInt(value.intValue()); - } else { - output.writeZero(INT_BYTES_LENGTH); - } + output.writeInt(value != null ? value.intValue() : 0); } /** @@ -244,11 +206,7 @@ public final class ByteBufWriteUtil { * ByteBuf, where value or zeros are written. */ public static void writeUnsignedInt(final Uint32 value, final ByteBuf output) { - if (value != null) { - output.writeInt(value.intValue()); - } else { - output.writeZero(INT_BYTES_LENGTH); - } + output.writeInt(value != null ? value.intValue() : 0); } /** @@ -264,11 +222,7 @@ public final class ByteBufWriteUtil { */ @Deprecated(forRemoval = true) public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) { - if (value != null) { - output.writeLong(value.longValue()); - } else { - output.writeZero(LONG_BYTES_LENGTH); - } + output.writeLong(value != null ? value.longValue() : 0L); } /** @@ -282,11 +236,7 @@ public final class ByteBufWriteUtil { * ByteBuf, where value or zeros are written. */ public static void writeUnsignedLong(final Uint64 value, final ByteBuf output) { - if (value != null) { - output.writeLong(value.longValue()); - } else { - output.writeZero(LONG_BYTES_LENGTH); - } + output.writeLong(value != null ? value.longValue() : 0L); } /** @@ -302,7 +252,7 @@ public final class ByteBufWriteUtil { if (ipv4Address != null) { output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address)); } else { - output.writeZero(Ipv4Util.IP4_LENGTH); + output.writeInt(0); } } @@ -319,7 +269,7 @@ public final class ByteBufWriteUtil { if (ipv4Address != null) { output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address)); } else { - output.writeZero(Ipv4Util.IP4_LENGTH); + output.writeInt(0); } } @@ -421,7 +371,7 @@ public final class ByteBufWriteUtil { if (value != null) { output.writeBytes(value.getValue()); } else { - output.writeZero(FLOAT32_BYTES_LENGTH); + output.writeInt(0); } } } diff --git a/util/src/test/java/org/opendaylight/protocol/util/ByteBufWriteUtilTest.java b/util/src/test/java/org/opendaylight/protocol/util/ByteBufWriteUtilTest.java index e6f25e8d32..1a6db061dd 100644 --- a/util/src/test/java/org/opendaylight/protocol/util/ByteBufWriteUtilTest.java +++ b/util/src/test/java/org/opendaylight/protocol/util/ByteBufWriteUtilTest.java @@ -74,7 +74,7 @@ public class ByteBufWriteUtilTest { @Test public void testWriteMediumValue() { final byte[] result = { 0, 0, 5 }; - final ByteBuf output = Unpooled.buffer(ByteBufWriteUtil.MEDIUM_BYTES_LENGTH); + final ByteBuf output = Unpooled.buffer(3); writeMedium(5, output); assertArrayEquals(result, output.array()); -- 2.36.6