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 com.google.common.primitives.UnsignedBytes;
14 import java.net.Inet4Address;
15 import java.net.Inet6Address;
16 import java.net.InetAddress;
17 import java.net.UnknownHostException;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.yang.binding.util.StringValueObjectFactory;
22 * A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
24 * FIXME: IPv6 addresses are not emitted in canonical format as specified by the model.
27 public abstract class AbstractIetfInetUtil<A4, P4, A6, P6, A> {
28 private final StringValueObjectFactory<A4> address4Factory;
29 private final StringValueObjectFactory<P4> prefix4Factory;
30 private final StringValueObjectFactory<A6> address6Factory;
31 private final StringValueObjectFactory<P6> prefix6Factory;
33 protected AbstractIetfInetUtil(final Class<A4> addr4Class, final Class<P4> prefix4Class,
34 final Class<A6> addr6Class, final Class<P6> prefix6Class) {
35 this.address4Factory = StringValueObjectFactory.create(addr4Class, "0.0.0.0");
36 this.prefix4Factory = StringValueObjectFactory.create(prefix4Class, "0.0.0.0/0");
37 this.address6Factory = StringValueObjectFactory.create(addr6Class, "::0");
38 this.prefix6Factory = StringValueObjectFactory.create(prefix6Class, "::0/0");
41 protected abstract A ipv4Address(A4 addr);
42 protected abstract A ipv6Address(A6 addr);
44 @Nonnull public final A ipAddressFor(@Nonnull final byte[] bytes) {
45 switch (bytes.length) {
47 return ipv4Address(ipv4AddressFor(bytes));
49 return ipv6Address(ipv6AddressFor(bytes));
51 throw new IllegalArgumentException("Invalid array length " + bytes.length);
55 @Nonnull public final A ipAddressFor(@Nonnull final InetAddress addr) {
56 Preconditions.checkNotNull(addr, "Address must not be null");
57 if (addr instanceof Inet4Address) {
58 return ipv4Address(ipv4AddressFor(addr));
59 } else if (addr instanceof Inet6Address) {
60 return ipv6Address(ipv6AddressFor(addr));
62 throw new IllegalArgumentException("Unhandled address " + addr);
67 * Create an Ipv4Address by interpreting input bytes as an IPv4 address.
69 * @param bytes 4-byte array
70 * @return An Ipv4Address object
71 * @throws IllegalArgumentException if bytes has length different from 4
72 * @throws NullPointerException if bytes is null
74 @Nonnull public final A4 ipv4AddressFor(@Nonnull final byte[] bytes) {
75 return address4Factory.newInstance(addressStringV4(bytes));
79 * Create an Ipv4Address by interpreting an {@link Inet4Address}.
81 * @param addr An {@link Inet4Address}
82 * @return An Ipv4Address object
83 * @throws IllegalArgumentException if addr is not an {@link Inet4Address}
84 * @throws NullPointerException if addr is null
86 @Nonnull public final A4 ipv4AddressFor(@Nonnull final InetAddress addr) {
87 Preconditions.checkNotNull(addr, "Address must not be null");
88 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
89 return address4Factory.newInstance(addr.getHostAddress());
93 * Create a /32 Ipv4Prefix by interpreting input bytes as an IPv4 address.
95 * @param bytes four-byte array
96 * @return An Ipv4Prefix object
97 * @throws IllegalArgumentException if bytes has length different from 4
98 * @throws NullPointerException if bytes is null
100 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] bytes) {
101 return prefix4Factory.newInstance(prefixStringV4(bytes));
105 * Create a Ipv4Prefix by combining the address with a mask. The address
106 * bytes are interpreted as an address and the specified mask is concatenated to
107 * it. The address bytes are not masked, hence input <code>address = { 1, 2, 3, 4 }</code>
108 * and <code>mask=24</code> will result in <code>1.2.3.4/24</code>.
110 * @param address Input address as a 4-byte array
111 * @param mask Prefix mask
112 * @return An Ipv4Prefix object
113 * @throws IllegalArgumentException if bytes has length different from 4 or if mask is not in range 0-32
114 * @throws NullPointerException if bytes is null
116 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] address, final int mask) {
117 return prefix4Factory.newInstance(prefixStringV4(address, mask));
121 * Create a /32 Ipv4Prefix for an {@link Inet4Address}
123 * @param addr An {@link Inet4Address}
124 * @return An Ipv4Prefix object
125 * @throws IllegalArgumentException if addr is not an Inet4Address
126 * @throws NullPointerException if adds is null
128 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final InetAddress addr) {
129 Preconditions.checkNotNull(addr, "Address must not be null");
130 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
131 return prefix4Factory.newInstance(addr.getHostAddress() + "/32");
135 * Create a Ipv4Prefix by combining the address with a mask. The address bytes are not masked.
137 * @param addr An {@link Inet4Address}
138 * @param mask Prefix mask
139 * @return An Ipv4Prefix object
140 * @throws IllegalArgumentException if addr is not an Inet4Address or if mask is not in range 0-32
141 * @throws NullPointerException if addr is null
143 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final InetAddress addr, final int mask) {
144 Preconditions.checkNotNull(addr, "Address must not be null");
145 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
146 Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
147 return prefix4Factory.newInstance(addr.getHostAddress() + '/' + mask);
151 * Create an Ipv6Address by interpreting input bytes as an IPv6 address.
153 * @param bytes 16-byte array
154 * @return An Ipv6Address object
155 * @throws IllegalArgumentException if bytes has length different from 16
156 * @throws NullPointerException if bytes is null
158 @Nonnull public final A6 ipv6AddressFor(@Nonnull final byte[] bytes) {
159 return address6Factory.newInstance(addressStringV6(bytes));
163 * Create an Ipv6Address by interpreting an {@link Inet6Address}.
165 * @param addr An {@link Inet6Address}
166 * @return An Ipv6Address object
167 * @throws IllegalArgumentException if addr is not an {@link Inet6Address}
168 * @throws NullPointerException if addr is null
170 @Nonnull public final A6 ipv6AddressFor(@Nonnull final InetAddress addr) {
171 Preconditions.checkNotNull(addr, "Address must not be null");
172 Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
173 return address6Factory.newInstance(addr.getHostAddress());
177 * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv6 address.
179 * @param bytes four-byte array
180 * @return An Ipv6Prefix object
181 * @throws IllegalArgumentException if bytes has length different from 16
182 * @throws NullPointerException if bytes is null
184 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] bytes) {
185 return prefix6Factory.newInstance(addressStringV6(bytes) + "/128");
189 * Create a Ipv6Prefix by combining the address with a mask. The address
190 * bytes are interpreted as an address and the specified mask is concatenated to
191 * it. The address bytes are not masked.
193 * @param address Input address as a 4-byte array
194 * @param mask Prefix mask
195 * @return An Ipv6Prefix object
196 * @throws IllegalArgumentException if bytes has length different from 16 or if mask is not in range 0-128
197 * @throws NullPointerException if bytes is null
199 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] address, final int mask) {
200 Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
201 return prefix6Factory.newInstance(addressStringV6(address) + '/' + mask);
205 * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv4 address.
207 * @param addr an {@link Inet6Address}
208 * @return An Ipv6Prefix object
209 * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
210 * @throws NullPointerException if addr is null
212 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr) {
213 return prefix6Factory.newInstance(addressStringV6(addr) + "/128");
217 * Create a Ipv6Prefix by combining the address with a mask. The address
218 * bytes are interpreted as an address and the specified mask is concatenated to
219 * it. The address bytes are not masked.
221 * @param addr Input address
222 * @param mask Prefix mask
223 * @return An Ipv6Prefix object
224 * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
225 * @throws NullPointerException if addr is null
227 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr, final int mask) {
228 Preconditions.checkNotNull(addr, "Address must not be null");
229 Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
230 Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
231 return prefix6Factory.newInstance(addressStringV6(addr) + '/' + mask);
234 private static void appendIpv4String(final StringBuilder sb, final byte[] bytes) {
235 Preconditions.checkArgument(bytes.length == 4, "IPv4 address length is 4 bytes");
237 sb.append(UnsignedBytes.toInt(bytes[0]));
238 for (int i = 1; i < 4; ++i) {
240 sb.append(UnsignedBytes.toInt(bytes[i]));
244 private static String addressStringV4(final byte[] bytes) {
245 final StringBuilder sb = new StringBuilder(15);
246 appendIpv4String(sb, bytes);
247 return sb.toString();
250 private static String addressStringV6(final byte[] bytes) {
251 Preconditions.checkArgument(bytes.length == 16, "IPv6 address length is 16 bytes");
254 return addressStringV6(Inet6Address.getByAddress(bytes));
255 } catch (UnknownHostException e) {
256 throw new IllegalArgumentException(String.format("Invalid input %s", bytes), e);
260 private static String addressStringV6(final InetAddress addr) {
261 return InetAddresses.toAddrString(addr);
264 private static String prefixStringV4(final byte[] bytes) {
265 final StringBuilder sb = new StringBuilder(18);
266 appendIpv4String(sb, bytes);
268 return sb.toString();
271 private static String prefixStringV4(final byte[] bytes, final int mask) {
272 Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
274 final StringBuilder sb = new StringBuilder(18);
275 appendIpv4String(sb, bytes);
278 return sb.toString();