c17a42e0795cb02cf6d11c9c29fc79bb052943d8
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / instructions / AbstractInstructionSerializerTest.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.instructions;
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.openflowjava.protocol.impl.util.InstructionConstants;
18 import org.opendaylight.openflowplugin.impl.protocol.serialization.AbstractSerializerTest;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction;
20
21 public abstract class AbstractInstructionSerializerTest extends AbstractSerializerTest {
22     private AbstractInstructionSerializer serializer;
23
24     @Override
25     protected void init() {
26         serializer = getRegistry().getSerializer(new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, getClazz()));
27     }
28
29     protected void assertInstruction(final Instruction instruction, final Consumer<ByteBuf> assertBody) {
30         // Header serialization
31         final ByteBuf bufferHeader = UnpooledByteBufAllocator.DEFAULT.buffer();
32         serializer.serializeHeader(instruction, bufferHeader);
33         assertEquals(bufferHeader.readUnsignedShort(), getType());
34         assertEquals(bufferHeader.readUnsignedShort(), InstructionConstants.INSTRUCTION_IDS_LENGTH);
35         assertEquals(bufferHeader.readableBytes(), 0);
36
37         // Header and body serialization
38         final ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
39         serializer.serialize(instruction, buffer);
40         assertEquals(buffer.readUnsignedShort(), getType());
41         assertEquals(buffer.readUnsignedShort(), getLength());
42         assertBody.accept(buffer);
43         assertEquals(buffer.readableBytes(), 0);
44     }
45
46     protected AbstractInstructionSerializer getSerializer() {
47         return serializer;
48     }
49
50     protected abstract Class<? extends Instruction> getClazz();
51
52     protected abstract int getType();
53
54     protected abstract int getLength();
55 }