Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF13ActionsSerializerTest.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 import io.netty.buffer.UnpooledByteBufAllocator;\r
12 \r
13 import java.util.ArrayList;\r
14 import java.util.List;\r
15 \r
16 import org.junit.Assert;\r
17 import org.junit.Before;\r
18 import org.junit.Test;\r
19 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;\r
20 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;\r
21 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;\r
22 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeAction;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeActionBuilder;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.GroupIdAction;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.GroupIdActionBuilder;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsTtlAction;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsTtlActionBuilder;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlAction;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlActionBuilder;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsAction;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsActionBuilder;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntryBuilder;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdAction;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdActionBuilder;\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.CopyTtlIn;\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.CopyTtlOut;\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.DecMplsTtl;\r
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.DecNwTtl;\r
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Group;\r
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;\r
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopMpls;\r
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopPbb;\r
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopVlan;\r
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushMpls;\r
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushPbb;\r
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;\r
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetField;\r
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetMplsTtl;\r
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwTtl;\r
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetQueue;\r
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;\r
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;\r
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;\r
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;\r
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;\r
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;\r
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;\r
65 \r
66 /**\r
67  * @author michal.polkorab\r
68  * \r
69  */\r
70 public class OF13ActionsSerializerTest {\r
71 \r
72     private SerializerRegistry registry;\r
73     private OFSerializer<Action> actionSerializer;\r
74 \r
75     /**\r
76      * Initializes serializer table and stores correct factory in field\r
77      */\r
78     @Before\r
79     public void startUp() {\r
80         registry = new SerializerRegistryImpl();\r
81         registry.init();\r
82         actionSerializer = registry.getSerializer(\r
83                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, Action.class));\r
84     }\r
85 \r
86     /**\r
87      * Testing correct serialization of actions\r
88      */\r
89     @Test\r
90     public void test() {\r
91         List<Action> actions = new ArrayList<>();\r
92         ActionBuilder actionBuilder = new ActionBuilder();\r
93         actionBuilder.setType(Output.class);\r
94         PortActionBuilder port = new PortActionBuilder();\r
95         port.setPort(new PortNumber(42L));\r
96         actionBuilder.addAugmentation(PortAction.class, port.build());\r
97         MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();\r
98         maxLen.setMaxLength(52);\r
99         actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());\r
100         actions.add(actionBuilder.build());\r
101         actionBuilder = new ActionBuilder();\r
102         actionBuilder.setType(CopyTtlOut.class);\r
103         actions.add(actionBuilder.build());\r
104         actionBuilder = new ActionBuilder();\r
105         actionBuilder.setType(CopyTtlIn.class);\r
106         actions.add(actionBuilder.build());\r
107         actionBuilder = new ActionBuilder();\r
108         actionBuilder.setType(SetMplsTtl.class);\r
109         MplsTtlActionBuilder mplsTtl = new MplsTtlActionBuilder();\r
110         mplsTtl.setMplsTtl((short) 4);\r
111         actionBuilder.addAugmentation(MplsTtlAction.class, mplsTtl.build());\r
112         actions.add(actionBuilder.build());\r
113         actionBuilder = new ActionBuilder();\r
114         actionBuilder.setType(DecMplsTtl.class);\r
115         actions.add(actionBuilder.build());\r
116         actionBuilder = new ActionBuilder();\r
117         actionBuilder.setType(PushVlan.class);\r
118         EthertypeActionBuilder etherType = new EthertypeActionBuilder();\r
119         etherType.setEthertype(new EtherType(16));\r
120         actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());\r
121         actions.add(actionBuilder.build());\r
122         actionBuilder = new ActionBuilder();\r
123         actionBuilder.setType(PopVlan.class);\r
124         actions.add(actionBuilder.build());\r
125         actionBuilder = new ActionBuilder();\r
126         actionBuilder.setType(PushMpls.class);\r
127         etherType = new EthertypeActionBuilder();\r
128         etherType.setEthertype(new EtherType(17));\r
129         actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());\r
130         actions.add(actionBuilder.build());\r
131         actionBuilder = new ActionBuilder();\r
132         actionBuilder.setType(PopMpls.class);\r
133         etherType = new EthertypeActionBuilder();\r
134         etherType.setEthertype(new EtherType(18));\r
135         actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());\r
136         actions.add(actionBuilder.build());\r
137         actionBuilder = new ActionBuilder();\r
138         actionBuilder.setType(SetQueue.class);\r
139         QueueIdActionBuilder queueId = new QueueIdActionBuilder();\r
140         queueId.setQueueId(1234L);\r
141         actionBuilder.addAugmentation(QueueIdAction.class, queueId.build());\r
142         actions.add(actionBuilder.build());\r
143         actionBuilder = new ActionBuilder();\r
144         actionBuilder.setType(Group.class);\r
145         GroupIdActionBuilder group = new GroupIdActionBuilder();\r
146         group.setGroupId(555L);\r
147         actionBuilder.addAugmentation(GroupIdAction.class, group.build());\r
148         actions.add(actionBuilder.build());\r
149         actionBuilder = new ActionBuilder();\r
150         actionBuilder.setType(SetNwTtl.class);\r
151         NwTtlActionBuilder nwTtl = new NwTtlActionBuilder();\r
152         nwTtl.setNwTtl((short) 8);\r
153         actionBuilder.addAugmentation(NwTtlAction.class, nwTtl.build());\r
154         actions.add(actionBuilder.build());\r
155         actionBuilder = new ActionBuilder();\r
156         actionBuilder.setType(DecNwTtl.class);\r
157         actions.add(actionBuilder.build());\r
158         actionBuilder = new ActionBuilder();\r
159         actionBuilder.setType(SetField.class);\r
160         OxmFieldsActionBuilder matchEntries = new OxmFieldsActionBuilder();\r
161         List<MatchEntries> entries = new ArrayList<>();\r
162         MatchEntriesBuilder matchBuilder = new MatchEntriesBuilder();\r
163         matchBuilder.setOxmClass(OpenflowBasicClass.class);\r
164         matchBuilder.setOxmMatchField(InPort.class);\r
165         matchBuilder.setHasMask(false);\r
166         PortNumberMatchEntryBuilder portBuilder = new PortNumberMatchEntryBuilder();\r
167         portBuilder.setPortNumber(new PortNumber(1L));\r
168         matchBuilder.addAugmentation(PortNumberMatchEntry.class, portBuilder.build());\r
169         entries.add(matchBuilder.build());\r
170         matchEntries.setMatchEntries(entries);\r
171         actionBuilder.addAugmentation(OxmFieldsAction.class, matchEntries.build());\r
172         actions.add(actionBuilder.build());\r
173         actionBuilder = new ActionBuilder();\r
174         actionBuilder.setType(PushPbb.class);\r
175         etherType = new EthertypeActionBuilder();\r
176         etherType.setEthertype(new EtherType(19));\r
177         actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());\r
178         actions.add(actionBuilder.build());\r
179         actionBuilder = new ActionBuilder();\r
180         actionBuilder.setType(PopPbb.class);\r
181         actions.add(actionBuilder.build());\r
182         \r
183         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
184         CodingUtils.serializeList(actions, actionSerializer, out);\r
185         \r
186         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());\r
187         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());\r
188         Assert.assertEquals("Wrong action port", 42, out.readUnsignedInt());\r
189         Assert.assertEquals("Wrong action max-length", 52, out.readUnsignedShort());\r
190         out.skipBytes(6);\r
191         Assert.assertEquals("Wrong action type", 11, out.readUnsignedShort());\r
192         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
193         out.skipBytes(4);\r
194         Assert.assertEquals("Wrong action type", 12, out.readUnsignedShort());\r
195         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
196         out.skipBytes(4);\r
197         Assert.assertEquals("Wrong action type", 15, out.readUnsignedShort());\r
198         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
199         Assert.assertEquals("Wrong action mpls-ttl", 4, out.readUnsignedByte());\r
200         out.skipBytes(3);\r
201         Assert.assertEquals("Wrong action type", 16, out.readUnsignedShort());\r
202         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
203         out.skipBytes(4);\r
204         Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());\r
205         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
206         Assert.assertEquals("Wrong action ethertype", 16, out.readUnsignedShort());\r
207         out.skipBytes(2);\r
208         Assert.assertEquals("Wrong action type", 18, out.readUnsignedShort());\r
209         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
210         out.skipBytes(4);\r
211         Assert.assertEquals("Wrong action type", 19, out.readUnsignedShort());\r
212         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
213         Assert.assertEquals("Wrong action ethertype", 17, out.readUnsignedShort());\r
214         out.skipBytes(2);\r
215         Assert.assertEquals("Wrong action type", 20, out.readUnsignedShort());\r
216         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
217         Assert.assertEquals("Wrong action ethertype", 18, out.readUnsignedShort());\r
218         out.skipBytes(2);\r
219         Assert.assertEquals("Wrong action type", 21, out.readUnsignedShort());\r
220         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
221         Assert.assertEquals("Wrong action queue-id", 1234, out.readUnsignedInt());\r
222         Assert.assertEquals("Wrong action type", 22, out.readUnsignedShort());\r
223         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
224         Assert.assertEquals("Wrong action group", 555, out.readUnsignedInt());\r
225         Assert.assertEquals("Wrong action type", 23, out.readUnsignedShort());\r
226         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
227         Assert.assertEquals("Wrong action nw-ttl", 8, out.readUnsignedByte());\r
228         out.skipBytes(3);\r
229         Assert.assertEquals("Wrong action type", 24, out.readUnsignedShort());\r
230         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
231         out.skipBytes(4);\r
232         Assert.assertEquals("Wrong action type", 25, out.readUnsignedShort());\r
233         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());\r
234         Assert.assertEquals("Wrong match entry class", 0x8000, out.readUnsignedShort());\r
235         Assert.assertEquals("Wrong match entry field & mask", 0, out.readUnsignedByte());\r
236         Assert.assertEquals("Wrong match entry length", 4, out.readUnsignedByte());\r
237         Assert.assertEquals("Wrong match entry value", 1, out.readUnsignedInt());\r
238         out.skipBytes(4);\r
239         Assert.assertEquals("Wrong action type", 26, out.readUnsignedShort());\r
240         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
241         Assert.assertEquals("Wrong action ethertype", 19, out.readUnsignedShort());\r
242         out.skipBytes(2);\r
243         Assert.assertEquals("Wrong action type", 27, out.readUnsignedShort());\r
244         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
245         out.skipBytes(4);\r
246         Assert.assertTrue("Unread data", out.readableBytes() == 0);\r
247     }\r
248 \r
249 }\r