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
9 package org.opendaylight.openflowplugin.impl.protocol.serialization.instructions;
11 import static org.junit.Assert.assertEquals;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.UnpooledByteBufAllocator;
15 import java.util.function.Consumer;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.impl.util.InstructionConstants;
19 import org.opendaylight.openflowplugin.impl.protocol.serialization.AbstractSerializerTest;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction;
22 public abstract class AbstractInstructionSerializerTest extends AbstractSerializerTest {
23 private AbstractInstructionSerializer serializer;
26 protected void init() {
27 serializer = getRegistry().getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, getClazz()));
30 protected void assertInstruction(final Instruction instruction, final Consumer<ByteBuf> assertBody) {
31 // Header serialization
32 final ByteBuf bufferHeader = UnpooledByteBufAllocator.DEFAULT.buffer();
33 serializer.serializeHeader(instruction, bufferHeader);
34 assertEquals(bufferHeader.readUnsignedShort(), getType());
35 assertEquals(bufferHeader.readUnsignedShort(), InstructionConstants.INSTRUCTION_IDS_LENGTH);
36 assertEquals(bufferHeader.readableBytes(), 0);
38 // Header and body serialization
39 final ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
40 serializer.serialize(instruction, buffer);
41 assertEquals(buffer.readUnsignedShort(), getType());
42 assertEquals(buffer.readUnsignedShort(), getLength());
43 assertBody.accept(buffer);
44 assertEquals(buffer.readableBytes(), 0);
47 protected AbstractInstructionSerializer getSerializer() {
51 protected abstract Class<? extends Instruction> getClazz();
53 protected abstract int getType();
55 protected abstract int getLength();