2 * Copyright (c) 2013 Cisco 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
8 package org.opendaylight.protocol.concepts;
10 import java.net.Inet6Address;
11 import java.net.UnknownHostException;
12 import java.util.Collections;
13 import java.util.List;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.Lists;
21 import com.google.common.primitives.UnsignedBytes;
24 * Util class for creating generated Ipv6Address.
26 public class Ipv6Util {
28 public static Ipv6Address addressForBytes(final byte[] bytes) {
30 return new Ipv6Address(Inet6Address.getByAddress(bytes).toString());
31 } catch (final UnknownHostException e) {
32 throw new IllegalArgumentException(e.getMessage());
36 public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
37 Preconditions.checkArgument(length <= bytes.length * 8);
38 return new Ipv6Prefix(addressForBytes(bytes).toString() + "/" + length);
41 public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
42 if (bytes.length == 0)
43 return Collections.emptyList();
45 final List<Ipv6Prefix> list = Lists.newArrayList();
47 while (byteOffset < bytes.length) {
48 final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
50 final int byteCount = (bitLength % 8 != 0) ? (bitLength / 8) + 1 : bitLength / 8;
51 list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
52 byteOffset += byteCount;