2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowjava.protocol.impl.util;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
17 import org.junit.Assert;
18 import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
23 * @author michal.polkorab
26 public abstract class BufferHelper {
31 public static final Long DEFAULT_XID = 0x01020304L;
32 private static final byte[] XID = new byte[] { 0x01, 0x02, 0x03, 0x04 };
36 * @return ByteBuf filled with OpenFlow protocol message without first 4
39 public static ByteBuf buildBuffer(byte[] payload) {
40 ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
42 bb.writeBytes(payload);
47 * @param payload String in hex format
48 * @return ByteBuf filled with OpenFlow protocol message without first 4
51 public static ByteBuf buildBuffer(String payload) {
52 return buildBuffer(ByteBufUtils.hexStringToBytes(payload));
56 * @return ByteBuf filled with OpenFlow protocol header message without first 4
59 public static ByteBuf buildBuffer() {
60 ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
62 bb.writeBytes(new byte[0]);
67 * Use version 1.3 for encoded message
68 * @param input ByteBuf to be checked for correct OpenFlow Protocol header
69 * @param msgType type of received message
70 * @param length expected length of message in header
72 public static void checkHeaderV13(ByteBuf input, byte msgType, int length) {
73 checkHeader(input, msgType, length, (short) EncodeConstants.OF13_VERSION_ID);
77 * Use version 1.0 for encoded message
78 * @param input ByteBuf to be checked for correct OpenFlow Protocol header
79 * @param msgType type of received message
80 * @param length expected length of message in header
82 public static void checkHeaderV10(ByteBuf input, byte msgType, int length) {
83 checkHeader(input, msgType, length, (short) EncodeConstants.OF10_VERSION_ID);
86 private static void checkHeader(ByteBuf input, byte msgType, int length, Short version) {
87 Assert.assertEquals("Wrong version", version, Short.valueOf(input.readByte()));
88 Assert.assertEquals("Wrong type", msgType, input.readByte());
89 Assert.assertEquals("Wrong length", length, input.readUnsignedShort());
90 Assert.assertEquals("Wrong Xid", DEFAULT_XID, Long.valueOf(input.readUnsignedInt()));
95 * @param ofHeader OpenFlow protocol header
97 public static void checkHeaderV13(OfHeader ofHeader) {
98 checkHeader(ofHeader, (short) EncodeConstants.OF13_VERSION_ID);
102 * @param ofHeader OpenFlow protocol header
104 public static void checkHeaderV10(OfHeader ofHeader) {
105 checkHeader(ofHeader, (short) EncodeConstants.OF10_VERSION_ID);
108 private static void checkHeader(OfHeader ofHeader, Short version) {
109 Assert.assertEquals("Wrong version", version, ofHeader.getVersion());
110 Assert.assertEquals("Wrong Xid", DEFAULT_XID, ofHeader.getXid());
115 * @param version wire protocol number used
116 * @throws NoSuchMethodException
117 * @throws SecurityException
118 * @throws IllegalAccessException
119 * @throws IllegalArgumentException
120 * @throws InvocationTargetException
122 public static void setupHeader(Object builder, int version) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
123 Method m = builder.getClass().getMethod("setVersion", Short.class);
124 m.invoke(builder, (short) version);
125 Method m2 = builder.getClass().getMethod("setXid", Long.class);
126 m2.invoke(builder, BufferHelper.DEFAULT_XID);
130 * Use version 1.3 for decoding message
131 * @param decoder decoder instance
132 * @param bb data input buffer
133 * @return message decoded pojo
135 public static <E extends DataObject> E decodeV13(OFDeserializer<E> decoder, ByteBuf bb) {
136 return bufferToMessage(decoder, EncodeConstants.OF13_VERSION_ID, bb);
140 * Use version 1.0 for decoding message
141 * @param decoder decoder instance
142 * @param bb data input buffer
143 * @return message decoded pojo
145 public static <E extends DataObject> E decodeV10(OFDeserializer<E> decoder, ByteBuf bb) {
146 return bufferToMessage(decoder, EncodeConstants.OF10_VERSION_ID, bb);
149 private static <E extends DataObject> E bufferToMessage(OFDeserializer<E> decoder, short version, ByteBuf bb) {
150 return decoder.bufferToMessage(bb, version);