Bump mdsal to 5.0.2
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / BufferHelper.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.util;
9
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;
22
23 /**
24  * Helper class for buffers.
25  *
26  * @author michal.polkorab
27  */
28 public abstract class BufferHelper {
29
30     public static final Uint32 DEFAULT_XID = Uint32.valueOf(0x01020304L);
31     private static final byte[] XID = new byte[] { 0x01, 0x02, 0x03, 0x04 };
32
33     /**
34      * Build a ByteBuffer.
35      *
36      * @param payload the payload data
37      * @return ByteBuf filled with OpenFlow protocol message without first 4
38      *         bytes
39      */
40     public static ByteBuf buildBuffer(final byte[] payload) {
41         ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
42         bb.writeBytes(XID);
43         bb.writeBytes(payload);
44         return bb;
45     }
46
47     /**
48      * Build a ByteBuffer.
49      *
50      * @param payload String in hex format
51      * @return ByteBuf filled with OpenFlow protocol message without first 4
52      *         bytes
53      */
54     public static ByteBuf buildBuffer(final String payload) {
55         return buildBuffer(ByteBufUtils.hexStringToBytes(payload));
56     }
57
58     /**
59      * Build a ByteBuffer.
60      *
61      * @return ByteBuf filled with OpenFlow protocol header message without first 4
62      *         bytes
63      */
64     public static ByteBuf buildBuffer() {
65         ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
66         bb.writeBytes(XID);
67         bb.writeBytes(new byte[0]);
68         return bb;
69     }
70
71     /**
72      * Use version 1.3 for encoded message.
73      *
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
77      */
78     public static void checkHeaderV13(final ByteBuf input, final byte msgType, final int length) {
79         checkHeader(input, msgType, length, (short) EncodeConstants.OF13_VERSION_ID);
80     }
81
82     /**
83      * Checks a 1.3 header.
84      *
85      * @param ofHeader OpenFlow protocol header
86      */
87     public static void checkHeaderV13(final OfHeader ofHeader) {
88         checkHeader(ofHeader, Uint8.valueOf(EncodeConstants.OF13_VERSION_ID));
89     }
90
91     /**
92      * Use version 1.0 for encoded message.
93      *
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
97      */
98     public static void checkHeaderV10(final ByteBuf input, final byte msgType, final int length) {
99         checkHeader(input, msgType, length, (short) EncodeConstants.OF10_VERSION_ID);
100     }
101
102     /**
103      * Checks a 1.0 header.
104      *
105      * @param ofHeader OpenFlow protocol header
106      */
107     public static void checkHeaderV10(final OfHeader ofHeader) {
108         checkHeader(ofHeader, Uint8.valueOf(EncodeConstants.OF10_VERSION_ID));
109     }
110
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()));
116     }
117
118     /**
119      * Check version and xid of OFP header.
120      *
121      * @param ofHeader OpenFlow protocol header
122      * @param version OpenFlow protocol version
123      */
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());
127     }
128
129     /**
130      * Sets up a header.
131      *
132      * @param builder builder
133      * @param version wire protocol number used
134      */
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);
141     }
142
143     /**
144      * Decode message.
145      *
146      * @param decoder decoder instance
147      * @param bb data input buffer
148      * @return message decoded pojo
149      */
150     public static <E extends DataContainer> E deserialize(final OFDeserializer<E> decoder, final ByteBuf bb) {
151         return decoder.deserialize(bb);
152     }
153
154 }