Remove IpAddress handler duplicated code 08/72008/2
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Tue, 15 May 2018 08:11:59 +0000 (10:11 +0200)
committerClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Tue, 15 May 2018 10:50:26 +0000 (12:50 +0200)
- create util for parse, serialize IpAddress

Change-Id: I4eec0ad9658c28b4ff238eb24266d9a2303fb9f0
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/IpAddressUtil.java [new file with mode: 0644]
bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/EthSegRParser.java
bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/nlri/IncMultEthTagRParser.java

diff --git a/bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/IpAddressUtil.java b/bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/IpAddressUtil.java
new file mode 100644 (file)
index 0000000..82570c8
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.bgp.concepts;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import javax.annotation.Nonnull;
+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.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
+
+/**
+ * Utility class for IpAddress models type(like Originator Route Ip) serialization and parsing.
+ *
+ * @author Claudio D. Gasparini
+ */
+public final class IpAddressUtil {
+    private static final int ZERO_BYTE = 1;
+
+    private IpAddressUtil() {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Returns IpAddress from byte array containing ipAddress lenght + ipAddress.
+     *
+     * @param buffer containing ip address
+     * @return IpAddress
+     */
+    @Nonnull
+    public static IpAddress addressForByteBuf(@Nonnull final ByteBuf buffer) {
+        final int ipLength = buffer.readUnsignedByte();
+        if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
+            return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
+        } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
+            return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
+        }
+        throw new IllegalStateException("Unexpected size");
+    }
+
+    /**
+     * Returns IpAddress from byte array containing ipAddress based on ByteArray length.
+     *
+     * @param buffer containing ip address
+     * @return IpAddress
+     */
+    @Nonnull
+    public static IpAddress addressForByteBufWOLength(@Nonnull final ByteBuf buffer) {
+        final int ipLength = buffer.readableBytes();
+        if (ipLength == Ipv6Util.IPV6_LENGTH) {
+            return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
+        } else if (ipLength == Ipv4Util.IP4_LENGTH) {
+            return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
+        }
+        throw new IllegalStateException("Unexpected size");
+    }
+
+    /**
+     * Returns byte array containing IpAddress length and IpAddress.
+     *
+     * @param address containing ipv4 or ipv6 address
+     * @return byte array
+     */
+    @Nonnull
+    public static ByteBuf bytesFor(@Nonnull final IpAddress address) {
+        final ByteBuf body = Unpooled.buffer();
+        if (address.getIpv4Address() != null) {
+            body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
+            body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4Address()));
+        } else if (address.getIpv6Address() != null) {
+            body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
+            body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address()));
+        } else {
+            body.writeZero(ZERO_BYTE);
+        }
+        return body;
+    }
+
+    /**
+     * Returns byte array containing IpAddress.
+     *
+     * @param address containing ipv4 or ipv6 address
+     * @return byte array
+     */
+    @Nonnull
+    public static ByteBuf bytesWOLengthFor(@Nonnull final IpAddress address) {
+        final ByteBuf body = Unpooled.buffer();
+        if (address.getIpv4Address() != null) {
+            body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4Address()));
+        } else if (address.getIpv6Address() != null) {
+            body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address()));
+        } else {
+            body.writeZero(ZERO_BYTE);
+        }
+        return body;
+    }
+
+
+    public static IpAddress extractIpAddress(@Nonnull final DataContainerNode<?> route,
+            @Nonnull final NodeIdentifier rdNid) {
+        final NormalizedNode<?, ?> rdNode = NormalizedNodes.findNode(route, rdNid).orElse(null);
+        if (rdNode != null) {
+            return IpAddressBuilder.getDefaultInstance((String) rdNode.getValue());
+        }
+        return null;
+    }
+}
index 2423631bad861a5c4aa3288944c38566c13bc12e..063f8cb00993eac49117fa7bc2bc4bf399d315c8 100644 (file)
@@ -8,15 +8,13 @@
 
 package org.opendaylight.protocol.bgp.evpn.impl.nlri;
 
-import static java.util.Objects.requireNonNull;
 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractOrigRouteIp;
 
 import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
+import org.opendaylight.bgp.concepts.IpAddressUtil;
 import org.opendaylight.protocol.bgp.evpn.spi.pojo.SimpleEsiTypeRegistry;
-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.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.NlriType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.es.route.EsRoute;
@@ -40,7 +38,7 @@ final class EthSegRParser extends AbstractEvpnNlri {
             "Wrong length of array of bytes. Passed: %s ;", buffer);
 
         final Esi esi = SimpleEsiTypeRegistry.getInstance().parseEsi(buffer.readSlice(ESI_SIZE));
-        final IpAddress ip = requireNonNull(parseOrigRouteIp(buffer));
+        final IpAddress ip = IpAddressUtil.addressForByteBuf(buffer);
 
         final EsRouteBuilder builder = new EsRouteBuilder().setEsi(esi).setOrigRouteIp(ip);
         return new EsRouteCaseBuilder().setEsRoute(builder.build()).build();
@@ -58,7 +56,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 = serializeOrigRouteIp(evpn.getOrigRouteIp());
+        final ByteBuf orig = IpAddressUtil.bytesFor(evpn.getOrigRouteIp());
         Preconditions.checkArgument(orig.readableBytes() > 0);
         body.writeBytes(orig);
         return body;
@@ -76,28 +74,4 @@ final class EthSegRParser extends AbstractEvpnNlri {
         builder.setOrigRouteIp(extractOrigRouteIp(evpn));
         return new EsRouteCaseBuilder().setEsRoute(builder.build()).build();
     }
-
-    static IpAddress parseOrigRouteIp(final ByteBuf buffer) {
-        final int ipLength = buffer.readUnsignedByte();
-        if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
-            return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
-        } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
-            return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
-        }
-        return null;
-    }
-
-    static ByteBuf serializeOrigRouteIp(final IpAddress origRouteIp) {
-        final ByteBuf body = Unpooled.buffer();
-        if (origRouteIp.getIpv4Address() != null) {
-            body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
-            body.writeBytes(Ipv4Util.bytesForAddress(origRouteIp.getIpv4Address()));
-        } else if (origRouteIp.getIpv6Address() != null) {
-            body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
-            body.writeBytes(Ipv6Util.bytesForAddress(origRouteIp.getIpv6Address()));
-        } else {
-            body.writeZero(ZERO_BYTE);
-        }
-        return body;
-    }
 }
index 83f466d3a5b0d032e1260224caddfdafec6fc599..f6ca9d2bc4fddd97dfc95baaefac18d055065c12 100644 (file)
@@ -8,13 +8,13 @@
 
 package org.opendaylight.protocol.bgp.evpn.impl.nlri;
 
-import static java.util.Objects.requireNonNull;
 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractETI;
 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractOrigRouteIp;
 
 import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
+import org.opendaylight.bgp.concepts.IpAddressUtil;
 import org.opendaylight.protocol.util.ByteBufWriteUtil;
 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.evpn.rev180329.NlriType;
@@ -47,7 +47,7 @@ final class IncMultEthTagRParser extends AbstractEvpnNlri {
                 "Wrong length of array of bytes. Passed: %s ;", buffer);
 
         final EthernetTagId eti = new EthernetTagIdBuilder().setVlanId(buffer.readUnsignedInt()).build();
-        IpAddress ip = requireNonNull(EthSegRParser.parseOrigRouteIp(buffer));
+        IpAddress ip = IpAddressUtil.addressForByteBuf(buffer);
         final IncMultiEthernetTagResBuilder builder = new IncMultiEthernetTagResBuilder()
                 .setEthernetTagId(eti).setOrigRouteIp(ip);
         return new IncMultiEthernetTagResCaseBuilder().setIncMultiEthernetTagRes(builder.build()).build();
@@ -67,7 +67,7 @@ final class IncMultEthTagRParser extends AbstractEvpnNlri {
         final IncMultiEthernetTagRes evpn = ((IncMultiEthernetTagResCase) evpnChoice).getIncMultiEthernetTagRes();
         final ByteBuf body = Unpooled.buffer();
         ByteBufWriteUtil.writeUnsignedInt(evpn.getEthernetTagId().getVlanId(), body);
-        final ByteBuf orig = EthSegRParser.serializeOrigRouteIp(evpn.getOrigRouteIp());
+        final ByteBuf orig = IpAddressUtil.bytesFor(evpn.getOrigRouteIp());
         Preconditions.checkArgument(orig.readableBytes() > 0);
         body.writeBytes(orig);
         return body;