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