Use zero-compressed IPv6 addresses
[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 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;
20
21 /**
22  * A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
23  *
24  * FIXME: IPv6 addresses are not emitted in canonical format as specified by the model.
25  */
26 @Beta
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;
32
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");
39     }
40
41     protected abstract A ipv4Address(A4 addr);
42     protected abstract A ipv6Address(A6 addr);
43
44     @Nonnull public final A ipAddressFor(@Nonnull final byte[] bytes) {
45         switch (bytes.length) {
46             case 4:
47                 return ipv4Address(ipv4AddressFor(bytes));
48             case 16:
49                 return ipv6Address(ipv6AddressFor(bytes));
50             default:
51                 throw new IllegalArgumentException("Invalid array length " + bytes.length);
52         }
53     }
54
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));
61         } else {
62             throw new IllegalArgumentException("Unhandled address " + addr);
63         }
64     }
65
66     /**
67      * Create an Ipv4Address by interpreting input bytes as an IPv4 address.
68      *
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
73      */
74     @Nonnull public final A4 ipv4AddressFor(@Nonnull final byte[] bytes) {
75         return address4Factory.newInstance(addressStringV4(bytes));
76     }
77
78     /**
79      * Create an Ipv4Address by interpreting an {@link Inet4Address}.
80      *
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
85      */
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());
90     }
91
92     /**
93      * Create a /32 Ipv4Prefix by interpreting input bytes as an IPv4 address.
94      *
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
99      */
100     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] bytes) {
101         return prefix4Factory.newInstance(prefixStringV4(bytes));
102     }
103
104     /**
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>.
109      *
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
115      */
116     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] address, final int mask) {
117         return prefix4Factory.newInstance(prefixStringV4(address, mask));
118     }
119
120     /**
121      * Create a /32 Ipv4Prefix for an {@link Inet4Address}
122      *
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
127      */
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");
132     }
133
134     /**
135      * Create a Ipv4Prefix by combining the address with a mask. The address bytes are not masked.
136      *
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
142      */
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);
148     }
149
150     /**
151      * Create an Ipv6Address by interpreting input bytes as an IPv6 address.
152      *
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
157      */
158     @Nonnull public final A6 ipv6AddressFor(@Nonnull final byte[] bytes) {
159         return address6Factory.newInstance(addressStringV6(bytes));
160     }
161
162     /**
163      * Create an Ipv6Address by interpreting an {@link Inet6Address}.
164      *
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
169      */
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());
174     }
175
176     /**
177      * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv6 address.
178      *
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
183      */
184     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] bytes) {
185         return prefix6Factory.newInstance(addressStringV6(bytes) + "/128");
186     }
187
188     /**
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.
192      *
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
198      */
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);
202     }
203
204     /**
205      * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv4 address.
206      *
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
211      */
212     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr) {
213         return prefix6Factory.newInstance(addressStringV6(addr) + "/128");
214     }
215
216     /**
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.
220      *
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
226      */
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);
232     }
233
234     private static void appendIpv4String(final StringBuilder sb, final byte[] bytes) {
235         Preconditions.checkArgument(bytes.length == 4, "IPv4 address length is 4 bytes");
236
237         sb.append(UnsignedBytes.toInt(bytes[0]));
238         for (int i = 1; i < 4; ++i) {
239             sb.append('.');
240             sb.append(UnsignedBytes.toInt(bytes[i]));
241         }
242     }
243
244     private static String addressStringV4(final byte[] bytes) {
245         final StringBuilder sb = new StringBuilder(15);
246         appendIpv4String(sb, bytes);
247         return sb.toString();
248     }
249
250     private static String addressStringV6(final byte[] bytes) {
251         Preconditions.checkArgument(bytes.length == 16, "IPv6 address length is 16 bytes");
252
253         try {
254             return addressStringV6(Inet6Address.getByAddress(bytes));
255         } catch (UnknownHostException e) {
256             throw new IllegalArgumentException(String.format("Invalid input %s", bytes), e);
257         }
258     }
259
260     private static String addressStringV6(final InetAddress addr) {
261         return InetAddresses.toAddrString(addr);
262     }
263
264     private static String prefixStringV4(final byte[] bytes) {
265         final StringBuilder sb = new StringBuilder(18);
266         appendIpv4String(sb, bytes);
267         sb.append("/32");
268         return sb.toString();
269     }
270
271     private static String prefixStringV4(final byte[] bytes, final int mask) {
272         Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
273
274         final StringBuilder sb = new StringBuilder(18);
275         appendIpv4String(sb, bytes);
276         sb.append('/');
277         sb.append(mask);
278         return sb.toString();
279     }
280 }