Bug 1277 - Move ByteBuffUtils to separate bundle
[openflowjava.git] / 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
9 package org.opendaylight.openflowjava.protocol.impl.util;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
13
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16
17 import org.junit.Assert;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22
23 /**
24  * @author michal.polkorab
25  * 
26  */
27 public abstract class BufferHelper {
28
29     /**
30      * 
31      */
32     public static final Long DEFAULT_XID = 0x01020304L;
33     private static final byte[] XID = new byte[] { 0x01, 0x02, 0x03, 0x04 };
34
35     /**
36      * @param payload
37      * @return ByteBuf filled with OpenFlow protocol message without first 4
38      *         bytes
39      */
40     public static ByteBuf buildBuffer(byte[] payload) {
41         ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
42         bb.writeBytes(XID);
43         bb.writeBytes(payload);
44         return bb;
45     }
46     
47     /**
48      * @param payload String in hex format
49      * @return ByteBuf filled with OpenFlow protocol message without first 4
50      *         bytes
51      */
52     public static ByteBuf buildBuffer(String payload) {
53         return buildBuffer(ByteBufUtils.hexStringToBytes(payload));
54     }
55     
56     /**
57      * @return ByteBuf filled with OpenFlow protocol header message without first 4
58      *         bytes
59      */
60     public static ByteBuf buildBuffer() {
61         ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
62         bb.writeBytes(XID);
63         bb.writeBytes(new byte[0]);
64         return bb;
65     }
66
67     /**
68      * Use version 1.3 for encoded message
69      * @param input ByteBuf to be checked for correct OpenFlow Protocol header
70      * @param msgType type of received message
71      * @param length expected length of message in header
72      */
73     public static void checkHeaderV13(ByteBuf input, byte msgType, int length) {
74         checkHeader(input, msgType, length, (short) EncodeConstants.OF13_VERSION_ID);
75     }
76     
77     /**
78      * Use version 1.0 for encoded message
79      * @param input ByteBuf to be checked for correct OpenFlow Protocol header
80      * @param msgType type of received message
81      * @param length expected length of message in header
82      */
83     public static void checkHeaderV10(ByteBuf input, byte msgType, int length) {
84         checkHeader(input, msgType, length, (short) EncodeConstants.OF10_VERSION_ID);
85     }
86     
87     private static void checkHeader(ByteBuf input, byte msgType, int length, Short version) {
88         Assert.assertEquals("Wrong version", version, Short.valueOf(input.readByte()));
89         Assert.assertEquals("Wrong type", msgType, input.readByte());
90         Assert.assertEquals("Wrong length", length, input.readUnsignedShort());
91         Assert.assertEquals("Wrong Xid", DEFAULT_XID, Long.valueOf(input.readUnsignedInt()));
92     }
93     
94
95     /**
96      * @param ofHeader OpenFlow protocol header
97      */
98     public static void checkHeaderV13(OfHeader ofHeader) {
99         checkHeader(ofHeader, (short) EncodeConstants.OF13_VERSION_ID);
100     }
101     
102     /**
103      * @param ofHeader OpenFlow protocol header
104      */
105     public static void checkHeaderV10(OfHeader ofHeader) {
106         checkHeader(ofHeader, (short) EncodeConstants.OF10_VERSION_ID);
107     }
108     
109     private static void checkHeader(OfHeader ofHeader, Short version) {
110         Assert.assertEquals("Wrong version", version, ofHeader.getVersion());
111         Assert.assertEquals("Wrong Xid", DEFAULT_XID, ofHeader.getXid());
112     }
113     
114     /**
115      * @param builder
116      * @param version wire protocol number used
117      * @throws NoSuchMethodException
118      * @throws SecurityException
119      * @throws IllegalAccessException
120      * @throws IllegalArgumentException
121      * @throws InvocationTargetException
122      */
123     public static void setupHeader(Object builder, int version) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
124         Method m = builder.getClass().getMethod("setVersion", Short.class);
125         m.invoke(builder, (short) version);
126         Method m2 = builder.getClass().getMethod("setXid", Long.class);
127         m2.invoke(builder, BufferHelper.DEFAULT_XID);
128     }
129
130     /**
131      * Decode message
132      * @param decoder decoder instance
133      * @param bb data input buffer
134      * @return message decoded pojo
135      */
136     public static <E extends DataObject> E deserialize(OFDeserializer<E> decoder, ByteBuf bb) {
137         return decoder.deserialize(bb);
138     }
139
140 }