Migrate pcep-ietf-stateful07 to use ByteBufUtils 40/86740/4
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 6 Jan 2020 14:26:06 +0000 (15:26 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 6 Jan 2020 15:53:05 +0000 (16:53 +0100)
Most of the parser interactions can be reformulated in terms
of ByteBufUtils, do that.

Change-Id: I7576c9de5649509172c91c6904478fe4be0cf154
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/PathBindingTlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv4TlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv6TlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LspUpdateErrorTlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07RSVPErrorSpecTlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07SrpObjectParser.java

index 18766427909cadc90397eb5361a06e094c584b92..29835f778fd672ec68c875e06298959fc4c24ab3 100644 (file)
@@ -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
index 25c7664719ba38603180e6f7a2b8e388c3158106..04a177b1aa0ad3b1922534b85d63b7b6649a7a91 100644 (file)
@@ -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.");
index fc64d8c3736ba46ec79d0adfee428a8cd427f3f4..007b270f511980dda0b00565557e32b34c03680d 100644 (file)
@@ -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.");
index db07363f20a75b363650fcb9bc1b8b8062803a29..dc31906b2f76e3b27df92200f8f146efec89d690 100644 (file)
@@ -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);
     }
 }
index 46f010927cac389324d60bd273bb1c0f7a92d680..c88b2b994c1e5be86310f110698fc4ed1f9fac77 100644 (file)
@@ -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);
     }
 
index 36faaaa212833210884a72b28cac7e3bd3452bc5..976db9b2f810afb328c0e77e5a9999fbf24cedb9 100644 (file)
@@ -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<Tlvs
         serializeFlags(srp, body);
         final SrpIdNumber srpId = srp.getOperationId();
         checkArgument(srpId != null, "SrpId is mandatory.");
-        writeUnsignedInt(srpId.getValue(), body);
+        ByteBufUtils.write(body, srpId.getValue());
         serializeTlvs(srp.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
@@ -96,14 +95,13 @@ public class Stateful07SrpObjectParser extends AbstractObjectWithTlvsParser<Tlvs
     }
 
     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
-        if (tlvs == null) {
-            return;
-        }
-        if (tlvs.getSymbolicPathName() != null) {
-            serializeTlv(tlvs.getSymbolicPathName(), body);
-        }
-        if (tlvs.getPathSetupType() != null) {
-            serializeTlv(tlvs.getPathSetupType(), body);
+        if (tlvs != null) {
+            if (tlvs.getSymbolicPathName() != null) {
+                serializeTlv(tlvs.getSymbolicPathName(), body);
+            }
+            if (tlvs.getPathSetupType() != null) {
+                serializeTlv(tlvs.getPathSetupType(), body);
+            }
         }
     }