Javadoc update
[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.List;\r
9 import java.util.Map;\r
10 import java.util.Map.Entry;\r
11 \r
12 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
14 \r
15 /** Class for common operations on ByteBuf\r
16  * @author michal.polkorab\r
17  * @author timotej.kubas\r
18  */\r
19 public abstract class ByteBufUtils {\r
20 \r
21 \r
22     /**\r
23      * Converts ByteBuf into String\r
24      * @param bb input ByteBuf\r
25      * @return String\r
26      */\r
27     public static String byteBufToHexString(ByteBuf bb) {\r
28         StringBuffer sb = new StringBuffer();\r
29         for (int i = 0; i < bb.readableBytes(); i++) {\r
30             short b = bb.getUnsignedByte(i);\r
31             sb.append(String.format("%02x ", b));\r
32         }\r
33         return sb.toString();\r
34     }\r
35     \r
36     /**\r
37      * Converts String into byte[]\r
38      * @param hexSrc input String\r
39      * @return byte[] filled with input data\r
40      */\r
41     public static byte[] hexStringToBytes(String hexSrc) {\r
42         return hexStringToBytes(hexSrc, true);\r
43     }\r
44     \r
45     /**\r
46      * Converts String into byte[]\r
47      * @param hexSrc input String\r
48      * @param withSpaces if there are spaces in string \r
49      * @return byte[] filled with input data\r
50      */\r
51     public static byte[] hexStringToBytes(String hexSrc, boolean withSpaces ) {\r
52         String splitPattern = "\\s+";\r
53         if (!withSpaces) {\r
54             splitPattern = "(?<=\\G.{2})";\r
55         }\r
56         \r
57         String[] byteChips = hexSrc.split(splitPattern);\r
58         byte[] result = new byte[byteChips.length];\r
59         for (int i = 0; i < byteChips.length; i++) {\r
60             result[i] = (byte) Short.parseShort(byteChips[i], 16);\r
61         }\r
62         return result;\r
63     }\r
64     \r
65     /**\r
66      * Creates ByteBuf filled with specified data\r
67      * @param hexSrc input String of bytes in hex format\r
68      * @return ByteBuf with specified hexString converted\r
69      */\r
70     public static ByteBuf hexStringToByteBuf(String hexSrc) {\r
71         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
72         hexStringToByteBuf(hexSrc, out);\r
73         return out;\r
74     }\r
75     \r
76     /**\r
77      * Creates ByteBuf filled with specified data\r
78      * @param hexSrc input String of bytes in hex format\r
79      * @param out ByteBuf with specified hexString converted\r
80      */\r
81     public static void hexStringToByteBuf(String hexSrc, ByteBuf out) {\r
82         out.writeBytes(hexStringToBytes(hexSrc));\r
83     }\r
84     \r
85     /**\r
86      * Fills specified ByteBuf with 0 (zeros) of desired length, used for padding\r
87      * @param length\r
88      * @param out ByteBuf to be padded\r
89      */\r
90     public static void padBuffer(int length, ByteBuf out) {\r
91         for (int i = 0; i < length; i++) {\r
92             out.writeByte(0);\r
93         }\r
94     }\r
95     \r
96     /**\r
97      * Create standard OF header\r
98      * @param factory serialization factory \r
99      * @param message POJO\r
100      * @param out writing buffer\r
101      */\r
102     public static <E extends OfHeader> void writeOFHeader(OFSerializer<E> factory, E message, ByteBuf out) { \r
103         out.writeByte(message.getVersion());\r
104         out.writeByte(factory.getMessageType());\r
105         out.writeShort(factory.computeLength(message));\r
106         out.writeInt(message.getXid().intValue());\r
107 \r
108     }\r
109 \r
110     /**\r
111      * Fills the bitmask from boolean map where key is bit position\r
112      * @param booleanMap bit to boolean mapping\r
113      * @return bit mask\r
114      */\r
115     public static int fillBitMaskFromMap(Map<Integer, Boolean> booleanMap) {\r
116         int bitmask = 0;\r
117         \r
118         for (Entry<Integer, Boolean> iterator : booleanMap.entrySet()) {\r
119             if (iterator.getValue() != null && iterator.getValue().booleanValue()) {\r
120                 bitmask |= 1 << iterator.getKey();\r
121             }\r
122         }\r
123         return bitmask;\r
124     }\r
125     \r
126     /**\r
127      * Fills the bitmask from boolean list where key is bit position\r
128      * @param booleanList bit to boolean mapping\r
129      * @return bit mask\r
130      */\r
131     public static int[] fillBitMaskFromList(List<Boolean> booleanList) {\r
132         int[] bitmask;\r
133         int index = 0;\r
134         int arrayIndex = 0;\r
135         if ((booleanList.size() % Integer.SIZE) != 0) {\r
136             bitmask = new int[booleanList.size() / Integer.SIZE + 1];\r
137         } else {\r
138             bitmask = new int[booleanList.size() / Integer.SIZE];\r
139         }\r
140         for (Boolean currElement : booleanList) {\r
141             if (currElement != null && currElement.booleanValue()) {\r
142                 bitmask[arrayIndex] |= 1 << index;\r
143             }\r
144             index++;\r
145             arrayIndex = index / Integer.SIZE;\r
146         }\r
147         return bitmask;\r
148     }\r
149 \r
150     /**\r
151      * Converts byte array into String\r
152      * @param array input byte array\r
153      * @return String\r
154      */\r
155     public static String bytesToHexString(byte[] array) {\r
156         StringBuffer sb = new StringBuffer();\r
157         for (int i = 0; i < array.length; i++) {\r
158             short b = array[i];\r
159             sb.append(String.format("%02x ", b));\r
160         }\r
161         return sb.toString();\r
162     }\r
163 }\r