From 625b421f3c28e0457f039017b1160662d622f4bc Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Wed, 8 Jun 2016 20:51:32 +0200 Subject: HONEYCOMB-62: Add Ip readers + Fix 1 interface reads. Interface reader worked only if GET interfaces-state was executed + Fix readSubtree for augmentations. Comoposite readers did not check child readers for augmentations, only direct children. Change-Id: I2bc433e3e5785453062ab262b9edabc72c333bf0 Signed-off-by: Maros Marsalek --- .../v3po/translate/v3po/util/TranslateUtils.java | 53 +++++++++++++++++----- .../translate/v3po/util/TranslateUtilsTest.java | 22 +++++++++ 2 files changed, 63 insertions(+), 12 deletions(-) (limited to 'v3po/vpp-translate-utils') diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java index 9c3fea615..d68ea19b7 100644 --- a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java @@ -16,23 +16,25 @@ package io.fd.honeycomb.v3po.translate.v3po.util; -import com.google.common.base.Splitter; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.openvpp.jvpp.VppBaseCallException; -import org.openvpp.jvpp.dto.JVppReply; +import static com.google.common.base.Preconditions.checkArgument; -import javax.annotation.Nonnegative; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; +import com.google.common.base.Splitter; +import com.google.common.net.InetAddresses; +import java.net.UnknownHostException; +import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.function.BiConsumer; - -import static com.google.common.base.Preconditions.checkArgument; +import javax.annotation.Nonnegative; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.VppBaseCallException; +import org.openvpp.jvpp.dto.JVppReply; public final class TranslateUtils { @@ -77,16 +79,43 @@ public final class TranslateUtils { } } + /** + * Transform Ipv4 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 + */ public static byte[] ipv4AddressNoZoneToArray(final Ipv4AddressNoZone ipv4Addr) { byte[] retval = new byte[4]; String[] dots = ipv4Addr.getValue().split("\\."); - for (int d = 3; d >= 0; d--) { - retval[d] = (byte) (Short.parseShort(dots[3 - d]) & 0xff); + for (int d = 0; d < 4; d++) { + retval[d] = (byte) (Short.parseShort(dots[d]) & 0xff); } return retval; } + /** + * Parse byte array returned by VPP representing an Ipv4 address. Vpp returns IP byte arrays in reversed order. + * + * @return Ipv4AddressNoZone containing string representation of IPv4 address constructed from submitted bytes. No + * change in order. + */ + @Nonnull + public static Ipv4AddressNoZone arrayToIpv4AddressNoZone(@Nonnull byte[] ip) { + // VPP sends ipv4 in a 16 byte array + if(ip.length == 16) { + ip = Arrays.copyOfRange(ip, 0, 4); + } + try { + // Not reversing the byte array here!! because the IP coming from VPP is in reversed byte order + // compared to byte order it was submitted + return new Ipv4AddressNoZone(InetAddresses.toAddrString(InetAddresses.fromLittleEndianByteArray(ip))); + } catch (UnknownHostException e) { + throw new IllegalArgumentException("Unable to parse ipv4", e); + } + } + /** * Return (interned) string from byte array while removing \u0000. * Strings represented as fixed length byte[] from vpp contain \u0000. diff --git a/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java b/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java index 0a851e8c4..ba3861b99 100644 --- a/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java +++ b/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java @@ -4,9 +4,31 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; public class TranslateUtilsTest { + @Test + public void testIpv4NoZone() throws Exception { + final Ipv4AddressNoZone ipv4Addr = new Ipv4AddressNoZone("192.168.1.1"); + byte[] bytes = TranslateUtils.ipv4AddressNoZoneToArray(ipv4Addr); + assertEquals((byte)192, bytes[0]); + // Simulating the magic of VPP + bytes = reverseBytes(bytes); + final Ipv4AddressNoZone ipv4AddressNoZone = TranslateUtils.arrayToIpv4AddressNoZone(bytes); + assertEquals(ipv4Addr, ipv4AddressNoZone); + } + + private byte[] reverseBytes(final byte[] bytes) { + final byte[] reversed = new byte[bytes.length]; + int i = 1; + for (byte aByte : bytes) { + reversed[bytes.length - i++] = aByte; + } + + return reversed; + } + @Test public void testToString() { final byte[] expected = "test".getBytes(); -- cgit 1.2.3-korg