Extend openflow-protocol-impl serialization
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / RoleReplyMessageFactoryTest.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 java.math.BigInteger;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
18 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
21 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutputBuilder;
25
26 /**
27  * @author giuseppex.petralia@intel.com
28  *
29  */
30 public class RoleReplyMessageFactoryTest {
31     private OFSerializer<RoleRequestOutput> factory;
32     private static final byte MESSAGE_TYPE = 25;
33     private static final byte PADDING = 4;
34
35     @Before
36     public void startUp() {
37         SerializerRegistry registry = new SerializerRegistryImpl();
38         registry.init();
39         factory = registry
40                 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, RoleRequestOutput.class));
41     }
42
43     @Test
44     public void testSerialize() throws Exception {
45         RoleRequestOutputBuilder builder = new RoleRequestOutputBuilder();
46         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
47         builder.setRole(ControllerRole.forValue(0));
48         builder.setGenerationId(BigInteger.valueOf(1L));
49         RoleRequestOutput message = builder.build();
50
51         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
52         factory.serialize(message, serializedBuffer);
53         BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 24);
54         Assert.assertEquals("Wrong role", message.getRole().getIntValue(),
55                 ControllerRole.forValue((int) serializedBuffer.readUnsignedInt()).getIntValue());
56         serializedBuffer.skipBytes(PADDING);
57         byte[] genId = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
58         serializedBuffer.readBytes(genId);
59         Assert.assertEquals("Wrong generation ID", message.getGenerationId(), new BigInteger(1, genId));
60     }
61 }