Add DottedQuad/int interface
[mdsal.git] / model / ietf / ietf-type-util / src / main / java / org / opendaylight / mdsal / model / ietf / util / Ipv4Utils.java
index b79e2866810c2f813db0e873caa92db322e4cd1a..50d2178086629a2e5b6e109ce80dfdca64551d23 100644 (file)
@@ -35,4 +35,30 @@ final class Ipv4Utils {
 
         bytes[out] = (byte) val;
     }
+
+    static int addressBits(final String str, final int limit) {
+        int prev = 0;
+        int current = 0;
+        for (int i = 0, shift = 24; i < limit; ++i) {
+            final char c = str.charAt(i);
+            if (c == '.') {
+                prev |= current << shift;
+                shift -= 8;
+                current = 0;
+            } else {
+                current = 10 * current + c - '0';
+            }
+        }
+        return prev | current;
+    }
+
+    static byte @NonNull[] addressBytes(final String str, final int limit) {
+        final byte[] bytes = new byte[4];
+        Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, limit);
+        return bytes;
+    }
+
+    static String addressString(final int bits) {
+        return (bits >>> 24) + "." + (bits >>> 16 & 0xFF) + "." + (bits >>> 8 & 0xFF) + "." + (bits & 0xFF);
+    }
 }