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.instructions;
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.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;
21 public abstract class AbstractInstructionSerializerTest extends AbstractSerializerTest {
22 private AbstractInstructionSerializer serializer;
25 protected void init() {
26 serializer = getRegistry().getSerializer(new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, getClazz()));
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);
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);
46 protected AbstractInstructionSerializer getSerializer() {
50 protected abstract Class<? extends Instruction> getClazz();
52 protected abstract int getType();
54 protected abstract int getLength();