Extensibility support (deserialization part)
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF10ActionsDeserializerTest.java
1 /*\r
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.openflowjava.protocol.impl.util;\r
9 \r
10 import io.netty.buffer.ByteBuf;\r
11 \r
12 import java.util.List;\r
13 \r
14 import org.junit.Assert;\r
15 import org.junit.Before;\r
16 import org.junit.Test;\r
17 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;\r
18 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;\r
19 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;\r
20 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;\r
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DlAddressAction;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IpAddressAction;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTosAction;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdAction;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpAction;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidAction;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;\r
31 \r
32 /**\r
33  * @author michal.polkorab\r
34  *\r
35  */\r
36 public class OF10ActionsDeserializerTest {\r
37 \r
38     private OFDeserializer<Action> actionsDeserializer;\r
39 \r
40     /**\r
41      * Initializes deserializer registry and lookups correct deserializer\r
42      */\r
43     @Before\r
44     public void startUp() {\r
45         DeserializerRegistry registry = new DeserializerRegistryImpl();\r
46         registry.init();\r
47         actionsDeserializer = registry.getDeserializer(\r
48                 new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, EncodeConstants.EMPTY_VALUE,\r
49                         Action.class));\r
50     }\r
51 \r
52     /**\r
53      * Testing correct deserialization of actions (OF v1.0)\r
54      */\r
55     @Test\r
56     public void test() {\r
57         ByteBuf message = BufferHelper.buildBuffer("00 00 00 08 00 10 20 00 "\r
58                 + "00 01 00 08 10 10 00 00 "\r
59                 + "00 02 00 08 25 00 00 00 "\r
60                 + "00 03 00 08 00 00 00 00 "\r
61                 + "00 04 00 10 01 02 03 04 05 06 00 00 00 00 00 00 "\r
62                 + "00 05 00 10 02 03 04 05 06 07 00 00 00 00 00 00 "\r
63                 + "00 06 00 08 0A 00 00 01 "\r
64                 + "00 07 00 08 0B 00 00 02 "\r
65                 + "00 08 00 08 01 00 00 00 "\r
66                 + "00 09 00 08 00 02 00 00 "\r
67                 + "00 0A 00 08 00 03 00 00 "\r
68                 + "00 0B 00 10 00 04 00 00 00 00 00 00 00 00 00 30");\r
69         \r
70         message.skipBytes(4); // skip XID\r
71         List<Action> actions = DecodingUtils.deserializeList(message.readableBytes(),\r
72                 message, actionsDeserializer);\r
73         Assert.assertEquals("Wrong number of actions", 12, actions.size());\r
74         Action action1 = actions.get(0);\r
75         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
76                 + ".openflow.common.action.rev130731.Output", action1.getType().getName());\r
77         Assert.assertEquals("Wrong port", 16,\r
78                 action1.getAugmentation(PortAction.class).getPort().getValue().intValue());\r
79         Assert.assertEquals("Wrong max-length", 8192,\r
80                 action1.getAugmentation(MaxLengthAction.class).getMaxLength().intValue());\r
81         Action action2 = actions.get(1);\r
82         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
83                 + ".openflow.common.action.rev130731.SetVlanVid", action2.getType().getName());\r
84         Assert.assertEquals("Wrong vlan-vid", 4112,\r
85                 action2.getAugmentation(VlanVidAction.class).getVlanVid().intValue());\r
86         Action action3 = actions.get(2);\r
87         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
88                 + ".openflow.common.action.rev130731.SetVlanPcp", action3.getType().getName());\r
89         Assert.assertEquals("Wrong vlan-pcp", 37,\r
90                 action3.getAugmentation(VlanPcpAction.class).getVlanPcp().intValue());\r
91         Action action4 = actions.get(3);\r
92         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
93                 + ".openflow.common.action.rev130731.StripVlan", action4.getType().getName());\r
94         Action action5 = actions.get(4);\r
95         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
96                 + ".openflow.common.action.rev130731.SetDlSrc", action5.getType().getName());\r
97         Assert.assertArrayEquals("Wrong dl-src", ByteBufUtils.macAddressToBytes("01:02:03:04:05:06"), \r
98                 ByteBufUtils.macAddressToBytes(action5.getAugmentation(DlAddressAction.class).getDlAddress().getValue()));\r
99         Action action6 = actions.get(5);\r
100         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
101                 + ".openflow.common.action.rev130731.SetDlDst", action6.getType().getName());\r
102         Assert.assertArrayEquals("Wrong dl-dst", ByteBufUtils.macAddressToBytes("02:03:04:05:06:07"), \r
103                 ByteBufUtils.macAddressToBytes(action6.getAugmentation(DlAddressAction.class).getDlAddress().getValue()));\r
104         Action action7 = actions.get(6);\r
105         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
106                 + ".openflow.common.action.rev130731.SetNwSrc", action7.getType().getName());\r
107         Assert.assertEquals("Wrong nw-src", new Ipv4Address("10.0.0.1"),\r
108                 action7.getAugmentation(IpAddressAction.class).getIpAddress());\r
109         Action action8 = actions.get(7);\r
110         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
111                 + ".openflow.common.action.rev130731.SetNwDst", action8.getType().getName());\r
112         Assert.assertEquals("Wrong nw-dst", new Ipv4Address("11.0.0.2"),\r
113                 action8.getAugmentation(IpAddressAction.class).getIpAddress());\r
114         Action action9 = actions.get(8);\r
115         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
116                 + ".openflow.common.action.rev130731.SetNwTos", action9.getType().getName());\r
117         Assert.assertEquals("Wrong nw-tos", 1, action9.getAugmentation(NwTosAction.class).getNwTos().intValue());\r
118         Action action10 = actions.get(9);\r
119         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
120                 + ".openflow.common.action.rev130731.SetTpSrc", action10.getType().getName());\r
121         Assert.assertEquals("Wrong port", 2, action10.getAugmentation(PortAction.class)\r
122                 .getPort().getValue().intValue());\r
123         Action action11 = actions.get(10);\r
124         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
125                 + ".openflow.common.action.rev130731.SetTpDst", action11.getType().getName());\r
126         Assert.assertEquals("Wrong port", 3, action11.getAugmentation(PortAction.class)\r
127                 .getPort().getValue().intValue());\r
128         Action action12 = actions.get(11);\r
129         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"\r
130                 + ".openflow.common.action.rev130731.Enqueue", action12.getType().getName());\r
131         Assert.assertEquals("Wrong port", 4, action12.getAugmentation(PortAction.class)\r
132                 .getPort().getValue().intValue());\r
133         Assert.assertEquals("Wrong queue-id", 48,\r
134                 action12.getAugmentation(QueueIdAction.class).getQueueId().intValue());\r
135     }\r
136 \r
137 }\r