Extend openflow-protocol-impl serialization
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / GetFeaturesOutputFactoryTest.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.Capabilities;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
25
26 public class GetFeaturesOutputFactoryTest {
27     private OFSerializer<GetFeaturesOutput> factory;
28     private static final byte MESSAGE_TYPE = 6;
29     private static final byte PADDING = 2;
30
31     @Before
32     public void startUp() {
33         SerializerRegistry registry = new SerializerRegistryImpl();
34         registry.init();
35         factory = registry
36                 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, GetFeaturesOutput.class));
37     }
38
39     @Test
40     public void testSerialize() throws Exception {
41         GetFeaturesOutputBuilder builder = new GetFeaturesOutputBuilder();
42         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
43         builder.setDatapathId(BigInteger.valueOf(1234L));
44         builder.setBuffers(1234L);
45         builder.setTables((short) 12);
46         builder.setAuxiliaryId((short) 12);
47         builder.setCapabilities(new Capabilities(true, false, true, false, true, false, true));
48         builder.setReserved(1234L);
49         GetFeaturesOutput message = builder.build();
50
51         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
52         factory.serialize(message, serializedBuffer);
53
54         BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 32);
55         Assert.assertEquals("Wrong DatapathId", message.getDatapathId().longValue(), serializedBuffer.readLong());
56         Assert.assertEquals("Wrong Buffer ID", message.getBuffers().longValue(), serializedBuffer.readInt());
57         Assert.assertEquals("Wrong tables", message.getTables().shortValue(), serializedBuffer.readUnsignedByte());
58         Assert.assertEquals("Wrong auxiliary ID", message.getAuxiliaryId().shortValue(),
59                 serializedBuffer.readUnsignedByte());
60         serializedBuffer.skipBytes(PADDING);
61         Assert.assertEquals("Wrong Capabilities", message.getCapabilities(),
62                 createCapabilities(serializedBuffer.readInt()));
63         Assert.assertEquals("Wrong reserved", message.getReserved().longValue(), serializedBuffer.readInt());
64     }
65
66     private static Capabilities createCapabilities(int input) {
67         final Boolean one = (input & (1 << 0)) > 0;
68         final Boolean two = (input & (1 << 1)) > 0;
69         final Boolean three = (input & (1 << 2)) > 0;
70         final Boolean four = (input & (1 << 3)) > 0;
71         final Boolean five = (input & (1 << 5)) > 0;
72         final Boolean six = (input & (1 << 6)) > 0;
73         final Boolean seven = (input & (1 << 8)) > 0;
74         return new Capabilities(one, four, five, seven, three, six, two);
75     }
76 }