Eliminate IpAddressUtil.bytesWOLengthFor()
[bgpcep.git] / bgp / extensions / mvpn / src / main / java / org / opendaylight / protocol / bgp / mvpn / impl / nlri / AbstractMvpnNlri.java
index 9c8b054469e45617ab92ff8d62185c537e20ac2f..7461e71114677aead29cc2c10f67695b49f8bf22 100644 (file)
@@ -5,48 +5,78 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.bgp.mvpn.impl.nlri;
 
+import static java.util.Objects.requireNonNull;
+
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
+import org.eclipse.jdt.annotation.NonNull;
 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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.MulticastSourceRdGrouping;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.mvpn.MvpnChoice;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.s.pmsi.a.d.grouping.SPmsiADBuilder;
+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;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev200120.s.pmsi.a.d.grouping.SPmsiADBuilder;
 
 /**
  * Abstract Mvpn Nlri.
  *
  * @author Claudio D. Gasparini
  */
-abstract class AbstractMvpnNlri<T extends MvpnChoice> implements MvpnSerializer<T>, MvpnParser<T> {
-    static MulticastSourceRdGrouping parseRDMulticastSource(final ByteBuf buffer) {
-        final SPmsiADBuilder builder = new SPmsiADBuilder();
-        builder.setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(buffer));
-        final IpAddress address = IpAddressUtil.addressForByteBuf(buffer);
-        builder.setMulticastSource(address);
-        return builder.build();
+public abstract class AbstractMvpnNlri<T extends MvpnChoice> implements MvpnSerializer<T>, MvpnParser<T> {
+    private final @NonNull Class<T> choice;
+    private final @NonNull NlriType type;
+    private final int intType;
+
+    protected AbstractMvpnNlri(final Class<T> choice, final NlriType type) {
+        this.choice = requireNonNull(choice);
+        this.type = requireNonNull(type);
+        intType = type.getIntValue();
     }
 
-    static void serializeRDMulticastSource(final MulticastSourceRdGrouping route, final ByteBuf output) {
-        RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), output);
-        output.writeBytes(IpAddressUtil.bytesFor(route.getMulticastSource()));
+    @Override
+    public final Class<T> getClazz() {
+        return choice;
+    }
+
+    @Override
+    public final NlriType getType() {
+        return type;
     }
 
     @Override
     public final ByteBuf serializeMvpn(final T mvpn) {
         final ByteBuf output = Unpooled.buffer();
         final ByteBuf body = serializeBody(mvpn);
-        output.writeByte(getType());
+        output.writeByte(intType);
         output.writeByte(body.readableBytes());
         output.writeBytes(body);
         return output;
     }
 
     protected abstract ByteBuf serializeBody(T mvpn);
+
+    static final MulticastSourceRdGrouping parseRDMulticastSource(final ByteBuf buffer) {
+        return new SPmsiADBuilder()
+            .setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(buffer))
+            .setMulticastSource(IpAddressUtil.addressForByteBuf(buffer))
+            .build();
+    }
+
+    static final void serializeRDMulticastSource(final MulticastSourceRdGrouping route, final ByteBuf output) {
+        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()));
+    }
 }