1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
3 package org.opendaylight.openflowjava.protocol.impl.util;
\r
5 import io.netty.buffer.ByteBuf;
\r
6 import io.netty.buffer.UnpooledByteBufAllocator;
\r
8 import java.util.ArrayList;
\r
9 import java.util.List;
\r
10 import java.util.Map;
\r
11 import java.util.Map.Entry;
\r
13 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
\r
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
\r
17 import com.google.common.base.Joiner;
\r
19 /** Class for common operations on ByteBuf
\r
20 * @author michal.polkorab
\r
21 * @author timotej.kubas
\r
23 public abstract class ByteBufUtils {
\r
25 private static final byte MAC_ADDRESS_LENGTH = 6;
\r
28 * Converts ByteBuf into String
\r
29 * @param bb input ByteBuf
\r
32 public static String byteBufToHexString(ByteBuf bb) {
\r
33 StringBuffer sb = new StringBuffer();
\r
34 for (int i = 0; i < bb.readableBytes(); i++) {
\r
35 short b = bb.getUnsignedByte(i);
\r
36 sb.append(String.format("%02x ", b));
\r
38 return sb.toString();
\r
42 * Converts String into byte[]
\r
43 * @param hexSrc input String
\r
44 * @return byte[] filled with input data
\r
46 public static byte[] hexStringToBytes(String hexSrc) {
\r
47 return hexStringToBytes(hexSrc, true);
\r
51 * Converts String into byte[]
\r
52 * @param hexSrc input String
\r
53 * @param withSpaces if there are spaces in string
\r
54 * @return byte[] filled with input data
\r
56 public static byte[] hexStringToBytes(String hexSrc, boolean withSpaces ) {
\r
57 String splitPattern = "\\s+";
\r
59 splitPattern = "(?<=\\G.{2})";
\r
62 String[] byteChips = hexSrc.split(splitPattern);
\r
63 byte[] result = new byte[byteChips.length];
\r
64 for (int i = 0; i < byteChips.length; i++) {
\r
65 result[i] = (byte) Short.parseShort(byteChips[i], 16);
\r
71 * Creates ByteBuf filled with specified data
\r
72 * @param hexSrc input String of bytes in hex format
\r
73 * @return ByteBuf with specified hexString converted
\r
75 public static ByteBuf hexStringToByteBuf(String hexSrc) {
\r
76 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
\r
77 hexStringToByteBuf(hexSrc, out);
\r
82 * Creates ByteBuf filled with specified data
\r
83 * @param hexSrc input String of bytes in hex format
\r
84 * @param out ByteBuf with specified hexString converted
\r
86 public static void hexStringToByteBuf(String hexSrc, ByteBuf out) {
\r
87 out.writeBytes(hexStringToBytes(hexSrc));
\r
91 * Fills specified ByteBuf with 0 (zeros) of desired length, used for padding
\r
93 * @param out ByteBuf to be padded
\r
95 public static void padBuffer(int length, ByteBuf out) {
\r
96 for (int i = 0; i < length; i++) {
\r
102 * Create standard OF header
\r
103 * @param factory serialization factory
\r
104 * @param message POJO
\r
105 * @param out writing buffer
\r
107 public static <E extends OfHeader> void writeOFHeader(OFSerializer<E> factory, E message, ByteBuf out) {
\r
108 out.writeByte(message.getVersion());
\r
109 out.writeByte(factory.getMessageType());
\r
110 out.writeShort(factory.computeLength(message));
\r
111 out.writeInt(message.getXid().intValue());
\r
116 * Fills the bitmask from boolean map where key is bit position
\r
117 * @param booleanMap bit to boolean mapping
\r
120 public static int fillBitMaskFromMap(Map<Integer, Boolean> booleanMap) {
\r
123 for (Entry<Integer, Boolean> iterator : booleanMap.entrySet()) {
\r
124 if (iterator.getValue() != null && iterator.getValue().booleanValue()) {
\r
125 bitmask |= 1 << iterator.getKey();
\r
132 * Fills the bitmask from boolean list where key is bit position
\r
133 * @param booleanList bit to boolean mapping
\r
136 public static int[] fillBitMaskFromList(List<Boolean> booleanList) {
\r
139 int arrayIndex = 0;
\r
140 if ((booleanList.size() % Integer.SIZE) != 0) {
\r
141 bitmask = new int[booleanList.size() / Integer.SIZE + 1];
\r
143 bitmask = new int[booleanList.size() / Integer.SIZE];
\r
145 for (Boolean currElement : booleanList) {
\r
146 if (currElement != null && currElement.booleanValue()) {
\r
147 bitmask[arrayIndex] |= 1 << index;
\r
150 arrayIndex = index / Integer.SIZE;
\r
156 * Converts byte array into String
\r
157 * @param array input byte array
\r
160 public static String bytesToHexString(byte[] array) {
\r
161 StringBuffer sb = new StringBuffer();
\r
162 for (int i = 0; i < array.length; i++) {
\r
163 short b = array[i];
\r
164 sb.append(String.format("%02x ", b));
\r
166 return sb.toString();
\r
170 * Converts macAddress to byte array
\r
171 * @param macAddress
\r
172 * @return byte representation of mac address
\r
173 * @see {@link MacAddress}
\r
175 public static byte[] macAddressToBytes(String macAddress) {
\r
176 String[] sequences = macAddress.split(":");
\r
177 byte[] result = new byte[MAC_ADDRESS_LENGTH];
\r
178 for (int i = 0; i < sequences.length; i++) {
\r
179 result[i] = (byte) Short.parseShort(sequences[i], 16);
\r
185 * Converts mac address represented in bytes to String
\r
187 * @return String representation of mac address
\r
188 * @see {@link MacAddress}
\r
190 public static String macAddressToString(byte[] address) {
\r
191 List<String> groups = new ArrayList<>();
\r
192 for(int i=0; i < MAC_ADDRESS_LENGTH; i++){
\r
193 groups.add(String.format("%02X", address[i]));
\r
195 Joiner joiner = Joiner.on(":");
\r
196 return joiner.join(groups);
\r