Merge "Error code support for ErrorMessage"
[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 expected length of message in header\r
66      */\r
67     public static void checkHeaderV13(ByteBuf input, byte msgType, int length) {\r
68         checkHeader(input, msgType, length, (short) EncodeConstants.OF13_VERSION_ID);\r
69     }\r
70     \r
71     /**\r
72      * Use version 1.0 for encoded message\r
73      * @param input ByteBuf to be checked for correct OpenFlow Protocol header\r
74      * @param msgType type of received message\r
75      * @param length expected length of message in header\r
76      */\r
77     public static void checkHeaderV10(ByteBuf input, byte msgType, int length) {\r
78         checkHeader(input, msgType, length, (short) EncodeConstants.OF10_VERSION_ID);\r
79     }\r
80     \r
81     private static void checkHeader(ByteBuf input, byte msgType, int length, Short version) {\r
82         Assert.assertEquals("Wrong version", version, Short.valueOf(input.readByte()));\r
83         Assert.assertEquals("Wrong type", msgType, input.readByte());\r
84         Assert.assertEquals("Wrong length", length, input.readUnsignedShort());\r
85         Assert.assertEquals("Wrong Xid", DEFAULT_XID, Long.valueOf(input.readUnsignedInt()));\r
86     }\r
87     \r
88 \r
89     /**\r
90      * @param ofHeader OpenFlow protocol header\r
91      */\r
92     public static void checkHeaderV13(OfHeader ofHeader) {\r
93         checkHeader(ofHeader, (short) EncodeConstants.OF13_VERSION_ID);\r
94     }\r
95     \r
96     /**\r
97      * @param ofHeader OpenFlow protocol header\r
98      */\r
99     public static void checkHeaderV10(OfHeader ofHeader) {\r
100         checkHeader(ofHeader, (short) EncodeConstants.OF10_VERSION_ID);\r
101     }\r
102     \r
103     private static void checkHeader(OfHeader ofHeader, Short version) {\r
104         Assert.assertEquals("Wrong version", version, ofHeader.getVersion());\r
105         Assert.assertEquals("Wrong Xid", DEFAULT_XID, ofHeader.getXid());\r
106     }\r
107     \r
108     /**\r
109      * @param builder\r
110      * @param version wire protocol number used\r
111      * @throws NoSuchMethodException\r
112      * @throws SecurityException\r
113      * @throws IllegalAccessException\r
114      * @throws IllegalArgumentException\r
115      * @throws InvocationTargetException\r
116      */\r
117     public static void setupHeader(Object builder, int version) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {\r
118         Method m = builder.getClass().getMethod("setVersion", Short.class);\r
119         m.invoke(builder, (short) version);\r
120         Method m2 = builder.getClass().getMethod("setXid", Long.class);\r
121         m2.invoke(builder, BufferHelper.DEFAULT_XID);\r
122     }\r
123     \r
124     /**\r
125      * Use version 1.3 for decoding message\r
126      * @param decoder decoder instance\r
127      * @param bb data input buffer\r
128      * @return message decoded pojo\r
129      */\r
130     public static <E extends DataObject> E decodeV13(OFDeserializer<E> decoder, ByteBuf bb) {\r
131         return bufferToMessage(decoder, EncodeConstants.OF13_VERSION_ID, bb);\r
132     }\r
133     \r
134     /**\r
135      * Use version 1.0 for decoding message\r
136      * @param decoder decoder instance\r
137      * @param bb data input buffer\r
138      * @return message decoded pojo\r
139      */\r
140     public static <E extends DataObject> E decodeV10(OFDeserializer<E> decoder, ByteBuf bb) {\r
141         return bufferToMessage(decoder, EncodeConstants.OF10_VERSION_ID, bb);\r
142     }\r
143     \r
144     private static <E extends DataObject> E bufferToMessage(OFDeserializer<E> decoder, short version, ByteBuf bb) {\r
145         return decoder.bufferToMessage(bb, version);\r
146     }\r
147     \r
148     /**\r
149      * Use OF-protocol version 1.3\r
150      * @param encoder serialize factory\r
151      * @param out buffer the result will be written into\r
152      * @param pojo input message\r
153      */\r
154     public static <E extends DataObject> void encodeV13(OFSerializer<E> encoder, ByteBuf out, E pojo) {\r
155         messageToBuffer(encoder, out, pojo, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
156     }\r
157     \r
158     private static <E extends DataObject> void messageToBuffer(\r
159             OFSerializer<E> encoder, ByteBuf out, E pojo, Short version) {\r
160         encoder.messageToBuffer(version, out, pojo);\r
161     }\r
162 }\r