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 org.opendaylight.yangtools.yang.binding.util.StringValueObjectFactory;
23 * A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
26 public abstract class AbstractIetfInetUtil<A4, P4, A6, P6, A> {
27 private final StringValueObjectFactory<A4> address4Factory;
28 private final StringValueObjectFactory<P4> prefix4Factory;
29 private final StringValueObjectFactory<A6> address6Factory;
30 private final StringValueObjectFactory<P6> prefix6Factory;
32 protected AbstractIetfInetUtil(final Class<A4> addr4Class, final Class<P4> prefix4Class,
33 final Class<A6> addr6Class, final Class<P6> prefix6Class) {
34 this.address4Factory = StringValueObjectFactory.create(addr4Class, "0.0.0.0");
35 this.prefix4Factory = StringValueObjectFactory.create(prefix4Class, "0.0.0.0/0");
36 this.address6Factory = StringValueObjectFactory.create(addr6Class, "::0");
37 this.prefix6Factory = StringValueObjectFactory.create(prefix6Class, "::0/0");
40 protected abstract A ipv4Address(A4 addr);
41 protected abstract A ipv6Address(A6 addr);
42 protected abstract String ipv4AddressString(A4 addr);
43 protected abstract String ipv6AddressString(A6 addr);
44 protected abstract String ipv4PrefixString(P4 prefix);
45 protected abstract String ipv6PrefixString(P6 prefix);
47 @Nonnull public final A ipAddressFor(@Nonnull final byte[] bytes) {
48 switch (bytes.length) {
50 return ipv4Address(ipv4AddressFor(bytes));
52 return ipv6Address(ipv6AddressFor(bytes));
54 throw new IllegalArgumentException("Invalid array length " + bytes.length);
58 @Nonnull public final A ipAddressFor(@Nonnull final InetAddress addr) {
59 Preconditions.checkNotNull(addr, "Address must not be null");
60 if (addr instanceof Inet4Address) {
61 return ipv4Address(ipv4AddressFor(addr));
62 } else if (addr instanceof Inet6Address) {
63 return ipv6Address(ipv6AddressFor(addr));
65 throw new IllegalArgumentException("Unhandled address " + addr);
70 * Create an Ipv4Address by interpreting input bytes as an IPv4 address.
72 * @param bytes 4-byte array
73 * @return An Ipv4Address object
74 * @throws IllegalArgumentException if bytes has length different from 4
75 * @throws NullPointerException if bytes is null
77 @Nonnull public final A4 ipv4AddressFor(@Nonnull final byte[] bytes) {
78 return address4Factory.newInstance(addressStringV4(bytes));
82 * Create an Ipv4Address by interpreting an {@link Inet4Address}.
84 * @param addr An {@link Inet4Address}
85 * @return An Ipv4Address object
86 * @throws IllegalArgumentException if addr is not an {@link Inet4Address}
87 * @throws NullPointerException if addr is null
89 @Nonnull public final A4 ipv4AddressFor(@Nonnull final InetAddress addr) {
90 Preconditions.checkNotNull(addr, "Address must not be null");
91 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
92 return address4Factory.newInstance(addr.getHostAddress());
95 @Nonnull public final A4 ipv4AddressFrom(@Nonnull final P4 prefix) {
96 return prefixToAddress(address4Factory, ipv4PrefixString(prefix));
100 * Create a /32 Ipv4Prefix by interpreting input bytes as an IPv4 address.
102 * @param bytes four-byte array
103 * @return An Ipv4Prefix object
104 * @throws IllegalArgumentException if bytes has length different from 4
105 * @throws NullPointerException if bytes is null
107 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] bytes) {
108 return prefix4Factory.newInstance(prefixStringV4(bytes));
112 * Create a Ipv4Prefix by combining the address with a mask. The address
113 * bytes are interpreted as an address and the specified mask is concatenated to
114 * it. The address bytes are not masked, hence input <code>address = { 1, 2, 3, 4 }</code>
115 * and <code>mask=24</code> will result in <code>1.2.3.4/24</code>.
117 * @param address Input address as a 4-byte array
118 * @param mask Prefix mask
119 * @return An Ipv4Prefix object
120 * @throws IllegalArgumentException if bytes has length different from 4 or if mask is not in range 0-32
121 * @throws NullPointerException if bytes is null
123 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] address, final int mask) {
124 return prefix4Factory.newInstance(prefixStringV4(address, mask));
128 * Create a /32 Ipv4Prefix for an {@link Inet4Address}
130 * @param addr An {@link Inet4Address}
131 * @return An Ipv4Prefix object
132 * @throws IllegalArgumentException if addr is not an Inet4Address
133 * @throws NullPointerException if adds is null
135 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final InetAddress addr) {
136 Preconditions.checkNotNull(addr, "Address must not be null");
137 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
138 return prefix4Factory.newInstance(addr.getHostAddress() + "/32");
142 * Create a Ipv4Prefix by combining the address with a mask. The address bytes are not masked.
144 * @param addr An {@link Inet4Address}
145 * @param mask Prefix mask
146 * @return An Ipv4Prefix object
147 * @throws IllegalArgumentException if addr is not an Inet4Address or if mask is not in range 0-32
148 * @throws NullPointerException if addr is null
150 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final InetAddress addr, final int mask) {
151 Preconditions.checkNotNull(addr, "Address must not be null");
152 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
153 Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
154 return prefix4Factory.newInstance(addr.getHostAddress() + '/' + mask);
157 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final A4 addr) {
158 Preconditions.checkNotNull(addr, "Address must not be null");
159 return prefix4Factory.newInstance(ipv4AddressString(addr) + "/32");
162 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final A4 addr, final int mask) {
163 Preconditions.checkNotNull(addr, "Address must not be null");
164 Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
165 return prefix4Factory.newInstance(ipv4AddressString(addr) + '/' + mask);
168 @Nonnull public final Entry<A4, Integer> splitIpv4Prefix(@Nonnull final P4 prefix) {
169 return splitPrefix(address4Factory, ipv4PrefixString(prefix));
173 * Create an Ipv6Address by interpreting input bytes as an IPv6 address.
175 * @param bytes 16-byte array
176 * @return An Ipv6Address object
177 * @throws IllegalArgumentException if bytes has length different from 16
178 * @throws NullPointerException if bytes is null
180 @Nonnull public final A6 ipv6AddressFor(@Nonnull final byte[] bytes) {
181 return address6Factory.newInstance(addressStringV6(bytes));
185 * Create an Ipv6Address by interpreting an {@link Inet6Address}.
187 * @param addr An {@link Inet6Address}
188 * @return An Ipv6Address object
189 * @throws IllegalArgumentException if addr is not an {@link Inet6Address}
190 * @throws NullPointerException if addr is null
192 @Nonnull public final A6 ipv6AddressFor(@Nonnull final InetAddress addr) {
193 Preconditions.checkNotNull(addr, "Address must not be null");
194 Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
195 return address6Factory.newInstance(addressStringV6(addr));
198 @Nonnull public final A6 ipv6AddressFrom(@Nonnull final P6 prefix) {
199 return prefixToAddress(address6Factory, ipv6PrefixString(prefix));
203 * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv6 address.
205 * @param bytes four-byte array
206 * @return An Ipv6Prefix object
207 * @throws IllegalArgumentException if bytes has length different from 16
208 * @throws NullPointerException if bytes is null
210 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] bytes) {
211 return prefix6Factory.newInstance(addressStringV6(bytes) + "/128");
215 * Create a Ipv6Prefix by combining the address with a mask. The address
216 * bytes are interpreted as an address and the specified mask is concatenated to
217 * it. The address bytes are not masked.
219 * @param address Input address as a 4-byte array
220 * @param mask Prefix mask
221 * @return An Ipv6Prefix object
222 * @throws IllegalArgumentException if bytes has length different from 16 or if mask is not in range 0-128
223 * @throws NullPointerException if bytes is null
225 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] address, final int mask) {
226 Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
227 return prefix6Factory.newInstance(addressStringV6(address) + '/' + mask);
231 * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv4 address.
233 * @param addr an {@link Inet6Address}
234 * @return An Ipv6Prefix object
235 * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
236 * @throws NullPointerException if addr is null
238 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr) {
239 return prefix6Factory.newInstance(addressStringV6(addr) + "/128");
243 * Create a Ipv6Prefix by combining the address with a mask. The address
244 * bytes are interpreted as an address and the specified mask is concatenated to
245 * it. The address bytes are not masked.
247 * @param addr Input address
248 * @param mask Prefix mask
249 * @return An Ipv6Prefix object
250 * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
251 * @throws NullPointerException if addr is null
253 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr, final int mask) {
254 Preconditions.checkNotNull(addr, "Address must not be null");
255 Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
256 Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
257 return prefix6Factory.newInstance(addressStringV6(addr) + '/' + mask);
260 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final A6 addr) {
261 Preconditions.checkNotNull(addr, "Address must not be null");
262 return prefix6Factory.newInstance(ipv6AddressString(addr) + "/128");
265 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final A6 addr, final int mask) {
266 Preconditions.checkNotNull(addr, "Address must not be null");
267 Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
268 return prefix6Factory.newInstance(ipv6AddressString(addr) + '/' + mask);
271 @Nonnull public final Entry<A6, Integer> splitIpv6Prefix(@Nonnull final P6 prefix) {
272 return splitPrefix(address6Factory, ipv6PrefixString(prefix));
275 private static <T> T prefixToAddress(final StringValueObjectFactory<T> factory, final String str) {
276 return factory.newInstance(str.substring(0, str.lastIndexOf('/')));
279 private static <T> Entry<T, Integer> splitPrefix(final StringValueObjectFactory<T> factory, final String str) {
280 final int slash = str.lastIndexOf('/');
281 return new SimpleImmutableEntry<>(factory.newInstance(str.substring(0, slash)),
282 Integer.valueOf(str.substring(slash + 1)));
285 private static void appendIpv4String(final StringBuilder sb, final byte[] bytes) {
286 Preconditions.checkArgument(bytes.length == 4, "IPv4 address length is 4 bytes");
288 sb.append(Byte.toUnsignedInt(bytes[0]));
289 for (int i = 1; i < 4; ++i) {
291 sb.append(Byte.toUnsignedInt(bytes[i]));
295 private static String addressStringV4(final byte[] bytes) {
296 final StringBuilder sb = new StringBuilder(15);
297 appendIpv4String(sb, bytes);
298 return sb.toString();
301 private static String addressStringV6(final byte[] bytes) {
302 Preconditions.checkArgument(bytes.length == 16, "IPv6 address length is 16 bytes");
305 return addressStringV6(Inet6Address.getByAddress(bytes));
306 } catch (UnknownHostException e) {
307 throw new IllegalArgumentException(String.format("Invalid input %s", bytes), e);
311 private static String addressStringV6(final InetAddress addr) {
312 return InetAddresses.toAddrString(addr);
315 private static String prefixStringV4(final byte[] bytes) {
316 final StringBuilder sb = new StringBuilder(18);
317 appendIpv4String(sb, bytes);
319 return sb.toString();
322 private static String prefixStringV4(final byte[] bytes, final int mask) {
323 Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
325 final StringBuilder sb = new StringBuilder(18);
326 appendIpv4String(sb, bytes);
329 return sb.toString();