Bug 1277 - Move ByteBuffUtils to separate bundle
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10PortModInputMessageFactoryTest.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.ByteBufUtils;
23 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
30
31 /**
32  * @author michal.polkorab
33  *
34  */
35 public class OF10PortModInputMessageFactoryTest {
36
37     private SerializerRegistry registry;
38     private OFSerializer<PortModInput> portModFactory;
39
40     /**
41      * Initializes serializer registry and stores correct factory in field
42      */
43     @Before
44     public void startUp() {
45         registry = new SerializerRegistryImpl();
46         registry.init();
47         portModFactory = registry.getSerializer(
48                 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, PortModInput.class));
49     }
50
51     /**
52      * Testing of {@link OF10PortModInputMessageFactory} for correct translation from POJO
53      * @throws Exception 
54      */
55     @Test
56     public void testPortModInput() throws Exception {
57         PortModInputBuilder builder = new PortModInputBuilder();
58         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
59         builder.setPortNo(new PortNumber(6633L));
60         builder.setHwAddress(new MacAddress("08:00:27:00:B0:EB"));
61         builder.setConfigV10(new PortConfigV10(true, false, false, true, false, false, true));
62         builder.setMaskV10(new PortConfigV10(false, true, true, false, false, true, false));
63         builder.setAdvertiseV10(new PortFeaturesV10(true, true, false, false, false, false,
64                 false, true, true, false, false, false));
65         PortModInput message = builder.build();
66         
67         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
68         portModFactory.serialize(message, out);
69         
70         BufferHelper.checkHeaderV10(out, (byte) 15, 32);
71         Assert.assertEquals("Wrong PortNo", message.getPortNo().getValue().longValue(), out.readUnsignedShort());
72         byte[] address = new byte[6];
73         out.readBytes(address);
74         Assert.assertEquals("Wrong MacAddress", message.getHwAddress(),
75                 new MacAddress(ByteBufUtils.macAddressToString(address)));
76         Assert.assertEquals("Wrong config", 21, out.readUnsignedInt());
77         Assert.assertEquals("Wrong mask", 98, out.readUnsignedInt());
78         Assert.assertEquals("Wrong advertise", 652, out.readUnsignedInt());
79         out.skipBytes(4);
80     }
81
82 }