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