From 2ebb9c3b8a7f81a692bef07901449b9c018663c2 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 6 Jan 2020 15:26:06 +0100 Subject: [PATCH] Migrate pcep-ietf-stateful07 to use ByteBufUtils Most of the parser interactions can be reformulated in terms of ByteBufUtils, do that. Change-Id: I7576c9de5649509172c91c6904478fe4be0cf154 Signed-off-by: Robert Varga --- .../ietf/stateful07/PathBindingTlvParser.java | 21 +++++------- .../Stateful07LSPIdentifierIpv4TlvParser.java | 19 ++++++----- .../Stateful07LSPIdentifierIpv6TlvParser.java | 13 ++++---- .../Stateful07LspUpdateErrorTlvParser.java | 7 ++-- .../Stateful07RSVPErrorSpecTlvParser.java | 33 ++++++++----------- .../stateful07/Stateful07SrpObjectParser.java | 18 +++++----- 6 files changed, 51 insertions(+), 60 deletions(-) diff --git a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/PathBindingTlvParser.java b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/PathBindingTlvParser.java index 1876642790..29835f778f 100644 --- a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/PathBindingTlvParser.java +++ b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/PathBindingTlvParser.java @@ -7,9 +7,9 @@ */ package org.opendaylight.protocol.pcep.ietf.stateful07; +import static com.google.common.base.Preconditions.checkArgument; import static java.util.Objects.requireNonNull; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -21,7 +21,6 @@ import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; import org.opendaylight.protocol.pcep.spi.TlvParser; import org.opendaylight.protocol.pcep.spi.TlvSerializer; import org.opendaylight.protocol.pcep.spi.TlvUtil; -import org.opendaylight.protocol.util.ByteBufWriteUtil; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.path.binding.tlv.PathBinding; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.path.binding.tlv.PathBindingBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.path.binding.tlv.path.binding.BindingTypeValue; @@ -77,16 +76,13 @@ public final class PathBindingTlvParser implements TlvParser, TlvSerializer { @Override public void serializeTlv(final Tlv tlv, final ByteBuf buffer) { - Preconditions.checkArgument(tlv instanceof PathBinding, - "The TLV must be PathBinding type, but was %s", tlv.getClass()); + checkArgument(tlv instanceof PathBinding, "The TLV must be PathBinding type, but was %s", tlv.getClass()); final PathBinding pTlv = (PathBinding) tlv; final BindingTypeValue bindingTypeValue = pTlv.getBindingTypeValue(); - Preconditions.checkArgument(bindingTypeValue != null, - "Missing Binding Value in Path Bidning TLV: %s", pTlv); + checkArgument(bindingTypeValue != null, "Missing Binding Value in Path Bidning TLV: %s", pTlv); final ByteBuf body = Unpooled.buffer(MPLS_BINDING_LENGTH); final PathBindingTlvCodec codec = BT_SERIALIZERS.get(bindingTypeValue.implementedInterface()); - Preconditions.checkArgument(codec != null, - "Unsupported Path Binding Type: %s", bindingTypeValue.implementedInterface()); + checkArgument(codec != null, "Unsupported Path Binding Type: %s", bindingTypeValue.implementedInterface()); codec.writeBinding(body, bindingTypeValue); TlvUtil.formatTlv(TYPE, body, buffer); @@ -112,9 +108,9 @@ public final class PathBindingTlvParser implements TlvParser, TlvSerializer { @Override void writeEntry(final ByteBuf buf, final BindingTypeValue bindingValue) { - Preconditions.checkArgument(bindingValue instanceof MplsLabel, "bindingValue is not MplsLabel"); + checkArgument(bindingValue instanceof MplsLabel, "bindingValue is not MplsLabel"); final MplsLabel mplsLabel = (MplsLabel) bindingValue; - ByteBufWriteUtil.writeUnsignedInt(Uint32.valueOf(getMplsStackEntry(mplsLabel.getMplsLabel())), buf); + ByteBufUtils.write(buf, Uint32.valueOf(getMplsStackEntry(mplsLabel.getMplsLabel()))); } @Override @@ -130,14 +126,13 @@ public final class PathBindingTlvParser implements TlvParser, TlvSerializer { @Override void writeEntry(final ByteBuf buf, final BindingTypeValue bindingValue) { - Preconditions.checkArgument(bindingValue instanceof MplsLabelEntry, - "bindingValue is not MplsLabelEntry"); + checkArgument(bindingValue instanceof MplsLabelEntry, "bindingValue is not MplsLabelEntry"); final MplsLabelEntry mplsEntry = (MplsLabelEntry) bindingValue; final long entry = getMplsStackEntry(mplsEntry.getLabel()) | mplsEntry.getTrafficClass().toJava() << TC_SHIFT | (mplsEntry.isBottomOfStack() ? 1 : 0) << S_SHIFT | mplsEntry.getTimeToLive().toJava(); - ByteBufWriteUtil.writeUnsignedInt(Uint32.valueOf(entry), buf); + ByteBufUtils.write(buf, Uint32.valueOf(entry)); } @Override diff --git a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv4TlvParser.java b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv4TlvParser.java index 25c7664719..04a177b1aa 100644 --- a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv4TlvParser.java +++ b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv4TlvParser.java @@ -9,8 +9,6 @@ package org.opendaylight.protocol.pcep.ietf.stateful07; import static com.google.common.base.Preconditions.checkArgument; import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address; -import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeShort; -import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; @@ -49,14 +47,18 @@ public final class Stateful07LSPIdentifierIpv4TlvParser implements TlvParser, Tl } checkArgument(buffer.readableBytes() == V4_LENGTH, "Length %s does not match LSP Identifiers Ipv4 tlv length.", buffer.readableBytes()); - final Ipv4Builder builder = new Ipv4Builder(); - builder.setIpv4TunnelSenderAddress(Ipv4Util.noZoneAddressForByteBuf(buffer)); + final Ipv4Builder builder = new Ipv4Builder() + .setIpv4TunnelSenderAddress(Ipv4Util.noZoneAddressForByteBuf(buffer)); final LspId lspId = new LspId(Uint32.valueOf(buffer.readUnsignedShort())); final TunnelId tunnelId = new TunnelId(ByteBufUtils.readUint16(buffer)); builder.setIpv4ExtendedTunnelId(new Ipv4ExtendedTunnelId(Ipv4Util.noZoneAddressForByteBuf(buffer))); builder.setIpv4TunnelEndpointAddress(Ipv4Util.noZoneAddressForByteBuf(buffer)); final AddressFamily afi = new Ipv4CaseBuilder().setIpv4(builder.build()).build(); - return new LspIdentifiersBuilder().setAddressFamily(afi).setLspId(lspId).setTunnelId(tunnelId).build(); + return new LspIdentifiersBuilder() + .setAddressFamily(afi) + .setLspId(lspId) + .setTunnelId(tunnelId) + .build(); } @Override @@ -72,9 +74,10 @@ public final class Stateful07LSPIdentifierIpv4TlvParser implements TlvParser, Tl checkArgument(ipv4.getIpv4TunnelSenderAddress() != null, "Ipv4TunnelSenderAddress is mandatory."); writeIpv4Address(ipv4.getIpv4TunnelSenderAddress(), body); checkArgument(lsp.getLspId() != null, "LspId is mandatory."); - writeShort(lsp.getLspId().getValue().shortValue(), body); - checkArgument(lsp.getTunnelId() != null, "TunnelId is mandatory."); - writeUnsignedShort(lsp.getTunnelId().getValue(), body); + body.writeShort(lsp.getLspId().getValue().shortValue()); + final TunnelId tunnelId = lsp.getTunnelId(); + checkArgument(tunnelId != null, "TunnelId is mandatory."); + ByteBufUtils.write(body, tunnelId.getValue()); checkArgument(ipv4.getIpv4ExtendedTunnelId() != null, "Ipv4ExtendedTunnelId is mandatory."); writeIpv4Address(ipv4.getIpv4ExtendedTunnelId(), body); checkArgument(ipv4.getIpv4TunnelEndpointAddress() != null, "Ipv4TunnelEndpointAddress is mandatory."); diff --git a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv6TlvParser.java b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv6TlvParser.java index fc64d8c373..007b270f51 100644 --- a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv6TlvParser.java +++ b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv6TlvParser.java @@ -9,8 +9,6 @@ package org.opendaylight.protocol.pcep.ietf.stateful07; import static com.google.common.base.Preconditions.checkArgument; import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address; -import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeShort; -import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; @@ -66,10 +64,13 @@ public final class Stateful07LSPIdentifierIpv6TlvParser implements TlvParser, Tl final Ipv6 ipv6 = ((Ipv6Case) lsp.getAddressFamily()).getIpv6(); checkArgument(ipv6.getIpv6TunnelSenderAddress() != null, "Ipv6TunnelSenderAddress is mandatory."); writeIpv6Address(ipv6.getIpv6TunnelSenderAddress(), body); - checkArgument(lsp.getLspId() != null, "LspId is mandatory."); - writeShort(lsp.getLspId().getValue().shortValue(), body); - checkArgument(lsp.getTunnelId() != null, "TunnelId is mandatory."); - writeUnsignedShort(lsp.getTunnelId().getValue(), body); + + final LspId lspId = lsp.getLspId(); + checkArgument(lspId != null, "LspId is mandatory."); + body.writeShort(lspId.getValue().shortValue()); + final TunnelId tunnelId = lsp.getTunnelId(); + checkArgument(tunnelId != null, "TunnelId is mandatory."); + ByteBufUtils.write(body, tunnelId.getValue()); checkArgument(ipv6.getIpv6ExtendedTunnelId() != null, "Ipv6ExtendedTunnelId is mandatory."); writeIpv6Address(ipv6.getIpv6ExtendedTunnelId(), body); checkArgument(ipv6.getIpv6TunnelEndpointAddress() != null, "Ipv6TunnelEndpointAddress is mandatory."); diff --git a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java index db07363f20..dc31906b2f 100644 --- a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java +++ b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java @@ -7,9 +7,8 @@ */ package org.opendaylight.protocol.pcep.ietf.stateful07; -import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt; +import static com.google.common.base.Preconditions.checkArgument; -import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; @@ -39,9 +38,9 @@ public final class Stateful07LspUpdateErrorTlvParser implements TlvParser, TlvSe @Override public void serializeTlv(final Tlv tlv, final ByteBuf buffer) { - Preconditions.checkArgument(tlv instanceof LspErrorCode, "LspErrorCodeTlv is mandatory."); + checkArgument(tlv instanceof LspErrorCode, "LspErrorCodeTlv is mandatory."); final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH); - writeUnsignedInt(((LspErrorCode) tlv).getErrorCode(), body); + ByteBufUtils.writeOrZero(body, ((LspErrorCode) tlv).getErrorCode()); TlvUtil.formatTlv(TYPE, body, buffer); } } diff --git a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07RSVPErrorSpecTlvParser.java b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07RSVPErrorSpecTlvParser.java index 46f010927c..c88b2b994c 100644 --- a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07RSVPErrorSpecTlvParser.java +++ b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07RSVPErrorSpecTlvParser.java @@ -7,13 +7,10 @@ */ package org.opendaylight.protocol.pcep.ietf.stateful07; +import static com.google.common.base.Preconditions.checkArgument; import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address; import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address; -import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte; -import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt; -import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort; -import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.nio.charset.StandardCharsets; @@ -81,7 +78,7 @@ public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSer @Override public void serializeTlv(final Tlv tlv, final ByteBuf buffer) { - Preconditions.checkArgument(tlv instanceof RsvpErrorSpec, "RSVPErrorSpecTlv is mandatory."); + checkArgument(tlv instanceof RsvpErrorSpec, "RSVPErrorSpecTlv is mandatory."); final RsvpErrorSpec rsvp = (RsvpErrorSpec) tlv; final ByteBuf body = Unpooled.buffer(); if (rsvp.getErrorType().implementedInterface().equals(RsvpCase.class)) { @@ -96,8 +93,8 @@ public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSer } private static UserCase parseUserError(final ByteBuf buffer) { - final UserErrorBuilder error = new UserErrorBuilder(); - error.setEnterprise(new EnterpriseNumber(ByteBufUtils.readUint32(buffer))); + final UserErrorBuilder error = new UserErrorBuilder() + .setEnterprise(new EnterpriseNumber(ByteBufUtils.readUint32(buffer))); error.setSubOrg(ByteBufUtils.readUint8(buffer)); final int errDescrLength = buffer.readUnsignedByte(); error.setValue(ByteBufUtils.readUint16(buffer)); @@ -107,15 +104,15 @@ public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSer } private static void serializerUserError(final UserError ue, final ByteBuf body) { - final byte[] desc = ue.getDescription() == null ? new byte[0] - : ue.getDescription().getBytes(StandardCharsets.UTF_8); + final String description = ue.getDescription(); + final byte[] desc = description == null ? new byte[0] : description.getBytes(StandardCharsets.UTF_8); final ByteBuf userErrorBuf = Unpooled.buffer(); - Preconditions.checkArgument(ue.getEnterprise() != null, "EnterpriseNumber is mandatory"); - writeUnsignedInt(ue.getEnterprise().getValue(), userErrorBuf); - writeUnsignedByte(ue.getSubOrg(), userErrorBuf); + final EnterpriseNumber enterprise = ue.getEnterprise(); + checkArgument(enterprise != null, "EnterpriseNumber is mandatory"); + ByteBufUtils.write(userErrorBuf, enterprise.getValue()); + ByteBufUtils.writeOrZero(userErrorBuf, ue.getSubOrg()); userErrorBuf.writeByte(desc.length); - Preconditions.checkArgument(ue.getValue() != null, "Value is mandatory."); - writeUnsignedShort(ue.getValue(), userErrorBuf); + ByteBufUtils.writeMandatory(userErrorBuf, ue.getValue(), "Value"); userErrorBuf.writeBytes(desc); userErrorBuf.writeZero(TlvUtil.getPadding(desc.length, TlvUtil.PADDED_TO)); formatRSVPObject(USER_ERROR_CLASS_NUM, USER_ERROR_CLASS_TYPE, userErrorBuf, body); @@ -140,7 +137,7 @@ public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSer flags.set(IN_PLACE, rsvp.getFlags().isInPlace()); flags.set(NOT_GUILTY, rsvp.getFlags().isNotGuilty()); final IpAddressNoZone node = rsvp.getNode(); - Preconditions.checkArgument(node != null, "Node is mandatory."); + checkArgument(node != null, "Node is mandatory."); final ByteBuf rsvpObjBuf = Unpooled.buffer(); int type = 0; if (node.getIpv4AddressNoZone() != null) { @@ -151,10 +148,8 @@ public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSer writeIpv6Address(node.getIpv6AddressNoZone(), rsvpObjBuf); } flags.toByteBuf(rsvpObjBuf); - Preconditions.checkArgument(rsvp.getCode() != null, "Code is mandatory."); - writeUnsignedByte(rsvp.getCode(), rsvpObjBuf); - Preconditions.checkArgument(rsvp.getValue() != null, "Value is mandatory."); - writeUnsignedShort(rsvp.getValue(), rsvpObjBuf); + ByteBufUtils.writeMandatory(rsvpObjBuf, rsvp.getCode(), "Code"); + ByteBufUtils.writeMandatory(rsvpObjBuf, rsvp.getValue(), "Value"); formatRSVPObject(RSVP_ERROR_CLASS_NUM, type, rsvpObjBuf, body); } diff --git a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07SrpObjectParser.java b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07SrpObjectParser.java index 36faaaa212..976db9b2f8 100644 --- a/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07SrpObjectParser.java +++ b/pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07SrpObjectParser.java @@ -8,7 +8,6 @@ package org.opendaylight.protocol.pcep.ietf.stateful07; import static com.google.common.base.Preconditions.checkArgument; -import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; @@ -86,7 +85,7 @@ public class Stateful07SrpObjectParser extends AbstractObjectWithTlvsParser