BUG-47 : moved Ipv4 and Ipv6 to generated source code.
[bgpcep.git] / concepts / src / main / java / org / opendaylight / protocol / concepts / AbstractAddressFamily.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.util.Collections;
11 import java.util.Set;
12
13 import org.opendaylight.protocol.util.ByteArray;
14 import com.google.common.collect.Sets;
15 import com.google.common.primitives.UnsignedBytes;
16
17 public abstract class AbstractAddressFamily<T extends NetworkAddress<?>> implements AddressFamily<T> {
18         @Override
19         public Set<Prefix<T>> prefixListForBytes(byte[] bytes) {
20                 if (bytes.length == 0)
21                         return Collections.emptySet();
22
23                 final Set<Prefix<T>> list = Sets.newHashSet();
24                 int byteOffset = 0;
25                 while (byteOffset < bytes.length) {
26                         final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
27                         byteOffset += 1;
28                         final int byteCount = (bitLength % 8 != 0) ? (bitLength / 8) + 1 : bitLength / 8;
29                         list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
30                         byteOffset += byteCount;
31                 }
32                 return list;
33         }
34 }
35