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.protocol.impl.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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
23 import com.google.common.base.Joiner;
25 /** Class for common operations on ByteBuf
26 * @author michal.polkorab
27 * @author timotej.kubas
29 public abstract class ByteBufUtils {
32 * Converts ByteBuf into String
33 * @param bb input ByteBuf
36 public static String byteBufToHexString(ByteBuf bb) {
37 StringBuffer sb = new StringBuffer();
38 for (int i = bb.readerIndex(); i < (bb.readerIndex() + bb.readableBytes()); i++) {
39 sb.append(String.format(" %02x", bb.getUnsignedByte(i)));
41 return sb.toString().trim();
45 * Converts String into byte[]
46 * @param hexSrc input String
47 * @return byte[] filled with input data
49 public static byte[] hexStringToBytes(String hexSrc) {
50 return hexStringToBytes(hexSrc, true);
54 * Converts String into byte[]
55 * @param hexSrc input String
56 * @param withSpaces if there are spaces in string
57 * @return byte[] filled with input data
59 public static byte[] hexStringToBytes(String hexSrc, boolean withSpaces ) {
60 String splitPattern = "\\s+";
62 splitPattern = "(?<=\\G.{2})";
65 String[] byteChips = hexSrc.split(splitPattern);
66 byte[] result = new byte[byteChips.length];
67 for (int i = 0; i < byteChips.length; i++) {
68 result[i] = (byte) Short.parseShort(byteChips[i], 16);
74 * Creates ByteBuf filled with specified data
75 * @param hexSrc input String of bytes in hex format
76 * @return ByteBuf with specified hexString converted
78 public static ByteBuf hexStringToByteBuf(String hexSrc) {
79 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
80 hexStringToByteBuf(hexSrc, out);
85 * Creates ByteBuf filled with specified data
86 * @param hexSrc input String of bytes in hex format
87 * @param out ByteBuf with specified hexString converted
89 public static void hexStringToByteBuf(String hexSrc, ByteBuf out) {
90 out.writeBytes(hexStringToBytes(hexSrc));
94 * Fills specified ByteBuf with 0 (zeros) of desired length, used for padding
96 * @param out ByteBuf to be padded
98 public static void padBuffer(int length, ByteBuf out) {
99 for (int i = 0; i < length; i++) {
106 * Create standard OF header
107 * @param msgType message code
108 * @param message POJO
109 * @param out writing buffer
110 * @param length ofheader length
112 public static <E extends OfHeader> void writeOFHeader(byte msgType, E message, ByteBuf out, int length) {
113 out.writeByte(message.getVersion());
114 out.writeByte(msgType);
115 out.writeShort(length);
116 out.writeInt(message.getXid().intValue());
120 * Write length standard OF header
121 * @param out writing buffer
123 public static void updateOFHeaderLength(ByteBuf out) {
124 out.setShort(EncodeConstants.OFHEADER_LENGTH_INDEX, out.readableBytes());
128 * Fills the bitmask from boolean map where key is bit position
129 * @param booleanMap bit to boolean mapping
132 public static int fillBitMaskFromMap(Map<Integer, Boolean> booleanMap) {
135 for (Entry<Integer, Boolean> iterator : booleanMap.entrySet()) {
136 if (iterator.getValue() != null && iterator.getValue().booleanValue()) {
137 bitmask |= 1 << iterator.getKey();
144 * Fills the bitmask from boolean list where key is bit position
145 * @param booleanList bit to boolean mapping
148 public static int[] fillBitMaskFromList(List<Boolean> booleanList) {
152 if ((booleanList.size() % Integer.SIZE) != 0) {
153 bitmask = new int[booleanList.size() / Integer.SIZE + 1];
155 bitmask = new int[booleanList.size() / Integer.SIZE];
157 for (Boolean currElement : booleanList) {
158 if (currElement != null && currElement.booleanValue()) {
159 bitmask[arrayIndex] |= 1 << index;
162 arrayIndex = index / Integer.SIZE;
168 * Converts byte array into String
169 * @param array input byte array
172 public static String bytesToHexString(byte[] array) {
173 StringBuffer sb = new StringBuffer();
174 for (int i = 0; i < array.length; i++) {
175 sb.append(String.format(" %02x", array[i]));
177 return sb.toString().trim();
181 * Converts macAddress to byte array
183 * @return byte representation of mac address
184 * @see {@link MacAddress}
186 @SuppressWarnings("javadoc")
187 public static byte[] macAddressToBytes(String macAddress) {
188 String[] sequences = macAddress.split(":");
189 byte[] result = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
190 for (int i = 0; i < sequences.length; i++) {
191 result[i] = (byte) Short.parseShort(sequences[i], 16);
197 * Converts mac address represented in bytes to String
199 * @return String representation of mac address
200 * @see {@link MacAddress}
202 @SuppressWarnings("javadoc")
203 public static String macAddressToString(byte[] address) {
204 List<String> groups = new ArrayList<>();
205 for(int i=0; i < EncodeConstants.MAC_ADDRESS_LENGTH; i++){
206 groups.add(String.format("%02X", address[i]));
208 Joiner joiner = Joiner.on(":");
209 return joiner.join(groups);
213 * Reads and parses null-terminated string from ByteBuf
215 * @param length maximal length of String
216 * @return String with name of port
218 public static String decodeNullTerminatedString(ByteBuf rawMessage, int length) {
219 byte[] name = new byte[length];
220 rawMessage.readBytes(name);
221 return new String(name).trim();