BUG-2825: add utility methods for instantiating DTOs
[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.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;
19
20 /**
21  * A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
22  *
23  * FIXME: IPv6 addresses are not emitted in canonical format as specified by the model.
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
43     @Nonnull public final A ipAddressFor(@Nonnull final byte[] bytes) {
44         switch (bytes.length) {
45             case 4:
46                 return ipv4Address(ipv4AddressFor(bytes));
47             case 16:
48                 return ipv6Address(ipv6AddressFor(bytes));
49             default:
50                 throw new IllegalArgumentException("Invalid array length " + bytes.length);
51         }
52     }
53
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));
60         } else {
61             throw new IllegalArgumentException("Unhandled address " + addr);
62         }
63     }
64
65     /**
66      * Create an Ipv4Address by interpreting input bytes as an IPv4 address.
67      *
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
72      */
73     @Nonnull public final A4 ipv4AddressFor(@Nonnull final byte[] bytes) {
74         return address4Factory.newInstance(addressStringV4(bytes));
75     }
76
77     /**
78      * Create an Ipv4Address by interpreting an {@link Inet4Address}.
79      *
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
84      */
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());
89     }
90
91     /**
92      * Create a /32 Ipv4Prefix by interpreting input bytes as an IPv4 address.
93      *
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
98      */
99     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] bytes) {
100         return prefix4Factory.newInstance(prefixStringV4(bytes));
101     }
102
103     /**
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>.
108      *
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
114      */
115     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] address, final int mask) {
116         return prefix4Factory.newInstance(prefixStringV4(address, mask));
117     }
118
119     /**
120      * Create a /32 Ipv4Prefix for an {@link Inet4Address}
121      *
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
126      */
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");
131     }
132
133     /**
134      * Create a Ipv4Prefix by combining the address with a mask. The address bytes are not masked.
135      *
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
141      */
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);
147     }
148
149     /**
150      * Create an Ipv6Address by interpreting input bytes as an IPv6 address.
151      *
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
156      */
157     @Nonnull public final A6 ipv6AddressFor(@Nonnull final byte[] bytes) {
158         return address6Factory.newInstance(addressStringV6(bytes));
159     }
160
161     /**
162      * Create an Ipv6Address by interpreting an {@link Inet6Address}.
163      *
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
168      */
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());
173     }
174
175     /**
176      * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv6 address.
177      *
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
182      */
183     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] bytes) {
184         return prefix6Factory.newInstance(addressStringV6(bytes) + "/128");
185     }
186
187     /**
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.
191      *
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
197      */
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);
201     }
202
203     /**
204      * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv4 address.
205      *
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
210      */
211     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr) {
212         return prefix6Factory.newInstance(addr.getHostAddress() + "/128");
213     }
214
215     /**
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.
219      *
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
225      */
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);
231     }
232
233     private static void appendIpv4String(final StringBuilder sb, final byte[] bytes) {
234         Preconditions.checkArgument(bytes.length == 4, "IPv4 address length is 4 bytes");
235
236         sb.append(UnsignedBytes.toInt(bytes[0]));
237         for (int i = 1; i < 4; ++i) {
238             sb.append('.');
239             sb.append(UnsignedBytes.toInt(bytes[i]));
240         }
241     }
242
243     private static String addressStringV4(final byte[] bytes) {
244         final StringBuilder sb = new StringBuilder(15);
245         appendIpv4String(sb, bytes);
246         return sb.toString();
247     }
248
249     private static String addressStringV6(final byte[] bytes) {
250         Preconditions.checkArgument(bytes.length == 16, "IPv6 address length is 16 bytes");
251
252         try {
253             return Inet6Address.getByAddress(bytes).getHostAddress();
254         } catch (UnknownHostException e) {
255             throw new IllegalArgumentException(String.format("Invalid input %s", bytes), e);
256         }
257     }
258
259     private static String prefixStringV4(final byte[] bytes) {
260         final StringBuilder sb = new StringBuilder(18);
261         appendIpv4String(sb, bytes);
262         sb.append("/32");
263         return sb.toString();
264     }
265
266     private static String prefixStringV4(final byte[] bytes, final int mask) {
267         Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
268
269         final StringBuilder sb = new StringBuilder(18);
270         appendIpv4String(sb, bytes);
271         sb.append('/');
272         sb.append(mask);
273         return sb.toString();
274     }
275 }