deb3df016fa01f297222483710cdc9f5f53e6c6a
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF13MatchSerializerTest.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
9 package org.opendaylight.openflowjava.protocol.impl.util;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
22 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
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;
56
57 /**
58  * @author michal.polkorab
59  *
60  */
61 public class OF13MatchSerializerTest {
62
63     private static final Logger LOG = LoggerFactory
64             .getLogger(OF13MatchSerializerTest.class);
65     private SerializerRegistry registry;
66     private OFSerializer<Match> matchSerializer;
67
68     /**
69      * Initializes serializer table and stores correct factory in field
70      */
71     @Before
72     public void startUp() {
73         registry = new SerializerRegistryImpl();
74         registry.init();
75         matchSerializer = registry.getSerializer(
76                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, Match.class));
77     }
78
79     /**
80      * Test for correct serialization of Ipv4Address match entry
81      */
82     @Test
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();
97
98         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
99         matchSerializer.serialize(match, out);
100
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());
110     }
111
112     /**
113      * Test for correct serialization of Ipv6Address match entry
114      */
115     @Test
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         builder.setMatchEntries(entries);
166         Match match = builder.build();
167         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
168         matchSerializer.serialize(match, out);
169
170         Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
171         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
172         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
173         Assert.assertEquals("Wrong field and mask", 52, out.readUnsignedByte());
174         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
175         Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
176         Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort());
177         Assert.assertEquals("Wrong ipv6 address", 3, out.readUnsignedShort());
178         Assert.assertEquals("Wrong ipv6 address", 4, out.readUnsignedShort());
179         Assert.assertEquals("Wrong ipv6 address", 5, out.readUnsignedShort());
180         Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort());
181         Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort());
182         Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
183         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
184         Assert.assertEquals("Wrong field and mask", 62, out.readUnsignedByte());
185         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
186         Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
187         Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort());
188         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
189         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
190         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
191         Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort());
192         Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort());
193         Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
194         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
195         Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
196         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
197         Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
198         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
199         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
200         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
201         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
202         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
203         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
204         Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
205         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
206         Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
207         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
208         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
209         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
210         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
211         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
212         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
213         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
214         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
215         Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
216         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
217         Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
218         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
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", 0, out.readUnsignedShort());
223         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
224         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
225         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
226         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
227     }
228
229     /**
230      * Test for correct serialization of incorrect Ipv6Address match entry
231      */
232     @Test(expected=IllegalStateException.class)
233     public void testIpv6Incorrect() {
234         MatchBuilder builder = new MatchBuilder();
235         builder.setType(OxmMatchType.class);
236         List<MatchEntries> entries = new ArrayList<>();
237         // ipv6 match entry with incorrect Ipv6 address
238         MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();
239         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
240         entriesBuilder.setOxmMatchField(Ipv6Src.class);
241         entriesBuilder.setHasMask(false);
242         Ipv6AddressMatchEntryBuilder addressBuilder = new Ipv6AddressMatchEntryBuilder();
243         addressBuilder.setIpv6Address(new Ipv6Address("1:2::::8"));
244         entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
245         entries.add(entriesBuilder.build());
246         builder.setMatchEntries(entries);
247         Match match = builder.build();
248         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
249         matchSerializer.serialize(match, out);
250     }
251
252     /**
253      * Test for correct serialization of Ipv4Address match entry
254      */
255     @Test
256     public void testIpv6Flabel() {
257         Match match = buildIpv6FLabelMatch(0x0f9e8dL, false, null);
258
259         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
260         matchSerializer.serialize(match, out);
261
262         Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
263         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
264         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
265         Assert.assertEquals("Wrong field and mask", 28<<1, out.readUnsignedByte());
266         out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
267         byte[] label = new byte[4];
268         out.readBytes(label);
269
270         LOG.debug("label: "+ ByteBufUtils.bytesToHexString(label));
271         Assert.assertArrayEquals("Wrong ipv6FLabel", new byte[]{0, 0x0f, (byte) 0x9e, (byte) 0x8d}, label);
272     }
273
274     /**
275      * Test for correct serialization of Ipv4Address match entry with mask
276      */
277     @Test
278     public void testIpv6FlabelWithMask() {
279         Match match = buildIpv6FLabelMatch(0x0f9e8dL, true, new byte[]{0, 0x0c, 0x7b, 0x6a});
280
281         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
282         matchSerializer.serialize(match, out);
283
284         Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
285         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
286         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
287         Assert.assertEquals("Wrong field and mask", 28<<1 | 1, out.readUnsignedByte());
288         out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
289         byte[] labelAndMask = new byte[8];
290         out.readBytes(labelAndMask);
291
292         LOG.debug("label: "+ByteBufUtils.bytesToHexString(labelAndMask));
293         Assert.assertArrayEquals("Wrong ipv6FLabel", new byte[]{0, 0x0f, (byte) 0x9e, (byte) 0x8d, 0, 0x0c, 0x7b, 0x6a}, labelAndMask);
294     }
295
296     /**
297      * Test for correct serialization of Ipv4Address match entry with wrong mask
298      */
299     @Test
300     public void testIpv6FlabelWithMaskBad() {
301         Match match = buildIpv6FLabelMatch(0x0f9e8dL, true, new byte[]{0x0c, 0x7b, 0x6a});
302
303         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
304
305         try {
306             matchSerializer.serialize(match, out);
307             Assert.fail("incorrect length of mask ignored");
308         } catch (IllegalArgumentException e) {
309             //expected
310         }
311     }
312
313     /**
314      * @param labelValue ipv6 flow label
315      * @param hasMask
316      * @param mask ipv6 flow label mask
317      * @return
318      */
319     private static Match buildIpv6FLabelMatch(long labelValue, boolean hasMask, byte[] mask) {
320         MatchBuilder builder = new MatchBuilder();
321         builder.setType(OxmMatchType.class);
322         List<MatchEntries> entries = new ArrayList<>();
323         MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();
324         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
325         entriesBuilder.setOxmMatchField(Ipv6Flabel.class);
326         entriesBuilder.setHasMask(hasMask);
327         Ipv6FlabelMatchEntryBuilder ip6FLabelBuilder = new Ipv6FlabelMatchEntryBuilder();
328         ip6FLabelBuilder.setIpv6Flabel(new Ipv6FlowLabel(labelValue));
329         entriesBuilder.addAugmentation(Ipv6FlabelMatchEntry.class, ip6FLabelBuilder.build());
330         MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
331         maskBuilder.setMask(mask);
332         entriesBuilder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
333         entries.add(entriesBuilder.build());
334         builder.setMatchEntries(entries);
335         Match match = builder.build();
336         return match;
337     }
338
339     /**
340      * Test Standard match type
341      */
342     @Test
343     public void testStandardMatchType() {
344         MatchBuilder builder = new MatchBuilder();
345         builder.setType(StandardMatchType.class);
346         Match match = builder.build();
347         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
348
349         matchSerializer.serialize(match, out);
350
351         Assert.assertEquals("Wrong match type", 0, out.readUnsignedShort());
352         Assert.assertEquals("Wrong match length", 4, out.readUnsignedShort());
353         Assert.assertEquals("Wrong padding", 0, out.readUnsignedInt());
354         Assert.assertEquals("Unexpected data", 0, out.readableBytes());
355     }
356
357     /**
358      * Test serialize experimenter match entry - with no experimenter
359      * match entry serializer registered
360      */
361     @Test(expected=IllegalStateException.class)
362     public void testSerializeExperimenterMatchEntry() {
363         List<MatchEntries> entries = new ArrayList<>();
364         MatchEntriesBuilder builder = new MatchEntriesBuilder();
365         builder.setOxmClass(ExperimenterClass.class);
366         builder.setOxmMatchField(OxmMatchFieldClass.class);
367         builder.setHasMask(true);
368         ExperimenterIdMatchEntryBuilder expIdBuilder = new ExperimenterIdMatchEntryBuilder();
369         expIdBuilder.setExperimenter(new ExperimenterId(42L));
370         builder.addAugmentation(ExperimenterIdMatchEntry.class, expIdBuilder.build());
371         entries.add(builder.build());
372         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
373
374         ((OF13MatchSerializer) matchSerializer).serializeMatchEntries(entries, out);
375     }
376
377     private class OxmMatchFieldClass extends MatchField {
378         // only for testing purposes
379     }
380 }