protected static final int DEFAULT_IPV4_PREFIX_VALUE = 0;
protected static final byte[] DEFAULT_IPV6_ADDRESS_BYTES = new byte[16];
- protected static final Ipv6Prefix IPV6_PREFIX = new Ipv6Prefix("2001:db8:a0b:12f0:0:0:0:1/64");
- protected static final Ipv6Prefix IPV6_2_PREFIX = new Ipv6Prefix("2001:db8:a0b:12f0:0:0:0:2/48");
+ protected static final Ipv6Prefix IPV6_PREFIX = new Ipv6Prefix("2001:db8:a0b:12f0::1/64");
+ protected static final Ipv6Prefix IPV6_2_PREFIX = new Ipv6Prefix("2001:db8:a0b:12f0::2/48");
protected static final byte[] IPV6_PREFIX_BYTES = {32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 1};
protected static final byte[] IPV6_2_PREFIX_BYTES = {32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 2};
protected static final byte IPV6_PREFIX_VALUE = (byte) 64;
if (prefixDetails != null) {
builder.setNat64PrefixId(prefixId);
builder.setNat64Prefix(
- arrayToIpv6Prefix(prefixDetails.prefix, UnsignedBytes.toInt(prefixDetails.prefixLen)));
+ toIpv6Prefix(prefixDetails.prefix, UnsignedBytes.toInt(prefixDetails.prefixLen)));
}
}
assertEquals(1, builder.getId().intValue());
assertEquals(1, builder.getKey().getId().intValue());
- assertEquals("2001:db8:a0b:12f0:0:0:0:1/24", builder.getDestinationPrefix().getValue());
+ assertEquals("2001:db8:a0b:12f0::1/24", builder.getDestinationPrefix().getValue());
NextHopOptions hopOptions = builder.getNextHopOptions();
assertTrue(hopOptions instanceof SpecialNextHop);
assertEquals(2, builder.getId().intValue());
assertEquals(2, builder.getKey().getId().intValue());
- assertEquals("2001:db8:a0b:12f0:0:0:0:2/22", builder.getDestinationPrefix().getValue());
+ assertEquals("2001:db8:a0b:12f0::2/22", builder.getDestinationPrefix().getValue());
NextHopOptions hopOptions = builder.getNextHopOptions();
assertTrue(hopOptions instanceof SimpleNextHop);
assertEquals(3, builder.getId().intValue());
assertEquals(3, builder.getKey().getId().intValue());
- assertEquals("2001:db8:a0b:12f0:0:0:0:2/16", builder.getDestinationPrefix().getValue());
+ assertEquals("2001:db8:a0b:12f0::2/16", builder.getDestinationPrefix().getValue());
NextHopOptions hopOptions = builder.getNextHopOptions();
assertTrue(hopOptions instanceof NextHopList);
import static com.google.common.base.Preconditions.checkNotNull;
-import com.google.common.net.InetAddresses;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
import java.util.Arrays;
import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
*/
default byte extractPrefix(Ipv4Prefix data) {
checkNotNull(data, "Cannot extract from null");
-
return Byte.valueOf(data.getValue().substring(data.getValue().indexOf('/') + 1));
}
- /**
- * Converts byte array to {@link Ipv4Prefix} with specified prefixLength
- */
- default Ipv4Prefix arrayToIpv4Prefix(final byte[] address, byte prefixLength) {
- Ipv4AddressNoZone addressPart = arrayToIpv4AddressNoZone(address);
-
- return new Ipv4Prefix(addressPart.getValue().concat("/").concat(String.valueOf(prefixLength)));
- }
-
/**
* Parse byte array returned by VPP representing an Ipv4 address. Expects array in big endian
*
if (ip.length == 16) {
ip = Arrays.copyOfRange(ip, 0, 4);
}
- try {
- return new Ipv4AddressNoZone(InetAddresses.toAddrString(InetAddress.getByAddress(ip)));
- } catch (UnknownHostException e) {
- throw new IllegalArgumentException("Unable to parse ipv4", e);
- }
+ return new Ipv4AddressNoZone(IetfInetUtil.INSTANCE.ipv4AddressFor(ip));
}
/**
}
default Ipv4Prefix toIpv4Prefix(final byte[] address, final int prefix) {
- try {
- return new Ipv4Prefix(
- String.format("%s/%s", InetAddress.getByAddress(address).getHostAddress(),
- String.valueOf(prefix)));
- } catch (UnknownHostException e) {
- throw new IllegalArgumentException(
- "Cannot create prefix for address[" + Arrays.toString(address) + "],prefix[" + prefix + "]", e);
- }
+ return IetfInetUtil.INSTANCE.ipv4PrefixFor(address, prefix);
}
}
import static com.google.common.base.Preconditions.checkState;
import com.google.common.net.InetAddresses;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.util.Arrays;
import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
/**
* Converts byte array to {@link Ipv6Prefix} with specified prefixLength
*/
- default Ipv6Prefix arrayToIpv6Prefix(final byte[] address, int prefixLength) {
- Ipv6AddressNoZone addressPart = arrayToIpv6AddressNoZone(address);
- return new Ipv6Prefix(addressPart.getValue().concat("/").concat(String.valueOf(prefixLength)));
+ default Ipv6Prefix toIpv6Prefix(final byte[] address, int prefix) {
+ return IetfInetUtil.INSTANCE.ipv6PrefixFor(address, prefix);
}
/**
@Nonnull
default Ipv6AddressNoZone arrayToIpv6AddressNoZone(@Nonnull byte[] ip) {
checkArgument(ip.length == 16, "Illegal array length");
-
- try {
- return new Ipv6AddressNoZone(InetAddresses.toAddrString(InetAddress.getByAddress(ip)));
- } catch (UnknownHostException e) {
- throw new IllegalArgumentException("Unable to parse ipv6", e);
- }
+ return new Ipv6AddressNoZone(IetfInetUtil.INSTANCE.ipv6AddressFor(ip));
}
/**
System.arraycopy(ip, 0, result, 0, 4);
return result;
}
-
- default Ipv6Prefix toIpv6Prefix(final byte[] address, final int prefix) {
- try {
- return new Ipv6Prefix(
- String.format("%s/%s", InetAddress.getByAddress(address).getHostAddress(),
- String.valueOf(prefix)));
- } catch (UnknownHostException e) {
- throw new IllegalArgumentException(
- "Cannot create prefix for address[" + Arrays.toString(address) + "],prefix[" + prefix + "]", e);
- }
- }
}
assertEquals(64, extractPrefix(new Ipv6Prefix(IPV6_COMPRESSED + "/64")));
}
+ @Test
+ public void testArrayToIpv6Prefix48() {
+ assertEquals("2001:db8:a0b:12f0::1/48",
+ toIpv6Prefix(new byte[]{32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 1},
+ (byte) 48).getValue());
+ }
+
@Test
public void testArrayToIpv6Prefix64() throws Exception {
- assertEquals(IPV6_COMPRESSED + "/64", arrayToIpv6Prefix(IPV6_BYTES, 64).getValue());
+ assertEquals(IPV6_COMPRESSED + "/64", toIpv6Prefix(IPV6_BYTES, 64).getValue());
}
@Test
public void testArrayToIpv6Prefix128() throws Exception {
- assertEquals(IPV6_COMPRESSED + "/128", arrayToIpv6Prefix(IPV6_BYTES, 128).getValue());
+ assertEquals(IPV6_COMPRESSED + "/128", toIpv6Prefix(IPV6_BYTES, 128).getValue());
}
@Test
public void testExtractPrefix() {
assertEquals(48, extractPrefix(new Ipv6Prefix("3ffe:1900:4545:3:200:f8ff:fe21:67cf/48")));
}
-
- @Test
- public void toPrefix() {
- assertEquals("2001:db8:a0b:12f0:0:0:0:1/48",
- toIpv6Prefix(new byte[]{32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 1},
- (byte) 48).getValue());
- }
}
\ No newline at end of file