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