Removed some sonar warnings from the rest of the project.
[bgpcep.git] / concepts / src / main / java / org / opendaylight / protocol / concepts / Ipv6Util.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.protocol.concepts;
9
10 import java.net.Inet6Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.List;
16
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
20
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.Lists;
23 import com.google.common.net.InetAddresses;
24 import com.google.common.primitives.Bytes;
25 import com.google.common.primitives.UnsignedBytes;
26
27 /**
28  * Util class for creating generated Ipv6Address.
29  */
30 public final class Ipv6Util {
31         private Ipv6Util() {
32         }
33
34         public static final int IPV6_LENGTH = 16;
35
36         private static InetAddress getAddress(final byte[] bytes) {
37                 try {
38                         return Inet6Address.getByAddress(bytes);
39                 } catch (final UnknownHostException e) {
40                         throw new IllegalArgumentException("Failed to construct IPv6 address", e);
41                 }
42         }
43
44         public static Ipv6Address addressForBytes(final byte[] bytes) {
45                 return new Ipv6Address(InetAddresses.toAddrString(getAddress(bytes)));
46         }
47
48         public static byte[] bytesForAddress(final Ipv6Address address) {
49                 final InetAddress a = InetAddresses.forString(address.getValue());
50                 Preconditions.checkArgument(a instanceof Inet6Address);
51                 return a.getAddress();
52         }
53
54         public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
55                 final String p = prefix.getValue();
56                 final int sep = p.indexOf('/');
57                 final InetAddress a = InetAddresses.forString(p.substring(0, sep));
58                 Preconditions.checkArgument(a instanceof Inet6Address);
59                 final byte[] bytes = a.getAddress();
60                 return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
61         }
62
63         public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
64                 Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
65                 final byte[] tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
66                 final InetAddress a = getAddress(tmp);
67                 return new Ipv6Prefix(InetAddresses.toAddrString(a) + '/' + length);
68         }
69
70         public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
71                 if (bytes.length == 0) {
72                         return Collections.emptyList();
73                 }
74
75                 final List<Ipv6Prefix> list = Lists.newArrayList();
76                 int byteOffset = 0;
77                 while (byteOffset < bytes.length) {
78                         final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
79                         byteOffset += 1;
80                         final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
81                         list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
82                         byteOffset += byteCount;
83                 }
84                 return list;
85         }
86 }