b008d1ac783bcfbc1f3443a8e6f3e9d022b3ca2b
[mdsal.git] / model / ietf / ietf-type-util / src / main / java / org / opendaylight / mdsal / model / ietf / util / AbstractIetfInetUtil.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.model.ietf.util;
9
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;
21
22 /**
23  * A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
24  */
25 @Beta
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;
31
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");
38     }
39
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);
46
47     @Nonnull public final A ipAddressFor(@Nonnull final byte[] bytes) {
48         switch (bytes.length) {
49             case 4:
50                 return ipv4Address(ipv4AddressFor(bytes));
51             case 16:
52                 return ipv6Address(ipv6AddressFor(bytes));
53             default:
54                 throw new IllegalArgumentException("Invalid array length " + bytes.length);
55         }
56     }
57
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));
64         } else {
65             throw new IllegalArgumentException("Unhandled address " + addr);
66         }
67     }
68
69     /**
70      * Create an Ipv4Address by interpreting input bytes as an IPv4 address.
71      *
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
76      */
77     @Nonnull public final A4 ipv4AddressFor(@Nonnull final byte[] bytes) {
78         return address4Factory.newInstance(addressStringV4(bytes));
79     }
80
81     /**
82      * Create an Ipv4Address by interpreting an {@link Inet4Address}.
83      *
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
88      */
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());
93     }
94
95     @Nonnull public final A4 ipv4AddressFrom(@Nonnull final P4 prefix) {
96         return prefixToAddress(address4Factory, ipv4PrefixString(prefix));
97     }
98
99     /**
100      * Create a /32 Ipv4Prefix by interpreting input bytes as an IPv4 address.
101      *
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
106      */
107     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] bytes) {
108         return prefix4Factory.newInstance(prefixStringV4(bytes));
109     }
110
111     /**
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>.
116      *
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
122      */
123     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] address, final int mask) {
124         return prefix4Factory.newInstance(prefixStringV4(address, mask));
125     }
126
127     /**
128      * Create a /32 Ipv4Prefix for an {@link Inet4Address}
129      *
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
134      */
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");
139     }
140
141     /**
142      * Create a Ipv4Prefix by combining the address with a mask. The address bytes are not masked.
143      *
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
149      */
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);
155     }
156
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");
160     }
161
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);
166     }
167
168     @Nonnull public final Entry<A4, Integer> splitIpv4Prefix(@Nonnull final P4 prefix) {
169         final String str = ipv4PrefixString(prefix);
170         final int slash = str.lastIndexOf('/');
171         final A4 addr = address4Factory.newInstance(str.substring(0, slash));
172         return new SimpleImmutableEntry<>(addr, Integer.valueOf(str.substring(slash + 1)));
173     }
174
175     /**
176      * Create an Ipv6Address by interpreting input bytes as an IPv6 address.
177      *
178      * @param bytes 16-byte array
179      * @return An Ipv6Address object
180      * @throws IllegalArgumentException if bytes has length different from 16
181      * @throws NullPointerException if bytes is null
182      */
183     @Nonnull public final A6 ipv6AddressFor(@Nonnull final byte[] bytes) {
184         return address6Factory.newInstance(addressStringV6(bytes));
185     }
186
187     /**
188      * Create an Ipv6Address by interpreting an {@link Inet6Address}.
189      *
190      * @param addr An {@link Inet6Address}
191      * @return An Ipv6Address object
192      * @throws IllegalArgumentException if addr is not an {@link Inet6Address}
193      * @throws NullPointerException if addr is null
194      */
195     @Nonnull public final A6 ipv6AddressFor(@Nonnull final InetAddress addr) {
196         Preconditions.checkNotNull(addr, "Address must not be null");
197         Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
198         return address6Factory.newInstance(addressStringV6(addr));
199     }
200
201     @Nonnull public final A6 ipv6AddressFrom(@Nonnull final P6 prefix) {
202         return prefixToAddress(address6Factory, ipv6PrefixString(prefix));
203     }
204
205     /**
206      * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv6 address.
207      *
208      * @param bytes four-byte array
209      * @return An Ipv6Prefix object
210      * @throws IllegalArgumentException if bytes has length different from 16
211      * @throws NullPointerException if bytes is null
212      */
213     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] bytes) {
214         return prefix6Factory.newInstance(addressStringV6(bytes) + "/128");
215     }
216
217     /**
218      * Create a Ipv6Prefix by combining the address with a mask. The address
219      * bytes are interpreted as an address and the specified mask is concatenated to
220      * it. The address bytes are not masked.
221      *
222      * @param address Input address as a 4-byte array
223      * @param mask Prefix mask
224      * @return An Ipv6Prefix object
225      * @throws IllegalArgumentException if bytes has length different from 16 or if mask is not in range 0-128
226      * @throws NullPointerException if bytes is null
227      */
228     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] address, final int mask) {
229         Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
230         return prefix6Factory.newInstance(addressStringV6(address) + '/' + mask);
231     }
232
233     /**
234      * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv4 address.
235      *
236      * @param addr an {@link Inet6Address}
237      * @return An Ipv6Prefix object
238      * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
239      * @throws NullPointerException if addr is null
240      */
241     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr) {
242         return prefix6Factory.newInstance(addressStringV6(addr) + "/128");
243     }
244
245     /**
246      * Create a Ipv6Prefix by combining the address with a mask. The address
247      * bytes are interpreted as an address and the specified mask is concatenated to
248      * it. The address bytes are not masked.
249      *
250      * @param addr Input address
251      * @param mask Prefix mask
252      * @return An Ipv6Prefix object
253      * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
254      * @throws NullPointerException if addr is null
255      */
256     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr, final int mask) {
257         Preconditions.checkNotNull(addr, "Address must not be null");
258         Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
259         Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
260         return prefix6Factory.newInstance(addressStringV6(addr) + '/' + mask);
261     }
262
263     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final A6 addr) {
264         Preconditions.checkNotNull(addr, "Address must not be null");
265         return prefix6Factory.newInstance(ipv6AddressString(addr) + "/128");
266     }
267
268     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final A6 addr, final int mask) {
269         Preconditions.checkNotNull(addr, "Address must not be null");
270         Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
271         return prefix6Factory.newInstance(ipv6AddressString(addr) + '/' + mask);
272     }
273
274     @Nonnull public final Entry<A6, Integer> splitIpv6Prefix(@Nonnull final P6 prefix) {
275         final String str = ipv6PrefixString(prefix);
276         final int slash = str.lastIndexOf('/');
277         final A6 addr = address6Factory.newInstance(str.substring(0, slash));
278         return new SimpleImmutableEntry<>(addr, Integer.valueOf(str.substring(slash + 1)));
279     }
280
281     private static <T> T prefixToAddress(final StringValueObjectFactory<T> factory, final String str) {
282         return factory.newInstance(str.substring(0, str.lastIndexOf('/')));
283     }
284
285     private static void appendIpv4String(final StringBuilder sb, final byte[] bytes) {
286         Preconditions.checkArgument(bytes.length == 4, "IPv4 address length is 4 bytes");
287
288         sb.append(Byte.toUnsignedInt(bytes[0]));
289         for (int i = 1; i < 4; ++i) {
290             sb.append('.');
291             sb.append(Byte.toUnsignedInt(bytes[i]));
292         }
293     }
294
295     private static String addressStringV4(final byte[] bytes) {
296         final StringBuilder sb = new StringBuilder(15);
297         appendIpv4String(sb, bytes);
298         return sb.toString();
299     }
300
301     private static String addressStringV6(final byte[] bytes) {
302         Preconditions.checkArgument(bytes.length == 16, "IPv6 address length is 16 bytes");
303
304         try {
305             return addressStringV6(Inet6Address.getByAddress(bytes));
306         } catch (UnknownHostException e) {
307             throw new IllegalArgumentException(String.format("Invalid input %s", bytes), e);
308         }
309     }
310
311     private static String addressStringV6(final InetAddress addr) {
312         return InetAddresses.toAddrString(addr);
313     }
314
315     private static String prefixStringV4(final byte[] bytes) {
316         final StringBuilder sb = new StringBuilder(18);
317         appendIpv4String(sb, bytes);
318         sb.append("/32");
319         return sb.toString();
320     }
321
322     private static String prefixStringV4(final byte[] bytes, final int mask) {
323         Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
324
325         final StringBuilder sb = new StringBuilder(18);
326         appendIpv4String(sb, bytes);
327         sb.append('/');
328         sb.append(mask);
329         return sb.toString();
330     }
331 }