2 * Copyright (c) 2013 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.openflowjava.protocol.impl.serialization.factories;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
14 import java.math.BigInteger;
16 import junit.framework.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
23 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
24 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
25 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
31 * @author timotej.kubas
32 * @author michal.polkorab
34 public class RoleRequestInputMessageFactoryTest {
35 private static final byte MESSAGE_TYPE = 24;
36 private static final int MESSAGE_LENGTH = 24;
37 private static final byte PADDING_IN_ROLE_REQUEST_MESSAGE = 4;
38 private SerializerRegistry registry;
39 private OFSerializer<RoleRequestInput> roleFactory;
42 * Initializes serializer registry and stores correct factory in field
45 public void startUp() {
46 registry = new SerializerRegistryImpl();
48 roleFactory = registry.getSerializer(
49 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, RoleRequestInput.class));
53 * Testing of {@link RoleRequestInputMessageFactory} for correct translation from POJO
57 public void testRoleRequestInputMessage() throws Exception {
58 RoleRequestInputBuilder builder = new RoleRequestInputBuilder();
59 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
60 builder.setRole(ControllerRole.forValue(2));
61 byte[] generationId = new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
62 builder.setGenerationId(new BigInteger(1, generationId));
63 RoleRequestInput message = builder.build();
65 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
66 roleFactory.serialize(message, out);
68 BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, MESSAGE_LENGTH);
69 Assert.assertEquals("Wrong role", message.getRole().getIntValue(), ControllerRole.forValue((int) out.readUnsignedInt()).getIntValue());
70 out.skipBytes(PADDING_IN_ROLE_REQUEST_MESSAGE);
71 byte[] genId = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
73 Assert.assertEquals("Wrong generation ID", message.getGenerationId(), new BigInteger(1, genId));