GetAsyncReplyMessageFactory, QueueGetConfigReplyMessageFactoryTest
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / ByteBufUtils.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 \r
3 package org.opendaylight.openflowjava.protocol.impl.util;\r
4 \r
5 import io.netty.buffer.ByteBuf;\r
6 import io.netty.buffer.UnpooledByteBufAllocator;\r
7 \r
8 import java.util.Map;\r
9 import java.util.Map.Entry;\r
10 \r
11 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
13 \r
14 /** Class for common operations on ByteBuf\r
15  *\r
16  * @author michal.polkorab\r
17  */\r
18 public abstract class ByteBufUtils {\r
19 \r
20     /**\r
21      * Converts ByteBuf into String\r
22      * @param bb input ByteBuf\r
23      * @return String\r
24      */\r
25     public static String byteBufToHexString(ByteBuf bb) {\r
26         StringBuffer sb = new StringBuffer();\r
27         for (int i = 0; i < bb.readableBytes(); i++) {\r
28             short b = bb.getUnsignedByte(i);\r
29             sb.append(String.format("%02x ", b));\r
30         }\r
31         return sb.toString();\r
32     }\r
33     \r
34     /**\r
35      * Converts String into byte[]\r
36      * @param hexSrc input String\r
37      * @return byte[] filled with input data\r
38      */\r
39     public static byte[] hexStringToBytes(String hexSrc) {\r
40         String[] byteChips = hexSrc.split("\\s+");\r
41         byte[] result = new byte[byteChips.length];\r
42         for (int i = 0; i < byteChips.length; i++) {\r
43             result[i] = (byte) Short.parseShort(byteChips[i], 16);\r
44         }\r
45         return result;\r
46     }\r
47     \r
48     /**\r
49      * Creates ByteBuf filled with specified data\r
50      * @param hexSrc input String of bytes in hex format\r
51      * @return ByteBuf with specified hexString converted\r
52      */\r
53     public static ByteBuf hexStringToByteBuf(String hexSrc) {\r
54         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
55         hexStringToByteBuf(hexSrc, out);\r
56         return out;\r
57     }\r
58     \r
59     /**\r
60      * Creates ByteBuf filled with specified data\r
61      * @param hexSrc input String of bytes in hex format\r
62      * @param out ByteBuf with specified hexString converted\r
63      */\r
64     public static void hexStringToByteBuf(String hexSrc, ByteBuf out) {\r
65         out.writeBytes(hexStringToBytes(hexSrc));\r
66     }\r
67     \r
68     /**\r
69      * Fills specified ByteBuf with 0 (zeros) of desired length, used for padding\r
70      * @param length\r
71      * @param out ByteBuf to be padded\r
72      */\r
73     public static void padBuffer(int length, ByteBuf out) {\r
74         for (int i = 0; i < length; i++) {\r
75             out.writeByte(0);\r
76         }\r
77     }\r
78     \r
79     /**\r
80      * Create standard OF header\r
81      * @param factory serialization factory \r
82      * @param message POJO\r
83      * @param out writing buffer\r
84      */\r
85     public static void writeOFHeader(OFSerializer<?> factory, OfHeader message, ByteBuf out) { \r
86         out.writeByte(message.getVersion());\r
87         out.writeByte(factory.getMessageType());\r
88         out.writeShort(factory.computeLength());\r
89         out.writeInt(message.getXid().intValue());\r
90 \r
91     }\r
92 \r
93     /**\r
94      * Fills the bitmask from boolean map where key is bit position\r
95      * @param booleanMap bit to boolean mapping\r
96      * @return bit mask\r
97      */\r
98     public static int fillBitMaskFromMap(Map<Integer, Boolean> booleanMap) {\r
99         int bitmask = 0;\r
100         \r
101         for (Entry<Integer, Boolean> iterator : booleanMap.entrySet()) {\r
102             if (iterator.getValue() != null && iterator.getValue().booleanValue()) {\r
103                 bitmask |= 1 << iterator.getKey();\r
104             }\r
105         }\r
106         return bitmask;\r
107     }\r
108 }\r