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