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
8 package org.opendaylight.openflowjava.protocol.impl.util;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import org.junit.Assert;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
19 import org.opendaylight.yangtools.yang.binding.DataContainer;
20 import org.opendaylight.yangtools.yang.common.Uint32;
21 import org.opendaylight.yangtools.yang.common.Uint8;
24 * Helper class for buffers.
26 * @author michal.polkorab
28 public abstract class BufferHelper {
30 public static final Uint32 DEFAULT_XID = Uint32.valueOf(0x01020304L);
31 private static final byte[] XID = new byte[] { 0x01, 0x02, 0x03, 0x04 };
36 * @param payload the payload data
37 * @return ByteBuf filled with OpenFlow protocol message without first 4
40 public static ByteBuf buildBuffer(final byte[] payload) {
41 ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
43 bb.writeBytes(payload);
50 * @param payload String in hex format
51 * @return ByteBuf filled with OpenFlow protocol message without first 4
54 public static ByteBuf buildBuffer(final String payload) {
55 return buildBuffer(ByteBufUtils.hexStringToBytes(payload));
61 * @return ByteBuf filled with OpenFlow protocol header message without first 4
64 public static ByteBuf buildBuffer() {
65 ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
67 bb.writeBytes(new byte[0]);
72 * Use version 1.3 for encoded message.
74 * @param input ByteBuf to be checked for correct OpenFlow Protocol header
75 * @param msgType type of received message
76 * @param length expected length of message in header
78 public static void checkHeaderV13(final ByteBuf input, final byte msgType, final int length) {
79 checkHeader(input, msgType, length, (short) EncodeConstants.OF13_VERSION_ID);
83 * Checks a 1.3 header.
85 * @param ofHeader OpenFlow protocol header
87 public static void checkHeaderV13(final OfHeader ofHeader) {
88 checkHeader(ofHeader, Uint8.valueOf(EncodeConstants.OF13_VERSION_ID));
92 * Use version 1.0 for encoded message.
94 * @param input ByteBuf to be checked for correct OpenFlow Protocol header
95 * @param msgType type of received message
96 * @param length expected length of message in header
98 public static void checkHeaderV10(final ByteBuf input, final byte msgType, final int length) {
99 checkHeader(input, msgType, length, (short) EncodeConstants.OF10_VERSION_ID);
103 * Checks a 1.0 header.
105 * @param ofHeader OpenFlow protocol header
107 public static void checkHeaderV10(final OfHeader ofHeader) {
108 checkHeader(ofHeader, Uint8.valueOf(EncodeConstants.OF10_VERSION_ID));
111 private static void checkHeader(final ByteBuf input, final byte msgType, final int length, final Short version) {
112 Assert.assertEquals("Wrong version", version, Short.valueOf(input.readByte()));
113 Assert.assertEquals("Wrong type", msgType, input.readByte());
114 Assert.assertEquals("Wrong length", length, input.readUnsignedShort());
115 Assert.assertEquals("Wrong Xid", DEFAULT_XID, Uint32.valueOf(input.readUnsignedInt()));
119 * Check version and xid of OFP header.
121 * @param ofHeader OpenFlow protocol header
122 * @param version OpenFlow protocol version
124 public static void checkHeader(final OfHeader ofHeader, final Uint8 version) {
125 Assert.assertEquals("Wrong version", version, ofHeader.getVersion());
126 Assert.assertEquals("Wrong Xid", DEFAULT_XID, ofHeader.getXid());
132 * @param builder builder
133 * @param version wire protocol number used
135 public static void setupHeader(final Object builder, final int version) throws NoSuchMethodException,
136 IllegalAccessException, InvocationTargetException {
137 Method method = builder.getClass().getMethod("setVersion", Short.class);
138 method.invoke(builder, (short) version);
139 Method m2 = builder.getClass().getMethod("setXid", Uint32.class);
140 m2.invoke(builder, BufferHelper.DEFAULT_XID);
146 * @param decoder decoder instance
147 * @param bb data input buffer
148 * @return message decoded pojo
150 public static <E extends DataContainer> E deserialize(final OFDeserializer<E> decoder, final ByteBuf bb) {
151 return decoder.deserialize(bb);