BUG-2825: reuse IPv4 address formatting 86/35386/4
authorRobert Varga <robert.varga@pantheon.sk>
Thu, 25 Feb 2016 10:55:54 +0000 (11:55 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 25 Feb 2016 15:23:30 +0000 (15:23 +0000)
Do not instantiate an Inet4Address, but dump the bytes using our
home-grown utility.

Change-Id: I1d32593ca7871ac4ce9eba880a3221def9a9619f
Signed-off-by: Robert Varga <robert.varga@pantheon.sk>
model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtil.java
model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv4Utils.java [new file with mode: 0644]
model/ietf/ietf-type-util/src/main/java/org/opendaylight/mdsal/model/ietf/util/Ipv6Utils.java

index 728682afd41ea0bdd10e23fbbca24a3b624e6e3d..2b30e1303313dd66812aaf837636324a7556b3a9 100644 (file)
@@ -106,7 +106,7 @@ public abstract class AbstractIetfInetUtil<A4, P4, A6, P6, A> {
         final String str = ipv4AddressString(addr);
         final byte[] bytes = new byte[INET4_LENGTH];
         final int percent = str.indexOf('%');
-        fillIpv4Bytes(bytes, str, percent == -1 ? str.length() : percent);
+        Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, percent == -1 ? str.length() : percent);
         return bytes;
     }
 
@@ -188,27 +188,11 @@ public abstract class AbstractIetfInetUtil<A4, P4, A6, P6, A> {
         final int slash = str.lastIndexOf('/');
 
         final byte[] bytes = new byte[INET4_LENGTH + 1];
-        fillIpv4Bytes(bytes, str, slash);
+        Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, slash);
         bytes[INET4_LENGTH] = (byte)Integer.parseInt(str.substring(slash + 1), 10);
         return bytes;
     }
 
-    private static void fillIpv4Bytes(final byte[] bytes, final String str, final int limit) {
-        int out = 0;
-        int val = 0;
-        for (int i = 0; i < limit; ++i) {
-            final char c = str.charAt(i);
-            if (c == '.') {
-                bytes[out++] = (byte) val;
-                val = 0;
-            } else {
-                val = 10 * val + (c - '0');
-            }
-        }
-
-        bytes[out] = (byte) val;
-    }
-
     /**
      * Create an Ipv6Address by interpreting input bytes as an IPv6 address.
      *
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
new file mode 100644 (file)
index 0000000..f8797a6
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2016 Pantheon Technologies s.r.o. 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;
+
+/**
+ * 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.
+ */
+final class Ipv4Utils {
+    private Ipv4Utils() {
+        throw new UnsupportedOperationException();
+    }
+
+    static void fillIpv4Bytes(final byte[] 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) {
+            final char c = str.charAt(i);
+            if (c == '.') {
+                bytes[out++] = (byte) val;
+                val = 0;
+            } else {
+                val = 10 * val + (c - '0');
+            }
+        }
+
+        bytes[out] = (byte) val;
+    }
+}
index e8fecef4376006a2812909612a7b356b0301e307..6672600dc1f5448c02f79158606b482b737956c9 100644 (file)
@@ -9,9 +9,6 @@ package org.opendaylight.mdsal.model.ietf.util;
 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Verify;
-import com.google.common.net.InetAddresses;
-import java.net.Inet4Address;
-import java.net.InetAddress;
 import javax.annotation.Nonnull;
 
 /**
@@ -102,18 +99,12 @@ final class Ipv6Utils {
            /* frankenstein - v4 attached to v6, mixed notation */
            if (ch == '.' && ((j + INADDR4SZ) <= INADDR6SZ)) {
 
-               /* this has passed the regexp so it is fairly safe to parse it
-                * straight away. As v4 addresses do not suffer from the same
-                * deficiencies as the java v6 implementation we can invoke it
-                * straight away and be done with it
+               /*
+                * This has passed the regexp so it is fairly safe to parse it
+                * straight away. Use the Ipv4Utils for that.
                 */
-               Preconditions.checkArgument(j != (INADDR6SZ - INADDR4SZ - 1), "Invalid v4 in v6 mapping in %s", addrStr);
-               InetAddress _inet_form = InetAddresses.forString(addrStr.substring(curtok, addrStrLen));
-
-               Preconditions.checkArgument(_inet_form instanceof Inet4Address);
-               System.arraycopy(_inet_form.getAddress(), 0, dst, j, INADDR4SZ);
+               Ipv4Utils.fillIpv4Bytes(dst, j, addrStr, curtok, addrStrLen);
                j += INADDR4SZ;
-
                saw_xdigit = false;
                break;
            }