Extensibility support (deserialization part)
[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.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21
22 /**
23  * @author michal.polkorab
24  * 
25  */
26 public abstract class BufferHelper {
27
28     /**
29      * 
30      */
31     public static final Long DEFAULT_XID = 0x01020304L;
32     private static final byte[] XID = new byte[] { 0x01, 0x02, 0x03, 0x04 };
33
34     /**
35      * @param payload
36      * @return ByteBuf filled with OpenFlow protocol message without first 4
37      *         bytes
38      */
39     public static ByteBuf buildBuffer(byte[] payload) {
40         ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
41         bb.writeBytes(XID);
42         bb.writeBytes(payload);
43         return bb;
44     }
45     
46     /**
47      * @param payload String in hex format
48      * @return ByteBuf filled with OpenFlow protocol message without first 4
49      *         bytes
50      */
51     public static ByteBuf buildBuffer(String payload) {
52         return buildBuffer(ByteBufUtils.hexStringToBytes(payload));
53     }
54     
55     /**
56      * @return ByteBuf filled with OpenFlow protocol header message without first 4
57      *         bytes
58      */
59     public static ByteBuf buildBuffer() {
60         ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer();
61         bb.writeBytes(XID);
62         bb.writeBytes(new byte[0]);
63         return bb;
64     }
65
66     /**
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
71      */
72     public static void checkHeaderV13(ByteBuf input, byte msgType, int length) {
73         checkHeader(input, msgType, length, (short) EncodeConstants.OF13_VERSION_ID);
74     }
75     
76     /**
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
81      */
82     public static void checkHeaderV10(ByteBuf input, byte msgType, int length) {
83         checkHeader(input, msgType, length, (short) EncodeConstants.OF10_VERSION_ID);
84     }
85     
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()));
91     }
92     
93
94     /**
95      * @param ofHeader OpenFlow protocol header
96      */
97     public static void checkHeaderV13(OfHeader ofHeader) {
98         checkHeader(ofHeader, (short) EncodeConstants.OF13_VERSION_ID);
99     }
100     
101     /**
102      * @param ofHeader OpenFlow protocol header
103      */
104     public static void checkHeaderV10(OfHeader ofHeader) {
105         checkHeader(ofHeader, (short) EncodeConstants.OF10_VERSION_ID);
106     }
107     
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());
111     }
112     
113     /**
114      * @param builder
115      * @param version wire protocol number used
116      * @throws NoSuchMethodException
117      * @throws SecurityException
118      * @throws IllegalAccessException
119      * @throws IllegalArgumentException
120      * @throws InvocationTargetException
121      */
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);
127     }
128
129     /**
130      * Decode message
131      * @param decoder decoder instance
132      * @param bb data input buffer
133      * @return message decoded pojo
134      */
135     public static <E extends DataObject> E deserialize(OFDeserializer<E> decoder, ByteBuf bb) {
136         return decoder.deserialize(bb);
137     }
138
139 }