Merge "Added more (de)serialization factories + ByteBufUtils methods"
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / BufferHelper.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.util;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 import io.netty.buffer.UnpooledByteBufAllocator;\r
6 \r
7 import java.lang.reflect.InvocationTargetException;\r
8 import java.lang.reflect.Method;\r
9 \r
10 import org.junit.Assert;\r
11 import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer;\r
12 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest;\r
13 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
15 import org.opendaylight.yangtools.yang.binding.DataObject;\r
16 \r
17 /**\r
18  * @author michal.polkorab\r
19  * \r
20  */\r
21 public abstract class BufferHelper {\r
22 \r
23     /**\r
24      * \r
25      */\r
26     public static final Long DEFAULT_XID = 0x01020304L;\r
27     private static final byte[] XID = new byte[] { 0x01, 0x02, 0x03, 0x04 };\r
28 \r
29     /**\r
30      * @param payload\r
31      * @return ByteBuf filled with OpenFlow protocol message without first 4\r
32      *         bytes\r
33      */\r
34     public static ByteBuf buildBuffer(byte[] payload) {\r
35         ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();\r
36         bb.writeBytes(XID);\r
37         bb.writeBytes(payload);\r
38         return bb;\r
39     }\r
40     \r
41     /**\r
42      * @param payload String in hex format\r
43      * @return ByteBuf filled with OpenFlow protocol message without first 4\r
44      *         bytes\r
45      */\r
46     public static ByteBuf buildBuffer(String payload) {\r
47         return buildBuffer(ByteBufUtils.hexStringToBytes(payload));\r
48     }\r
49     \r
50     /**\r
51      * @return ByteBuf filled with OpenFlow protocol header message without first 4\r
52      *         bytes\r
53      */\r
54     public static ByteBuf buildBuffer() {\r
55         ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();\r
56         bb.writeBytes(XID);\r
57         bb.writeBytes(new byte[0]);\r
58         return bb;\r
59     }\r
60 \r
61     /**\r
62      * Use version 1.3 for encoded message\r
63      * @param input\r
64      *            ByteBuf to be checked for correct OpenFlow Protocol header\r
65      * @param msgType\r
66      *            type of received message\r
67      * @param length TODO\r
68      */\r
69     public static void checkHeaderV13(ByteBuf input, byte msgType, int length) {\r
70         checkHeader(input, msgType, length, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
71     }\r
72     \r
73     private static void checkHeader(ByteBuf input, byte msgType, int length, Short version) {\r
74         Assert.assertEquals("Wrong version", version, Short.valueOf(input.readByte()));\r
75         Assert.assertEquals("Wrong type", msgType, input.readByte());\r
76         Assert.assertEquals("Wrong length", length, input.readUnsignedShort());\r
77         Assert.assertEquals("Wrong Xid", DEFAULT_XID, Long.valueOf(input.readUnsignedInt()));\r
78     }\r
79     \r
80 \r
81     /**\r
82      * @param ofHeader OpenFlow protocol header\r
83      */\r
84     public static void checkHeaderV13(OfHeader ofHeader) {\r
85         checkHeader(ofHeader,  HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
86     }\r
87     \r
88     private static void checkHeader(OfHeader ofHeader, Short version) {\r
89         Assert.assertEquals("Wrong version", version, ofHeader.getVersion());\r
90         Assert.assertEquals("Wrong Xid", DEFAULT_XID, ofHeader.getXid());\r
91     }\r
92     \r
93     /**\r
94      * @param builder\r
95      * @throws NoSuchMethodException\r
96      * @throws SecurityException\r
97      * @throws IllegalAccessException\r
98      * @throws IllegalArgumentException\r
99      * @throws InvocationTargetException\r
100      */\r
101     public static void setupHeader(Object builder) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {\r
102         Method m = builder.getClass().getMethod("setVersion", Short.class);\r
103         m.invoke(builder, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
104         Method m2 = builder.getClass().getMethod("setXid", Long.class);\r
105         m2.invoke(builder, BufferHelper.DEFAULT_XID);\r
106     }\r
107     \r
108     /**\r
109      * Use version 1.3 for decoding message\r
110      * @param decoder decoder instance\r
111      * @param bb data input buffer\r
112      * @return message decoded pojo\r
113      */\r
114     public static <E extends DataObject> E decodeV13(OFDeserializer<E> decoder, ByteBuf bb) {\r
115         return bufferToMessage(decoder, HelloMessageFactoryTest.VERSION_YET_SUPPORTED, bb);\r
116     }\r
117     \r
118     private static <E extends DataObject> E bufferToMessage(OFDeserializer<E> decoder, short version, ByteBuf bb) {\r
119         return decoder.bufferToMessage(bb, version);\r
120     }\r
121     \r
122     /**\r
123      * Use OF-protocol version 1.3\r
124      * @param encoder serialize factory\r
125      * @param out buffer the result will be written into\r
126      * @param pojo input message\r
127      */\r
128     public static <E extends DataObject> void encodeV13(OFSerializer<E> encoder, ByteBuf out, E pojo) {\r
129         messageToBuffer(encoder, out, pojo, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
130     }\r
131     \r
132     private static <E extends DataObject> void messageToBuffer(\r
133             OFSerializer<E> encoder, ByteBuf out, E pojo, Short version) {\r
134         encoder.messageToBuffer(version, out, pojo);\r
135     }\r
136 }\r