2 * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.model.ietf.util;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.net.InetAddresses;
13 import java.net.Inet4Address;
14 import java.net.Inet6Address;
15 import java.net.InetAddress;
16 import java.net.UnknownHostException;
17 import java.util.AbstractMap.SimpleImmutableEntry;
18 import java.util.Map.Entry;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.binding.util.StringValueObjectFactory;
24 * A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
27 public abstract class AbstractIetfInetUtil<A4, A4NZ extends A4, P4, A6, A6NZ extends A6, P6, A, ANZ, P> {
28 protected static final int INET4_LENGTH = 4;
29 protected static final int INET6_LENGTH = 16;
30 private final StringValueObjectFactory<A4NZ> nozone4Factory;
31 private final StringValueObjectFactory<P4> prefix4Factory;
32 private final StringValueObjectFactory<A6NZ> nozone6Factory;
33 private final StringValueObjectFactory<P6> prefix6Factory;
35 protected AbstractIetfInetUtil(final Class<A4NZ> nozone4Class, final Class<P4> prefix4Class,
36 final Class<A6NZ> nozone6Class, final Class<P6> prefix6Class) {
37 this.nozone4Factory = StringValueObjectFactory.create(nozone4Class, "0.0.0.0");
38 this.prefix4Factory = StringValueObjectFactory.create(prefix4Class, "0.0.0.0/0");
39 this.nozone6Factory = StringValueObjectFactory.create(nozone6Class, "::0");
40 this.prefix6Factory = StringValueObjectFactory.create(prefix6Class, "::0/0");
43 @Nonnull protected abstract A ipv4Address(@Nonnull A4 addr);
44 @Nonnull protected abstract A ipv6Address(@Nonnull A6 addr);
45 @Nonnull protected abstract ANZ ipv4AddressNoZone(@Nonnull A4NZ addr);
46 @Nonnull protected abstract ANZ ipv6AddressNoZone(@Nonnull A6NZ addr);
48 @Nonnull protected abstract P ipv4Prefix(@Nonnull P4 addr);
49 @Nonnull protected abstract P ipv6Prefix(@Nonnull P6 addr);
50 @Nullable protected abstract A4 maybeIpv4Address(@Nonnull A addr);
51 @Nullable protected abstract A6 maybeIpv6Address(@Nonnull A addr);
52 @Nonnull protected abstract String ipv4AddressString(@Nonnull A4 addr);
53 @Nonnull protected abstract String ipv6AddressString(@Nonnull A6 addr);
54 @Nonnull protected abstract String ipv4PrefixString(@Nonnull P4 prefix);
55 @Nonnull protected abstract String ipv6PrefixString(@Nonnull P6 prefix);
58 * Create an IpAddress by interpreting input bytes as an IPv4 or IPv6 address, based on array length.
60 * @param bytes 4-byte (IPv4) or 6-byte (IPv6) array
61 * @return An IpAddress object
62 * @throws IllegalArgumentException if bytes has length different from 4 or 6
63 * @throws NullPointerException if bytes is null
65 @Nonnull public final A ipAddressFor(@Nonnull final byte[] bytes) {
66 switch (bytes.length) {
68 return ipv4Address(ipv4AddressFor(bytes));
70 return ipv6Address(ipv6AddressFor(bytes));
72 throw new IllegalArgumentException("Invalid array length " + bytes.length);
77 * Create an IpAddress by interpreting input bytes as an IPv4 or IPv6 address, based on array length.
79 * @param bytes 4-byte (IPv4) or 6-byte (IPv6) array
80 * @return A no-zone IpAddress object
81 * @throws IllegalArgumentException if bytes has length different from 4 or 6
82 * @throws NullPointerException if bytes is null
84 @Nonnull public final ANZ ipAddressNoZoneFor(@Nonnull final byte[] bytes) {
85 switch (bytes.length) {
87 return ipv4AddressNoZone(ipv4AddressFor(bytes));
89 return ipv6AddressNoZone(ipv6AddressFor(bytes));
91 throw new IllegalArgumentException("Invalid array length " + bytes.length);
95 @Nonnull public final A ipAddressFor(@Nonnull final InetAddress addr) {
96 Preconditions.checkNotNull(addr, "Address must not be null");
97 if (addr instanceof Inet4Address) {
98 return ipv4Address(ipv4AddressFor(addr));
99 } else if (addr instanceof Inet6Address) {
100 return ipv6Address(ipv6AddressFor(addr));
102 throw new IllegalArgumentException("Unhandled address " + addr);
106 @Nonnull public final ANZ ipAddressNoZoneFor(@Nonnull final InetAddress addr) {
107 Preconditions.checkNotNull(addr, "Address must not be null");
108 if (addr instanceof Inet4Address) {
109 return ipv4AddressNoZone(ipv4AddressFor(addr));
110 } else if (addr instanceof Inet6Address) {
111 return ipv6AddressNoZone(ipv6AddressFor(addr));
113 throw new IllegalArgumentException("Unhandled address " + addr);
118 * Create an IpPrefix by combining the address with a mask. The address
119 * bytes are interpreted as an address and the specified mask is concatenated to
120 * it. The address bytes are not masked.
122 * @param bytes Input address as a 4-byte (IPv4) or 16-byte (IPv6) array
123 * @param mask Prefix mask
124 * @return An IpPrefix object
125 * @throws IllegalArgumentException if bytes has length different from 4 or 16 or if mask is not
126 * in range 0-32 or 0-128 respectively
127 * @throws NullPointerException if bytes is null
129 @Nonnull public final P ipPrefixFor(@Nonnull final byte[] bytes, final int mask) {
130 switch (bytes.length) {
132 return ipv4Prefix(ipv4PrefixFor(bytes, mask));
134 return ipv6Prefix(ipv6PrefixFor(bytes, mask));
136 throw new IllegalArgumentException("Invalid array length " + bytes.length);
140 @Nonnull public final P ipPrefixFor(@Nonnull final InetAddress addr, final int mask) {
141 Preconditions.checkNotNull(addr, "Address must not be null");
142 if (addr instanceof Inet4Address) {
143 return ipv4Prefix(ipv4PrefixFor(addr, mask));
144 } else if (addr instanceof Inet6Address) {
145 return ipv6Prefix(ipv6PrefixFor(addr, mask));
147 throw new IllegalArgumentException("Unhandled address " + addr);
151 @Nonnull public final InetAddress inetAddressFor(@Nonnull final A addr) {
152 final A4 v4 = maybeIpv4Address(addr);
154 return inet4AddressFor(v4);
156 final A6 v6 = maybeIpv6Address(addr);
157 Preconditions.checkArgument(v6 != null, "Address %s is neither IPv4 nor IPv6", addr);
158 return inet6AddressFor(v6);
161 @Nonnull public final Inet4Address inet4AddressFor(@Nonnull final A4 addr) {
163 return (Inet4Address) InetAddress.getByAddress(ipv4AddressBytes(addr));
164 } catch (UnknownHostException e) {
165 throw new IllegalArgumentException("Invalid address " + addr, e);
169 @Nonnull public final Inet6Address inet6AddressFor(@Nonnull final A6 addr) {
171 return (Inet6Address) InetAddress.getByAddress(ipv6AddressBytes(addr));
172 } catch (UnknownHostException e) {
173 throw new IllegalArgumentException("Invalid address " + addr, e);
178 * Create an Ipv4Address by interpreting input bytes as an IPv4 address.
180 * @param bytes 4-byte array
181 * @return An Ipv4Address object
182 * @throws IllegalArgumentException if bytes has length different from 4
183 * @throws NullPointerException if bytes is null
185 @Nonnull public final A4NZ ipv4AddressFor(@Nonnull final byte[] bytes) {
186 return nozone4Factory.newInstance(addressStringV4(bytes));
190 * Create an Ipv4Address by interpreting an {@link Inet4Address}.
192 * @param addr An {@link Inet4Address}
193 * @return An Ipv4Address object
194 * @throws IllegalArgumentException if addr is not an {@link Inet4Address}
195 * @throws NullPointerException if addr is null
197 @Nonnull public final A4NZ ipv4AddressFor(@Nonnull final InetAddress addr) {
198 Preconditions.checkNotNull(addr, "Address must not be null");
199 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
200 return nozone4Factory.newInstance(addr.getHostAddress());
203 @Nonnull public final A4NZ ipv4AddressFrom(@Nonnull final P4 prefix) {
204 return prefixToAddress(nozone4Factory, ipv4PrefixString(prefix));
207 @Nonnull public final byte[] ipv4AddressBytes(@Nonnull final A4 addr) {
209 * This implementation relies heavily on the input string having been validated to comply with
210 * the Ipv4Address pattern, which may include a zone index.
212 final String str = ipv4AddressString(addr);
213 final int percent = str.indexOf('%');
214 return ipv4AddressBytes(str, percent == -1 ? str.length() : percent);
217 protected static final byte[] ipv4AddressBytes(@Nonnull final String str, final int length) {
218 final byte[] bytes = new byte[INET4_LENGTH];
219 Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, length);
224 * Create a /32 Ipv4Prefix by interpreting input bytes as an IPv4 address.
226 * @param bytes four-byte array
227 * @return An Ipv4Prefix object
228 * @throws IllegalArgumentException if bytes has length different from 4
229 * @throws NullPointerException if bytes is null
231 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] bytes) {
232 return prefix4Factory.newInstance(prefixStringV4(bytes));
236 * Create a Ipv4Prefix by combining the address with a mask. The address
237 * bytes are interpreted as an address and the specified mask is concatenated to
238 * it. The address bytes are not masked, hence input <code>address = { 1, 2, 3, 4 }</code>
239 * and <code>mask=24</code> will result in <code>1.2.3.4/24</code>.
241 * @param address Input address as a 4-byte array
242 * @param mask Prefix mask
243 * @return An Ipv4Prefix object
244 * @throws IllegalArgumentException if bytes has length different from 4 or if mask is not in range 0-32
245 * @throws NullPointerException if bytes is null
247 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] address, final int mask) {
248 return prefix4Factory.newInstance(prefixStringV4(address, mask));
251 @Nonnull public final P4 ipv4PrefixForShort(@Nonnull final byte[] address, final int mask) {
253 // Easy case, reuse the template
254 return prefix4Factory.getTemplate();
257 return v4PrefixForShort(address, 0, mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1), mask);
260 @Nonnull public final P4 ipv4PrefixForShort(@Nonnull final byte[] array, final int startOffset, final int mask) {
262 // Easy case, reuse the template
263 return prefix4Factory.getTemplate();
266 return v4PrefixForShort(array, startOffset, mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1), mask);
270 * Create a /32 Ipv4Prefix for an {@link Inet4Address}
272 * @param addr An {@link Inet4Address}
273 * @return An Ipv4Prefix object
274 * @throws IllegalArgumentException if addr is not an Inet4Address
275 * @throws NullPointerException if adds is null
277 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final InetAddress addr) {
278 Preconditions.checkNotNull(addr, "Address must not be null");
279 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
280 return prefix4Factory.newInstance(addr.getHostAddress() + "/32");
284 * Create a Ipv4Prefix by combining the address with a mask. The address bytes are not masked.
286 * @param addr An {@link Inet4Address}
287 * @param mask Prefix mask
288 * @return An Ipv4Prefix object
289 * @throws IllegalArgumentException if addr is not an Inet4Address or if mask is not in range 0-32
290 * @throws NullPointerException if addr is null
292 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final InetAddress addr, final int mask) {
293 Preconditions.checkNotNull(addr, "Address must not be null");
294 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
295 Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
296 return prefix4Factory.newInstance(addr.getHostAddress() + '/' + mask);
299 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final A4 addr) {
300 Preconditions.checkNotNull(addr, "Address must not be null");
301 return prefix4Factory.newInstance(ipv4AddressString(addr) + "/32");
304 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final A4 addr, final int mask) {
305 Preconditions.checkNotNull(addr, "Address must not be null");
306 Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
307 return prefix4Factory.newInstance(ipv4AddressString(addr) + '/' + mask);
310 @Nonnull public final Entry<A4NZ, Integer> splitIpv4Prefix(@Nonnull final P4 prefix) {
311 return splitPrefix(nozone4Factory, ipv4PrefixString(prefix));
314 @Nonnull public final byte[] ipv4PrefixToBytes(@Nonnull final P4 prefix) {
315 final String str = ipv4PrefixString(prefix);
316 final int slash = str.lastIndexOf('/');
318 final byte[] bytes = new byte[INET4_LENGTH + 1];
319 Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, slash);
320 bytes[INET4_LENGTH] = (byte)Integer.parseInt(str.substring(slash + 1), 10);
325 * Create an Ipv6Address by interpreting input bytes as an IPv6 address.
327 * @param bytes 16-byte array
328 * @return An Ipv6Address object
329 * @throws IllegalArgumentException if bytes has length different from 16
330 * @throws NullPointerException if bytes is null
332 @Nonnull public final A6NZ ipv6AddressFor(@Nonnull final byte[] bytes) {
333 return nozone6Factory.newInstance(addressStringV6(bytes));
337 * Create an Ipv6Address by interpreting an {@link Inet6Address}.
339 * @param addr An {@link Inet6Address}
340 * @return An Ipv6Address object
341 * @throws IllegalArgumentException if addr is not an {@link Inet6Address}
342 * @throws NullPointerException if addr is null
344 @Nonnull public final A6NZ ipv6AddressFor(@Nonnull final InetAddress addr) {
345 Preconditions.checkNotNull(addr, "Address must not be null");
346 Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
347 return nozone6Factory.newInstance(addressStringV6(addr));
350 @Nonnull public final A6 ipv6AddressFrom(@Nonnull final P6 prefix) {
351 return prefixToAddress(nozone6Factory, ipv6PrefixString(prefix));
354 @Nonnull public final byte[] ipv6AddressBytes(@Nonnull final A6 addr) {
355 final String str = ipv6AddressString(addr);
356 final int percent = str.indexOf('%');
357 return ipv6AddressBytes(str, percent == -1 ? str.length() : percent);
360 protected static final byte[] ipv6AddressBytes(@Nonnull final String str, final int length) {
361 final byte[] bytes = new byte[INET6_LENGTH];
362 Ipv6Utils.fillIpv6Bytes(bytes, str, length);
368 * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv6 address.
370 * @param bytes four-byte array
371 * @return An Ipv6Prefix object
372 * @throws IllegalArgumentException if bytes has length different from 16
373 * @throws NullPointerException if bytes is null
375 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] bytes) {
376 return prefix6Factory.newInstance(addressStringV6(bytes) + "/128");
380 * Create a Ipv6Prefix by combining the address with a mask. The address
381 * bytes are interpreted as an address and the specified mask is concatenated to
382 * it. The address bytes are not masked.
384 * @param address Input address as a 16-byte array
385 * @param mask Prefix mask
386 * @return An Ipv6Prefix object
387 * @throws IllegalArgumentException if bytes has length different from 16 or if mask is not in range 0-128
388 * @throws NullPointerException if bytes is null
390 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] address, final int mask) {
391 Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
392 return prefix6Factory.newInstance(addressStringV6(address) + '/' + mask);
395 @Nonnull public final P6 ipv6PrefixForShort(@Nonnull final byte[] address, final int mask) {
396 return ipv6PrefixForShort(address, 0, mask);
399 @Nonnull public final P6 ipv6PrefixForShort(@Nonnull final byte[] array, final int startOffset, final int mask) {
401 // Easy case, reuse the template
402 return prefix6Factory.getTemplate();
405 Preconditions.checkArgument(mask > 0 && mask <= 128, "Invalid mask %s", mask);
406 final int size = mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1);
408 // Until we can instantiate an IPv6 address for a partial array, use a temporary buffer
409 byte[] tmp = new byte[INET6_LENGTH];
410 System.arraycopy(array, startOffset, tmp, 0, size);
411 return ipv6PrefixFor(tmp, mask);
415 * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv4 address.
417 * @param addr an {@link Inet6Address}
418 * @return An Ipv6Prefix object
419 * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
420 * @throws NullPointerException if addr is null
422 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr) {
423 return prefix6Factory.newInstance(addressStringV6(addr) + "/128");
427 * Create a Ipv6Prefix by combining the address with a mask. The address
428 * bytes are interpreted as an address and the specified mask is concatenated to
429 * it. The address bytes are not masked.
431 * @param addr Input address
432 * @param mask Prefix mask
433 * @return An Ipv6Prefix object
434 * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
435 * @throws NullPointerException if addr is null
437 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr, final int mask) {
438 Preconditions.checkNotNull(addr, "Address must not be null");
439 Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
440 Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
441 return prefix6Factory.newInstance(addressStringV6(addr) + '/' + mask);
444 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final A6 addr) {
445 Preconditions.checkNotNull(addr, "Address must not be null");
446 return prefix6Factory.newInstance(ipv6AddressString(addr) + "/128");
449 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final A6 addr, final int mask) {
450 Preconditions.checkNotNull(addr, "Address must not be null");
451 Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
452 return prefix6Factory.newInstance(ipv6AddressString(addr) + '/' + mask);
455 @Nonnull public final Entry<A6NZ, Integer> splitIpv6Prefix(@Nonnull final P6 prefix) {
456 return splitPrefix(nozone6Factory, ipv6PrefixString(prefix));
459 private static <T> T prefixToAddress(final StringValueObjectFactory<T> factory, final String str) {
460 return factory.newInstance(str.substring(0, str.lastIndexOf('/')));
463 private static <T> Entry<T, Integer> splitPrefix(final StringValueObjectFactory<T> factory, final String str) {
464 final int slash = str.lastIndexOf('/');
465 return new SimpleImmutableEntry<>(factory.newInstance(str.substring(0, slash)),
466 Integer.valueOf(str.substring(slash + 1)));
469 @Nonnull public final byte[] ipv6PrefixToBytes(@Nonnull final P6 prefix) {
470 final String str = ipv6PrefixString(prefix);
471 final byte[] bytes = new byte[INET6_LENGTH + 1];
472 final int slash = str.lastIndexOf('/');
473 Ipv6Utils.fillIpv6Bytes(bytes, str, slash);
474 bytes[INET6_LENGTH] = (byte)Integer.parseInt(str.substring(slash + 1), 10);
478 private static void appendIpv4String(final StringBuilder sb, final byte[] bytes) {
479 Preconditions.checkArgument(bytes.length == INET4_LENGTH, "IPv4 address length is 4 bytes");
481 sb.append(Byte.toUnsignedInt(bytes[0]));
482 for (int i = 1; i < INET4_LENGTH; ++i) {
484 sb.append(Byte.toUnsignedInt(bytes[i]));
488 private static String addressStringV4(final byte[] bytes) {
489 final StringBuilder sb = new StringBuilder(15);
490 appendIpv4String(sb, bytes);
491 return sb.toString();
494 private static String addressStringV6(final byte[] bytes) {
495 Preconditions.checkArgument(bytes.length == INET6_LENGTH, "IPv6 address length is 16 bytes");
498 return addressStringV6(Inet6Address.getByAddress(bytes));
499 } catch (UnknownHostException e) {
500 throw new IllegalArgumentException(String.format("Invalid input %s", bytes), e);
504 private static String addressStringV6(final InetAddress addr) {
505 return InetAddresses.toAddrString(addr);
508 private static String prefixStringV4(final byte[] bytes) {
509 final StringBuilder sb = new StringBuilder(18);
510 appendIpv4String(sb, bytes);
512 return sb.toString();
515 private static String prefixStringV4(final byte[] bytes, final int mask) {
516 Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
518 final StringBuilder sb = new StringBuilder(18);
519 appendIpv4String(sb, bytes);
522 return sb.toString();
525 private P4 v4PrefixForShort(@Nonnull final byte[] array, final int startOffset, final int size, final int mask) {
526 if (startOffset == 0 && size == INET4_LENGTH && array.length == INET4_LENGTH) {
527 // Easy case, fall back to non-short
528 return ipv4PrefixFor(array, mask);
531 final StringBuilder sb = new StringBuilder(18);
534 sb.append(Byte.toUnsignedInt(array[startOffset]));
535 for (int i = 1; i < size; i++) {
537 sb.append(Byte.toUnsignedInt(array[startOffset + i]));
541 for (int i = size; i < INET4_LENGTH; i++) {
546 Preconditions.checkArgument(mask > 0 && mask <= 32, "Invalid mask %s", mask);
550 return prefix4Factory.newInstance(sb.toString());