2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.util;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
13 import java.util.ArrayList;
14 import java.util.List;
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.rev131002.EthertypeAction;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.GroupIdAction;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.GroupIdActionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsTtlAction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsTtlActionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlAction;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlActionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsAction;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsActionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntryBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdAction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdActionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.CopyTtlIn;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.CopyTtlOut;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.DecMplsTtl;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.DecNwTtl;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Group;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopMpls;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopPbb;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopVlan;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushMpls;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushPbb;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetField;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetMplsTtl;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwTtl;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetQueue;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
66 * @author michal.polkorab
69 public class OF13ActionsSerializerTest {
71 private SerializerRegistry registry;
74 * Initializes serializer table and stores correct factory in field
77 public void startUp() {
78 registry = new SerializerRegistryImpl();
83 * Testing correct serialization of actions
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<MatchEntries> entries = new ArrayList<>();
158 MatchEntriesBuilder matchBuilder = new MatchEntriesBuilder();
159 matchBuilder.setOxmClass(OpenflowBasicClass.class);
160 matchBuilder.setOxmMatchField(InPort.class);
161 matchBuilder.setHasMask(false);
162 PortNumberMatchEntryBuilder portBuilder = new PortNumberMatchEntryBuilder();
163 portBuilder.setPortNumber(new PortNumber(1L));
164 matchBuilder.addAugmentation(PortNumberMatchEntry.class, portBuilder.build());
165 entries.add(matchBuilder.build());
166 matchEntries.setMatchEntries(entries);
167 actionBuilder.addAugmentation(OxmFieldsAction.class, matchEntries.build());
168 actions.add(actionBuilder.build());
169 actionBuilder = new ActionBuilder();
170 actionBuilder.setType(PushPbb.class);
171 etherType = new EthertypeActionBuilder();
172 etherType.setEthertype(new EtherType(19));
173 actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());
174 actions.add(actionBuilder.build());
175 actionBuilder = new ActionBuilder();
176 actionBuilder.setType(PopPbb.class);
177 actions.add(actionBuilder.build());
179 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
180 ListSerializer.serializeList(actions, TypeKeyMakerFactory
181 .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, out);
183 Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
184 Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
185 Assert.assertEquals("Wrong action port", 42, out.readUnsignedInt());
186 Assert.assertEquals("Wrong action max-length", 52, out.readUnsignedShort());
188 Assert.assertEquals("Wrong action type", 11, out.readUnsignedShort());
189 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
191 Assert.assertEquals("Wrong action type", 12, out.readUnsignedShort());
192 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
194 Assert.assertEquals("Wrong action type", 15, out.readUnsignedShort());
195 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
196 Assert.assertEquals("Wrong action mpls-ttl", 4, out.readUnsignedByte());
198 Assert.assertEquals("Wrong action type", 16, out.readUnsignedShort());
199 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
201 Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());
202 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
203 Assert.assertEquals("Wrong action ethertype", 16, out.readUnsignedShort());
205 Assert.assertEquals("Wrong action type", 18, out.readUnsignedShort());
206 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
208 Assert.assertEquals("Wrong action type", 19, out.readUnsignedShort());
209 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
210 Assert.assertEquals("Wrong action ethertype", 17, out.readUnsignedShort());
212 Assert.assertEquals("Wrong action type", 20, out.readUnsignedShort());
213 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
214 Assert.assertEquals("Wrong action ethertype", 18, out.readUnsignedShort());
216 Assert.assertEquals("Wrong action type", 21, out.readUnsignedShort());
217 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
218 Assert.assertEquals("Wrong action queue-id", 1234, out.readUnsignedInt());
219 Assert.assertEquals("Wrong action type", 22, out.readUnsignedShort());
220 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
221 Assert.assertEquals("Wrong action group", 555, out.readUnsignedInt());
222 Assert.assertEquals("Wrong action type", 23, out.readUnsignedShort());
223 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
224 Assert.assertEquals("Wrong action nw-ttl", 8, out.readUnsignedByte());
226 Assert.assertEquals("Wrong action type", 24, out.readUnsignedShort());
227 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
229 Assert.assertEquals("Wrong action type", 25, out.readUnsignedShort());
230 Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
231 Assert.assertEquals("Wrong match entry class", 0x8000, out.readUnsignedShort());
232 Assert.assertEquals("Wrong match entry field & mask", 0, out.readUnsignedByte());
233 Assert.assertEquals("Wrong match entry length", 4, out.readUnsignedByte());
234 Assert.assertEquals("Wrong match entry value", 1, out.readUnsignedInt());
236 Assert.assertEquals("Wrong action type", 26, out.readUnsignedShort());
237 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
238 Assert.assertEquals("Wrong action ethertype", 19, out.readUnsignedShort());
240 Assert.assertEquals("Wrong action type", 27, out.readUnsignedShort());
241 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
243 Assert.assertTrue("Unread data", out.readableBytes() == 0);
247 * Testing correct serialization of actions
250 public void testHeaders() {
251 List<Action> actions = new ArrayList<>();
252 ActionBuilder actionBuilder = new ActionBuilder();
253 actionBuilder.setType(Output.class);
254 PortActionBuilder port = new PortActionBuilder();
255 port.setPort(new PortNumber(42L));
256 actionBuilder.addAugmentation(PortAction.class, port.build());
257 MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();
258 maxLen.setMaxLength(52);
259 actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());
260 actions.add(actionBuilder.build());
261 actionBuilder = new ActionBuilder();
262 actionBuilder.setType(SetField.class);
263 OxmFieldsActionBuilder matchEntries = new OxmFieldsActionBuilder();
264 List<MatchEntries> entries = new ArrayList<>();
265 MatchEntriesBuilder matchBuilder = new MatchEntriesBuilder();
266 matchBuilder.setOxmClass(OpenflowBasicClass.class);
267 matchBuilder.setOxmMatchField(InPort.class);
268 matchBuilder.setHasMask(false);
269 PortNumberMatchEntryBuilder portBuilder = new PortNumberMatchEntryBuilder();
270 portBuilder.setPortNumber(new PortNumber(1L));
271 matchBuilder.addAugmentation(PortNumberMatchEntry.class, portBuilder.build());
272 entries.add(matchBuilder.build());
273 matchEntries.setMatchEntries(entries);
274 actionBuilder.addAugmentation(OxmFieldsAction.class, matchEntries.build());
275 actions.add(actionBuilder.build());
277 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
278 ListSerializer.serializeHeaderList(actions, TypeKeyMakerFactory
279 .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, out);
281 Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
282 Assert.assertEquals("Wrong action length", 4, out.readUnsignedShort());
283 Assert.assertEquals("Wrong action type", 25, out.readUnsignedShort());
284 Assert.assertEquals("Wrong action length", 4, out.readUnsignedShort());
285 Assert.assertTrue("Unread data", out.readableBytes() == 0);