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.
*
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.
*
--- /dev/null
+/*
+ * 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");
+ }
+}
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";
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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