1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
2 package org.opendaylight.openflowjava.protocol.impl.util;
\r
4 import io.netty.buffer.ByteBuf;
\r
5 import io.netty.buffer.UnpooledByteBufAllocator;
\r
7 import java.lang.reflect.InvocationTargetException;
\r
8 import java.lang.reflect.Method;
\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
18 * @author michal.polkorab
\r
21 public abstract class BufferHelper {
\r
26 public static final Long DEFAULT_XID = 0x01020304L;
\r
27 private static final byte[] XID = new byte[] { 0x01, 0x02, 0x03, 0x04 };
\r
31 * @return ByteBuf filled with OpenFlow protocol message without first 4
\r
34 public static ByteBuf buildBuffer(byte[] payload) {
\r
35 ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
\r
37 bb.writeBytes(payload);
\r
42 * @param payload String in hex format
\r
43 * @return ByteBuf filled with OpenFlow protocol message without first 4
\r
46 public static ByteBuf buildBuffer(String payload) {
\r
47 return buildBuffer(ByteBufUtils.hexStringToBytes(payload));
\r
51 * @return ByteBuf filled with OpenFlow protocol header message without first 4
\r
54 public static ByteBuf buildBuffer() {
\r
55 ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
\r
57 bb.writeBytes(new byte[0]);
\r
62 * Use version 1.3 for encoded message
\r
64 * ByteBuf to be checked for correct OpenFlow Protocol header
\r
66 * type of received message
\r
67 * @param length TODO
\r
69 public static void checkHeaderV13(ByteBuf input, byte msgType, int length) {
\r
70 checkHeader(input, msgType, length, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);
\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
82 * @param ofHeader OpenFlow protocol header
\r
84 public static void checkHeaderV13(OfHeader ofHeader) {
\r
85 checkHeader(ofHeader, HelloMessageFactoryTest.VERSION_YET_SUPPORTED);
\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
95 * @throws NoSuchMethodException
\r
96 * @throws SecurityException
\r
97 * @throws IllegalAccessException
\r
98 * @throws IllegalArgumentException
\r
99 * @throws InvocationTargetException
\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
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
114 public static <E extends DataObject> E decodeV13(OFDeserializer<E> decoder, ByteBuf bb) {
\r
115 return bufferToMessage(decoder, HelloMessageFactoryTest.VERSION_YET_SUPPORTED, bb);
\r
118 private static <E extends DataObject> E bufferToMessage(OFDeserializer<E> decoder, short version, ByteBuf bb) {
\r
119 return decoder.bufferToMessage(bb, version);
\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
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
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