package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715;
import com.google.common.annotations.Beta;
+
import org.opendaylight.mdsal.model.ietf.util.AbstractIetfInetUtil;
/**
* A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
*/
@Beta
-public final class IetfInetUtil extends AbstractIetfInetUtil<Ipv4Address, Ipv4Prefix, Ipv6Address, Ipv6Prefix, IpAddress> {
+public final class IetfInetUtil extends AbstractIetfInetUtil<Ipv4Address, Ipv4Prefix, Ipv6Address, Ipv6Prefix, IpAddress, IpPrefix> {
public static final IetfInetUtil INSTANCE = new IetfInetUtil();
private IetfInetUtil() {
return new IpAddress(addr);
}
+ @Override
+ protected IpPrefix ipv4Prefix(Ipv4Prefix addr) {
+ return new IpPrefix(addr);
+ }
+
+ @Override
+ protected IpPrefix ipv6Prefix(Ipv6Prefix addr) {
+ return new IpPrefix(addr);
+ }
+
@Override
protected String ipv4AddressString(final Ipv4Address addr) {
return addr.getValue();
* A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
*/
@Beta
-public abstract class AbstractIetfInetUtil<A4, P4, A6, P6, A> {
+public abstract class AbstractIetfInetUtil<A4, P4, A6, P6, A, P> {
private static final int INET4_LENGTH = 4;
private static final int INET6_LENGTH = 16;
private final StringValueObjectFactory<A4> address4Factory;
@Nonnull protected abstract A ipv4Address(@Nonnull A4 addr);
@Nonnull protected abstract A ipv6Address(@Nonnull A6 addr);
+ @Nonnull protected abstract P ipv4Prefix(@Nonnull P4 addr);
+ @Nonnull protected abstract P ipv6Prefix(@Nonnull P6 addr);
@Nullable protected abstract A4 maybeIpv4Address(@Nonnull A addr);
@Nullable protected abstract A6 maybeIpv6Address(@Nonnull A addr);
@Nonnull protected abstract String ipv4AddressString(@Nonnull A4 addr);
@Nonnull protected abstract String ipv4PrefixString(@Nonnull P4 prefix);
@Nonnull protected abstract String ipv6PrefixString(@Nonnull P6 prefix);
+ /**
+ * Create an IpAddress by interpreting input bytes as an IPv4 or IPv6 address, based on array length.
+ *
+ * @param bytes 4-byte (IPv4) or 6-byte (IPv6) array
+ * @return An IpAddress object
+ * @throws IllegalArgumentException if bytes has length different from 4 or 6
+ * @throws NullPointerException if bytes is null
+ */
@Nonnull public final A ipAddressFor(@Nonnull final byte[] bytes) {
switch (bytes.length) {
- case 4:
+ case INET4_LENGTH:
return ipv4Address(ipv4AddressFor(bytes));
- case 16:
+ case INET6_LENGTH:
return ipv6Address(ipv6AddressFor(bytes));
default:
throw new IllegalArgumentException("Invalid array length " + bytes.length);
}
}
+ /**
+ * Create an IpPrefix by combining the address with a mask. The address
+ * bytes are interpreted as an address and the specified mask is concatenated to
+ * it. The address bytes are not masked.
+ *
+ * @param bytes Input address as a 4-byte (IPv4) or 16-byte (IPv6) array
+ * @param mask Prefix mask
+ * @return An IpPrefix object
+ * @throws IllegalArgumentException if bytes has length different from 4 or 16 or if mask is not
+ * in range 0-32 or 0-128 respectively
+ * @throws NullPointerException if bytes is null
+ */
+
+ @Nonnull public final P ipPrefixFor(@Nonnull final byte[] bytes, final int mask) {
+ switch (bytes.length) {
+ case INET4_LENGTH:
+ return ipv4Prefix(ipv4PrefixFor(bytes, mask));
+ case INET6_LENGTH:
+ return ipv6Prefix(ipv6PrefixFor(bytes, mask));
+ default:
+ throw new IllegalArgumentException("Invalid array length " + bytes.length);
+ }
+ }
+
+ @Nonnull public final P ipPrefixFor(@Nonnull final InetAddress addr, final int mask) {
+ Preconditions.checkNotNull(addr, "Address must not be null");
+ if (addr instanceof Inet4Address) {
+ return ipv4Prefix(ipv4PrefixFor(addr, mask));
+ } else if (addr instanceof Inet6Address) {
+ return ipv6Prefix(ipv6PrefixFor(addr, mask));
+ } else {
+ throw new IllegalArgumentException("Unhandled address " + addr);
+ }
+ }
+
@Nonnull public final InetAddress inetAddressFor(@Nonnull final A addr) {
final A4 v4 = maybeIpv4Address(addr);
if (v4 != null) {
* bytes are interpreted as an address and the specified mask is concatenated to
* it. The address bytes are not masked.
*
- * @param address Input address as a 4-byte array
+ * @param address Input address as a 16-byte array
* @param mask Prefix mask
* @return An Ipv6Prefix object
* @throws IllegalArgumentException if bytes has length different from 16 or if mask is not in range 0-128