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
9 package org.opendaylight.openflowjava.protocol.impl.util;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
23 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
24 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
25 import org.opendaylight.openflowjava.util.ByteBufUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6FlowLabel;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterIdMatchEntry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterIdMatchEntryBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntryBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntryBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntry;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntryBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.StandardMatchType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ExperimenterClass;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Src;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Dst;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Flabel;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTarget;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Src;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MatchField;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.MatchBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * @author michal.polkorab
61 public class OF13MatchSerializerTest {
63 private static final Logger LOG = LoggerFactory
64 .getLogger(OF13MatchSerializerTest.class);
65 private SerializerRegistry registry;
66 private OFSerializer<Match> matchSerializer;
69 * Initializes serializer table and stores correct factory in field
72 public void startUp() {
73 registry = new SerializerRegistryImpl();
75 matchSerializer = registry.getSerializer(
76 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, Match.class));
80 * Test for correct serialization of Ipv4Address match entry
83 public void testIpv4Src() {
84 MatchBuilder builder = new MatchBuilder();
85 builder.setType(OxmMatchType.class);
86 List<MatchEntries> entries = new ArrayList<>();
87 MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();
88 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
89 entriesBuilder.setOxmMatchField(Ipv4Src.class);
90 entriesBuilder.setHasMask(false);
91 Ipv4AddressMatchEntryBuilder addressBuilder = new Ipv4AddressMatchEntryBuilder();
92 addressBuilder.setIpv4Address(new Ipv4Address("1.2.3.4"));
93 entriesBuilder.addAugmentation(Ipv4AddressMatchEntry.class, addressBuilder.build());
94 entries.add(entriesBuilder.build());
95 builder.setMatchEntries(entries);
96 Match match = builder.build();
98 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
99 matchSerializer.serialize(match, out);
101 Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
102 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
103 Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
104 Assert.assertEquals("Wrong field and mask", 22, out.readUnsignedByte());
105 out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
106 Assert.assertEquals("Wrong ip address (first number)", 1, out.readUnsignedByte());
107 Assert.assertEquals("Wrong ip address (second number)", 2, out.readUnsignedByte());
108 Assert.assertEquals("Wrong ip address (third number)", 3, out.readUnsignedByte());
109 Assert.assertEquals("Wrong ip address (fourth number)", 4, out.readUnsignedByte());
113 * Test for correct serialization of Ipv6Address match entry
115 @Test(expected=IllegalStateException.class)
116 public void testIpv6Various() {
117 MatchBuilder builder = new MatchBuilder();
118 builder.setType(OxmMatchType.class);
119 List<MatchEntries> entries = new ArrayList<>();
120 // ipv6 match entry with correct Ipv6 address
121 MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();
122 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
123 entriesBuilder.setOxmMatchField(Ipv6Src.class);
124 entriesBuilder.setHasMask(false);
125 Ipv6AddressMatchEntryBuilder addressBuilder = new Ipv6AddressMatchEntryBuilder();
126 addressBuilder.setIpv6Address(new Ipv6Address("1:2:3:4:5:6:7:8"));
127 entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
128 entries.add(entriesBuilder.build());
129 // ipv6 match entry with abbreviated Ipv6 address
130 entriesBuilder = new MatchEntriesBuilder();
131 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
132 entriesBuilder.setOxmMatchField(Ipv6NdTarget.class);
133 entriesBuilder.setHasMask(false);
134 addressBuilder = new Ipv6AddressMatchEntryBuilder();
135 addressBuilder.setIpv6Address(new Ipv6Address("1:2::6:7:8"));
136 entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
137 entries.add(entriesBuilder.build());
138 // ipv6 match entry with abbreviated Ipv6 address
139 entriesBuilder = new MatchEntriesBuilder();
140 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
141 entriesBuilder.setOxmMatchField(Ipv6Dst.class);
142 entriesBuilder.setHasMask(false);
143 addressBuilder = new Ipv6AddressMatchEntryBuilder();
144 addressBuilder.setIpv6Address(new Ipv6Address("1::8"));
145 entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
146 entries.add(entriesBuilder.build());
147 // ipv6 match entry with abbreviated Ipv6 address
148 entriesBuilder = new MatchEntriesBuilder();
149 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
150 entriesBuilder.setOxmMatchField(Ipv6Dst.class);
151 entriesBuilder.setHasMask(false);
152 addressBuilder = new Ipv6AddressMatchEntryBuilder();
153 addressBuilder.setIpv6Address(new Ipv6Address("::1"));
154 entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
155 entries.add(entriesBuilder.build());
156 // ipv6 match entry with abbreviated Ipv6 address
157 entriesBuilder = new MatchEntriesBuilder();
158 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
159 entriesBuilder.setOxmMatchField(Ipv6Dst.class);
160 entriesBuilder.setHasMask(false);
161 addressBuilder = new Ipv6AddressMatchEntryBuilder();
162 addressBuilder.setIpv6Address(new Ipv6Address("::"));
163 entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
164 entries.add(entriesBuilder.build());
165 // ipv6 match entry with incorrect Ipv6 address (longer)
166 entriesBuilder = new MatchEntriesBuilder();
167 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
168 entriesBuilder.setOxmMatchField(Ipv6Dst.class);
169 entriesBuilder.setHasMask(false);
170 addressBuilder = new Ipv6AddressMatchEntryBuilder();
171 addressBuilder.setIpv6Address(new Ipv6Address("1:2:3:4:5:6:7:8:9"));
172 entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
173 entries.add(entriesBuilder.build());
174 // ipv6 match entry with too abbreviated Ipv6 address
175 entriesBuilder = new MatchEntriesBuilder();
176 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
177 entriesBuilder.setOxmMatchField(Ipv6NdTarget.class);
178 entriesBuilder.setHasMask(false);
179 addressBuilder = new Ipv6AddressMatchEntryBuilder();
180 addressBuilder.setIpv6Address(new Ipv6Address("1:2::::8"));
181 entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
182 entries.add(entriesBuilder.build());
183 builder.setMatchEntries(entries);
184 Match match = builder.build();
185 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
186 matchSerializer.serialize(match, out);
188 Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
189 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
190 Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
191 Assert.assertEquals("Wrong field and mask", 52, out.readUnsignedByte());
192 Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
193 Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
194 Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort());
195 Assert.assertEquals("Wrong ipv6 address", 3, out.readUnsignedShort());
196 Assert.assertEquals("Wrong ipv6 address", 4, out.readUnsignedShort());
197 Assert.assertEquals("Wrong ipv6 address", 5, out.readUnsignedShort());
198 Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort());
199 Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort());
200 Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
201 Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
202 Assert.assertEquals("Wrong field and mask", 62, out.readUnsignedByte());
203 Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
204 Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
205 Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort());
206 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
207 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
208 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
209 Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort());
210 Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort());
211 Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
212 Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
213 Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
214 Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
215 Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
216 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
217 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
218 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
219 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
220 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
221 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
222 Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
223 Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
224 Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
225 Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
226 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
227 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
228 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
229 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
230 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
231 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
232 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
233 Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
234 Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
235 Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
236 Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
237 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
238 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
239 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
240 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
241 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
242 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
243 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
244 Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
245 Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
246 Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
247 Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
248 Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
249 Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort());
250 Assert.assertEquals("Wrong ipv6 address", 3, out.readUnsignedShort());
251 Assert.assertEquals("Wrong ipv6 address", 4, out.readUnsignedShort());
252 Assert.assertEquals("Wrong ipv6 address", 5, out.readUnsignedShort());
253 Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort());
254 Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort());
255 Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
259 * Test for correct serialization of Ipv4Address match entry
262 public void testIpv6Flabel() {
263 Match match = buildIpv6FLabelMatch(0x0f9e8dL, false, null);
265 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
266 matchSerializer.serialize(match, out);
268 Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
269 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
270 Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
271 Assert.assertEquals("Wrong field and mask", 28<<1, out.readUnsignedByte());
272 out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
273 byte[] label = new byte[4];
274 out.readBytes(label);
276 LOG.debug("label: "+ ByteBufUtils.bytesToHexString(label));
277 Assert.assertArrayEquals("Wrong ipv6FLabel", new byte[]{0, 0x0f, (byte) 0x9e, (byte) 0x8d}, label);
281 * Test for correct serialization of Ipv4Address match entry with mask
284 public void testIpv6FlabelWithMask() {
285 Match match = buildIpv6FLabelMatch(0x0f9e8dL, true, new byte[]{0, 0x0c, 0x7b, 0x6a});
287 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
288 matchSerializer.serialize(match, out);
290 Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
291 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
292 Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
293 Assert.assertEquals("Wrong field and mask", 28<<1 | 1, out.readUnsignedByte());
294 out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
295 byte[] labelAndMask = new byte[8];
296 out.readBytes(labelAndMask);
298 LOG.debug("label: "+ByteBufUtils.bytesToHexString(labelAndMask));
299 Assert.assertArrayEquals("Wrong ipv6FLabel", new byte[]{0, 0x0f, (byte) 0x9e, (byte) 0x8d, 0, 0x0c, 0x7b, 0x6a}, labelAndMask);
303 * Test for correct serialization of Ipv4Address match entry with wrong mask
306 public void testIpv6FlabelWithMaskBad() {
307 Match match = buildIpv6FLabelMatch(0x0f9e8dL, true, new byte[]{0x0c, 0x7b, 0x6a});
309 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
312 matchSerializer.serialize(match, out);
313 Assert.fail("incorrect length of mask ignored");
314 } catch (IllegalArgumentException e) {
320 * @param labelValue ipv6 flow label
322 * @param mask ipv6 flow label mask
325 private static Match buildIpv6FLabelMatch(long labelValue, boolean hasMask, byte[] mask) {
326 MatchBuilder builder = new MatchBuilder();
327 builder.setType(OxmMatchType.class);
328 List<MatchEntries> entries = new ArrayList<>();
329 MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();
330 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
331 entriesBuilder.setOxmMatchField(Ipv6Flabel.class);
332 entriesBuilder.setHasMask(hasMask);
333 Ipv6FlabelMatchEntryBuilder ip6FLabelBuilder = new Ipv6FlabelMatchEntryBuilder();
334 ip6FLabelBuilder.setIpv6Flabel(new Ipv6FlowLabel(labelValue));
335 entriesBuilder.addAugmentation(Ipv6FlabelMatchEntry.class, ip6FLabelBuilder.build());
336 MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
337 maskBuilder.setMask(mask);
338 entriesBuilder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
339 entries.add(entriesBuilder.build());
340 builder.setMatchEntries(entries);
341 Match match = builder.build();
346 * Test Standard match type
349 public void testStandardMatchType() {
350 MatchBuilder builder = new MatchBuilder();
351 builder.setType(StandardMatchType.class);
352 Match match = builder.build();
353 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
355 matchSerializer.serialize(match, out);
357 Assert.assertEquals("Wrong match type", 0, out.readUnsignedShort());
358 Assert.assertEquals("Wrong match length", 4, out.readUnsignedShort());
359 Assert.assertEquals("Wrong padding", 0, out.readUnsignedInt());
360 Assert.assertEquals("Unexpected data", 0, out.readableBytes());
364 * Test serialize experimenter match entry - with no experimenter
365 * match entry serializer registered
367 @Test(expected=IllegalStateException.class)
368 public void testSerializeExperimenterMatchEntry() {
369 List<MatchEntries> entries = new ArrayList<>();
370 MatchEntriesBuilder builder = new MatchEntriesBuilder();
371 builder.setOxmClass(ExperimenterClass.class);
372 builder.setOxmMatchField(OxmMatchFieldClass.class);
373 builder.setHasMask(true);
374 ExperimenterIdMatchEntryBuilder expIdBuilder = new ExperimenterIdMatchEntryBuilder();
375 expIdBuilder.setExperimenter(new ExperimenterId(42L));
376 builder.addAugmentation(ExperimenterIdMatchEntry.class, expIdBuilder.build());
377 entries.add(builder.build());
378 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
380 ((OF13MatchSerializer) matchSerializer).serializeMatchEntries(entries, out);
383 private class OxmMatchFieldClass extends MatchField {
384 // only for testing purposes