Refactor IpAddressUtil.bytesFor() 31/111731/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 19 May 2024 16:43:40 +0000 (18:43 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 19 May 2024 16:45:07 +0000 (18:45 +0200)
The case where both components are null is impossible, hence we can
eliminate assertions. That also means all users are immediately copying
the buffer into another buffer -- hence we name the new method
writeBytesFor().

Change-Id: I7595e40b4b486ec6a65d69a6059beed93b6d5e8c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/IpAddressUtil.java
bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/EthSegRParser.java
bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/IncMultEthTagRParser.java
bgp/extensions/mvpn/src/main/java/org/opendaylight/protocol/bgp/mvpn/impl/nlri/AbstractMvpnNlri.java
bgp/extensions/mvpn/src/main/java/org/opendaylight/protocol/bgp/mvpn/impl/nlri/CMulticastUtil.java
bgp/extensions/mvpn/src/main/java/org/opendaylight/protocol/bgp/mvpn/impl/nlri/MulticastGroupOpaqueUtil.java
bgp/extensions/mvpn/src/main/java/org/opendaylight/protocol/bgp/mvpn/impl/nlri/SourceActiveADHandler.java

index 5c9d52cecd5131d2be0dc89c5378c723ca1e199b..96573c29a7707b246dc9ed05dff7faf8577e12d2 100644 (file)
@@ -66,23 +66,25 @@ public final class IpAddressUtil {
     }
 
     /**
-     * Returns byte array containing IpAddress length and IpAddress.
+     * Writes the length of an {@link IpAddressNoZone} and its contents to specified buffer.
      *
      * @param address containing ipv4 or ipv6 address
-     * @return byte array
+     * @param output output buffer
      */
-    public static @NonNull ByteBuf bytesFor(final IpAddressNoZone address) {
-        final ByteBuf body = Unpooled.buffer();
-        if (address.getIpv4AddressNoZone() != null) {
-            body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
-            body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4AddressNoZone()));
-        } else if (address.getIpv6AddressNoZone() != null) {
-            body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
-            body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6AddressNoZone()));
+    public static void writeBytesFor(final IpAddressNoZone address, final ByteBuf output) {
+        final var ipv4 = address.getIpv4AddressNoZone();
+        if (ipv4 != null) {
+            output.writeByte(Ipv4Util.IP4_BITS_LENGTH);
+            output.writeBytes(Ipv4Util.bytesForAddress(ipv4));
         } else {
-            body.writeByte(0);
+            final var ipv6 = address.getIpv6AddressNoZone();
+            if (ipv6 != null) {
+                output.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
+                output.writeBytes(Ipv6Util.bytesForAddress(ipv6));
+            } else {
+                throw new IllegalStateException("Invalid address " + address);
+            }
         }
-        return body;
     }
 
     /**
index 00e62477dbabfd33238bca3977ae96c1415fb555..d4e6ce2e52a3bc13196e5047524cb2ba9c286de2 100644 (file)
@@ -54,9 +54,7 @@ final class EthSegRParser extends AbstractEvpnNlri {
         final EsRoute evpn = ((EsRouteCase) evpnInput).getEsRoute();
         final ByteBuf body = Unpooled.buffer();
         SimpleEsiTypeRegistry.getInstance().serializeEsi(evpn.getEsi(), body);
-        final ByteBuf orig = IpAddressUtil.bytesFor(evpn.getOrigRouteIp());
-        checkArgument(orig.readableBytes() > 0);
-        body.writeBytes(orig);
+        IpAddressUtil.writeBytesFor(evpn.getOrigRouteIp(), body);
         return body;
     }
 
index e3f01fed866bc580775522e277788e13ef6a830b..7b4d74066fbebb8e12839ee4b4e53f04391f1955 100644 (file)
@@ -66,9 +66,7 @@ final class IncMultEthTagRParser extends AbstractEvpnNlri {
         final IncMultiEthernetTagRes evpn = ((IncMultiEthernetTagResCase) evpnChoice).getIncMultiEthernetTagRes();
         final ByteBuf body = Unpooled.buffer();
         ByteBufUtils.writeOrZero(body, evpn.getEthernetTagId().getVlanId());
-        final ByteBuf orig = IpAddressUtil.bytesFor(evpn.getOrigRouteIp());
-        Preconditions.checkArgument(orig.readableBytes() > 0);
-        body.writeBytes(orig);
+        IpAddressUtil.writeBytesFor(evpn.getOrigRouteIp(), body);
         return body;
     }
 
index 8528c0b9f36f63703f257a8ab7ab8d5d0b1badf1..9a1b1b09f438e33703d28c729c7becd82427f3b9 100644 (file)
@@ -68,6 +68,6 @@ public abstract class AbstractMvpnNlri<T extends MvpnChoice> implements MvpnSeri
 
     static final void serializeRDMulticastSource(final MulticastSourceRdGrouping route, final ByteBuf output) {
         RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), output);
-        output.writeBytes(IpAddressUtil.bytesFor(route.getMulticastSource()));
+        IpAddressUtil.writeBytesFor(route.getMulticastSource(), output);
     }
 }
index 26ef5469f19360048605c2c06bdd272be82bd113..2fbb7252a322a39126b37bed661f597088f10b11 100644 (file)
@@ -39,7 +39,7 @@ final class CMulticastUtil {
         final ByteBuf nlriByteBuf = Unpooled.buffer();
         RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), nlriByteBuf);
         nlriByteBuf.writeInt(route.getSourceAs().getValue().intValue());
-        nlriByteBuf.writeBytes(IpAddressUtil.bytesFor(route.getMulticastSource()));
+        IpAddressUtil.writeBytesFor(route.getMulticastSource(), nlriByteBuf);
         MulticastGroupOpaqueUtil.bytesForMulticastGroup(route.getMulticastGroup(), nlriByteBuf);
         return nlriByteBuf;
     }
index db04121bcef36d1bdec2e7a3c07f208b386cbc47..f11e1e44c22b73ae34ab4c72156de760a9c07137 100644 (file)
@@ -32,23 +32,22 @@ final class MulticastGroupOpaqueUtil {
     }
 
     static MulticastGroup multicastGroupForByteBuf(final ByteBuf buffer) {
-        final short multicastGroupLength = buffer.readUnsignedByte();
-        switch (multicastGroupLength) {
-            case Ipv4Util.IP4_BITS_LENGTH:
-                return new CGAddressCaseBuilder()
-                    .setCGAddress(new IpAddressNoZone(Ipv4Util.addressForByteBuf(buffer))).build();
-            case Ipv6Util.IPV6_BITS_LENGTH:
-                return new CGAddressCaseBuilder()
-                    .setCGAddress(new IpAddressNoZone(Ipv6Util.addressForByteBuf(buffer))).build();
-            default:
-                return new LdpMpOpaqueValueCaseBuilder()
-                    .setLdpMpOpaqueValue(new LdpMpOpaqueValueBuilder(OpaqueUtil.parseOpaque(buffer)).build()).build();
-        }
+        return switch (buffer.readUnsignedByte()) {
+            case Ipv4Util.IP4_BITS_LENGTH -> new CGAddressCaseBuilder()
+                .setCGAddress(new IpAddressNoZone(Ipv4Util.addressForByteBuf(buffer)))
+                .build();
+            case Ipv6Util.IPV6_BITS_LENGTH -> new CGAddressCaseBuilder()
+                .setCGAddress(new IpAddressNoZone(Ipv6Util.addressForByteBuf(buffer)))
+                .build();
+            default -> new LdpMpOpaqueValueCaseBuilder()
+                .setLdpMpOpaqueValue(new LdpMpOpaqueValueBuilder(OpaqueUtil.parseOpaque(buffer)).build())
+                .build();
+        };
     }
 
     static void bytesForMulticastGroup(final @NonNull MulticastGroup group, final ByteBuf nlriByteBuf) {
-        if (group instanceof CGAddressCase) {
-            nlriByteBuf.writeBytes(IpAddressUtil.bytesFor(((CGAddressCase) group).getCGAddress()));
+        if (group instanceof CGAddressCase cgAddress) {
+            IpAddressUtil.writeBytesFor(cgAddress.getCGAddress(), nlriByteBuf);
         } else {
             OpaqueUtil.serializeOpaque(((LdpMpOpaqueValueCase) group).getLdpMpOpaqueValue(), nlriByteBuf);
         }
index fa821a6c2c95f6ead974746ebe191f29952744e2..e6f46e70e28ccfdf5bbf581ad103f8384ea2db58 100644 (file)
@@ -40,7 +40,7 @@ public final class SourceActiveADHandler extends AbstractMvpnNlri<SourceActiveAD
         final SourceActiveAD route = mvpn.getSourceActiveAD();
         final ByteBuf nlriByteBuf = Unpooled.buffer();
         serializeRDMulticastSource(route, nlriByteBuf);
-        nlriByteBuf.writeBytes(IpAddressUtil.bytesFor(route.getMulticastGroup()));
+        IpAddressUtil.writeBytesFor(route.getMulticastGroup(), nlriByteBuf);
         return nlriByteBuf;
     }
 }