2 * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
10 package org.opendaylight.openflowjava.util;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.UnpooledByteBufAllocator;
15 import java.util.ArrayList;
16 import java.util.List;
18 import java.util.Map.Entry;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
24 import com.google.common.base.Preconditions;
25 import com.google.common.base.Splitter;
26 import com.google.common.collect.Lists;
27 import com.google.common.primitives.UnsignedBytes;
29 /** Class for common operations on ByteBuf
30 * @author michal.polkorab
31 * @author timotej.kubas
33 public abstract class ByteBufUtils {
34 public static final Splitter DOT_SPLITTER = Splitter.on('.');
35 public static final Splitter COLON_SPLITTER = Splitter.on(':');
36 private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
39 * Converts ByteBuf into String
40 * @param bb input ByteBuf
43 public static String byteBufToHexString(final ByteBuf bb) {
44 StringBuffer sb = new StringBuffer();
45 for (int i = bb.readerIndex(); i < (bb.readerIndex() + bb.readableBytes()); i++) {
46 sb.append(String.format(" %02x", bb.getUnsignedByte(i)));
48 return sb.toString().trim();
52 * Converts String into byte[]
53 * @param hexSrc input String
54 * @return byte[] filled with input data
56 public static byte[] hexStringToBytes(final String hexSrc) {
57 return hexStringToBytes(hexSrc, true);
61 * Converts String into byte[]
62 * @param hexSrc input String
63 * @param withSpaces if there are spaces in string
64 * @return byte[] filled with input data
66 public static byte[] hexStringToBytes(final String hexSrc, final boolean withSpaces ) {
67 String splitPattern = "\\s+";
69 splitPattern = "(?<=\\G.{2})";
71 Iterable<String> tmp = Splitter.onPattern(splitPattern)
72 .omitEmptyStrings().split(hexSrc);
73 ArrayList<String> byteChips = Lists.newArrayList(tmp);
74 byte[] result = new byte[byteChips.size()];
76 for (String chip : byteChips) {
77 result[i] = (byte) Short.parseShort(chip, 16);
84 * Creates ByteBuf filled with specified data
85 * @param hexSrc input String of bytes in hex format
86 * @return ByteBuf with specified hexString converted
88 public static ByteBuf hexStringToByteBuf(final String hexSrc) {
89 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
90 hexStringToByteBuf(hexSrc, out);
95 * Creates ByteBuf filled with specified data
96 * @param hexSrc input String of bytes in hex format
97 * @param out ByteBuf with specified hexString converted
99 public static void hexStringToByteBuf(final String hexSrc, final ByteBuf out) {
100 out.writeBytes(hexStringToBytes(hexSrc));
104 * Fills specified ByteBuf with 0 (zeros) of desired length, used for padding
106 * @param out ByteBuf to be padded
107 * @deprecated Use {@link ByteBuf#writeZero(int)} directly.
110 public static void padBuffer(final int length, final ByteBuf out) {
111 out.writeZero(length);
115 * Create standard OF header
116 * @param msgType message code
117 * @param message POJO
118 * @param out writing buffer
119 * @param length ofheader length
121 public static <E extends OfHeader> void writeOFHeader(final byte msgType, final E message, final ByteBuf out, final int length) {
122 out.writeByte(message.getVersion());
123 out.writeByte(msgType);
124 out.writeShort(length);
125 out.writeInt(message.getXid().intValue());
129 * Write length standard OF header
130 * @param out writing buffer
132 public static void updateOFHeaderLength(final ByteBuf out) {
133 out.setShort(EncodeConstants.OFHEADER_LENGTH_INDEX, out.readableBytes());
137 * Fills the bitmask from boolean map where key is bit position
138 * @param booleanMap bit to boolean mapping
141 public static int fillBitMaskFromMap(final Map<Integer, Boolean> booleanMap) {
144 for (Entry<Integer, Boolean> iterator : booleanMap.entrySet()) {
145 if (iterator.getValue() != null && iterator.getValue().booleanValue()) {
146 bitmask |= 1 << iterator.getKey();
153 * Fills the bitmask from a set of bit values, starting at specified offset.
155 * @param offset Bit offset to start at
156 * @param values boolean bit values to fill
157 * @return Filled-in bitmask
159 public static int fillBitMask(final int offset, final boolean... values) {
163 for (boolean v : values) {
174 * Fills the bitmask from boolean list where key is bit position
175 * @param booleanList bit to boolean mapping
178 public static int[] fillBitMaskFromList(final List<Boolean> booleanList) {
182 if ((booleanList.size() % Integer.SIZE) != 0) {
183 bitmask = new int[booleanList.size() / Integer.SIZE + 1];
185 bitmask = new int[booleanList.size() / Integer.SIZE];
187 for (Boolean currElement : booleanList) {
188 if (currElement != null && currElement.booleanValue()) {
189 bitmask[arrayIndex] |= 1 << index;
192 arrayIndex = index / Integer.SIZE;
198 * Converts byte array into String
199 * @param array input byte array
202 public static String bytesToHexString(final byte[] array) {
203 StringBuffer sb = new StringBuffer();
204 for (byte element : array) {
205 sb.append(String.format(" %02x", element));
207 return sb.toString().trim();
210 private static int hexValue(final char c) {
211 if (c >= '0' && c <= '9') {
214 if (c >= 'a' && c <= 'f') {
217 if (c >= 'A' && c <= 'F') {
221 throw new IllegalArgumentException(String.format("Invalid character '%s' encountered", c));
225 * Converts macAddress to byte array
227 * @return byte representation of mac address
228 * @see {@link MacAddress}
230 * FIXME: this method does not support shortened values, e.g.
231 * "0:1:2:3:4:5", only "00:11:22:33:44:55".
233 public static byte[] macAddressToBytes(final String macAddress) {
234 final byte[] result = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
235 final char[] mac = macAddress.toCharArray();
238 for (int i = 0; i < EncodeConstants.MAC_ADDRESS_LENGTH - 1; ++i) {
239 result[i] = UnsignedBytes.checkedCast(
240 (hexValue(mac[offset++]) << 4) | hexValue(mac[offset++]));
241 Preconditions.checkArgument(mac[offset] == ':', "Invalid value: %s", macAddress);
245 result[EncodeConstants.MAC_ADDRESS_LENGTH - 1] =
246 UnsignedBytes.checkedCast(hexValue(mac[offset++]) << 4 | hexValue(mac[offset]));
250 private static final void appendHexByte(final StringBuilder sb, final byte b) {
251 final int v = UnsignedBytes.toInt(b);
252 sb.append(HEX_CHARS[v >>> 4]);
253 sb.append(HEX_CHARS[v & 15]);
256 private static void appendHexUnsignedShort(final StringBuilder sb, final int val) {
257 sb.append(ByteBufUtils.HEX_CHARS[(val >>> 12) & 15]);
258 sb.append(ByteBufUtils.HEX_CHARS[(val >>> 8) & 15]);
259 sb.append(ByteBufUtils.HEX_CHARS[(val >>> 4) & 15]);
260 sb.append(ByteBufUtils.HEX_CHARS[ val & 15]);
264 * Converts a MAC address represented in bytes to String
266 * @return String representation of a MAC address
267 * @see {@link MacAddress}
269 public static String macAddressToString(final byte[] address) {
270 Preconditions.checkArgument(address.length == EncodeConstants.MAC_ADDRESS_LENGTH);
272 final StringBuilder sb = new StringBuilder(17);
274 appendHexByte(sb, address[0]);
275 for (int i = 1; i < EncodeConstants.MAC_ADDRESS_LENGTH; i++) {
277 appendHexByte(sb, address[i]);
280 return sb.toString();
284 * Reads and parses null-terminated string from ByteBuf
286 * @param length maximal length of String
287 * @return String with name of port
289 public static String decodeNullTerminatedString(final ByteBuf rawMessage, final int length) {
290 byte[] name = new byte[length];
291 rawMessage.readBytes(name);
292 return new String(name).trim();
296 * Read an IPv4 address from a buffer and format it into dotted-quad string.
298 * @param buf Input buffer
299 * @return Dotted-quad string
301 public static String readIpv4Address(final ByteBuf buf) {
302 final StringBuilder sb = new StringBuilder(EncodeConstants.GROUPS_IN_IPV4_ADDRESS * 4 - 1);
304 sb.append(buf.readUnsignedByte());
305 for (int i = 1; i < EncodeConstants.GROUPS_IN_IPV4_ADDRESS; i++) {
307 sb.append(buf.readUnsignedByte());
310 return sb.toString();
314 * Read an IPv6 address from a buffer and format it into a string of eight groups of four
315 * hexadecimal digits separated by colons.
317 * @param buf Input buffer
318 * @return IPv6 address in string format
320 public static String readIpv6Address(final ByteBuf buf) {
321 final StringBuilder sb = new StringBuilder(EncodeConstants.GROUPS_IN_IPV6_ADDRESS * 5 - 1);
323 appendHexUnsignedShort(sb, buf.readUnsignedShort());
324 for (int i = 1; i < EncodeConstants.GROUPS_IN_IPV6_ADDRESS; i++) {
326 appendHexUnsignedShort(sb, buf.readUnsignedShort());
329 return sb.toString();