Add IpAddress-to-bytes conversion 47/35247/1
authorRobert Varga <rovarga@cisco.com>
Mon, 22 Feb 2016 16:38:41 +0000 (17:38 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 23 Feb 2016 11:16:29 +0000 (12:16 +0100)
This adds utilities aware of the validated format, making them correct
with respect to zone identifiers. The IPv4 conversion is also faster due
to the fact it can rely on well-formedness of the argument.

Change-Id: I28014d15107562e7dc6193eb6f8198586e839d5b
Signed-off-by: Robert Varga <rovarga@cisco.com>
model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtilTest.java [new file with mode: 0644]
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtilTest.java
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java [new file with mode: 0644]
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpUtil.java [new file with mode: 0644]
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacClass.java [new file with mode: 0644]
model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacUtil.java [new file with mode: 0644]

index e8a112aeace721d4b9bedb12af855fa0578be7e2..97343a5bac8acae4e1eac9aee506163c34746aa1 100644 (file)
@@ -96,6 +96,32 @@ public abstract class AbstractIetfInetUtil<A4, P4, A6, P6, A> {
         return prefixToAddress(address4Factory, ipv4PrefixString(prefix));
     }
 
+    @Nonnull public final byte[] ipv4AddressBytes(@Nonnull final A4 addr) {
+        /*
+         * This implementation relies heavily on the input string having been validated to comply with
+         * the Ipv4Address pattern, which may include a zone index.
+         */
+        final String str = ipv4AddressString(addr);
+        final byte[] bytes = new byte[4];
+
+        int out = 0;
+        int val = 0;
+        for (int i = 0; i < str.length(); ++i) {
+            final char c = str.charAt(i);
+            if (c == '%') {
+                break;
+            } else if (c == '.') {
+                bytes[out++] = (byte) val;
+                val = 0;
+            } else {
+                val = 10 * val + (c - '0');
+            }
+        }
+
+        bytes[out] = (byte) val;
+        return bytes;
+    }
+
     /**
      * Create a /32 Ipv4Prefix by interpreting input bytes as an IPv4 address.
      *
@@ -199,6 +225,16 @@ public abstract class AbstractIetfInetUtil<A4, P4, A6, P6, A> {
         return prefixToAddress(address6Factory, ipv6PrefixString(prefix));
     }
 
+    @Nonnull public final byte[] ipv6AddressBytes(@Nonnull final A6 addr) {
+        String str = ipv6AddressString(addr);
+        final int percent = str.indexOf('%');
+        if (percent != -1) {
+            str = str.substring(0, percent);
+        }
+
+        return InetAddresses.forString(str).getAddress();
+    }
+
     /**
      * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv6 address.
      *
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
new file mode 100644 (file)
index 0000000..98c5cec
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 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.junit.Assert.assertArrayEquals;
+import com.google.common.net.InetAddresses;
+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 testIpv4ToBytes() {
+        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");
+    }
+}
index 6faeebe787eb5435f85ae65e3297ac3a8708dd4c..7c5491e5aadb1329f8947648a85d88a36e7982cf 100644 (file)
@@ -10,34 +10,10 @@ package org.opendaylight.mdsal.model.ietf.util;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
-import com.google.common.base.Preconditions;
 import java.util.Arrays;
 import org.junit.Test;
 
 public class AbstractIetfYangUtilTest {
-    public static final class MacClass {
-        final String _value;
-
-        public MacClass(final String value) {
-            this._value = Preconditions.checkNotNull(value);
-        }
-
-        public MacClass(final MacClass template) {
-            this._value = template._value;
-        }
-    }
-
-    private static final class MacUtil extends AbstractIetfYangUtil<MacClass> {
-        MacUtil() {
-            super(MacClass.class);
-        }
-
-        @Override
-        protected String getValue(final MacClass macAddress) {
-            return macAddress._value;
-        }
-    }
-
     private static final MacUtil UTIL = new MacUtil();
     private static final byte[] BYTES = new byte[] { 1, 2, 30, 90, -5, -120 };
     private static final String CANON = "01:02:1e:5a:fb:88";
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
new file mode 100644 (file)
index 0000000..2f588e1
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+
+public final class IpClass {
+    final String _value;
+
+    public IpClass(final String value) {
+        this._value = Preconditions.checkNotNull(value);
+    }
+
+    public IpClass(final IpClass template) {
+        this._value = template._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
new file mode 100644 (file)
index 0000000..90f0ab4
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * 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;
+
+final class IpUtil extends AbstractIetfInetUtil<IpClass, IpClass, IpClass, IpClass, IpClass> {
+    IpUtil() {
+        super(IpClass.class, IpClass.class, IpClass.class, IpClass.class);
+    }
+
+    @Override
+    protected IpClass ipv4Address(final IpClass addr) {
+        return addr;
+    }
+
+    @Override
+    protected IpClass ipv6Address(final IpClass addr) {
+        return addr;
+    }
+
+    @Override
+    protected String ipv4AddressString(final IpClass addr) {
+        return addr._value;
+    }
+
+    @Override
+    protected String ipv6AddressString(final IpClass addr) {
+        return addr._value;
+    }
+
+    @Override
+    protected String ipv4PrefixString(final IpClass prefix) {
+        return prefix._value;
+    }
+
+    @Override
+    protected String ipv6PrefixString(final IpClass prefix) {
+        return prefix._value;
+    }
+}
\ No newline at end of file
diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacClass.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacClass.java
new file mode 100644 (file)
index 0000000..d1ceefb
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+
+public final class MacClass {
+    final String _value;
+
+    public MacClass(final String value) {
+        this._value = Preconditions.checkNotNull(value);
+    }
+
+    public MacClass(final MacClass template) {
+        this._value = template._value;
+    }
+}
\ No newline at end of file
diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacUtil.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/MacUtil.java
new file mode 100644 (file)
index 0000000..c1b4a77
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * 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;
+
+final class MacUtil extends AbstractIetfYangUtil<MacClass> {
+    MacUtil() {
+        super(MacClass.class);
+    }
+
+    @Override
+    protected String getValue(final MacClass macAddress) {
+        return macAddress._value;
+    }
+}
\ No newline at end of file