new serialization factories and tests
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / FlowModInputMessageFactoryTest.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
3 \r
4 import static org.junit.Assert.*;\r
5 import io.netty.buffer.ByteBuf;\r
6 import io.netty.buffer.UnpooledByteBufAllocator;\r
7 \r
8 import java.math.BigInteger;\r
9 \r
10 import junit.framework.Assert;\r
11 \r
12 import org.junit.Test;\r
13 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest;\r
14 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;\r
23 \r
24 /**\r
25  * @author timotej.kubas\r
26  * @author michal.polkorab\r
27  */\r
28 public class FlowModInputMessageFactoryTest {\r
29     private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;\r
30     /**\r
31      * @throws Exception \r
32      * Testing of {@link FlowModInputMessageFactory} for correct translation from POJO\r
33      */\r
34     @Test\r
35     public void testFlowModInputMessageFactory() throws Exception {\r
36         FlowModInputBuilder builder = new FlowModInputBuilder();\r
37         BufferHelper.setupHeader(builder);\r
38         byte[] cookie = new byte[]{0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};\r
39         builder.setCookie(new BigInteger(cookie));\r
40         byte[] cookieMask = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};\r
41         builder.setCookieMask(new BigInteger(cookieMask));\r
42         builder.setTableId(new TableId(65L));\r
43         builder.setCommand(FlowModCommand.forValue(2));\r
44         builder.setIdleTimeout(12);\r
45         builder.setHardTimeout(0);\r
46         builder.setPriority(126);\r
47         builder.setOutPort(new PortNumber(4422L));\r
48         builder.setOutGroup(98L);\r
49         builder.setFlags(new FlowModFlags(true, false, true, false, true));\r
50         FlowModInput message = builder.build();\r
51         \r
52         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
53         FlowModInputMessageFactory factory = FlowModInputMessageFactory.getInstance();\r
54         factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);\r
55         \r
56         BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength());\r
57         Assert.assertEquals("Wrong cookie", message.getCookie().longValue(), out.readLong());\r
58         Assert.assertEquals("Wrong cookieMask", message.getCookieMask().longValue(), out.readLong());\r
59         Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(), out.readByte());\r
60         Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readByte());\r
61         Assert.assertEquals("Wrong idleTimeOut", message.getIdleTimeout().intValue(), out.readShort());\r
62         Assert.assertEquals("Wrong hardTimeOut", message.getHardTimeout().intValue(), out.readShort());\r
63         Assert.assertEquals("Wrong priority", message.getPriority().intValue(), out.readShort());\r
64         Assert.assertEquals("Wrong outPort", message.getOutPort().getValue().intValue(), out.readUnsignedInt());\r
65         Assert.assertEquals("Wrong outGroup", message.getOutGroup().intValue(), out.readUnsignedInt());\r
66         Assert.assertEquals("Wrong flags", message.getFlags(), createFlowModFalgsFromBitmap(out.readShort()));\r
67         out.skipBytes(PADDING_IN_FLOW_MOD_MESSAGE);\r
68         // TODO implementation of match structure\r
69         // TODO implementation of instructions\r
70     }\r
71     \r
72     private static FlowModFlags createFlowModFalgsFromBitmap(short input){\r
73         final Boolean _oFPFFSENDFLOWREM = (input & (1 << 0)) > 0;\r
74         final Boolean _oFPFFCHECKOVERLAP = (input & (1 << 1)) > 0;\r
75         final Boolean _oFPFFRESETCOUNTS = (input & (1 << 2)) > 0; \r
76         final Boolean _oFPFFNOPKTCOUNTS = (input & (1 << 3)) > 0;\r
77         final Boolean _oFPFFNOBYTCOUNTS = (input & (1 << 4)) > 0;\r
78         return new FlowModFlags(_oFPFFCHECKOVERLAP, _oFPFFNOBYTCOUNTS, _oFPFFNOPKTCOUNTS, _oFPFFRESETCOUNTS, _oFPFFSENDFLOWREM);\r
79     }\r
80 \r
81 }\r