Eliminate IpAddressUtil.bytesWOLengthFor() 32/111732/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 19 May 2024 17:11:52 +0000 (19:11 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 19 May 2024 17:12:57 +0000 (19:12 +0200)
This method is only used by mvpn, hence AbstractMpvnNlri is a better
place for it. While we are at it, eliminate a ByteBuf allocation.

Change-Id: I523b589fce08b06d9b8bb6a883efcfda747a16f4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/IpAddressUtil.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/IntraAsIPmsiADHandler.java
bgp/extensions/mvpn/src/main/java/org/opendaylight/protocol/bgp/mvpn/impl/nlri/LeafADHandler.java
bgp/extensions/mvpn/src/main/java/org/opendaylight/protocol/bgp/mvpn/impl/nlri/SPmsiADHandler.java

index 96573c29a7707b246dc9ed05dff7faf8577e12d2..5e2c4b5b64fa2dc3a0e5b7c7476c9d28b22e8602 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.bgp.concepts;
 
 import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.protocol.util.Ipv4Util;
 import org.opendaylight.protocol.util.Ipv6Util;
@@ -87,24 +86,6 @@ public final class IpAddressUtil {
         }
     }
 
-    /**
-     * Returns byte array containing IpAddress.
-     *
-     * @param address containing ipv4 or ipv6 address
-     * @return byte array
-     */
-    public static @NonNull ByteBuf bytesWOLengthFor(final IpAddressNoZone address) {
-        final ByteBuf body = Unpooled.buffer();
-        if (address.getIpv4AddressNoZone() != null) {
-            body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4AddressNoZone()));
-        } else if (address.getIpv6AddressNoZone() != null) {
-            body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6AddressNoZone()));
-        } else {
-            body.writeByte(0);
-        }
-        return body;
-    }
-
     public static IpAddressNoZone extractIpAddress(final DataContainerNode route, final NodeIdentifier rdNid) {
         final var rdNode = NormalizedNodes.findNode(route, rdNid).orElse(null);
         if (rdNode == null) {
index 9a1b1b09f438e33703d28c729c7becd82427f3b9..7461e71114677aead29cc2c10f67695b49f8bf22 100644 (file)
@@ -16,6 +16,9 @@ import org.opendaylight.bgp.concepts.IpAddressUtil;
 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
 import org.opendaylight.protocol.bgp.mvpn.spi.nlri.MvpnParser;
 import org.opendaylight.protocol.bgp.mvpn.spi.nlri.MvpnSerializer;
+import org.opendaylight.protocol.util.Ipv4Util;
+import org.opendaylight.protocol.util.Ipv6Util;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.MulticastSourceRdGrouping;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.NlriType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.mvpn.MvpnChoice;
@@ -70,4 +73,10 @@ public abstract class AbstractMvpnNlri<T extends MvpnChoice> implements MvpnSeri
         RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), output);
         IpAddressUtil.writeBytesFor(route.getMulticastSource(), output);
     }
+
+    static final void serializeAddress(final IpAddressNoZone address, final ByteBuf output) {
+        final var ipv4 = address.getIpv4AddressNoZone();
+        output.writeBytes(ipv4 != null ? Ipv4Util.bytesForAddress(ipv4)
+            : Ipv6Util.bytesForAddress(address.getIpv6AddressNoZone()));
+    }
 }
index 50edfc3d1e59368d8c84aa4f0fa964bf5e6c8109..7e5d5f1bb150c8104a397096ed89ea6af508d96c 100644 (file)
@@ -50,9 +50,7 @@ public final class IntraAsIPmsiADHandler extends AbstractMvpnNlri<IntraAsIPmsiAD
         final IntraAsIPmsiAD route = mvpn.getIntraAsIPmsiAD();
         final ByteBuf nlriByteBuf = Unpooled.buffer();
         RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), nlriByteBuf);
-        final ByteBuf orig = IpAddressUtil.bytesWOLengthFor(route.getOrigRouteIp());
-        checkArgument(orig.readableBytes() > 0);
-        nlriByteBuf.writeBytes(orig);
+        serializeAddress(route.getOrigRouteIp(), nlriByteBuf);
         return nlriByteBuf;
     }
 }
index 17e9b4bc9e025808cb3c9642bd6f9ff5d955dd2c..b338578b1d4055d2608e5c12a3d6f799f09f0fef 100644 (file)
@@ -7,8 +7,6 @@
  */
 package org.opendaylight.protocol.bgp.mvpn.impl.nlri;
 
-import static com.google.common.base.Preconditions.checkArgument;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import org.opendaylight.bgp.concepts.IpAddressUtil;
@@ -70,9 +68,7 @@ public final class LeafADHandler extends AbstractMvpnNlri<LeafADCase> {
                     .mvpn.choice.SPmsiADCaseBuilder((SPmsiADCase) key).build();
         }
         nlriByteBuf.writeBytes(SimpleMvpnNlriRegistry.getInstance().serializeMvpn(keyCase));
-        final ByteBuf orig = IpAddressUtil.bytesWOLengthFor(leaf.getOrigRouteIp());
-        checkArgument(orig.readableBytes() > 0);
-        nlriByteBuf.writeBytes(orig);
+        serializeAddress(leaf.getOrigRouteIp(), nlriByteBuf);
         return nlriByteBuf;
     }
 }
index eab83c67c211d173ab71f0de9511c2c9909b44b3..c2fb08db1146a35c3ba09a098b71f9eb6d3ae6a9 100644 (file)
@@ -42,7 +42,7 @@ public final class SPmsiADHandler extends AbstractMvpnNlri<SPmsiADCase> {
         final ByteBuf nlriByteBuf = Unpooled.buffer();
         serializeRDMulticastSource(route, nlriByteBuf);
         MulticastGroupOpaqueUtil.bytesForMulticastGroup(route.getMulticastGroup(), nlriByteBuf);
-        nlriByteBuf.writeBytes(IpAddressUtil.bytesWOLengthFor(route.getOrigRouteIp()));
+        serializeAddress(route.getOrigRouteIp(), nlriByteBuf);
         return nlriByteBuf;
     }
 }