Migrate openflow-protocol-impl tests to Uint types
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10PacketInMessageFactoryTest.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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.openflowjava.protocol.impl.serialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
20 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
21 import org.opendaylight.openflowjava.util.ByteBufUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
25 import org.opendaylight.yangtools.yang.common.Uint16;
26 import org.opendaylight.yangtools.yang.common.Uint32;
27
28 /**
29  * Unit tests for OF10PacketInMessageFactory.
30  *
31  * @author giuseppex.petralia@intel.com
32  */
33 public class OF10PacketInMessageFactoryTest {
34     private OFSerializer<PacketInMessage> factory;
35     private static final byte MESSAGE_TYPE = 10;
36
37     @Before
38     public void startUp() {
39         SerializerRegistry registry = new SerializerRegistryImpl();
40         registry.init();
41         factory = registry.getSerializer(new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, PacketInMessage.class));
42     }
43
44     @Test
45     public void testSerialize() throws Exception {
46         PacketInMessageBuilder builder = new PacketInMessageBuilder();
47         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
48         builder.setBufferId(Uint32.ONE);
49         builder.setTotalLen(Uint16.ONE);
50         builder.setInPort(Uint16.ONE);
51         builder.setReason(PacketInReason.forValue(0));
52         byte[] data = ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14");
53         builder.setData(data);
54         PacketInMessage message = builder.build();
55
56         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
57         factory.serialize(message, serializedBuffer);
58         BufferHelper.checkHeaderV10(serializedBuffer, MESSAGE_TYPE, 34);
59         Assert.assertEquals("Wrong buffer id", message.getBufferId().longValue(), serializedBuffer.readUnsignedInt());
60         Assert.assertEquals("Wrong total len", message.getTotalLen().intValue(), serializedBuffer.readUnsignedShort());
61         Assert.assertEquals("Wrong port in", message.getInPort().intValue(), serializedBuffer.readUnsignedShort());
62         Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
63         serializedBuffer.skipBytes(1);
64         byte[] readData = new byte[serializedBuffer.readableBytes()];
65         serializedBuffer.readBytes(readData);
66         Assert.assertArrayEquals("Wrong data", message.getData(), readData);
67     }
68 }