diff options
Diffstat (limited to 'vpp-common/vpp-translate-utils/src/main/java/io/fd')
2 files changed, 7 insertions, 49 deletions
diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv4Translator.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv4Translator.java index 69ef7f3ab..24f4ae9aa 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv4Translator.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv4Translator.java @@ -18,11 +18,9 @@ package io.fd.hc2vpp.common.translate.util; import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.net.InetAddresses; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.Arrays; import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; @@ -57,20 +55,10 @@ public interface Ipv4Translator extends ByteDataTranslator { */ default byte extractPrefix(Ipv4Prefix data) { checkNotNull(data, "Cannot extract from null"); - return Byte.valueOf(data.getValue().substring(data.getValue().indexOf('/') + 1)); } /** - * Converts byte array to {@link Ipv4Prefix} with specified prefixLength - */ - default Ipv4Prefix arrayToIpv4Prefix(final byte[] address, byte prefixLength) { - Ipv4AddressNoZone addressPart = arrayToIpv4AddressNoZone(address); - - return new Ipv4Prefix(addressPart.getValue().concat("/").concat(String.valueOf(prefixLength))); - } - - /** * Parse byte array returned by VPP representing an Ipv4 address. Expects array in big endian * * @return Ipv4AddressNoZone containing string representation of IPv4 address constructed from submitted bytes. No @@ -82,11 +70,7 @@ public interface Ipv4Translator extends ByteDataTranslator { if (ip.length == 16) { ip = Arrays.copyOfRange(ip, 0, 4); } - try { - return new Ipv4AddressNoZone(InetAddresses.toAddrString(InetAddress.getByAddress(ip))); - } catch (UnknownHostException e) { - throw new IllegalArgumentException("Unable to parse ipv4", e); - } + return new Ipv4AddressNoZone(IetfInetUtil.INSTANCE.ipv4AddressFor(ip)); } /** @@ -110,13 +94,6 @@ public interface Ipv4Translator extends ByteDataTranslator { } default Ipv4Prefix toIpv4Prefix(final byte[] address, final int prefix) { - try { - return new Ipv4Prefix( - String.format("%s/%s", InetAddress.getByAddress(address).getHostAddress(), - String.valueOf(prefix))); - } catch (UnknownHostException e) { - throw new IllegalArgumentException( - "Cannot create prefix for address[" + Arrays.toString(address) + "],prefix[" + prefix + "]", e); - } + return IetfInetUtil.INSTANCE.ipv4PrefixFor(address, prefix); } } diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv6Translator.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv6Translator.java index 6a71d6af2..6d939af07 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv6Translator.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/Ipv6Translator.java @@ -21,10 +21,8 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import com.google.common.net.InetAddresses; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.Arrays; import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil; 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.IpPrefix; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address; @@ -68,9 +66,8 @@ public interface Ipv6Translator extends ByteDataTranslator { /** * Converts byte array to {@link Ipv6Prefix} with specified prefixLength */ - default Ipv6Prefix arrayToIpv6Prefix(final byte[] address, int prefixLength) { - Ipv6AddressNoZone addressPart = arrayToIpv6AddressNoZone(address); - return new Ipv6Prefix(addressPart.getValue().concat("/").concat(String.valueOf(prefixLength))); + default Ipv6Prefix toIpv6Prefix(final byte[] address, int prefix) { + return IetfInetUtil.INSTANCE.ipv6PrefixFor(address, prefix); } /** @@ -82,12 +79,7 @@ public interface Ipv6Translator extends ByteDataTranslator { @Nonnull default Ipv6AddressNoZone arrayToIpv6AddressNoZone(@Nonnull byte[] ip) { checkArgument(ip.length == 16, "Illegal array length"); - - try { - return new Ipv6AddressNoZone(InetAddresses.toAddrString(InetAddress.getByAddress(ip))); - } catch (UnknownHostException e) { - throw new IllegalArgumentException("Unable to parse ipv6", e); - } + return new Ipv6AddressNoZone(IetfInetUtil.INSTANCE.ipv6AddressFor(ip)); } /** @@ -117,15 +109,4 @@ public interface Ipv6Translator extends ByteDataTranslator { System.arraycopy(ip, 0, result, 0, 4); return result; } - - default Ipv6Prefix toIpv6Prefix(final byte[] address, final int prefix) { - try { - return new Ipv6Prefix( - String.format("%s/%s", InetAddress.getByAddress(address).getHostAddress(), - String.valueOf(prefix))); - } catch (UnknownHostException e) { - throw new IllegalArgumentException( - "Cannot create prefix for address[" + Arrays.toString(address) + "],prefix[" + prefix + "]", e); - } - } } |