BUG-47 : unfinished PCEP migration to generated DTOs.
[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.Collections;
14 import java.util.List;
15
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
19
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.Lists;
22 import com.google.common.primitives.UnsignedBytes;
23
24 /**
25  * Util class for creating generated Ipv6Address.
26  */
27 public class Ipv6Util {
28
29         public static Ipv6Address addressForBytes(final byte[] bytes) {
30                 try {
31                         return new Ipv6Address(Inet6Address.getByAddress(bytes).toString());
32                 } catch (final UnknownHostException e) {
33                         throw new IllegalArgumentException(e.getMessage());
34                 }
35         }
36
37         public static byte[] bytesForAddress(final Ipv6Address address) {
38                 Inet6Address a;
39                 try {
40                         a = (Inet6Address) InetAddress.getByName(address.getValue());
41                 } catch (final UnknownHostException e) {
42                         throw new IllegalArgumentException(e.getMessage());
43                 }
44                 return a.getAddress();
45         }
46
47         public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
48                 Preconditions.checkArgument(length <= bytes.length * 8);
49                 return new Ipv6Prefix(addressForBytes(bytes).toString() + "/" + length);
50         }
51
52         public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
53                 if (bytes.length == 0)
54                         return Collections.emptyList();
55
56                 final List<Ipv6Prefix> list = Lists.newArrayList();
57                 int byteOffset = 0;
58                 while (byteOffset < bytes.length) {
59                         final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
60                         byteOffset += 1;
61                         final int byteCount = (bitLength % 8 != 0) ? (bitLength / 8) + 1 : bitLength / 8;
62                         list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
63                         byteOffset += byteCount;
64                 }
65                 return list;
66         }
67 }