BUG-49 : fixed tests for bgp-parser.
[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.UnsignedBytes;
25
26 /**
27  * Util class for creating generated Ipv6Address.
28  */
29 public class Ipv6Util {
30
31         public static Ipv6Address addressForBytes(final byte[] bytes) {
32                 try {
33                         return new Ipv6Address(InetAddresses.toAddrString(Inet6Address.getByAddress(bytes)));
34                 } catch (final UnknownHostException e) {
35                         throw new IllegalArgumentException(e.getMessage());
36                 }
37         }
38
39         public static byte[] bytesForAddress(final Ipv6Address address) {
40                 Inet6Address a;
41                 try {
42                         a = (Inet6Address) InetAddress.getByName(address.getValue());
43                 } catch (final UnknownHostException e) {
44                         throw new IllegalArgumentException(e.getMessage());
45                 }
46                 return a.getAddress();
47         }
48
49         public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
50                 Preconditions.checkArgument(length <= bytes.length * 8);
51                 byte[] tmp = Arrays.copyOfRange(bytes, 0, 16);
52                 InetAddress a = null;
53                 try {
54                         a = InetAddress.getByAddress(tmp);
55                 } catch (UnknownHostException e) {
56                         throw new IllegalArgumentException(e.getMessage());
57                 }
58                 return new Ipv6Prefix(InetAddresses.toAddrString(a) + "/" + length);
59         }
60
61         public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
62                 if (bytes.length == 0)
63                         return Collections.emptyList();
64
65                 final List<Ipv6Prefix> list = Lists.newArrayList();
66                 int byteOffset = 0;
67                 while (byteOffset < bytes.length) {
68                         final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
69                         byteOffset += 1;
70                         final int byteCount = (bitLength % 8 != 0) ? (bitLength / 8) + 1 : bitLength / 8;
71                         list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
72                         byteOffset += byteCount;
73                 }
74                 return list;
75         }
76 }