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