From: Robert Varga Date: Mon, 15 May 2023 23:29:28 +0000 (+0200) Subject: Move AbstractIetfInetUtil X-Git-Tag: v12.0.0~131 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=5fa83f265c3b3c532a5ec9d6bf147619a0db88f6;p=mdsal.git Move AbstractIetfInetUtil As a first step in eliminating IetfInetUtil, move AbstractIetfInetUtil and its tests. JIRA: MDSAL-826 Change-Id: If558e9fc17f801097b868e8bb5ff261b241c3a2e Signed-off-by: Robert Varga --- diff --git a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv4Utils.java b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv4Utils.java index 50b843eb90..891e740570 100644 --- a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv4Utils.java +++ b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv4Utils.java @@ -9,24 +9,22 @@ package org.opendaylight.mdsal.model.ietf.util; import static com.google.common.base.Preconditions.checkArgument; -import com.google.common.annotations.Beta; import org.eclipse.jdt.annotation.NonNull; /** - * IPv4 address parsing for ietf-inet-types ipv4-address. This is an internal implementation class, not meant to be - * exposed in any shape or form to the outside world, as the code relies on the fact that the strings presented to it - * have been previously validated to conform to the regular expressions defined in the YANG model. + * IPv4 address parsing for {@code ietf-inet-types} ipv4-address. This is an internal implementation class, not meant to + * be used directly in any shape or form to the outside world, as the code relies on the fact that the strings presented + * to it have been previously validated to conform to the regular expressions defined in the YANG model. */ -@Beta public final class Ipv4Utils { - static final int INET4_LENGTH = 4; + public static final int INET4_LENGTH = 4; private Ipv4Utils() { // Hidden on purpose } - static void fillIpv4Bytes(final byte @NonNull[] bytes, final int byteStart, final String str, final int strStart, - final int strLimit) { + public static void fillIpv4Bytes(final byte @NonNull[] bytes, final int byteStart, final String str, + final int strStart, final int strLimit) { int out = byteStart; int val = 0; for (int i = strStart; i < strLimit; ++i) { @@ -73,7 +71,7 @@ public final class Ipv4Utils { return sb.toString(); } - static void appendIpv4String(final StringBuilder sb, final byte @NonNull[] bytes) { + public static void appendIpv4String(final StringBuilder sb, final byte @NonNull[] bytes) { checkArgument(bytes.length == INET4_LENGTH, "IPv4 address length is 4 bytes"); sb.append(Byte.toUnsignedInt(bytes[0])); diff --git a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv6Utils.java b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv6Utils.java index 9d42d3a42f..8ffd0066cc 100644 --- a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv6Utils.java +++ b/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv6Utils.java @@ -10,44 +10,43 @@ package org.opendaylight.mdsal.model.ietf.util; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Verify.verify; -import com.google.common.annotations.Beta; import java.util.Arrays; import java.util.HexFormat; import org.eclipse.jdt.annotation.NonNull; /** - * IPv6 address parsing for ietf-inet-types ipv6-address and ipv6-prefix. This is an internal implementation - * class, not meant to be exposed in any shape or form to the outside world, as the code relies on the fact that - * the strings presented to it have been previously validated to conform to the regular expressions defined in - * the YANG model. - */ -/* - * v6 routines added by Anton Ivanov on 14.6.2015 - * revised by Robert Varga + * IPv6 address parsing for {@code ietf-inet-types} ipv6-address and ipv6-prefix. This is an internal implementation + * class, not meant to be used directly in any shape or form to the outside world, as the code relies on the fact that + * the strings presented to it have been previously validated to conform to the regular expressions defined in the YANG + * model. * - * BIG FAT WARNING!!! - * Read all of the following before you touch any v6 code or decide to - * optimize it by invoking a "simple" Guava call + *

+ * IPv6 routines added by Anton Ivanov on 14.6.2015, revised by Robert Varga * - * Java IPv6 is fundamentally broken and Google libraries do not fix it. - * 1. Java will allways implicitly rewrite v4 mapped into v6 as a v4 address - * and there is absolutely no way to override this behaviour - * 2. Guava libraries cannot parse non-canonical IPv6. They will throw an - * exception. Even if they did, they re-use the same broken java code - * underneath. + *

+ * BIG FAT WARNING!!! + * Read all of the following before you touch any v6 code or decide to optimize it by invoking a "simple" Guava call. * + *

+ * Java IPv6 is fundamentally broken and Google libraries do not fix it. + *

    + *
  1. Java will always implicitly rewrite v4 mapped into v6 as a v4 address and there is absolutely no way to + * override this behaviour
  2. + *
  3. Guava libraries cannot parse non-canonical IPv6. They will throw an exception. Even if they did, they re-use + * the same broken java code underneath
  4. + *
* This is why we have to parse v6 by ourselves. * - * The following conversion code is based on inet_cidr_pton_ipv6 in NetBSD + *

+ * The following conversion code is based on inet_cidr_pton_ipv6 in NetBSD. * - * The original BSD code is licensed under standard BSD license. While we - * are not obliged to provide an attribution, credit where credit is due. - * As far as why it is similar to Sun's sun.net.util please ask Sun why - * their code has the same variable names, comments and code flow. + *

+ * The original BSD code is licensed under standard BSD license. While we are not obliged to provide an attribution, + * credit where credit is due. As far as why it is similar to Sun's sun.net.util please ask Sun why their code has the + * same variable names, comments and code flow. */ -@Beta public final class Ipv6Utils { - static final int INET6_LENGTH = 16; + public static final int INET6_LENGTH = 16; private Ipv6Utils() { // Hidden on purpose @@ -62,7 +61,7 @@ public final class Ipv6Utils { * @throws NullPointerException if ipv6address is null */ @SuppressWarnings("checkstyle:localVariableName") - static void fillIpv6Bytes(final byte @NonNull[] bytes, final String str, final int strLimit) { + public static void fillIpv6Bytes(final byte @NonNull[] bytes, final String str, final int strLimit) { // Leading :: requires some special handling. int i = 0; if (str.charAt(i) == ':') { diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtilTest.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtilTest.java deleted file mode 100644 index 83c1d12e2d..0000000000 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtilTest.java +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright (c) 2016 Cisco Systems, Inc. and others. 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.mdsal.model.ietf.util; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertThrows; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; - -import com.google.common.net.InetAddresses; -import java.net.Inet4Address; -import java.net.Inet6Address; -import java.net.InetAddress; -import java.net.UnknownHostException; -import org.junit.Test; - -public class AbstractIetfInetUtilTest { - private static final IpUtil UTIL = new IpUtil(); - - private static void assertV4Equals(final String literal, final String append) { - final byte[] expected = InetAddresses.forString(literal).getAddress(); - final byte[] actual = UTIL.ipv4AddressBytes(new IpClass(literal + append)); - assertArrayEquals(expected, actual); - } - - private static void assertV4Equals(final String literal) { - assertV4Equals(literal, ""); - } - - @Test - public void testIpToBytesAndBack() { - assertV4Equals("1.2.3.4"); - assertV4Equals("12.23.34.45"); - assertV4Equals("255.254.253.252"); - assertV4Equals("128.16.0.127"); - - assertV4Equals("1.2.3.4", "%5"); - assertV4Equals("12.23.34.45", "%5"); - assertV4Equals("255.254.253.252", "%5"); - assertV4Equals("128.16.0.127", "%5"); - - assertEquals(new IpClass("1.2.3.4").getValue().toLowerCase(), - UTIL.ipAddressFor(UTIL.ipv4AddressBytes(new IpClass("1.2.3.4"))).getValue().toLowerCase()); - assertNotEquals(new IpClass("2.3.4.5").getValue().toLowerCase(), - UTIL.ipAddressFor(UTIL.ipv4AddressBytes(new IpClass("1.2.3.4"))).getValue().toLowerCase()); - - assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329").getValue().toLowerCase(), - UTIL.ipAddressFor( - UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase()); - assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329").getValue().toLowerCase(), - UTIL.ipAddressFor( - UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase()); - - assertEquals(new IpClass("1.2.3.4").getValue().toLowerCase(), - UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("1.2.3.4"))).getValue().toLowerCase()); - assertNotEquals(new IpClass("2.3.4.5").getValue().toLowerCase(), - UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("1.2.3.4"))).getValue().toLowerCase()); - - assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329").getValue().toLowerCase(), - UTIL.ipAddressFor( - UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase()); - assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329").getValue().toLowerCase(), - UTIL.ipAddressFor( - UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase()); - } - - @Test - public void illegalArrayLengthForAddressTest() { - final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> UTIL.ipAddressFor(new byte[] { 0, 0, 0 })); - assertEquals("Invalid array length 3", ex.getMessage()); - } - - @Test - public void unhandledAddressTest() { - final InetAddress adr = mock(InetAddress.class); - doReturn("testAddress").when(adr).toString(); - final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> UTIL.ipAddressFor(adr)); - assertEquals("Unhandled address testAddress", ex.getMessage()); - } - - @Test - public void illegalArrayLengthforPrefixTest() { - final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> UTIL.ipPrefixFor(new byte[] { 0, 0, 0 }, 0)); - assertEquals("Invalid array length 3", ex.getMessage()); - } - - @Test - public void illegalAddressforPrefixTest() { - final InetAddress adr = mock(InetAddress.class); - doReturn("testAddress").when(adr).toString(); - - final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> UTIL.ipPrefixFor(adr, 0)); - assertEquals("Unhandled address testAddress", ex.getMessage()); - } - - @Test - public void ipv4Tests() { - assertEquals("1.2.3.4", UTIL.ipv4AddressFrom(new IpClass("1.2.3.4/16")).getValue()); - final IpClass ipClass = new IpClass("1.2.3.4"); - assertEquals("1.2.3.4/32", UTIL.ipv4PrefixFor(UTIL.ipv4AddressBytes(ipClass)).getValue()); - assertEquals("1.2.3.4/32", UTIL.ipv4PrefixFor(UTIL.inetAddressFor(ipClass)).getValue()); - assertEquals("1.2.3.4/32", UTIL.ipv4PrefixFor(ipClass).getValue()); - assertEquals("1.2.3.4/16", UTIL.ipv4PrefixFor(ipClass, 16).getValue()); - - assertEquals("0.0.0.0/0", UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0).getValue()); - assertEquals("1.2.3.4/32", UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 32).getValue()); - assertEquals("0.0.0.0/0", UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 0).getValue()); - assertEquals("1.2.3.4/32", UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 32).getValue()); - assertEquals("2.3.4.5/32", UTIL.ipv4PrefixForShort(new byte[] { 1, 2, 3, 4, 5 }, 1, 32).getValue()); - assertEquals("1.0.0.0/1", UTIL.ipv4PrefixForShort(new byte[] { 1, 2, 3, 4, 5 }, 0, 1).getValue()); - - assertEquals("1.2.3.4", UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getKey().getValue()); - assertEquals((Integer) 16, UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getValue()); - assertArrayEquals(new byte[] { 1,2,3,4,16 }, UTIL.ipv4PrefixToBytes(new IpClass("1.2.3.4/16"))); - } - - @Test - public void ipv6Tests() { - assertEquals("::0", UTIL.ipv6AddressFrom(new IpClass("::0/128")).getValue()); - final IpClass ipClass = new IpClass("::0"); - assertEquals("::/128", UTIL.ipv6PrefixFor(UTIL.ipv6AddressBytes(ipClass)).getValue()); - assertEquals("::/128", UTIL.ipv6PrefixFor(UTIL.inetAddressFor(ipClass)).getValue()); - assertEquals("::0/128", UTIL.ipv6PrefixFor(ipClass).getValue()); - assertEquals("::0/16", UTIL.ipv6PrefixFor(ipClass, 16).getValue()); - - assertEquals("::0/0", UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0).getValue()); - assertEquals("::/64", UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 64).getValue()); - assertEquals("::0/0", UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 0).getValue()); - assertEquals("::/32", UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 32).getValue()); - - assertEquals("::", UTIL.splitIpv6Prefix(new IpClass("::/32")).getKey().getValue()); - assertEquals((Integer) 32, UTIL.splitIpv6Prefix(new IpClass("::/32")).getValue()); - assertArrayEquals(new byte[] { 0, 10, 0, 0, 0, 0, 0, 0, 0, 11, 0, 12, 0, 13, 0, 14, 64 }, - UTIL.ipv6PrefixToBytes(new IpClass("A::B:C:D:E/64"))); - - // verify that an IPv4-mapped IPv6 address gets parsed as an IPv6 address - assertEquals("::ffff:ab0:eb", UTIL.ipv6AddressFor( - new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xff, (byte) 0xff, 0x0a, (byte) 0xb0, 0, (byte) 0xeb}) - .getValue()); - } - - @Test - public void prefixTest() { - assertEquals("0.0.0.0/16", UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("0.0.0.0")), 16).getValue()); - assertEquals("::/64", UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("::")), 64).getValue()); - - assertEquals("0.0.0.0/16", UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0 }, 16).getValue()); - assertEquals("::/64", - UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 64).getValue()); - } - - @Test - public void inetAddressTest() { - assertThat(UTIL.inetAddressFor(new IpClass("1.2.3.4")), instanceOf(Inet4Address.class)); - assertThat(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")), instanceOf(Inet6Address.class)); - } - - @Test - public void inet4AddressForWithExceptionTest() { - final IpClass ipClass = mock(IpClass.class); - doReturn("testClass").when(ipClass).toString(); - doAnswer(inv -> { - throw new UnknownHostException(); - }).when(ipClass).getValue(); - - final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> UTIL.inet4AddressFor(ipClass)); - assertEquals("Invalid address testClass", ex.getMessage()); - } - - @Test - public void inet6AddressForWithExceptionTest() { - final IpClass ipClass = mock(IpClass.class); - doReturn("testClass").when(ipClass).toString(); - doAnswer(inv -> { - throw new UnknownHostException(); - }).when(ipClass).getValue(); - - final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> UTIL.inet6AddressFor(ipClass)); - assertEquals("Invalid address testClass", ex.getMessage()); - } - - @Test - public void testIpv4AddressForBits() { - assertEquals("1.2.3.4", UTIL.ipv4AddressFor(0x01020304).getValue()); - assertEquals("255.255.255.255", UTIL.ipv4AddressFor(0xFFFFFFFF).getValue()); - } - - @Test - public void testIpv4AddressBits() { - assertEquals(0x01020304, UTIL.ipv4AddressBits(new IpClass("1.2.3.4"))); - assertEquals(0xFFFFFFFF, UTIL.ipv4AddressBits(new IpClass("255.255.255.255"))); - } - - @Test - public void testIpv4AddressNoZoneBits() { - assertEquals(0x01020304, UTIL.ipv4AddressNoZoneBits(new IpClass("1.2.3.4"))); - assertEquals(0xFFFFFFFF, UTIL.ipv4AddressNoZoneBits(new IpClass("255.255.255.255"))); - } -} diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java deleted file mode 100644 index 3e9d64474a..0000000000 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2016 Cisco Systems, Inc. and others. 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.mdsal.model.ietf.util; - -import static java.util.Objects.requireNonNull; - -@SuppressWarnings("checkstyle:memberName") -public class IpClass { - private final String _value; - - public IpClass(final String value) { - this._value = requireNonNull(value); - } - - public IpClass(final IpClass template) { - this._value = template._value; - } - - String getValue() { - return this._value; - } -} \ No newline at end of file diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpUtil.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpUtil.java deleted file mode 100644 index e28e16fd4d..0000000000 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpUtil.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (c) 2016 Cisco Systems, Inc. and others. 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.mdsal.model.ietf.util; - -import java.util.regex.Pattern; -import org.checkerframework.checker.regex.qual.Regex; - -final class IpUtil extends AbstractIetfInetUtil { - - @Regex - private static final String IP_V4_REGEX = "^\\d+\\.\\d+\\.\\d+\\.\\d+$"; - private static final Pattern IP_V4_PATTERN = Pattern.compile(IP_V4_REGEX); - - IpUtil() { - super(IpClass.class, IpClass.class, IpClass.class, IpClass.class); - } - - @Override - protected IpClass ipv4Address(final IpClass addr) { - return addr; - } - - - @Override - protected IpClass ipv4AddressNoZone(final IpClass addr) { - return addr; - } - - @Override - protected IpClass ipv6Address(final IpClass addr) { - return addr; - } - - @Override - protected IpClass ipv6AddressNoZone(final IpClass addr) { - return addr; - } - - @Override - protected IpClass ipv4Prefix(final IpClass addr) { - return addr; - } - - @Override - protected IpClass ipv6Prefix(final IpClass addr) { - return addr; - } - - @Override - protected String ipv4AddressString(final IpClass addr) { - return addr.getValue(); - } - - @Override - protected String ipv6AddressString(final IpClass addr) { - return addr.getValue(); - } - - @Override - protected String ipv4PrefixString(final IpClass prefix) { - return prefix.getValue(); - } - - @Override - protected String ipv6PrefixString(final IpClass prefix) { - return prefix.getValue(); - } - - @Override - protected IpClass maybeIpv4Address(final IpClass addr) { - return IP_V4_PATTERN.matcher(addr.getValue()).matches() ? addr : null; - } - - @Override - protected IpClass maybeIpv4AddressNoZone(final IpClass addr) { - return maybeIpv4Address(addr); - } - - @Override - protected IpClass maybeIpv6Address(final IpClass addr) { - return addr.getValue().indexOf(':') != -1 ? addr : null; - } - - @Override - protected IpClass maybeIpv6AddressNoZone(final IpClass addr) { - return maybeIpv6Address(addr); - } -} \ No newline at end of file diff --git a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java b/model/ietf/rfc6991-ietf-inet-types/src/main/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev130715/AbstractIetfInetUtil.java similarity index 75% rename from model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java rename to model/ietf/rfc6991-ietf-inet-types/src/main/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev130715/AbstractIetfInetUtil.java index 1a1eeebe3c..9155fec79e 100644 --- a/model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java +++ b/model/ietf/rfc6991-ietf-inet-types/src/main/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev130715/AbstractIetfInetUtil.java @@ -5,7 +5,7 @@ * 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.mdsal.model.ietf.util; +package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715; import static com.google.common.base.Preconditions.checkArgument; import static java.util.Objects.requireNonNull; @@ -21,57 +21,51 @@ import java.util.Map.Entry; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.mdsal.binding.spec.reflect.StringValueObjectFactory; +import org.opendaylight.mdsal.model.ietf.util.Ipv4Utils; +import org.opendaylight.mdsal.model.ietf.util.Ipv6Utils; /** * A set of utility methods to efficiently instantiate various ietf-inet-types DTOs. */ @Beta @SuppressWarnings("checkstyle:classTypeParameterName") -public abstract class AbstractIetfInetUtil { - private final StringValueObjectFactory address4NoZoneFactory; - private final StringValueObjectFactory prefix4Factory; - private final StringValueObjectFactory address6NoZoneFactory; - private final StringValueObjectFactory prefix6Factory; - private final Class addr4nzClass; - private final Class addr6nzClass; +public abstract class AbstractIetfInetUtil { + private final StringValueObjectFactory address4NoZoneFactory = + StringValueObjectFactory.create(Ipv4AddressNoZone.class, "0.0.0.0"); + private final StringValueObjectFactory prefix4Factory = + StringValueObjectFactory.create(Ipv4Prefix.class, "0.0.0.0/0"); + private final StringValueObjectFactory address6NoZoneFactory = + StringValueObjectFactory.create(Ipv6AddressNoZone.class, "::0"); + private final StringValueObjectFactory prefix6Factory = + StringValueObjectFactory.create(Ipv6Prefix.class, "::0/0"); - protected AbstractIetfInetUtil(final Class addr4nzClass, final Class prefix4Class, - final Class addr6nzClass, final Class prefix6Class) { - this.addr4nzClass = requireNonNull(addr4nzClass); - this.addr6nzClass = requireNonNull(addr6nzClass); - this.address4NoZoneFactory = StringValueObjectFactory.create(addr4nzClass, "0.0.0.0"); - this.prefix4Factory = StringValueObjectFactory.create(prefix4Class, "0.0.0.0/0"); - this.address6NoZoneFactory = StringValueObjectFactory.create(addr6nzClass, "::0"); - this.prefix6Factory = StringValueObjectFactory.create(prefix6Class, "::0/0"); - } - - protected abstract @NonNull A ipv4Address(@NonNull A4NZ addr); + protected abstract @NonNull IpAddress ipv4Address(@NonNull Ipv4AddressNoZone addr); - protected abstract @NonNull ANZ ipv4AddressNoZone(@NonNull A4NZ addr); + protected abstract @NonNull IpAddressNoZone ipv4AddressNoZone(@NonNull Ipv4AddressNoZone addr); - protected abstract @NonNull A ipv6Address(@NonNull A6NZ addr); + protected abstract @NonNull IpAddress ipv6Address(@NonNull Ipv6AddressNoZone addr); - protected abstract @NonNull ANZ ipv6AddressNoZone(@NonNull A6NZ addr); + protected abstract @NonNull IpAddressNoZone ipv6AddressNoZone(@NonNull Ipv6AddressNoZone addr); - protected abstract @Nullable A4 maybeIpv4Address(@NonNull A addr); + protected abstract @Nullable Ipv4Address maybeIpv4Address(@NonNull IpAddress addr); - protected abstract @Nullable A4NZ maybeIpv4AddressNoZone(@NonNull ANZ addr); + protected abstract @Nullable Ipv4AddressNoZone maybeIpv4AddressNoZone(@NonNull IpAddressNoZone addr); - protected abstract @Nullable A6 maybeIpv6Address(@NonNull A addr); + protected abstract @Nullable Ipv6Address maybeIpv6Address(@NonNull IpAddress addr); - protected abstract @Nullable A6NZ maybeIpv6AddressNoZone(@NonNull ANZ addr); + protected abstract @Nullable Ipv6AddressNoZone maybeIpv6AddressNoZone(@NonNull IpAddressNoZone addr); - protected abstract @NonNull P ipv4Prefix(@NonNull P4 addr); + protected abstract @NonNull IpPrefix ipv4Prefix(@NonNull Ipv4Prefix addr); - protected abstract @NonNull P ipv6Prefix(@NonNull P6 addr); + protected abstract @NonNull IpPrefix ipv6Prefix(@NonNull Ipv6Prefix addr); - protected abstract @NonNull String ipv4AddressString(@NonNull A4 addr); + protected abstract @NonNull String ipv4AddressString(@NonNull Ipv4Address addr); - protected abstract @NonNull String ipv6AddressString(@NonNull A6 addr); + protected abstract @NonNull String ipv6AddressString(@NonNull Ipv6Address addr); - protected abstract @NonNull String ipv4PrefixString(@NonNull P4 prefix); + protected abstract @NonNull String ipv4PrefixString(@NonNull Ipv4Prefix prefix); - protected abstract @NonNull String ipv6PrefixString(@NonNull P6 prefix); + protected abstract @NonNull String ipv6PrefixString(@NonNull Ipv6Prefix prefix); /** * Create an IpAddress by interpreting input bytes as an IPv4 or IPv6 address, based on array length. @@ -81,7 +75,7 @@ public abstract class AbstractIetfInetUtil ipv4Address(ipv4AddressFor(bytes)); case Ipv6Utils.INET6_LENGTH -> ipv6Address(ipv6AddressFor(bytes)); @@ -89,7 +83,7 @@ public abstract class AbstractIetfInetUtil ipv4AddressNoZone(ipv4AddressFor(bytes)); case Ipv6Utils.INET6_LENGTH -> ipv6AddressNoZone(ipv6AddressFor(bytes)); @@ -120,7 +114,7 @@ public abstract class AbstractIetfInetUtil ipv4Prefix(ipv4PrefixFor(bytes, mask)); case Ipv6Utils.INET6_LENGTH -> ipv6Prefix(ipv6PrefixFor(bytes, mask)); @@ -159,7 +153,7 @@ public abstract class AbstractIetfInetUtil= 0 && mask <= 32, "Invalid mask %s", mask); return prefix4Factory.newInstance(addr + '/' + mask); } - public final @NonNull Entry splitIpv4Prefix(final @NonNull P4 prefix) { + public final @NonNull Entry splitIpv4Prefix(final @NonNull Ipv4Prefix prefix) { return splitPrefix(address4NoZoneFactory, ipv4PrefixString(prefix)); } - public final byte @NonNull[] ipv4PrefixToBytes(final @NonNull P4 prefix) { + public final byte @NonNull[] ipv4PrefixToBytes(final @NonNull Ipv4Prefix prefix) { final String str = ipv4PrefixString(prefix); final int slash = str.lastIndexOf('/'); @@ -423,7 +418,7 @@ public abstract class AbstractIetfInetUtil= 0 && mask <= 128, "Invalid mask %s", mask); return prefix6Factory.newInstance(addressStringV6(address) + '/' + mask); } @@ -509,7 +504,7 @@ public abstract class AbstractIetfInetUtil= 0 && mask <= 128, "Invalid mask %s", mask); return prefix6Factory.newInstance(addressStringV6(addr) + '/' + mask); } - public final @NonNull P6 ipv6PrefixFor(final @NonNull A6 addr) { + public final @NonNull Ipv6Prefix ipv6PrefixFor(final @NonNull Ipv6Address addr) { return prefix6Factory.newInstance(stripZone(ipv6AddressString(requireAddress(addr))) + "/128"); } - public final @NonNull P6 ipv6PrefixFor(final @NonNull A6 addr, final int mask) { + public final @NonNull Ipv6Prefix ipv6PrefixFor(final @NonNull Ipv6Address addr, final int mask) { return newIpv6Prefix(stripZone(ipv6AddressString(requireAddress(addr))), mask); } - public final @NonNull P6 ipv6PrefixForNoZone(final @NonNull A6NZ addr) { + public final @NonNull Ipv6Prefix ipv6PrefixForNoZone(final @NonNull Ipv6AddressNoZone addr) { return prefix6Factory.newInstance(ipv6AddressString(requireAddress(addr)) + "/128"); } - public final @NonNull P6 ipv6PrefixForNoZone(final @NonNull A6NZ addr, final int mask) { + public final @NonNull Ipv6Prefix ipv6PrefixForNoZone(final @NonNull Ipv6AddressNoZone addr, final int mask) { return newIpv6Prefix(ipv6AddressString(requireAddress(addr)), mask); } - public final @NonNull P6 ipv6PrefixForShort(final byte @NonNull[] address, final int mask) { + public final @NonNull Ipv6Prefix ipv6PrefixForShort(final byte @NonNull[] address, final int mask) { return ipv6PrefixForShort(address, 0, mask); } - public final @NonNull P6 ipv6PrefixForShort(final byte @NonNull[] array, final int startOffset, final int mask) { + public final @NonNull Ipv6Prefix ipv6PrefixForShort(final byte @NonNull[] array, final int startOffset, + final int mask) { if (mask == 0) { // Easy case, reuse the template return prefix6Factory.getTemplate(); @@ -564,12 +560,12 @@ public abstract class AbstractIetfInetUtil= 0 && mask <= 128, "Invalid mask %s", mask); return prefix6Factory.newInstance(addr + '/' + mask); } - public final @NonNull Entry splitIpv6Prefix(final @NonNull P6 prefix) { + public final @NonNull Entry splitIpv6Prefix(final @NonNull Ipv6Prefix prefix) { return splitPrefix(address6NoZoneFactory, ipv6PrefixString(prefix)); } @@ -584,7 +580,7 @@ public abstract class AbstractIetfInetUtil { +public final class IetfInetUtil extends AbstractIetfInetUtil { public static final @NonNull IetfInetUtil INSTANCE = new IetfInetUtil(); private static final Pattern HOST_IPV4_PATTERN = Pattern.compile( @@ -53,7 +51,7 @@ public final class IetfInetUtil extends AbstractIetfInetUtil