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