2 * Copyright (c) 2015 Cisco Systems, Inc., Brocade, Communications Systems, Inc. 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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
11 import java.math.BigInteger;
12 import java.net.Inet4Address;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Iterator;
18 import java.util.List;
20 import com.google.common.collect.Lists;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.Preconditions;
31 import com.google.common.base.Splitter;
32 import com.google.common.base.Strings;
33 import com.google.common.collect.Iterators;
34 import com.google.common.net.InetAddresses;
35 import com.google.common.primitives.UnsignedBytes;
39 * Created by Martin Bobak <mbobak@cisco.com> on 5.3.2015.
40 * v6 routines added by Anton Ivanov on 14.6.2015
41 * Arbitrary masks by sai.marapareddy@gmail.com
43 public final class IpConversionUtil {
45 private static final Logger LOG = LoggerFactory.getLogger(IpConversionUtil.class);
46 public static final String PREFIX_SEPARATOR = "/";
47 public static final Splitter PREFIX_SPLITTER = Splitter.on('/');
48 private static final int INADDR4SZ = 4;
49 private static final int INADDR6SZ = 16;
50 private static final int INT16SZ = 2;
51 private static final int IPV4_ADDRESS_LENGTH = 32;
52 private static final int IPV6_ADDRESS_LENGTH = 128;
53 private static final String DEFAULT_ARBITRARY_BIT_MASK = "255.255.255.255";
56 * Prefix bytearray lookup table. We concatenate the prefixes
57 * to a single byte array and perform offset lookups to ensure
58 * the table is contiguous and save some space.
60 private static final byte[] PREFIX_BYTEARRAYS;
62 final byte[] a = new byte[(INADDR6SZ * Byte.SIZE + 1) * INADDR6SZ];
65 for (int p = 0; p <= INADDR6SZ * Byte.SIZE; ++p) {
67 for (int i = 0; i < INADDR6SZ; ++i) {
68 a[offset++] = (byte) nextNibble(prefix);
73 PREFIX_BYTEARRAYS = a;
76 private IpConversionUtil() {
77 throw new UnsupportedOperationException("This class should not be instantiated.");
80 public static Iterator<String> splitToParts(final Ipv4Prefix ipv4Prefix) {
81 return PREFIX_SPLITTER.split(ipv4Prefix.getValue()).iterator();
84 public static Iterator<String> splitToParts(final Ipv4Address ipv4Address) {
85 /* Invalid (Ab)use of ip address as prefix!!! */
86 return Iterators.forArray(ipv4Address.getValue(), String.valueOf(IPV4_ADDRESS_LENGTH));
89 public static Iterator<String> splitToParts(final Ipv6Prefix ipv6Prefix) {
90 return PREFIX_SPLITTER.split(ipv6Prefix.getValue()).iterator();
93 public static Iterator<String> splitToParts(final Ipv6Address ipv6Address) {
94 /* Invalid (Ab)use of ip address as prefix!!! */
95 return Iterators.forArray(ipv6Address.getValue(), String.valueOf(IPV6_ADDRESS_LENGTH));
98 /* This forest of functions has a purpose:
100 * 1. There are multiple coding styles around the plugin, this is necessary in order to have
101 * one mechanism to convert them all, one mechanism to find them...
102 * 2. I hope that one day yangtools will actually deliver code fit for purpose in a packet
103 * processing application (presently it is not. When this happens, these can be optimized
104 * for "side-load" of pre-vetted data. Example. IP Address (v4 or v6) is prevetted left of the
105 * prefix. It should be loadable into Prefix without _RERUNNING_ 100ms+ of regexps. When (and if)
106 * that happens, it will be a simple fix here without chasing it across the whole plugin.
109 public static Ipv4Prefix createPrefix(final Ipv4Address ipv4Address){
110 return IetfInetUtil.INSTANCE.ipv4PrefixFor(ipv4Address);
113 public static Ipv4Prefix createPrefix(final Ipv4Address ipv4Address, final String mask){
115 * Ipv4Address has already validated the address part of the prefix,
116 * It is mandated to comply to the same regexp as the address
117 * There is absolutely no point rerunning additional checks vs this
118 * Note - there is no canonical form check here!!!
120 if (null != mask && !mask.isEmpty()) {
121 return new Ipv4Prefix(ipv4Address.getValue() + PREFIX_SEPARATOR + mask);
123 return new Ipv4Prefix(ipv4Address.getValue() + PREFIX_SEPARATOR + IPV4_ADDRESS_LENGTH);
127 public static Ipv4Prefix createPrefix(final Ipv4Address ipv4Address, final int intmask){
128 return IetfInetUtil.INSTANCE.ipv4PrefixFor(ipv4Address, intmask);
131 public static Ipv4Prefix createPrefix(final Ipv4Address ipv4Address, final byte [] bytemask){
132 return IetfInetUtil.INSTANCE.ipv4PrefixFor(ipv4Address, countBits(bytemask));
135 public static DottedQuad createArbitraryBitMask(final byte [] bytemask) {
136 DottedQuad dottedQuad = null;
137 if (bytemask == null ) {
138 dottedQuad = new DottedQuad(DEFAULT_ARBITRARY_BIT_MASK);
141 dottedQuad = new DottedQuad(InetAddress.getByAddress(bytemask).getHostAddress());
142 } catch (UnknownHostException e) {
143 LOG.error("Failed to create the dottedQuad notation for the given mask ", e);
149 public static Ipv6Prefix createPrefix(final Ipv6Address ipv6Address){
150 return IetfInetUtil.INSTANCE.ipv6PrefixFor(ipv6Address);
153 public static Ipv6Prefix createPrefix(final Ipv6Address ipv6Address, final String mask){
155 * Ipv6Address has already validated the address part of the prefix,
156 * It is mandated to comply to the same regexp as the address
157 * There is absolutely no point rerunning additional checks vs this
158 * Note - there is no canonical form check here!!!
160 if (Strings.isNullOrEmpty(mask)) {
161 return new Ipv6Prefix(ipv6Address.getValue() + PREFIX_SEPARATOR + String.valueOf(IPV6_ADDRESS_LENGTH));
163 return new Ipv6Prefix(ipv6Address.getValue() + PREFIX_SEPARATOR + mask);
167 public static Ipv6Prefix createPrefix(final Ipv6Address ipv6Address, final int intmask){
168 return IetfInetUtil.INSTANCE.ipv6PrefixFor(ipv6Address, intmask);
171 public static Ipv6Prefix createPrefix(final Ipv6Address ipv6Address, final byte [] bytemask){
172 return IetfInetUtil.INSTANCE.ipv6PrefixFor(ipv6Address, countBits(bytemask));
175 public static Integer extractPrefix(final Ipv4Prefix ipv4Prefix) {
176 return IetfInetUtil.INSTANCE.splitIpv4Prefix(ipv4Prefix).getValue();
179 public static Integer extractPrefix(final Ipv6Prefix ipv6Prefix) {
180 return IetfInetUtil.INSTANCE.splitIpv6Prefix(ipv6Prefix).getValue();
183 public static Integer extractPrefix(final Ipv4Address ipv4Prefix) {
184 return IPV4_ADDRESS_LENGTH;
187 public static Integer extractPrefix(final Ipv6Address ipv6Prefix) {
193 * Read all of the following before you touch any v6 code or decide to
194 * optimize it by invoking a "simple" Guava call
196 * Java IPv6 is fundamentally broken and Google libraries do not fix it.
197 * 1. Java will allways implicitly rewrite v4 mapped into v6 as a v4 address
198 * and there is absolutely no way to override this behaviour
199 * 2. Guava libraries cannot parse non-canonical IPv6. They will throw an
200 * exception. Even if they did, they re-use the same broken java code
203 * This is why we have to parse v6 by ourselves.
205 * The following conversion code is based on inet_cidr_pton_ipv6 in NetBSD
207 * The original BSD code is licensed under standard BSD license. While we
208 * are not obliged to provide an attribution, credit where credit is due.
209 * As far as why it is similar to Sun's sun.net.util please ask Sun why
210 * their code has the same variable names, comments and code flow.
216 * Convert Ipv6Address object to a valid Canonical v6 address in byte format
218 * @param ipv6Address - v6 Address object
219 * @return - byte array of size 16. Last byte contains netmask
223 public static byte[] canonicalBinaryV6Address(final Ipv6Address ipv6Address) {
225 * Do not modify this routine to take direct strings input!!!
226 * Key checks have been removed based on the assumption that
227 * the input is validated via regexps in Ipv6Prefix()
230 Iterable<String> splittedV6Address = Splitter.on("%")
233 .split(ipv6Address.getValue());
234 List<String> partsV6Address = Lists.newArrayList(splittedV6Address.iterator());
240 /* Isn't it fun - the above variable names are the same in BSD and Sun sources */
244 char[] src = partsV6Address.get(0).toCharArray();
246 byte[] dst = new byte[INADDR6SZ];
248 int src_length = src.length;
253 /* Leading :: requires some special handling. */
255 /* Isn't it fun - the above comment is again the same in BSD and Sun sources,
256 * We will derive our code from BSD. Shakespear always sounds better
257 * in original Clingon. So does Dilbert.
261 Preconditions.checkArgument(src[++i] == ':', "Invalid v6 address");
269 while (i < src_length) {
271 int chval = Character.digit(ch, 16);
273 /* Business as usual - ipv6 address digit.
274 * We can remove all checks from the original BSD code because
275 * the regexp has already verified that we are not being fed
276 * anything bigger than 0xffff between the separators.
291 /* no need to check separator position validity - regexp does that */
296 /* removed overrun check - the regexp checks for valid data */
298 dst[j++] = (byte) ((val >>> 8) & 0xff);
299 dst[j++] = (byte) (val & 0xff);
305 /* frankenstein - v4 attached to v6, mixed notation */
307 if (ch == '.' && ((j + INADDR4SZ) <= INADDR6SZ)) {
309 /* this has passed the regexp so it is fairly safe to parse it
310 * straight away. As v4 addresses do not suffer from the same
311 * defficiencies as the java v6 implementation we can invoke it
312 * straight away and be done with it
315 Preconditions.checkArgument(j != (INADDR6SZ - INADDR4SZ - 1), "Invalid v4 in v6 mapping");
317 InetAddress _inet_form = InetAddresses.forString(partsV6Address.get(0).substring(curtok, src_length));
319 Preconditions.checkArgument(_inet_form instanceof Inet4Address);
320 System.arraycopy(_inet_form.getAddress(), 0, dst, j, INADDR4SZ);
326 /* removed parser exit on invalid char - no need to do it, regexp checks it */
329 Preconditions.checkArgument(j + INT16SZ <= INADDR6SZ, "Overrun in v6 parsing, should not occur");
330 dst[j++] = (byte) ((val >> 8) & 0xff);
331 dst[j++] = (byte) (val & 0xff);
337 Preconditions.checkArgument(j != INADDR6SZ, "Overrun in v6 parsing, should not occur");
338 for (i = 1; i <= n; i++) {
339 dst[INADDR6SZ - i] = dst[colonp + n - i];
340 dst[colonp + n - i] = 0;
345 Preconditions.checkArgument(j == INADDR6SZ, "Overrun in v6 parsing, should not occur");
350 public static String byteArrayV6AddressToString (final byte [] _binary_form) throws UnknownHostException{
351 /* DO NOT DIY!!! - InetAddresses will actually print correct canonical
352 * zero compressed form.
354 return InetAddresses.toAddrString(InetAddress.getByAddress(_binary_form));
357 private static int nextNibble(final int mask) {
364 return 0xff << (8 - mask);
368 * Convert Ipv6Prefix object to a valid Canonical v6 prefix in byte format
370 * @param ipv6Prefix - v6 prefix object
371 * @return - byte array of size 16 + 1. Last byte contains netmask
373 public static byte[] canonicalBinaryV6Prefix(final Ipv6Prefix ipv6Prefix) {
375 * Do not modify this routine to take direct strings input!!!
376 * Key checks have been removed based on the assumption that
377 * the input is validated via regexps in Ipv6Prefix()
382 Iterable<String> splittedV6Prefix = Splitter.on("/")
385 .split(ipv6Prefix.getValue());
386 List<String> partsV6Prefix = Lists.newArrayList(splittedV6Prefix.iterator());
388 boolean valid = true;
391 mask = Integer.parseInt(partsV6Prefix.get(1));
395 } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
399 Preconditions.checkArgument(valid, "Supplied netmask in %s is invalid", ipv6Prefix.getValue());
406 /* Isn't it fun - the above variable names are the same in BSD and Sun sources */
410 char[] src = partsV6Prefix.get(0).toCharArray();
412 byte[] dst = new byte[INADDR6SZ + 1];
416 int src_length = src.length;
421 /* Leading :: requires some special handling. */
423 /* Isn't it fun - the above comment is again the same in BSD and Sun sources,
424 * We will derive our code from BSD. Shakespear always sounds better
425 * in original Clingon. So does Dilbert.
429 Preconditions.checkArgument(src[++i] == ':', "Invalid v6 address");
437 while (i < src_length) {
439 int chval = Character.digit(ch, 16);
441 /* Business as usual - ipv6 address digit.
442 * We can remove all checks from the original BSD code because
443 * the regexp has already verified that we are not being fed
444 * anything bigger than 0xffff between the separators.
459 /* no need to check separator position validity - regexp does that */
464 /* removed overrun check - the regexp checks for valid data */
469 /* stop parsing if we are past the mask */
473 dst[j] = (byte) ((val >> 8) & nextNibble(m)); j++; m = m - 8;
476 /* stop parsing if we are past the mask */
480 dst[j] = (byte) (val & nextNibble(m)); j++; m = m - 8;
486 /* frankenstein - v4 attached to v6, mixed notation */
488 if (ch == '.' && ((j + INADDR4SZ) <= INADDR6SZ)) {
490 /* this has passed the regexp so it is fairly safe to parse it
491 * straight away. As v4 addresses do not suffer from the same
492 * defficiencies as the java v6 implementation we can invoke it
493 * straight away and be done with it
496 Preconditions.checkArgument(j != (INADDR6SZ - INADDR4SZ - 1), "Invalid v4 in v6 mapping");
498 InetAddress _inet_form = InetAddresses.forString(partsV6Prefix.get(0).substring(curtok, src_length));
500 Preconditions.checkArgument(_inet_form instanceof Inet4Address);
501 System.arraycopy(_inet_form.getAddress(), 0, dst, j, INADDR4SZ);
507 /* removed parser exit on ivalid char - no need to do it, regexp checks it */
510 Preconditions.checkArgument(j + INT16SZ <= INADDR6SZ, "Overrun in v6 parsing, should not occur");
511 dst[j] = (byte) ((val >> 8) & nextNibble(m)) ; j++; m = m - 8;
512 dst[j] = (byte) (val & nextNibble(m)); j++; m = m - 8;
515 if ((j < INADDR6SZ) && (m < 0)) {
517 for (i = j; i < INADDR6SZ; i++) {
525 Preconditions.checkArgument(j != INADDR6SZ, "Overrun in v6 parsing, should not occur");
526 for (i = 1; i <= n; i++) {
527 dst[INADDR6SZ - i] = dst[colonp + n - i];
528 dst[colonp + n - i] = 0;
532 Preconditions.checkArgument(j == INADDR6SZ, "Overrun in v6 parsing, should not occur");
535 dst[INADDR6SZ] = (byte) mask;
540 * Print a v6 prefix in byte array + 1 notation
541 * @param _binary_form - prefix, in byte [] form, last byte is netmask
542 * @return string of v6 prefix
543 * @throws UnknownHostException unknown host exception
545 public static String byteArrayV6PrefixToString(final byte [] _binary_form) throws UnknownHostException {
546 /* NO DIY!!! - InetAddresses will actually print correct canonical
547 * zero compressed form
549 StringBuilder sb = new StringBuilder();
550 /* Yang RFC specifies that the normalized form is RFC 5952, note - java
551 * core type is not RFC compliant, guava is.
554 InetAddresses.toAddrString(
555 InetAddress.getByAddress(
556 Arrays.copyOf(_binary_form, INADDR6SZ)
561 sb.append(_binary_form[INADDR6SZ] & 0xff);
562 return sb.toString();
565 private static int ipv6PrefixByteArrayOffset(final int mask) {
570 final int ret = mask * INADDR6SZ;
571 if (ret < PREFIX_BYTEARRAYS.length) {
574 return PREFIX_BYTEARRAYS.length - INADDR6SZ;
579 * Canonicalize a v6 prefix while in binary form
581 * @param prefix - prefix, in byte [] form
582 * @param mask - mask - number of bits
584 public static void canonicalizeIpv6Prefix(final byte [] prefix, final int mask) {
585 final int offset = ipv6PrefixByteArrayOffset(mask);
587 for (int i = 0; i < INADDR6SZ; i++) {
588 prefix[i] &= PREFIX_BYTEARRAYS[offset + i];
592 public static byte[] convertIpv6PrefixToByteArray(final int prefix) {
593 final int offset = ipv6PrefixByteArrayOffset(prefix);
595 return Arrays.copyOfRange(PREFIX_BYTEARRAYS, offset, offset + INADDR6SZ);
598 public static Ipv6Address extractIpv6Address(final Ipv6Prefix ipv6Prefix) {
599 return IetfInetUtil.INSTANCE.ipv6AddressFrom(ipv6Prefix);
602 public static Ipv4Address extractIpv4Address(final Ipv4Prefix ipv4Prefix) {
603 Iterator<String> addressParts = PREFIX_SPLITTER.split(ipv4Prefix.getValue()).iterator();
604 return new Ipv4Address(addressParts.next());
607 public static DottedQuad extractIpv4AddressMask(final Ipv4Prefix ipv4Prefix) {
608 Iterator<String> addressParts = PREFIX_SPLITTER.split(ipv4Prefix.getValue()).iterator();
611 if (addressParts.hasNext()) {
612 cidrMask = Integer.parseInt(addressParts.next());
615 maskBits = 0xffffffff << IPV4_ADDRESS_LENGTH - cidrMask;
616 String mask = String.format("%d.%d.%d.%d", (maskBits & 0x0000000000ff000000L) >> 24, (maskBits & 0x0000000000ff0000) >> 16, (maskBits & 0x0000000000ff00) >> 8, maskBits & 0xff);
617 DottedQuad netMask = new DottedQuad(mask);
621 public static Integer extractIpv6Prefix(final Ipv6Prefix ipv6Prefix) {
622 return IetfInetUtil.INSTANCE.splitIpv6Prefix(ipv6Prefix).getValue();
625 public static int countBits(final byte[] mask) {
627 for (byte b : mask) {
628 netmask += Integer.bitCount(UnsignedBytes.toInt(b));
633 public static final byte[] convertArbitraryMaskToByteArray(DottedQuad mask) {
635 if (mask != null && mask.getValue() != null) {
636 maskValue = mask.getValue();
638 maskValue = DEFAULT_ARBITRARY_BIT_MASK;
640 InetAddress maskInIpFormat = null;
642 maskInIpFormat = InetAddress.getByName(maskValue);
643 } catch (UnknownHostException e) {
644 LOG.error ("Failed to resolve the ip address of the mask",e);
646 byte[] bytes = maskInIpFormat.getAddress();
650 public static boolean isArbitraryBitMask(byte[] byteMask) {
651 if (byteMask == null) {
654 ArrayList<Integer> integerMaskArrayList = new ArrayList<Integer>();
656 // converting byte array to bits
657 maskInBits = new BigInteger(1, byteMask).toString(2);
659 ArrayList<String> stringMaskArrayList = new ArrayList<String>(Arrays.asList(maskInBits.split("(?!^)")));
660 for (String string:stringMaskArrayList) {
661 integerMaskArrayList.add(Integer.parseInt(string));
663 return checkArbitraryBitMask(integerMaskArrayList);
667 private static boolean checkArbitraryBitMask(ArrayList<Integer> arrayList) {
668 // checks 0*1* case - Leading zeros in arrayList are truncated
669 if (arrayList.size()>0 && arrayList.size()<IPV4_ADDRESS_LENGTH) {
673 for (int i=0; i<arrayList.size()-1;i++) {
674 if (arrayList.get(i) ==0 && arrayList.get(i+1) == 1) {