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