e06e8a61c72646a42396ce69fe26483965b765ed
[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 ByteBuf to be checked for correct OpenFlow Protocol header\r
64      * @param msgType type of received message\r
65      * @param length TODO\r
66      */\r
67     public static void checkHeaderV13(ByteBuf input, byte msgType, int length) {\r
68         checkHeader(input, msgType, length, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
69     }\r
70     \r
71     private static void checkHeader(ByteBuf input, byte msgType, int length, Short version) {\r
72         Assert.assertEquals("Wrong version", version, Short.valueOf(input.readByte()));\r
73         Assert.assertEquals("Wrong type", msgType, input.readByte());\r
74         Assert.assertEquals("Wrong length", length, input.readUnsignedShort());\r
75         Assert.assertEquals("Wrong Xid", DEFAULT_XID, Long.valueOf(input.readUnsignedInt()));\r
76     }\r
77     \r
78 \r
79     /**\r
80      * @param ofHeader OpenFlow protocol header\r
81      */\r
82     public static void checkHeaderV13(OfHeader ofHeader) {\r
83         checkHeader(ofHeader,  HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
84     }\r
85     \r
86     private static void checkHeader(OfHeader ofHeader, Short version) {\r
87         Assert.assertEquals("Wrong version", version, ofHeader.getVersion());\r
88         Assert.assertEquals("Wrong Xid", DEFAULT_XID, ofHeader.getXid());\r
89     }\r
90     \r
91     /**\r
92      * @param builder\r
93      * @throws NoSuchMethodException\r
94      * @throws SecurityException\r
95      * @throws IllegalAccessException\r
96      * @throws IllegalArgumentException\r
97      * @throws InvocationTargetException\r
98      */\r
99     public static void setupHeader(Object builder) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {\r
100         Method m = builder.getClass().getMethod("setVersion", Short.class);\r
101         m.invoke(builder, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
102         Method m2 = builder.getClass().getMethod("setXid", Long.class);\r
103         m2.invoke(builder, BufferHelper.DEFAULT_XID);\r
104     }\r
105     \r
106     /**\r
107      * Use version 1.3 for decoding message\r
108      * @param decoder decoder instance\r
109      * @param bb data input buffer\r
110      * @return message decoded pojo\r
111      */\r
112     public static <E extends DataObject> E decodeV13(OFDeserializer<E> decoder, ByteBuf bb) {\r
113         return bufferToMessage(decoder, HelloMessageFactoryTest.VERSION_YET_SUPPORTED, bb);\r
114     }\r
115     \r
116     private static <E extends DataObject> E bufferToMessage(OFDeserializer<E> decoder, short version, ByteBuf bb) {\r
117         return decoder.bufferToMessage(bb, version);\r
118     }\r
119     \r
120     /**\r
121      * Use OF-protocol version 1.3\r
122      * @param encoder serialize factory\r
123      * @param out buffer the result will be written into\r
124      * @param pojo input message\r
125      */\r
126     public static <E extends DataObject> void encodeV13(OFSerializer<E> encoder, ByteBuf out, E pojo) {\r
127         messageToBuffer(encoder, out, pojo, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
128     }\r
129     \r
130     private static <E extends DataObject> void messageToBuffer(\r
131             OFSerializer<E> encoder, ByteBuf out, E pojo, Short version) {\r
132         encoder.messageToBuffer(version, out, pojo);\r
133     }\r
134 }\r