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.primitives.UnsignedBytes;
13 import java.net.Inet4Address;
14 import java.net.Inet6Address;
15 import java.net.InetAddress;
16 import java.net.UnknownHostException;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.yang.binding.util.StringValueObjectFactory;
21 * A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
23 * FIXME: IPv6 addresses are not emitted in canonical format as specified by the model.
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);
43 @Nonnull public final A ipAddressFor(@Nonnull final byte[] bytes) {
44 switch (bytes.length) {
46 return ipv4Address(ipv4AddressFor(bytes));
48 return ipv6Address(ipv6AddressFor(bytes));
50 throw new IllegalArgumentException("Invalid array length " + bytes.length);
54 @Nonnull public final A ipAddressFor(@Nonnull final InetAddress addr) {
55 Preconditions.checkNotNull(addr, "Address must not be null");
56 if (addr instanceof Inet4Address) {
57 return ipv4Address(ipv4AddressFor(addr));
58 } else if (addr instanceof Inet6Address) {
59 return ipv6Address(ipv6AddressFor(addr));
61 throw new IllegalArgumentException("Unhandled address " + addr);
66 * Create an Ipv4Address by interpreting input bytes as an IPv4 address.
68 * @param bytes 4-byte array
69 * @return An Ipv4Address object
70 * @throws IllegalArgumentException if bytes has length different from 4
71 * @throws NullPointerException if bytes is null
73 @Nonnull public final A4 ipv4AddressFor(@Nonnull final byte[] bytes) {
74 return address4Factory.newInstance(addressStringV4(bytes));
78 * Create an Ipv4Address by interpreting an {@link Inet4Address}.
80 * @param addr An {@link Inet4Address}
81 * @return An Ipv4Address object
82 * @throws IllegalArgumentException if addr is not an {@link Inet4Address}
83 * @throws NullPointerException if addr is null
85 @Nonnull public final A4 ipv4AddressFor(@Nonnull final InetAddress addr) {
86 Preconditions.checkNotNull(addr, "Address must not be null");
87 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
88 return address4Factory.newInstance(addr.getHostAddress());
92 * Create a /32 Ipv4Prefix by interpreting input bytes as an IPv4 address.
94 * @param bytes four-byte array
95 * @return An Ipv4Prefix object
96 * @throws IllegalArgumentException if bytes has length different from 4
97 * @throws NullPointerException if bytes is null
99 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] bytes) {
100 return prefix4Factory.newInstance(prefixStringV4(bytes));
104 * Create a Ipv4Prefix by combining the address with a mask. The address
105 * bytes are interpreted as an address and the specified mask is concatenated to
106 * it. The address bytes are not masked, hence input <code>address = { 1, 2, 3, 4 }</code>
107 * and <code>mask=24</code> will result in <code>1.2.3.4/24</code>.
109 * @param address Input address as a 4-byte array
110 * @param mask Prefix mask
111 * @return An Ipv4Prefix object
112 * @throws IllegalArgumentException if bytes has length different from 4 or if mask is not in range 0-32
113 * @throws NullPointerException if bytes is null
115 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] address, final int mask) {
116 return prefix4Factory.newInstance(prefixStringV4(address, mask));
120 * Create a /32 Ipv4Prefix for an {@link Inet4Address}
122 * @param addr An {@link Inet4Address}
123 * @return An Ipv4Prefix object
124 * @throws IllegalArgumentException if addr is not an Inet4Address
125 * @throws NullPointerException if adds is null
127 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final InetAddress addr) {
128 Preconditions.checkNotNull(addr, "Address must not be null");
129 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
130 return prefix4Factory.newInstance(addr.getHostAddress() + "/32");
134 * Create a Ipv4Prefix by combining the address with a mask. The address bytes are not masked.
136 * @param addr An {@link Inet4Address}
137 * @param mask Prefix mask
138 * @return An Ipv4Prefix object
139 * @throws IllegalArgumentException if addr is not an Inet4Address or if mask is not in range 0-32
140 * @throws NullPointerException if addr is null
142 @Nonnull public final P4 ipv4PrefixFor(@Nonnull final InetAddress addr, final int mask) {
143 Preconditions.checkNotNull(addr, "Address must not be null");
144 Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
145 Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
146 return prefix4Factory.newInstance(addr.getHostAddress() + '/' + mask);
150 * Create an Ipv6Address by interpreting input bytes as an IPv6 address.
152 * @param bytes 16-byte array
153 * @return An Ipv6Address object
154 * @throws IllegalArgumentException if bytes has length different from 16
155 * @throws NullPointerException if bytes is null
157 @Nonnull public final A6 ipv6AddressFor(@Nonnull final byte[] bytes) {
158 return address6Factory.newInstance(addressStringV6(bytes));
162 * Create an Ipv6Address by interpreting an {@link Inet6Address}.
164 * @param addr An {@link Inet6Address}
165 * @return An Ipv6Address object
166 * @throws IllegalArgumentException if addr is not an {@link Inet6Address}
167 * @throws NullPointerException if addr is null
169 @Nonnull public final A6 ipv6AddressFor(@Nonnull final InetAddress addr) {
170 Preconditions.checkNotNull(addr, "Address must not be null");
171 Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
172 return address6Factory.newInstance(addr.getHostAddress());
176 * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv6 address.
178 * @param bytes four-byte array
179 * @return An Ipv6Prefix object
180 * @throws IllegalArgumentException if bytes has length different from 16
181 * @throws NullPointerException if bytes is null
183 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] bytes) {
184 return prefix6Factory.newInstance(addressStringV6(bytes) + "/128");
188 * Create a Ipv6Prefix by combining the address with a mask. The address
189 * bytes are interpreted as an address and the specified mask is concatenated to
190 * it. The address bytes are not masked.
192 * @param address Input address as a 4-byte array
193 * @param mask Prefix mask
194 * @return An Ipv6Prefix object
195 * @throws IllegalArgumentException if bytes has length different from 16 or if mask is not in range 0-128
196 * @throws NullPointerException if bytes is null
198 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] address, final int mask) {
199 Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
200 return prefix6Factory.newInstance(addressStringV6(address) + '/' + mask);
204 * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv4 address.
206 * @param addr an {@link Inet6Address}
207 * @return An Ipv6Prefix object
208 * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
209 * @throws NullPointerException if addr is null
211 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr) {
212 return prefix6Factory.newInstance(addr.getHostAddress() + "/128");
216 * Create a Ipv6Prefix by combining the address with a mask. The address
217 * bytes are interpreted as an address and the specified mask is concatenated to
218 * it. The address bytes are not masked.
220 * @param addr Input address
221 * @param mask Prefix mask
222 * @return An Ipv6Prefix object
223 * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
224 * @throws NullPointerException if addr is null
226 @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr, final int mask) {
227 Preconditions.checkNotNull(addr, "Address must not be null");
228 Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
229 Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
230 return prefix6Factory.newInstance(addr.getHostAddress() + '/' + mask);
233 private static void appendIpv4String(final StringBuilder sb, final byte[] bytes) {
234 Preconditions.checkArgument(bytes.length == 4, "IPv4 address length is 4 bytes");
236 sb.append(UnsignedBytes.toInt(bytes[0]));
237 for (int i = 1; i < 4; ++i) {
239 sb.append(UnsignedBytes.toInt(bytes[i]));
243 private static String addressStringV4(final byte[] bytes) {
244 final StringBuilder sb = new StringBuilder(15);
245 appendIpv4String(sb, bytes);
246 return sb.toString();
249 private static String addressStringV6(final byte[] bytes) {
250 Preconditions.checkArgument(bytes.length == 16, "IPv6 address length is 16 bytes");
253 return Inet6Address.getByAddress(bytes).getHostAddress();
254 } catch (UnknownHostException e) {
255 throw new IllegalArgumentException(String.format("Invalid input %s", bytes), e);
259 private static String prefixStringV4(final byte[] bytes) {
260 final StringBuilder sb = new StringBuilder(18);
261 appendIpv4String(sb, bytes);
263 return sb.toString();
266 private static String prefixStringV4(final byte[] bytes, final int mask) {
267 Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
269 final StringBuilder sb = new StringBuilder(18);
270 appendIpv4String(sb, bytes);
273 return sb.toString();