MacAddress (de)serialization fixed
[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.ArrayList;\r
9 import java.util.List;\r
10 import java.util.Map;\r
11 import java.util.Map.Entry;\r
12 \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
16 \r
17 import com.google.common.base.Joiner;\r
18 \r
19 /** Class for common operations on ByteBuf\r
20  * @author michal.polkorab\r
21  * @author timotej.kubas\r
22  */\r
23 public abstract class ByteBufUtils {\r
24 \r
25     private static final byte MAC_ADDRESS_LENGTH = 6;\r
26 \r
27     /**\r
28      * Converts ByteBuf into String\r
29      * @param bb input ByteBuf\r
30      * @return String\r
31      */\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
37         }\r
38         return sb.toString();\r
39     }\r
40     \r
41     /**\r
42      * Converts String into byte[]\r
43      * @param hexSrc input String\r
44      * @return byte[] filled with input data\r
45      */\r
46     public static byte[] hexStringToBytes(String hexSrc) {\r
47         return hexStringToBytes(hexSrc, true);\r
48     }\r
49     \r
50     /**\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
55      */\r
56     public static byte[] hexStringToBytes(String hexSrc, boolean withSpaces ) {\r
57         String splitPattern = "\\s+";\r
58         if (!withSpaces) {\r
59             splitPattern = "(?<=\\G.{2})";\r
60         }\r
61         \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
66         }\r
67         return result;\r
68     }\r
69     \r
70     /**\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
74      */\r
75     public static ByteBuf hexStringToByteBuf(String hexSrc) {\r
76         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
77         hexStringToByteBuf(hexSrc, out);\r
78         return out;\r
79     }\r
80     \r
81     /**\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
85      */\r
86     public static void hexStringToByteBuf(String hexSrc, ByteBuf out) {\r
87         out.writeBytes(hexStringToBytes(hexSrc));\r
88     }\r
89     \r
90     /**\r
91      * Fills specified ByteBuf with 0 (zeros) of desired length, used for padding\r
92      * @param length\r
93      * @param out ByteBuf to be padded\r
94      */\r
95     public static void padBuffer(int length, ByteBuf out) {\r
96         for (int i = 0; i < length; i++) {\r
97             out.writeByte(0);\r
98         }\r
99     }\r
100     \r
101     /**\r
102      * Create standard OF header\r
103      * @param factory serialization factory \r
104      * @param message POJO\r
105      * @param out writing buffer\r
106      */\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
112 \r
113     }\r
114 \r
115     /**\r
116      * Fills the bitmask from boolean map where key is bit position\r
117      * @param booleanMap bit to boolean mapping\r
118      * @return bit mask\r
119      */\r
120     public static int fillBitMaskFromMap(Map<Integer, Boolean> booleanMap) {\r
121         int bitmask = 0;\r
122         \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
126             }\r
127         }\r
128         return bitmask;\r
129     }\r
130     \r
131     /**\r
132      * Fills the bitmask from boolean list where key is bit position\r
133      * @param booleanList bit to boolean mapping\r
134      * @return bit mask\r
135      */\r
136     public static int[] fillBitMaskFromList(List<Boolean> booleanList) {\r
137         int[] bitmask;\r
138         int index = 0;\r
139         int arrayIndex = 0;\r
140         if ((booleanList.size() % Integer.SIZE) != 0) {\r
141             bitmask = new int[booleanList.size() / Integer.SIZE + 1];\r
142         } else {\r
143             bitmask = new int[booleanList.size() / Integer.SIZE];\r
144         }\r
145         for (Boolean currElement : booleanList) {\r
146             if (currElement != null && currElement.booleanValue()) {\r
147                 bitmask[arrayIndex] |= 1 << index;\r
148             }\r
149             index++;\r
150             arrayIndex = index / Integer.SIZE;\r
151         }\r
152         return bitmask;\r
153     }\r
154 \r
155     /**\r
156      * Converts byte array into String\r
157      * @param array input byte array\r
158      * @return String\r
159      */\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
165         }\r
166         return sb.toString();\r
167     }\r
168     \r
169     /**\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
174      */\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
180         }\r
181         return result;\r
182     }\r
183     \r
184     /**\r
185      * Converts mac address represented in bytes to String\r
186      * @param address\r
187      * @return String representation of mac address\r
188      * @see {@link MacAddress}\r
189      */\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
194         }\r
195         Joiner joiner = Joiner.on(":");\r
196         return joiner.join(groups); \r
197     }\r
198 }\r