Modernize codebase
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / match / AbstractMatchEntrySerializerTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.openflowplugin.impl.protocol.serialization.match;
9
10 import static org.junit.Assert.assertEquals;
11
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;
19
20 public abstract class AbstractMatchEntrySerializerTest extends AbstractSerializerTest {
21     private MatchSerializer serializer;
22
23     @Override
24     protected void init() {
25         serializer = getRegistry().getSerializer(new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, Match.class));
26     }
27
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);
33
34         final int length = (hasMask ? 2 : 1) * getLength();
35
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
44         );
45
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);
52
53         int paddingRemainder = length % EncodeConstants.PADDING;
54
55         if (paddingRemainder != 0) {
56             buffer.skipBytes(EncodeConstants.PADDING - paddingRemainder);
57         }
58
59         assertEquals(buffer.readableBytes(), 0);
60     }
61
62     protected MatchSerializer getSerializer() {
63         return serializer;
64     }
65
66     protected abstract int getOxmFieldCode();
67
68     protected abstract int getOxmClassCode();
69
70     protected abstract short getLength();
71
72 }