Add Instruction deserializers
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / instruction / WriteMetadataInstructionDeserializerTest.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
9 package org.opendaylight.openflowplugin.impl.protocol.deserialization.instruction;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertArrayEquals;
13
14 import java.math.BigInteger;
15
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.impl.util.InstructionConstants;
19 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCase;
22
23 import io.netty.buffer.ByteBuf;
24 import io.netty.buffer.UnpooledByteBufAllocator;
25
26 public class WriteMetadataInstructionDeserializerTest extends AbstractInstructionDeserializerTest {
27
28     @Test
29     public void testDeserialize() throws Exception {
30         final ByteBuf in = UnpooledByteBufAllocator.DEFAULT.buffer();
31         final BigInteger metadata = BigInteger.valueOf(1234567890L);
32         final BigInteger metadataMask = BigInteger.valueOf(9876543210L);
33         writeHeader(in);
34         in.writeZero(InstructionConstants.PADDING_IN_WRITE_METADATA);
35         in.writeBytes(ByteUtil.convertBigIntegerToNBytes(metadata, EncodeConstants.SIZE_OF_LONG_IN_BYTES));
36         in.writeBytes(ByteUtil.convertBigIntegerToNBytes(metadataMask, EncodeConstants.SIZE_OF_LONG_IN_BYTES));
37
38         final Instruction instruction = deserializeInstruction(in);
39         assertEquals(WriteMetadataCase.class, instruction.getImplementedInterface());
40         assertArrayEquals(metadata.toByteArray(),
41                 WriteMetadataCase.class.cast(instruction).getWriteMetadata().getMetadata().toByteArray());
42         assertArrayEquals(metadataMask.toByteArray(),
43                 WriteMetadataCase.class.cast(instruction).getWriteMetadata().getMetadataMask().toByteArray());
44         assertEquals(0, in.readableBytes());
45     }
46
47     @Override
48     protected short getType() {
49         return InstructionConstants.WRITE_METADATA_TYPE;
50     }
51
52     @Override
53     protected short getLength() {
54         return InstructionConstants.STANDARD_INSTRUCTION_LENGTH;
55     }
56
57 }