Fix AbstractIetfInetUtil.ipv6AddressFrom()
[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 javax.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.binding.util.StringValueObjectFactory;
22
23 /**
24  * A set of utility methods to efficiently instantiate various ietf-inet-types DTOs.
25  */
26 @Beta
27 public abstract class AbstractIetfInetUtil<A4, A4NZ extends A4, P4, A6, A6NZ extends A6, P6, A, ANZ, P> {
28     protected static final int INET4_LENGTH = 4;
29     protected static final int INET6_LENGTH = 16;
30     private final StringValueObjectFactory<A4NZ> nozone4Factory;
31     private final StringValueObjectFactory<P4> prefix4Factory;
32     private final StringValueObjectFactory<A6NZ> nozone6Factory;
33     private final StringValueObjectFactory<P6> prefix6Factory;
34
35     protected AbstractIetfInetUtil(final Class<A4NZ> nozone4Class, final Class<P4> prefix4Class,
36             final Class<A6NZ> nozone6Class, final Class<P6> prefix6Class) {
37         this.nozone4Factory = StringValueObjectFactory.create(nozone4Class, "0.0.0.0");
38         this.prefix4Factory = StringValueObjectFactory.create(prefix4Class, "0.0.0.0/0");
39         this.nozone6Factory = StringValueObjectFactory.create(nozone6Class, "::0");
40         this.prefix6Factory = StringValueObjectFactory.create(prefix6Class, "::0/0");
41     }
42
43     @Nonnull protected abstract A ipv4Address(@Nonnull A4 addr);
44     @Nonnull protected abstract A ipv6Address(@Nonnull A6 addr);
45     @Nonnull protected abstract ANZ ipv4AddressNoZone(@Nonnull A4NZ addr);
46     @Nonnull protected abstract ANZ ipv6AddressNoZone(@Nonnull A6NZ addr);
47
48     @Nonnull protected abstract P ipv4Prefix(@Nonnull P4 addr);
49     @Nonnull protected abstract P ipv6Prefix(@Nonnull P6 addr);
50     @Nullable protected abstract A4 maybeIpv4Address(@Nonnull A addr);
51     @Nullable protected abstract A6 maybeIpv6Address(@Nonnull A addr);
52     @Nonnull protected abstract String ipv4AddressString(@Nonnull A4 addr);
53     @Nonnull protected abstract String ipv6AddressString(@Nonnull A6 addr);
54     @Nonnull protected abstract String ipv4PrefixString(@Nonnull P4 prefix);
55     @Nonnull protected abstract String ipv6PrefixString(@Nonnull P6 prefix);
56
57     /**
58      * Create an IpAddress by interpreting input bytes as an IPv4 or IPv6 address, based on array length.
59      *
60      * @param bytes 4-byte (IPv4) or 6-byte (IPv6) array
61      * @return An IpAddress object
62      * @throws IllegalArgumentException if bytes has length different from 4 or 6
63      * @throws NullPointerException if bytes is null
64      */
65     @Nonnull public final A ipAddressFor(@Nonnull final byte[] bytes) {
66         switch (bytes.length) {
67             case INET4_LENGTH:
68                 return ipv4Address(ipv4AddressFor(bytes));
69             case INET6_LENGTH:
70                 return ipv6Address(ipv6AddressFor(bytes));
71             default:
72                 throw new IllegalArgumentException("Invalid array length " + bytes.length);
73         }
74     }
75
76     /**
77      * Create an IpAddress by interpreting input bytes as an IPv4 or IPv6 address, based on array length.
78      *
79      * @param bytes 4-byte (IPv4) or 6-byte (IPv6) array
80      * @return A no-zone IpAddress object
81      * @throws IllegalArgumentException if bytes has length different from 4 or 6
82      * @throws NullPointerException if bytes is null
83      */
84     @Nonnull public final ANZ ipAddressNoZoneFor(@Nonnull final byte[] bytes) {
85         switch (bytes.length) {
86             case INET4_LENGTH:
87                 return ipv4AddressNoZone(ipv4AddressFor(bytes));
88             case INET6_LENGTH:
89                 return ipv6AddressNoZone(ipv6AddressFor(bytes));
90             default:
91                 throw new IllegalArgumentException("Invalid array length " + bytes.length);
92         }
93     }
94
95     @Nonnull public final A ipAddressFor(@Nonnull final InetAddress addr) {
96         Preconditions.checkNotNull(addr, "Address must not be null");
97         if (addr instanceof Inet4Address) {
98             return ipv4Address(ipv4AddressFor(addr));
99         } else if (addr instanceof Inet6Address) {
100             return ipv6Address(ipv6AddressFor(addr));
101         } else {
102             throw new IllegalArgumentException("Unhandled address " + addr);
103         }
104     }
105
106     @Nonnull public final ANZ ipAddressNoZoneFor(@Nonnull final InetAddress addr) {
107         Preconditions.checkNotNull(addr, "Address must not be null");
108         if (addr instanceof Inet4Address) {
109             return ipv4AddressNoZone(ipv4AddressFor(addr));
110         } else if (addr instanceof Inet6Address) {
111             return ipv6AddressNoZone(ipv6AddressFor(addr));
112         } else {
113             throw new IllegalArgumentException("Unhandled address " + addr);
114         }
115     }
116
117     /**
118      * Create an IpPrefix by combining the address with a mask. The address
119      * bytes are interpreted as an address and the specified mask is concatenated to
120      * it. The address bytes are not masked.
121      *
122      * @param bytes Input address as a 4-byte (IPv4) or 16-byte (IPv6) array
123      * @param mask Prefix mask
124      * @return An IpPrefix object
125      * @throws IllegalArgumentException if bytes has length different from 4 or 16 or if mask is not
126      *         in range 0-32 or 0-128 respectively
127      * @throws NullPointerException if bytes is null
128      */
129     @Nonnull public final P ipPrefixFor(@Nonnull final byte[] bytes, final int mask) {
130         switch (bytes.length) {
131             case INET4_LENGTH:
132                 return ipv4Prefix(ipv4PrefixFor(bytes, mask));
133             case INET6_LENGTH:
134                 return ipv6Prefix(ipv6PrefixFor(bytes, mask));
135             default:
136                 throw new IllegalArgumentException("Invalid array length " + bytes.length);
137         }
138     }
139
140     @Nonnull public final P ipPrefixFor(@Nonnull final InetAddress addr, final int mask) {
141         Preconditions.checkNotNull(addr, "Address must not be null");
142         if (addr instanceof Inet4Address) {
143             return ipv4Prefix(ipv4PrefixFor(addr, mask));
144         } else if (addr instanceof Inet6Address) {
145             return ipv6Prefix(ipv6PrefixFor(addr, mask));
146         } else {
147             throw new IllegalArgumentException("Unhandled address " + addr);
148         }
149     }
150
151     @Nonnull public final InetAddress inetAddressFor(@Nonnull final A addr) {
152         final A4 v4 = maybeIpv4Address(addr);
153         if (v4 != null) {
154             return inet4AddressFor(v4);
155         }
156         final A6 v6 = maybeIpv6Address(addr);
157         Preconditions.checkArgument(v6 != null, "Address %s is neither IPv4 nor IPv6", addr);
158         return inet6AddressFor(v6);
159     }
160
161     @Nonnull public final Inet4Address inet4AddressFor(@Nonnull final A4 addr) {
162         try {
163             return (Inet4Address) InetAddress.getByAddress(ipv4AddressBytes(addr));
164         } catch (UnknownHostException e) {
165             throw new IllegalArgumentException("Invalid address " + addr, e);
166         }
167     }
168
169     @Nonnull public final Inet6Address inet6AddressFor(@Nonnull final A6 addr) {
170         try {
171             return (Inet6Address) InetAddress.getByAddress(ipv6AddressBytes(addr));
172         } catch (UnknownHostException e) {
173             throw new IllegalArgumentException("Invalid address " + addr, e);
174         }
175     }
176
177     /**
178      * Create an Ipv4Address by interpreting input bytes as an IPv4 address.
179      *
180      * @param bytes 4-byte array
181      * @return An Ipv4Address object
182      * @throws IllegalArgumentException if bytes has length different from 4
183      * @throws NullPointerException if bytes is null
184      */
185     @Nonnull public final A4NZ ipv4AddressFor(@Nonnull final byte[] bytes) {
186         return nozone4Factory.newInstance(addressStringV4(bytes));
187     }
188
189     /**
190      * Create an Ipv4Address by interpreting an {@link Inet4Address}.
191      *
192      * @param addr An {@link Inet4Address}
193      * @return An Ipv4Address object
194      * @throws IllegalArgumentException if addr is not an {@link Inet4Address}
195      * @throws NullPointerException if addr is null
196      */
197     @Nonnull public final A4NZ ipv4AddressFor(@Nonnull final InetAddress addr) {
198         Preconditions.checkNotNull(addr, "Address must not be null");
199         Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
200         return nozone4Factory.newInstance(addr.getHostAddress());
201     }
202
203     @Nonnull public final A4NZ ipv4AddressFrom(@Nonnull final P4 prefix) {
204         return prefixToAddress(nozone4Factory, ipv4PrefixString(prefix));
205     }
206
207     @Nonnull public final byte[] ipv4AddressBytes(@Nonnull final A4 addr) {
208         /*
209          * This implementation relies heavily on the input string having been validated to comply with
210          * the Ipv4Address pattern, which may include a zone index.
211          */
212         final String str = ipv4AddressString(addr);
213         final int percent = str.indexOf('%');
214         return ipv4AddressBytes(str, percent == -1 ? str.length() : percent);
215     }
216
217     protected static final byte[] ipv4AddressBytes(@Nonnull final String str, final int length) {
218         final byte[] bytes = new byte[INET4_LENGTH];
219         Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, length);
220         return bytes;
221     }
222
223     /**
224      * Create a /32 Ipv4Prefix by interpreting input bytes as an IPv4 address.
225      *
226      * @param bytes four-byte array
227      * @return An Ipv4Prefix object
228      * @throws IllegalArgumentException if bytes has length different from 4
229      * @throws NullPointerException if bytes is null
230      */
231     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] bytes) {
232         return prefix4Factory.newInstance(prefixStringV4(bytes));
233     }
234
235     /**
236      * Create a Ipv4Prefix by combining the address with a mask. The address
237      * bytes are interpreted as an address and the specified mask is concatenated to
238      * it. The address bytes are not masked, hence input <code>address = { 1, 2, 3, 4 }</code>
239      * and <code>mask=24</code> will result in <code>1.2.3.4/24</code>.
240      *
241      * @param address Input address as a 4-byte array
242      * @param mask Prefix mask
243      * @return An Ipv4Prefix object
244      * @throws IllegalArgumentException if bytes has length different from 4 or if mask is not in range 0-32
245      * @throws NullPointerException if bytes is null
246      */
247     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final byte[] address, final int mask) {
248         return prefix4Factory.newInstance(prefixStringV4(address, mask));
249     }
250
251     @Nonnull public final P4 ipv4PrefixForShort(@Nonnull final byte[] address, final int mask) {
252         if (mask == 0) {
253             // Easy case, reuse the template
254             return prefix4Factory.getTemplate();
255         }
256
257         return v4PrefixForShort(address, 0, mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1), mask);
258     }
259
260     @Nonnull public final P4 ipv4PrefixForShort(@Nonnull final byte[] array, final int startOffset, final int mask) {
261         if (mask == 0) {
262             // Easy case, reuse the template
263             return prefix4Factory.getTemplate();
264         }
265
266         return v4PrefixForShort(array, startOffset, mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1), mask);
267     }
268
269     /**
270      * Create a /32 Ipv4Prefix for an {@link Inet4Address}
271      *
272      * @param addr An {@link Inet4Address}
273      * @return An Ipv4Prefix object
274      * @throws IllegalArgumentException if addr is not an Inet4Address
275      * @throws NullPointerException if adds is null
276      */
277     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final InetAddress addr) {
278         Preconditions.checkNotNull(addr, "Address must not be null");
279         Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
280         return prefix4Factory.newInstance(addr.getHostAddress() + "/32");
281     }
282
283     /**
284      * Create a Ipv4Prefix by combining the address with a mask. The address bytes are not masked.
285      *
286      * @param addr An {@link Inet4Address}
287      * @param mask Prefix mask
288      * @return An Ipv4Prefix object
289      * @throws IllegalArgumentException if addr is not an Inet4Address or if mask is not in range 0-32
290      * @throws NullPointerException if addr is null
291      */
292     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final InetAddress addr, final int mask) {
293         Preconditions.checkNotNull(addr, "Address must not be null");
294         Preconditions.checkArgument(addr instanceof Inet4Address, "Address has to be an Inet4Address");
295         Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
296         return prefix4Factory.newInstance(addr.getHostAddress() + '/' + mask);
297     }
298
299     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final A4 addr) {
300         Preconditions.checkNotNull(addr, "Address must not be null");
301         return prefix4Factory.newInstance(ipv4AddressString(addr) + "/32");
302     }
303
304     @Nonnull public final P4 ipv4PrefixFor(@Nonnull final A4 addr, final int mask) {
305         Preconditions.checkNotNull(addr, "Address must not be null");
306         Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
307         return prefix4Factory.newInstance(ipv4AddressString(addr) + '/' + mask);
308     }
309
310     @Nonnull public final Entry<A4NZ, Integer> splitIpv4Prefix(@Nonnull final P4 prefix) {
311         return splitPrefix(nozone4Factory, ipv4PrefixString(prefix));
312     }
313
314     @Nonnull public final byte[] ipv4PrefixToBytes(@Nonnull final P4 prefix) {
315         final String str = ipv4PrefixString(prefix);
316         final int slash = str.lastIndexOf('/');
317
318         final byte[] bytes = new byte[INET4_LENGTH + 1];
319         Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, slash);
320         bytes[INET4_LENGTH] = (byte)Integer.parseInt(str.substring(slash + 1), 10);
321         return bytes;
322     }
323
324     /**
325      * Create an Ipv6Address by interpreting input bytes as an IPv6 address.
326      *
327      * @param bytes 16-byte array
328      * @return An Ipv6Address object
329      * @throws IllegalArgumentException if bytes has length different from 16
330      * @throws NullPointerException if bytes is null
331      */
332     @Nonnull public final A6NZ ipv6AddressFor(@Nonnull final byte[] bytes) {
333         return nozone6Factory.newInstance(addressStringV6(bytes));
334     }
335
336     /**
337      * Create an Ipv6Address by interpreting an {@link Inet6Address}.
338      *
339      * @param addr An {@link Inet6Address}
340      * @return An Ipv6Address object
341      * @throws IllegalArgumentException if addr is not an {@link Inet6Address}
342      * @throws NullPointerException if addr is null
343      */
344     @Nonnull public final A6NZ ipv6AddressFor(@Nonnull final InetAddress addr) {
345         Preconditions.checkNotNull(addr, "Address must not be null");
346         Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
347         return nozone6Factory.newInstance(addressStringV6(addr));
348     }
349
350     @Nonnull public final A6NZ ipv6AddressFrom(@Nonnull final P6 prefix) {
351         return prefixToAddress(nozone6Factory, ipv6PrefixString(prefix));
352     }
353
354     @Nonnull public final byte[] ipv6AddressBytes(@Nonnull final A6 addr) {
355         final String str = ipv6AddressString(addr);
356         final int percent = str.indexOf('%');
357         return ipv6AddressBytes(str, percent == -1 ? str.length() : percent);
358     }
359
360     protected static final byte[] ipv6AddressBytes(@Nonnull final String str, final int length) {
361         final byte[] bytes = new byte[INET6_LENGTH];
362         Ipv6Utils.fillIpv6Bytes(bytes, str, length);
363         return bytes;
364     }
365
366
367     /**
368      * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv6 address.
369      *
370      * @param bytes four-byte array
371      * @return An Ipv6Prefix object
372      * @throws IllegalArgumentException if bytes has length different from 16
373      * @throws NullPointerException if bytes is null
374      */
375     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] bytes) {
376         return prefix6Factory.newInstance(addressStringV6(bytes) + "/128");
377     }
378
379     /**
380      * Create a Ipv6Prefix by combining the address with a mask. The address
381      * bytes are interpreted as an address and the specified mask is concatenated to
382      * it. The address bytes are not masked.
383      *
384      * @param address Input address as a 16-byte array
385      * @param mask Prefix mask
386      * @return An Ipv6Prefix object
387      * @throws IllegalArgumentException if bytes has length different from 16 or if mask is not in range 0-128
388      * @throws NullPointerException if bytes is null
389      */
390     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final byte[] address, final int mask) {
391         Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
392         return prefix6Factory.newInstance(addressStringV6(address) + '/' + mask);
393     }
394
395     @Nonnull public final P6 ipv6PrefixForShort(@Nonnull final byte[] address, final int mask) {
396         return ipv6PrefixForShort(address, 0, mask);
397     }
398
399     @Nonnull public final P6 ipv6PrefixForShort(@Nonnull final byte[] array, final int startOffset, final int mask) {
400         if (mask == 0) {
401             // Easy case, reuse the template
402             return prefix6Factory.getTemplate();
403         }
404
405         Preconditions.checkArgument(mask > 0 && mask <= 128, "Invalid mask %s", mask);
406         final int size = mask / Byte.SIZE + (mask % Byte.SIZE == 0 ? 0 : 1);
407
408         // Until we can instantiate an IPv6 address for a partial array, use a temporary buffer
409         byte[] tmp = new byte[INET6_LENGTH];
410         System.arraycopy(array, startOffset, tmp, 0, size);
411         return ipv6PrefixFor(tmp, mask);
412     }
413
414     /**
415      * Create a /128 Ipv6Prefix by interpreting input bytes as an IPv4 address.
416      *
417      * @param addr an {@link Inet6Address}
418      * @return An Ipv6Prefix object
419      * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
420      * @throws NullPointerException if addr is null
421      */
422     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr) {
423         return prefix6Factory.newInstance(addressStringV6(addr) + "/128");
424     }
425
426     /**
427      * Create a Ipv6Prefix by combining the address with a mask. The address
428      * bytes are interpreted as an address and the specified mask is concatenated to
429      * it. The address bytes are not masked.
430      *
431      * @param addr Input address
432      * @param mask Prefix mask
433      * @return An Ipv6Prefix object
434      * @throws IllegalArgumentException if addr is not an Inet6Address or if mask is not in range 0-128
435      * @throws NullPointerException if addr is null
436      */
437     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final InetAddress addr, final int mask) {
438         Preconditions.checkNotNull(addr, "Address must not be null");
439         Preconditions.checkArgument(addr instanceof Inet6Address, "Address has to be an Inet6Address");
440         Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
441         return prefix6Factory.newInstance(addressStringV6(addr) + '/' + mask);
442     }
443
444     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final A6 addr) {
445         Preconditions.checkNotNull(addr, "Address must not be null");
446         return prefix6Factory.newInstance(ipv6AddressString(addr) + "/128");
447     }
448
449     @Nonnull public final P6 ipv6PrefixFor(@Nonnull final A6 addr, final int mask) {
450         Preconditions.checkNotNull(addr, "Address must not be null");
451         Preconditions.checkArgument(mask >= 0 && mask <= 128, "Invalid mask %s", mask);
452         return prefix6Factory.newInstance(ipv6AddressString(addr) + '/' + mask);
453     }
454
455     @Nonnull public final Entry<A6NZ, Integer> splitIpv6Prefix(@Nonnull final P6 prefix) {
456         return splitPrefix(nozone6Factory, ipv6PrefixString(prefix));
457     }
458
459     private static <T> T prefixToAddress(final StringValueObjectFactory<T> factory, final String str) {
460         return factory.newInstance(str.substring(0, str.lastIndexOf('/')));
461     }
462
463     private static <T> Entry<T, Integer> splitPrefix(final StringValueObjectFactory<T> factory, final String str) {
464         final int slash = str.lastIndexOf('/');
465         return new SimpleImmutableEntry<>(factory.newInstance(str.substring(0, slash)),
466                 Integer.valueOf(str.substring(slash + 1)));
467     }
468
469     @Nonnull public final byte[] ipv6PrefixToBytes(@Nonnull final P6 prefix) {
470         final String str = ipv6PrefixString(prefix);
471         final byte[] bytes = new byte[INET6_LENGTH + 1];
472         final int slash = str.lastIndexOf('/');
473         Ipv6Utils.fillIpv6Bytes(bytes, str, slash);
474         bytes[INET6_LENGTH] = (byte)Integer.parseInt(str.substring(slash + 1), 10);
475         return bytes;
476     }
477
478     private static void appendIpv4String(final StringBuilder sb, final byte[] bytes) {
479         Preconditions.checkArgument(bytes.length == INET4_LENGTH, "IPv4 address length is 4 bytes");
480
481         sb.append(Byte.toUnsignedInt(bytes[0]));
482         for (int i = 1; i < INET4_LENGTH; ++i) {
483             sb.append('.');
484             sb.append(Byte.toUnsignedInt(bytes[i]));
485         }
486     }
487
488     private static String addressStringV4(final byte[] bytes) {
489         final StringBuilder sb = new StringBuilder(15);
490         appendIpv4String(sb, bytes);
491         return sb.toString();
492     }
493
494     private static String addressStringV6(final byte[] bytes) {
495         Preconditions.checkArgument(bytes.length == INET6_LENGTH, "IPv6 address length is 16 bytes");
496
497         try {
498             return addressStringV6(Inet6Address.getByAddress(bytes));
499         } catch (UnknownHostException e) {
500             throw new IllegalArgumentException(String.format("Invalid input %s", bytes), e);
501         }
502     }
503
504     private static String addressStringV6(final InetAddress addr) {
505         return InetAddresses.toAddrString(addr);
506     }
507
508     private static String prefixStringV4(final byte[] bytes) {
509         final StringBuilder sb = new StringBuilder(18);
510         appendIpv4String(sb, bytes);
511         sb.append("/32");
512         return sb.toString();
513     }
514
515     private static String prefixStringV4(final byte[] bytes, final int mask) {
516         Preconditions.checkArgument(mask >= 0 && mask <= 32, "Invalid mask %s", mask);
517
518         final StringBuilder sb = new StringBuilder(18);
519         appendIpv4String(sb, bytes);
520         sb.append('/');
521         sb.append(mask);
522         return sb.toString();
523     }
524
525     private P4 v4PrefixForShort(@Nonnull final byte[] array, final int startOffset, final int size, final int mask) {
526         if (startOffset == 0 && size == INET4_LENGTH && array.length == INET4_LENGTH) {
527             // Easy case, fall back to non-short
528             return ipv4PrefixFor(array, mask);
529         }
530
531         final StringBuilder sb = new StringBuilder(18);
532
533         // Add from address
534         sb.append(Byte.toUnsignedInt(array[startOffset]));
535         for (int i = 1; i < size; i++) {
536             sb.append('.');
537             sb.append(Byte.toUnsignedInt(array[startOffset + i]));
538         }
539
540         // Add zeros
541         for (int i = size; i < INET4_LENGTH; i++) {
542             sb.append(".0");
543         }
544
545         // Add mask
546         Preconditions.checkArgument(mask > 0 && mask <= 32, "Invalid mask %s", mask);
547         sb.append('/');
548         sb.append(mask);
549
550         return prefix4Factory.newInstance(sb.toString());
551     }
552 }