bd41db6ee10433015dceddbff8fcdcba8646e9f8
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / SetConfigMessageFactoryTest.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
14 import org.junit.Assert;
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.api.util.EncodeConstants;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInputBuilder;
26
27 /**
28  * @author timotej.kubas
29  * @author michal.polkorab
30  */
31 public class SetConfigMessageFactoryTest {
32     private static final byte MESSAGE_TYPE = 9;
33     private static final int MESSAGE_LENGTH = 12;
34     private SerializerRegistry registry;
35     private OFSerializer<SetConfigInput> setConfigFactory;
36
37     /**
38      * Initializes serializer registry and stores correct factory in field
39      */
40     @Before
41     public void startUp() {
42         registry = new SerializerRegistryImpl();
43         registry.init();
44         setConfigFactory = registry.getSerializer(
45                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, SetConfigInput.class));
46     }
47
48     /**
49      * Testing of {@link SetConfigMessageFactory} for correct translation from POJO
50      * @throws Exception 
51      */
52     @Test
53     public void testSetConfigMessageV13() throws Exception {
54         SetConfigInputBuilder builder = new SetConfigInputBuilder();
55         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
56         SwitchConfigFlag flag = SwitchConfigFlag.FRAGNORMAL;
57         builder.setFlags(flag);
58         builder.setMissSendLen(10);
59         SetConfigInput message = builder.build();
60         
61         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
62         setConfigFactory.serialize(message, out);
63         
64         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, MESSAGE_LENGTH);
65         Assert.assertEquals("Wrong flags", SwitchConfigFlag.FRAGNORMAL.getIntValue(), out.readUnsignedShort());
66         Assert.assertEquals("Wrong missSendLen", 10, out.readUnsignedShort());
67     }
68     
69     /**
70      * Testing of {@link SetConfigMessageFactory} for correct translation from POJO
71      * @throws Exception 
72      */
73     @Test
74     public void testSetConfigMessageV10() throws Exception {
75         SetConfigInputBuilder builder = new SetConfigInputBuilder();
76         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
77         SwitchConfigFlag flag = SwitchConfigFlag.OFPCFRAGDROP;
78         builder.setFlags(flag);
79         builder.setMissSendLen(85);
80         SetConfigInput message = builder.build();
81         
82         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
83         setConfigFactory.serialize(message, out);
84         
85         BufferHelper.checkHeaderV10(out, MESSAGE_TYPE, MESSAGE_LENGTH);
86         Assert.assertEquals("Wrong flags", SwitchConfigFlag.OFPCFRAGDROP.getIntValue(), out.readUnsignedShort());
87         Assert.assertEquals("Wrong missSendLen", 85, out.readUnsignedShort());
88     }
89 }