Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / TableModInputMessageFactoryTest.java
1 /*
2  * Copyright (c) 2013 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.openflowjava.protocol.impl.serialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
13 import junit.framework.Assert;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
21 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
22 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInputBuilder;
27
28 /**
29  * @author timotej.kubas
30  * @author michal.polkorab
31  */
32 public class TableModInputMessageFactoryTest {
33     private static final byte MESSAGE_TYPE = 17;
34     private static final byte PADDING_IN_TABLE_MOD_MESSAGE = 3;
35     private SerializerRegistry registry;
36     private OFSerializer<TableModInput> tableModFactory;
37
38     /**
39      * Initializes serializer registry and stores correct factory in field
40      */
41     @Before
42     public void startUp() {
43         registry = new SerializerRegistryImpl();
44         registry.init();
45         tableModFactory = registry.getSerializer(
46                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, TableModInput.class));
47     }
48
49     /**
50      * Testing of {@link TableModInputMessageFactory} for correct translation from POJO
51      * @throws Exception 
52      */
53     @Test
54     public void testTableModInput() throws Exception {
55         TableModInputBuilder builder = new TableModInputBuilder();
56         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
57         builder.setTableId(new TableId(9L));
58         builder.setConfig(new TableConfig(true));
59         TableModInput message = builder.build();
60         
61         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
62         tableModFactory.serialize(message, out);
63         
64         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 16);
65         Assert.assertEquals("Wrong TableID", message.getTableId().getValue().intValue(), out.readUnsignedByte());
66         out.skipBytes(PADDING_IN_TABLE_MOD_MESSAGE);
67         Assert.assertEquals("Wrong TableConfig", 8, out.readUnsignedInt());
68     }
69     
70 }