2 * Copyright (c) 2016 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.openflowplugin.impl.protocol.serialization.match;
10 import static org.junit.Assert.assertEquals;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.UnpooledByteBufAllocator;
14 import java.util.function.Consumer;
15 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowplugin.impl.protocol.serialization.AbstractSerializerTest;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
20 public abstract class AbstractMatchEntrySerializerTest extends AbstractSerializerTest {
21 private MatchSerializer serializer;
24 protected void init() {
25 serializer = getRegistry().getSerializer(new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, Match.class));
28 protected void assertMatch(final Match match,
29 final boolean hasMask,
30 final Consumer<ByteBuf> assertBody) {
31 final ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
32 serializer.serialize(match, buffer);
34 final int length = (hasMask ? 2 : 1) * getLength();
36 assertEquals(buffer.readShort(), 1); // OXM_MATCH_TYPE
37 assertEquals(buffer.readShort(), // Total length of match
38 Short.BYTES // OXM_MATCH_TYPE length
39 + Short.BYTES // LENGTH length
40 + Short.BYTES // OXM_CLASS_CODE length
41 + Byte.BYTES // OXM field and mask length
42 + Byte.BYTES // OXM field and mask length length
43 + length // length of data in match entry
46 assertEquals(buffer.readUnsignedShort(), getOxmClassCode());
47 final short fieldAndMask = buffer.readUnsignedByte();
48 assertEquals(getOxmFieldCode(), fieldAndMask >>> 1);
49 assertEquals(hasMask, (fieldAndMask & 1) != 0);
50 assertEquals(buffer.readUnsignedByte(), length);
51 assertBody.accept(buffer);
53 int paddingRemainder = length % EncodeConstants.PADDING;
55 if (paddingRemainder != 0) {
56 buffer.skipBytes(EncodeConstants.PADDING - paddingRemainder);
59 assertEquals(buffer.readableBytes(), 0);
62 protected MatchSerializer getSerializer() {
66 protected abstract int getOxmFieldCode();
68 protected abstract int getOxmClassCode();
70 protected abstract short getLength();