import io.netty.buffer.ByteBuf;\r
import io.netty.buffer.UnpooledByteBufAllocator;\r
\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.lang.reflect.Method;\r
+\r
+import org.junit.Assert;\r
+import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer;\r
+import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest;\r
+import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
+import org.opendaylight.yangtools.yang.binding.DataObject;\r
+\r
/**\r
* @author michal.polkorab\r
- *\r
+ * \r
*/\r
public abstract class BufferHelper {\r
\r
/**\r
* \r
*/\r
- private static final byte[] XID = new byte[]{0x01, 0x02, 0x03, 0x04};\r
+ public static final Long DEFAULT_XID = 0x01020304L;\r
+ private static final byte[] XID = new byte[] { 0x01, 0x02, 0x03, 0x04 };\r
\r
/**\r
* @param payload\r
- * @return ByteBuf filled with OpenFlow protocol message without first 2 bytes\r
+ * @return ByteBuf filled with OpenFlow protocol message without first 4\r
+ * bytes\r
*/\r
public static ByteBuf buildBuffer(byte[] payload) {\r
ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();\r
bb.writeBytes(payload);\r
return bb;\r
}\r
+ \r
+ /**\r
+ * @param payload String in hex format\r
+ * @return ByteBuf filled with OpenFlow protocol message without first 4\r
+ * bytes\r
+ */\r
+ public static ByteBuf buildBuffer(String payload) {\r
+ return buildBuffer(ByteBufUtils.hexStringToBytes(payload));\r
+ }\r
+ \r
+ /**\r
+ * @return ByteBuf filled with OpenFlow protocol header message without first 4\r
+ * bytes\r
+ */\r
+ public static ByteBuf buildBuffer() {\r
+ ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();\r
+ bb.writeBytes(XID);\r
+ bb.writeBytes(new byte[0]);\r
+ return bb;\r
+ }\r
+\r
+ /**\r
+ * Use version 1.3 for encoded message\r
+ * @param input\r
+ * ByteBuf to be checked for correct OpenFlow Protocol header\r
+ * @param msgType\r
+ * type of received message\r
+ * @param length TODO\r
+ */\r
+ public static void checkHeaderV13(ByteBuf input, byte msgType, int length) {\r
+ checkHeader(input, msgType, length, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
+ }\r
+ \r
+ private static void checkHeader(ByteBuf input, byte msgType, int length, Short version) {\r
+ Assert.assertEquals("Wrong version", version, Short.valueOf(input.readByte()));\r
+ Assert.assertEquals("Wrong type", msgType, input.readByte());\r
+ Assert.assertEquals("Wrong length", length, input.readUnsignedShort());\r
+ Assert.assertEquals("Wrong Xid", DEFAULT_XID, Long.valueOf(input.readUnsignedInt()));\r
+ }\r
+ \r
\r
+ /**\r
+ * @param ofHeader OpenFlow protocol header\r
+ */\r
+ public static void checkHeaderV13(OfHeader ofHeader) {\r
+ checkHeader(ofHeader, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
+ }\r
+ \r
+ private static void checkHeader(OfHeader ofHeader, Short version) {\r
+ Assert.assertEquals("Wrong version", version, ofHeader.getVersion());\r
+ Assert.assertEquals("Wrong Xid", DEFAULT_XID, ofHeader.getXid());\r
+ }\r
+ \r
+ /**\r
+ * @param builder\r
+ * @throws NoSuchMethodException\r
+ * @throws SecurityException\r
+ * @throws IllegalAccessException\r
+ * @throws IllegalArgumentException\r
+ * @throws InvocationTargetException\r
+ */\r
+ public static void setupHeader(Object builder) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {\r
+ Method m = builder.getClass().getMethod("setVersion", Short.class);\r
+ m.invoke(builder, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
+ Method m2 = builder.getClass().getMethod("setXid", Long.class);\r
+ m2.invoke(builder, BufferHelper.DEFAULT_XID);\r
+ }\r
+ \r
+ /**\r
+ * Use version 1.3 for decoding message\r
+ * @param decoder decoder instance\r
+ * @param bb data input buffer\r
+ * @return message decoded pojo\r
+ */\r
+ public static <E extends DataObject> E decodeV13(OFDeserializer<E> decoder, ByteBuf bb) {\r
+ return bufferToMessage(decoder, HelloMessageFactoryTest.VERSION_YET_SUPPORTED, bb);\r
+ }\r
+ \r
+ private static <E extends DataObject> E bufferToMessage(OFDeserializer<E> decoder, short version, ByteBuf bb) {\r
+ return decoder.bufferToMessage(bb, version);\r
+ }\r
+ \r
+ /**\r
+ * Use OF-protocol version 1.3\r
+ * @param encoder serialize factory\r
+ * @param out buffer the result will be written into\r
+ * @param pojo input message\r
+ */\r
+ public static <E extends DataObject> void encodeV13(OFSerializer<E> encoder, ByteBuf out, E pojo) {\r
+ messageToBuffer(encoder, out, pojo, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);\r
+ }\r
+ \r
+ private static <E extends DataObject> void messageToBuffer(\r
+ OFSerializer<E> encoder, ByteBuf out, E pojo, Short version) {\r
+ encoder.messageToBuffer(version, out, pojo);\r
+ }\r
}\r