diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2017-01-19 09:54:45 +0100 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2017-01-19 09:54:45 +0100 |
commit | bf5707db440d320642090e10fab56233e2399e0f (patch) | |
tree | ce63a2237d7ce888cb484a595afd4a41d1455548 /vpp-common/vpp-translate-utils | |
parent | 3b11d9eb878b29825a2e8111d1c520ef3acddc7a (diff) |
HONEYCOMB-339: fix ip6 address string to byte conversion
Change-Id: I8c1d20b2be23bce27903b9d4149f59d3b83aacaa
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'vpp-common/vpp-translate-utils')
2 files changed, 31 insertions, 31 deletions
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 396391dd7..c4a4ba38e 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 @@ -24,10 +24,7 @@ import com.google.common.net.InetAddresses; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; import javax.annotation.Nonnull; -import org.apache.commons.lang3.StringUtils; 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; @@ -39,32 +36,14 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types. */ public interface Ipv6Translator extends ByteDataTranslator { - default byte[] ipv6AddressNoZoneToArray(@Nonnull final String address) { - byte[] retval = new byte[16]; - - //splits address and add ommited zeros for easier parsing - List<String> segments = Arrays.asList(address.split(":")) - .stream() - .map(segment -> StringUtils.repeat('0', 4 - segment.length()) + segment) - .collect(Collectors.toList()); - - byte index = 0; - for (String segment : segments) { - - String firstPart = segment.substring(0, 2); - String secondPart = segment.substring(2); - - //first part should be ommited - if ("00".equals(firstPart)) { - index++; - } else { - retval[index++] = ((byte) Short.parseShort(firstPart, 16)); - } - - retval[index++] = ((byte) Short.parseShort(secondPart, 16)); - } - - return retval; + /** + * Transform Ipv6 address to a byte array acceptable by VPP. VPP expects incoming byte array to be in the same order + * as the address. + * + * @return byte array with address bytes + */ + default byte[] ipv6AddressNoZoneToArray(@Nonnull final Ipv6Address address) { + return Impl.ipv6AddressNoZoneToArray(address.getValue()); } /** @@ -74,7 +53,7 @@ public interface Ipv6Translator extends ByteDataTranslator { * @return byte array with address bytes */ default byte[] ipv6AddressNoZoneToArray(@Nonnull final Ipv6AddressNoZone ipv6Addr) { - return ipv6AddressNoZoneToArray(ipv6Addr.getValue()); + return Impl.ipv6AddressNoZoneToArray(ipv6Addr.getValue()); } /** @@ -163,4 +142,15 @@ public interface Ipv6Translator extends ByteDataTranslator { "Cannot create prefix for address[" + Arrays.toString(address) + "],prefix[" + prefix + "]"); } } + + class Impl { + private static byte[] ipv6AddressNoZoneToArray(@Nonnull final String address){ + try { + // No lookup performed for literal ipv6 addresses + return InetAddress.getByName(address).getAddress(); + } catch (UnknownHostException e) { + throw new IllegalArgumentException("Invalid address supplied", e); + } + } + } } diff --git a/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/Ipv6TranslatorTest.java b/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/Ipv6TranslatorTest.java index 1099f6809..a620cbdd2 100644 --- a/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/Ipv6TranslatorTest.java +++ b/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/Ipv6TranslatorTest.java @@ -28,7 +28,17 @@ public class Ipv6TranslatorTest implements Ipv6Translator { public void testIpv6NoZone() { final Ipv6AddressNoZone ipv6Addr = new Ipv6AddressNoZone("3ffe:1900:4545:3:200:f8ff:fe21:67cf"); byte[] bytes = ipv6AddressNoZoneToArray(ipv6Addr); - assertEquals((byte) 63, bytes[0]); + assertEquals((byte) 0x3f, bytes[0]); + final Ipv6AddressNoZone ivp6AddressNoZone = arrayToIpv6AddressNoZone(bytes); + assertEquals(ipv6Addr, ivp6AddressNoZone); + } + + @Test + public void testIpv6NoZoneEmptyGroup() { + final Ipv6AddressNoZone ipv6Addr = new Ipv6AddressNoZone("10::10"); + byte[] bytes = ipv6AddressNoZoneToArray(ipv6Addr); + assertEquals((byte) 0, bytes[0]); + assertEquals((byte) 0x10, bytes[1]); final Ipv6AddressNoZone ivp6AddressNoZone = arrayToIpv6AddressNoZone(bytes); assertEquals(ipv6Addr, ivp6AddressNoZone); } |