Merge "Bug 1277 - Move ByteBuffUtils to separate bundle"
[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.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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6FlowLabel;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntryBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntryBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntry;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6FlabelMatchEntryBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Src;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Dst;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Flabel;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTarget;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Src;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.MatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * @author michal.polkorab
52  *
53  */
54 public class OF13MatchSerializerTest {
55
56     private static final Logger LOG = LoggerFactory
57             .getLogger(OF13MatchSerializerTest.class);
58     private SerializerRegistry registry;
59     private OFSerializer<Match> matchSerializer;
60
61     /**
62      * Initializes serializer table and stores correct factory in field
63      */
64     @Before
65     public void startUp() {
66         registry = new SerializerRegistryImpl();
67         registry.init();
68         matchSerializer = registry.getSerializer(
69                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, Match.class));
70     }
71
72     /**
73      * Test for correct serialization of Ipv4Address match entry
74      */
75     @Test
76     public void testIpv4Src() {
77         MatchBuilder builder = new MatchBuilder();
78         builder.setType(OxmMatchType.class);
79         List<MatchEntries> entries = new ArrayList<>();
80         MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();
81         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
82         entriesBuilder.setOxmMatchField(Ipv4Src.class);
83         entriesBuilder.setHasMask(false);
84         Ipv4AddressMatchEntryBuilder addressBuilder = new Ipv4AddressMatchEntryBuilder();
85         addressBuilder.setIpv4Address(new Ipv4Address("1.2.3.4"));
86         entriesBuilder.addAugmentation(Ipv4AddressMatchEntry.class, addressBuilder.build());
87         entries.add(entriesBuilder.build());
88         builder.setMatchEntries(entries);
89         Match match = builder.build();
90         
91         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
92         matchSerializer.serialize(match, out);
93         
94         Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
95         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
96         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
97         Assert.assertEquals("Wrong field and mask", 22, out.readUnsignedByte());
98         out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
99         Assert.assertEquals("Wrong ip address (first number)", 1, out.readUnsignedByte());
100         Assert.assertEquals("Wrong ip address (second number)", 2, out.readUnsignedByte());
101         Assert.assertEquals("Wrong ip address (third number)", 3, out.readUnsignedByte());
102         Assert.assertEquals("Wrong ip address (fourth number)", 4, out.readUnsignedByte());
103     }
104     
105     /**
106      * Test for correct serialization of Ipv6Address match entry
107      */
108     @Test
109     public void testIpv6Various() {
110         MatchBuilder builder = new MatchBuilder();
111         builder.setType(OxmMatchType.class);
112         List<MatchEntries> entries = new ArrayList<>();
113         // ipv6 match entry with correct Ipv6 address
114         MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();
115         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
116         entriesBuilder.setOxmMatchField(Ipv6Src.class);
117         entriesBuilder.setHasMask(false);
118         Ipv6AddressMatchEntryBuilder addressBuilder = new Ipv6AddressMatchEntryBuilder();
119         addressBuilder.setIpv6Address(new Ipv6Address("1:2:3:4:5:6:7:8"));
120         entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
121         entries.add(entriesBuilder.build());
122         // ipv6 match entry with abbreviated Ipv6 address
123         entriesBuilder = new MatchEntriesBuilder();
124         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
125         entriesBuilder.setOxmMatchField(Ipv6NdTarget.class);
126         entriesBuilder.setHasMask(false);
127         addressBuilder = new Ipv6AddressMatchEntryBuilder();
128         addressBuilder.setIpv6Address(new Ipv6Address("1:2::6:7:8"));
129         entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
130         entries.add(entriesBuilder.build());
131         // ipv6 match entry with abbreviated Ipv6 address
132         entriesBuilder = new MatchEntriesBuilder();
133         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
134         entriesBuilder.setOxmMatchField(Ipv6Dst.class);
135         entriesBuilder.setHasMask(false);
136         addressBuilder = new Ipv6AddressMatchEntryBuilder();
137         addressBuilder.setIpv6Address(new Ipv6Address("1::8"));
138         entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
139         entries.add(entriesBuilder.build());
140         // ipv6 match entry with abbreviated Ipv6 address
141         entriesBuilder = new MatchEntriesBuilder();
142         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
143         entriesBuilder.setOxmMatchField(Ipv6Dst.class);
144         entriesBuilder.setHasMask(false);
145         addressBuilder = new Ipv6AddressMatchEntryBuilder();
146         addressBuilder.setIpv6Address(new Ipv6Address("::1"));
147         entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
148         entries.add(entriesBuilder.build());
149         // ipv6 match entry with abbreviated Ipv6 address
150         entriesBuilder = new MatchEntriesBuilder();
151         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
152         entriesBuilder.setOxmMatchField(Ipv6Dst.class);
153         entriesBuilder.setHasMask(false);
154         addressBuilder = new Ipv6AddressMatchEntryBuilder();
155         addressBuilder.setIpv6Address(new Ipv6Address("::"));
156         entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
157         entries.add(entriesBuilder.build());
158         // ipv6 match entry with incorrect Ipv6 address (longer)
159         entriesBuilder = new MatchEntriesBuilder();
160         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
161         entriesBuilder.setOxmMatchField(Ipv6Dst.class);
162         entriesBuilder.setHasMask(false);
163         addressBuilder = new Ipv6AddressMatchEntryBuilder();
164         addressBuilder.setIpv6Address(new Ipv6Address("1:2:3:4:5:6:7:8:9"));
165         entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build());
166         entries.add(entriesBuilder.build());
167         builder.setMatchEntries(entries);
168         Match match = builder.build();
169         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
170         matchSerializer.serialize(match, out);
171         
172         Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
173         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
174         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
175         Assert.assertEquals("Wrong field and mask", 52, out.readUnsignedByte());
176         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
177         Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
178         Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort());
179         Assert.assertEquals("Wrong ipv6 address", 3, out.readUnsignedShort());
180         Assert.assertEquals("Wrong ipv6 address", 4, out.readUnsignedShort());
181         Assert.assertEquals("Wrong ipv6 address", 5, out.readUnsignedShort());
182         Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort());
183         Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort());
184         Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
185         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
186         Assert.assertEquals("Wrong field and mask", 62, out.readUnsignedByte());
187         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
188         Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
189         Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort());
190         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
191         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
192         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
193         Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort());
194         Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort());
195         Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
196         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
197         Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
198         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
199         Assert.assertEquals("Wrong ipv6 address", 1, 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", 0, out.readUnsignedShort());
205         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
206         Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
207         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
208         Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
209         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
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", 0, out.readUnsignedShort());
216         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
217         Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
218         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
219         Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
220         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
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         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
228         Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort());
229         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
230         Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte());
231         Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte());
232         Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort());
233         Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort());
234         Assert.assertEquals("Wrong ipv6 address", 3, out.readUnsignedShort());
235         Assert.assertEquals("Wrong ipv6 address", 4, out.readUnsignedShort());
236         Assert.assertEquals("Wrong ipv6 address", 5, out.readUnsignedShort());
237         Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort());
238         Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort());
239         Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort());
240     }
241     
242     /**
243      * Test for correct serialization of Ipv4Address match entry
244      */
245     @Test
246     public void testIpv6Flabel() {
247         Match match = buildIpv6FLabelMatch(0x0f9e8dL, false, null);
248         
249         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
250         matchSerializer.serialize(match, out);
251         
252         Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
253         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
254         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
255         Assert.assertEquals("Wrong field and mask", 28<<1, out.readUnsignedByte());
256         out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
257         byte[] label = new byte[4];
258         out.readBytes(label);
259         
260         LOG.debug("label: "+ByteBufUtils.bytesToHexString(label));
261         Assert.assertArrayEquals("Wrong ipv6FLabel", new byte[]{0, 0x0f, (byte) 0x9e, (byte) 0x8d}, label);
262     }
263     
264     /**
265      * Test for correct serialization of Ipv4Address match entry with mask
266      */
267     @Test
268     public void testIpv6FlabelWithMask() {
269         Match match = buildIpv6FLabelMatch(0x0f9e8dL, true, new byte[]{0, 0x0c, 0x7b, 0x6a});
270         
271         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
272         matchSerializer.serialize(match, out);
273         
274         Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
275         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
276         Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort());
277         Assert.assertEquals("Wrong field and mask", 28<<1 | 1, out.readUnsignedByte());
278         out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
279         byte[] labelAndMask = new byte[8];
280         out.readBytes(labelAndMask);
281         
282         LOG.debug("label: "+ByteBufUtils.bytesToHexString(labelAndMask));
283         Assert.assertArrayEquals("Wrong ipv6FLabel", new byte[]{0, 0x0f, (byte) 0x9e, (byte) 0x8d, 0, 0x0c, 0x7b, 0x6a}, labelAndMask);
284     }
285     
286     /**
287      * Test for correct serialization of Ipv4Address match entry with wrong mask
288      */
289     @Test
290     public void testIpv6FlabelWithMaskBad() {
291         Match match = buildIpv6FLabelMatch(0x0f9e8dL, true, new byte[]{0x0c, 0x7b, 0x6a});
292         
293         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
294         
295         try {
296             matchSerializer.serialize(match, out);
297             Assert.fail("incorrect length of mask ignored");
298         } catch (IllegalArgumentException e) {
299             //expected
300         }
301     }
302
303     /**
304      * @param labelValue ipv6 flow label
305      * @param hasMask
306      * @param mask ipv6 flow label mask
307      * @return
308      */
309     private static Match buildIpv6FLabelMatch(long labelValue, boolean hasMask, byte[] mask) {
310         MatchBuilder builder = new MatchBuilder();
311         builder.setType(OxmMatchType.class);
312         List<MatchEntries> entries = new ArrayList<>();
313         MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();
314         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
315         entriesBuilder.setOxmMatchField(Ipv6Flabel.class);
316         entriesBuilder.setHasMask(hasMask);
317         Ipv6FlabelMatchEntryBuilder ip6FLabelBuilder = new Ipv6FlabelMatchEntryBuilder();
318         ip6FLabelBuilder.setIpv6Flabel(new Ipv6FlowLabel(labelValue));
319         entriesBuilder.addAugmentation(Ipv6FlabelMatchEntry.class, ip6FLabelBuilder.build());
320         MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
321         maskBuilder.setMask(mask);
322         entriesBuilder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
323         entries.add(entriesBuilder.build());
324         builder.setMatchEntries(entries);
325         Match match = builder.build();
326         return match;
327     }
328     
329     
330
331 }