Merge "Quickfix - Vlan-vid match entry"
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10FlowModInputMessageFactoryTest.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 io.netty.buffer.ByteBuf;\r
5 import io.netty.buffer.UnpooledByteBufAllocator;\r
6 \r
7 import java.math.BigInteger;\r
8 import java.util.ArrayList;\r
9 import java.util.List;\r
10 \r
11 import org.junit.Assert;\r
12 import org.junit.Test;\r
13 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
14 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
15 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;\r
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;\r
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IpAddressAction;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IpAddressActionBuilder;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwDst;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetTpSrc;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsListBuilder;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.ActionBuilder;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10Builder;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;\r
34 \r
35 /**\r
36  * @author michal.polkorab\r
37  *\r
38  */\r
39 public class OF10FlowModInputMessageFactoryTest {\r
40 \r
41     /**\r
42      * @throws Exception \r
43      * Testing of {@link OF10FlowModInputMessageFactory} for correct translation from POJO\r
44      */\r
45     @Test\r
46     public void testFlowModInputMessageFactory() throws Exception {\r
47         FlowModInputBuilder builder = new FlowModInputBuilder();\r
48         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);\r
49         MatchV10Builder matchBuilder = new MatchV10Builder();\r
50         matchBuilder.setWildcards(new FlowWildcardsV10(true, true, true, true, true, true, true, true, true, true, true));\r
51         matchBuilder.setNwSrcMask((short) 0);\r
52         matchBuilder.setNwDstMask((short) 0);\r
53         matchBuilder.setInPort(58);\r
54         matchBuilder.setDlSrc(new MacAddress("01:01:01:01:01:01"));\r
55         matchBuilder.setDlDst(new MacAddress("ff:ff:ff:ff:ff:ff"));\r
56         matchBuilder.setDlVlan(18);\r
57         matchBuilder.setDlVlanPcp((short) 5);\r
58         matchBuilder.setDlType(42);\r
59         matchBuilder.setNwTos((short) 4);\r
60         matchBuilder.setNwProto((short) 7);\r
61         matchBuilder.setNwSrc(new Ipv4Address("8.8.8.8"));\r
62         matchBuilder.setNwDst(new Ipv4Address("16.16.16.16"));\r
63         matchBuilder.setTpSrc(6653);\r
64         matchBuilder.setTpDst(6633);\r
65         builder.setMatchV10(matchBuilder.build());\r
66         byte[] cookie = new byte[]{0x00, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01};\r
67         builder.setCookie(new BigInteger(cookie));\r
68         builder.setCommand(FlowModCommand.forValue(0));\r
69         builder.setIdleTimeout(12);\r
70         builder.setHardTimeout(16);\r
71         builder.setPriority(1);\r
72         builder.setBufferId(2L);\r
73         builder.setOutPort(new PortNumber(4422L));\r
74         builder.setFlagsV10(new FlowModFlagsV10(true, false, true));\r
75         List<ActionsList> actions = new ArrayList<>();\r
76         ActionsListBuilder actionsListBuilder = new ActionsListBuilder();\r
77         ActionBuilder actionBuilder = new ActionBuilder();\r
78         actionBuilder.setType(SetNwDst.class);\r
79         IpAddressActionBuilder ipBuilder = new IpAddressActionBuilder();\r
80         ipBuilder.setIpAddress(new Ipv4Address("2.2.2.2"));\r
81         actionBuilder.addAugmentation(IpAddressAction.class, ipBuilder.build());\r
82         actionsListBuilder.setAction(actionBuilder.build());\r
83         actions.add(actionsListBuilder.build());\r
84         actionBuilder = new ActionBuilder();\r
85         actionBuilder.setType(SetTpSrc.class);\r
86         PortActionBuilder portBuilder = new PortActionBuilder();\r
87         portBuilder.setPort(new PortNumber(42L));\r
88         actionBuilder.addAugmentation(PortAction.class, portBuilder.build());\r
89         actionsListBuilder.setAction(actionBuilder.build());\r
90         actions.add(actionsListBuilder.build());\r
91         builder.setActionsList(actions);\r
92         FlowModInput message = builder.build();\r
93         \r
94         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
95         OF10FlowModInputMessageFactory factory = OF10FlowModInputMessageFactory.getInstance();\r
96         factory.messageToBuffer(EncodeConstants.OF10_VERSION_ID, out, message);\r
97         \r
98         BufferHelper.checkHeaderV10(out, factory.getMessageType(), factory.computeLength(message));\r
99         Assert.assertEquals("Wrong wildcards", 4194303, out.readUnsignedInt());\r
100         Assert.assertEquals("Wrong inPort", 58, out.readUnsignedShort());\r
101         byte[] dlSrc = new byte[6];\r
102         out.readBytes(dlSrc);\r
103         Assert.assertEquals("Wrong dlSrc", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));\r
104         byte[] dlDst = new byte[6];\r
105         out.readBytes(dlDst);\r
106         Assert.assertEquals("Wrong dlDst", "FF:FF:FF:FF:FF:FF", ByteBufUtils.macAddressToString(dlDst));\r
107         Assert.assertEquals("Wrong dlVlan", 18, out.readUnsignedShort());\r
108         Assert.assertEquals("Wrong dlVlanPcp", 5, out.readUnsignedByte());\r
109         out.skipBytes(1);\r
110         Assert.assertEquals("Wrong dlType", 42, out.readUnsignedShort());\r
111         Assert.assertEquals("Wrong nwTos", 4, out.readUnsignedByte());\r
112         Assert.assertEquals("Wrong nwProto", 7, out.readUnsignedByte());\r
113         out.skipBytes(2);\r
114         Assert.assertEquals("Wrong nwSrc", 134744072, out.readUnsignedInt());\r
115         Assert.assertEquals("Wrong nwDst", 269488144, out.readUnsignedInt());\r
116         Assert.assertEquals("Wrong tpSrc", 6653, out.readUnsignedShort());\r
117         Assert.assertEquals("Wrong tpDst", 6633, out.readUnsignedShort());\r
118         byte[] cookieRead = new byte[8];\r
119         out.readBytes(cookieRead);\r
120         Assert.assertArrayEquals("Wrong cookie", cookie, cookieRead);\r
121         Assert.assertEquals("Wrong command", 0, out.readUnsignedShort());\r
122         Assert.assertEquals("Wrong idleTimeOut", 12, out.readUnsignedShort());\r
123         Assert.assertEquals("Wrong hardTimeOut", 16, out.readUnsignedShort());\r
124         Assert.assertEquals("Wrong priority", 1, out.readUnsignedShort());\r
125         Assert.assertEquals("Wrong bufferId", 2, out.readUnsignedInt());\r
126         Assert.assertEquals("Wrong outPort", 4422, out.readUnsignedShort());\r
127         Assert.assertEquals("Wrong flags", 3, out.readUnsignedShort());\r
128         Assert.assertEquals("Wrong action - type", 7, out.readUnsignedShort());\r
129         Assert.assertEquals("Wrong action - length", 8, out.readUnsignedShort());\r
130         Assert.assertEquals("Wrong flags", 33686018, out.readUnsignedInt());\r
131         Assert.assertEquals("Wrong action - type", 9, out.readUnsignedShort());\r
132         Assert.assertEquals("Wrong action - length", 8, out.readUnsignedShort());\r
133         Assert.assertEquals("Wrong flags", 42, out.readUnsignedShort());\r
134         out.skipBytes(2);\r
135     }\r
136 \r
137 }\r