Bug 2756 - Action model update
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF13ActionsSerializerTest.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowjava.protocol.impl.util;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlInCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlOutCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.DecMplsTtlCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.DecNwTtlCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.GroupCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopMplsCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopPbbCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopVlanCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushMplsCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushPbbCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushVlanCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetFieldCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetMplsTtlCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwTtlCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetQueueCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.group._case.GroupActionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.pop.mpls._case.PopMplsActionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.push.mpls._case.PushMplsActionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.push.pbb._case.PushPbbActionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.push.vlan._case.PushVlanActionBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.field._case.SetFieldActionBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.mpls.ttl._case.SetMplsTtlActionBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.nw.ttl._case.SetNwTtlActionBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.queue._case.SetQueueActionBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPort;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCaseBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPortBuilder;
58
59 /**
60  * @author michal.polkorab
61  *
62  */
63 public class OF13ActionsSerializerTest {
64
65     private SerializerRegistry registry;
66
67     /**
68      * Initializes serializer table and stores correct factory in field
69      */
70     @Before
71     public void startUp() {
72         registry = new SerializerRegistryImpl();
73         registry.init();
74     }
75
76     /**
77      * Testing correct serialization of actions
78      */
79     @Test
80     public void test() {
81         List<Action> actions = new ArrayList<>();
82         ActionBuilder actionBuilder = new ActionBuilder();
83         OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
84         OutputActionBuilder outputBuilder = new OutputActionBuilder();
85         outputBuilder.setPort(new PortNumber(42L));
86         outputBuilder.setMaxLength(52);
87         caseBuilder.setOutputAction(outputBuilder.build());
88         actionBuilder.setActionChoice(caseBuilder.build());
89         actions.add(actionBuilder.build());
90         actionBuilder = new ActionBuilder();
91         actionBuilder.setActionChoice(new CopyTtlOutCaseBuilder().build());
92         actions.add(actionBuilder.build());
93         actionBuilder = new ActionBuilder();
94         actionBuilder.setActionChoice(new CopyTtlInCaseBuilder().build());
95         actions.add(actionBuilder.build());
96         actionBuilder = new ActionBuilder();
97         SetMplsTtlCaseBuilder setMplsTtlCaseBuilder = new SetMplsTtlCaseBuilder();
98         SetMplsTtlActionBuilder setMplsTtlBuilder = new SetMplsTtlActionBuilder();
99         setMplsTtlBuilder.setMplsTtl((short) 4);
100         setMplsTtlCaseBuilder.setSetMplsTtlAction(setMplsTtlBuilder.build());
101         actionBuilder.setActionChoice(setMplsTtlCaseBuilder.build());
102         actions.add(actionBuilder.build());
103         actionBuilder = new ActionBuilder();
104         actionBuilder.setActionChoice(new DecMplsTtlCaseBuilder().build());
105         actions.add(actionBuilder.build());
106         actionBuilder = new ActionBuilder();
107         PushVlanCaseBuilder pushVlanCaseBuilder = new PushVlanCaseBuilder();
108         PushVlanActionBuilder pushVlanBuilder = new PushVlanActionBuilder();
109         pushVlanBuilder.setEthertype(new EtherType(new EtherType(16)));
110         pushVlanCaseBuilder.setPushVlanAction(pushVlanBuilder.build());
111         actionBuilder.setActionChoice(pushVlanCaseBuilder.build());
112         actions.add(actionBuilder.build());
113         actionBuilder = new ActionBuilder();
114         actionBuilder.setActionChoice(new PopVlanCaseBuilder().build());
115         actions.add(actionBuilder.build());
116         actionBuilder = new ActionBuilder();
117         PushMplsCaseBuilder pushMplsCaseBuilder = new PushMplsCaseBuilder();
118         PushMplsActionBuilder pushMplsBuilder = new PushMplsActionBuilder();
119         pushMplsBuilder.setEthertype(new EtherType(new EtherType(17)));
120         pushMplsCaseBuilder.setPushMplsAction(pushMplsBuilder.build());
121         actionBuilder.setActionChoice(pushMplsCaseBuilder.build());
122         actions.add(actionBuilder.build());
123         actionBuilder = new ActionBuilder();
124         PopMplsCaseBuilder popMplsCaseBuilder = new PopMplsCaseBuilder();
125         PopMplsActionBuilder popMplsBuilder = new PopMplsActionBuilder();
126         popMplsBuilder.setEthertype(new EtherType(new EtherType(18)));
127         popMplsCaseBuilder.setPopMplsAction(popMplsBuilder.build());
128         actionBuilder.setActionChoice(popMplsCaseBuilder.build());
129         actions.add(actionBuilder.build());
130         actionBuilder = new ActionBuilder();
131         SetQueueCaseBuilder setQueueCaseBuilder = new SetQueueCaseBuilder();
132         SetQueueActionBuilder setQueueBuilder = new SetQueueActionBuilder();
133         setQueueBuilder.setQueueId(1234L);
134         setQueueCaseBuilder.setSetQueueAction(setQueueBuilder.build());
135         actionBuilder.setActionChoice(setQueueCaseBuilder.build());
136         actions.add(actionBuilder.build());
137         actionBuilder = new ActionBuilder();
138         GroupCaseBuilder groupCaseBuilder = new GroupCaseBuilder();
139         GroupActionBuilder groupActionBuilder = new GroupActionBuilder();
140         groupActionBuilder.setGroupId(555L);
141         groupCaseBuilder.setGroupAction(groupActionBuilder.build());
142         actionBuilder.setActionChoice(groupCaseBuilder.build());
143         actions.add(actionBuilder.build());
144         actionBuilder = new ActionBuilder();
145         SetNwTtlCaseBuilder nwTtlCaseBuilder = new SetNwTtlCaseBuilder();
146         SetNwTtlActionBuilder nwTtlBuilder = new SetNwTtlActionBuilder();
147         nwTtlBuilder.setNwTtl((short) 8);
148         nwTtlCaseBuilder.setSetNwTtlAction(nwTtlBuilder.build());
149         actionBuilder.setActionChoice(nwTtlCaseBuilder.build());
150         actions.add(actionBuilder.build());
151         actionBuilder = new ActionBuilder();
152         actionBuilder.setActionChoice(new DecNwTtlCaseBuilder().build());
153         actions.add(actionBuilder.build());
154         actionBuilder = new ActionBuilder();
155         SetFieldCaseBuilder setFieldCaseBuilder = new SetFieldCaseBuilder();
156         SetFieldActionBuilder setFieldBuilder = new SetFieldActionBuilder();
157         List<MatchEntry> entries = new ArrayList<>();
158         MatchEntryBuilder matchBuilder = new MatchEntryBuilder();
159         matchBuilder.setOxmClass(OpenflowBasicClass.class);
160         matchBuilder.setOxmMatchField(InPort.class);
161         matchBuilder.setHasMask(false);
162         InPortCaseBuilder inPortCaseBuilder = new InPortCaseBuilder();
163         InPortBuilder inPortBuilder = new InPortBuilder();
164         inPortBuilder.setPortNumber(new PortNumber(1L));
165         inPortCaseBuilder.setInPort(inPortBuilder.build());
166         matchBuilder.setMatchEntryValue(inPortCaseBuilder.build());
167         entries.add(matchBuilder.build());
168         setFieldBuilder.setMatchEntry(entries);
169         setFieldCaseBuilder.setSetFieldAction(setFieldBuilder.build());
170         actionBuilder.setActionChoice(setFieldCaseBuilder.build());
171         actions.add(actionBuilder.build());
172         actionBuilder = new ActionBuilder();
173         PushPbbCaseBuilder pushPbbCaseBuilder = new PushPbbCaseBuilder();
174         PushPbbActionBuilder pushPbbBuilder = new PushPbbActionBuilder();
175         pushPbbBuilder.setEthertype(new EtherType(new EtherType(19)));
176         pushPbbCaseBuilder.setPushPbbAction(pushPbbBuilder.build());
177         actionBuilder.setActionChoice(pushPbbCaseBuilder.build());
178         actions.add(actionBuilder.build());
179         actionBuilder = new ActionBuilder();
180         actionBuilder.setActionChoice(new PopPbbCaseBuilder().build());
181         actions.add(actionBuilder.build());
182
183         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
184         ListSerializer.serializeList(actions, TypeKeyMakerFactory
185                 .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, out);
186
187         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
188         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
189         Assert.assertEquals("Wrong action port", 42, out.readUnsignedInt());
190         Assert.assertEquals("Wrong action max-length", 52, out.readUnsignedShort());
191         out.skipBytes(6);
192         Assert.assertEquals("Wrong action type", 11, out.readUnsignedShort());
193         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
194         out.skipBytes(4);
195         Assert.assertEquals("Wrong action type", 12, out.readUnsignedShort());
196         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
197         out.skipBytes(4);
198         Assert.assertEquals("Wrong action type", 15, out.readUnsignedShort());
199         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
200         Assert.assertEquals("Wrong action mpls-ttl", 4, out.readUnsignedByte());
201         out.skipBytes(3);
202         Assert.assertEquals("Wrong action type", 16, out.readUnsignedShort());
203         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
204         out.skipBytes(4);
205         Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());
206         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
207         Assert.assertEquals("Wrong action ethertype", 16, out.readUnsignedShort());
208         out.skipBytes(2);
209         Assert.assertEquals("Wrong action type", 18, out.readUnsignedShort());
210         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
211         out.skipBytes(4);
212         Assert.assertEquals("Wrong action type", 19, out.readUnsignedShort());
213         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
214         Assert.assertEquals("Wrong action ethertype", 17, out.readUnsignedShort());
215         out.skipBytes(2);
216         Assert.assertEquals("Wrong action type", 20, out.readUnsignedShort());
217         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
218         Assert.assertEquals("Wrong action ethertype", 18, out.readUnsignedShort());
219         out.skipBytes(2);
220         Assert.assertEquals("Wrong action type", 21, out.readUnsignedShort());
221         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
222         Assert.assertEquals("Wrong action queue-id", 1234, out.readUnsignedInt());
223         Assert.assertEquals("Wrong action type", 22, out.readUnsignedShort());
224         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
225         Assert.assertEquals("Wrong action group", 555, out.readUnsignedInt());
226         Assert.assertEquals("Wrong action type", 23, out.readUnsignedShort());
227         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
228         Assert.assertEquals("Wrong action nw-ttl", 8, out.readUnsignedByte());
229         out.skipBytes(3);
230         Assert.assertEquals("Wrong action type", 24, out.readUnsignedShort());
231         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
232         out.skipBytes(4);
233         Assert.assertEquals("Wrong action type", 25, out.readUnsignedShort());
234         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
235         Assert.assertEquals("Wrong match entry class", 0x8000, out.readUnsignedShort());
236         Assert.assertEquals("Wrong match entry field & mask", 0, out.readUnsignedByte());
237         Assert.assertEquals("Wrong match entry length", 4, out.readUnsignedByte());
238         Assert.assertEquals("Wrong match entry value", 1, out.readUnsignedInt());
239         out.skipBytes(4);
240         Assert.assertEquals("Wrong action type", 26, out.readUnsignedShort());
241         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
242         Assert.assertEquals("Wrong action ethertype", 19, out.readUnsignedShort());
243         out.skipBytes(2);
244         Assert.assertEquals("Wrong action type", 27, out.readUnsignedShort());
245         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
246         out.skipBytes(4);
247         Assert.assertTrue("Unread data", out.readableBytes() == 0);
248     }
249
250     /**
251      * Testing correct serialization of actions
252      */
253     @Test
254     public void testHeaders() {
255         List<Action> actions = new ArrayList<>();
256         ActionBuilder actionBuilder = new ActionBuilder();
257         OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
258         OutputActionBuilder outputBuilder = new OutputActionBuilder();
259         outputBuilder.setPort(new PortNumber(42L));
260         outputBuilder.setMaxLength(52);
261         caseBuilder.setOutputAction(outputBuilder.build());
262         actionBuilder.setActionChoice(caseBuilder.build());
263         actions.add(actionBuilder.build());
264         actionBuilder = new ActionBuilder();
265         SetFieldCaseBuilder setFieldCaseBuilder = new SetFieldCaseBuilder();
266         SetFieldActionBuilder setFieldBuilder = new SetFieldActionBuilder();
267         List<MatchEntry> entries = new ArrayList<>();
268         MatchEntryBuilder matchBuilder = new MatchEntryBuilder();
269         matchBuilder.setOxmClass(OpenflowBasicClass.class);
270         matchBuilder.setOxmMatchField(InPort.class);
271         matchBuilder.setHasMask(false);
272         InPortCaseBuilder inPortCaseBuilder = new InPortCaseBuilder();
273         InPortBuilder inPortBuilder = new InPortBuilder();
274         inPortBuilder.setPortNumber(new PortNumber(1L));
275         inPortCaseBuilder.setInPort(inPortBuilder.build());
276         matchBuilder.setMatchEntryValue(inPortCaseBuilder.build());
277         entries.add(matchBuilder.build());
278         setFieldBuilder.setMatchEntry(entries);
279         setFieldCaseBuilder.setSetFieldAction(setFieldBuilder.build());
280         actionBuilder.setActionChoice(setFieldCaseBuilder.build());
281         actions.add(actionBuilder.build());
282
283         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
284         ListSerializer.serializeHeaderList(actions, TypeKeyMakerFactory
285                 .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, out);
286
287         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
288         Assert.assertEquals("Wrong action length", 4, out.readUnsignedShort());
289         Assert.assertEquals("Wrong action type", 25, out.readUnsignedShort());
290         Assert.assertEquals("Wrong action length", 4, out.readUnsignedShort());
291         Assert.assertTrue("Unread data", out.readableBytes() == 0);
292     }
293 }