Fix yangtools reference
[openflowplugin.git] / openflowjava / 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 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
20 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInputBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint16;
25
26 /**
27  * Unit tests for SetConfigMessageFactory.
28  *
29  * @author timotej.kubas
30  * @author michal.polkorab
31  */
32 public class SetConfigMessageFactoryTest {
33     private static final byte MESSAGE_TYPE = 9;
34     private static final int MESSAGE_LENGTH = 12;
35     private SerializerRegistry registry;
36     private OFSerializer<SetConfigInput> setConfigFactory;
37
38     /**
39      * Initializes serializer registry and stores correct factory in field.
40      */
41     @Before
42     public void startUp() {
43         registry = new SerializerRegistryImpl();
44         registry.init();
45         setConfigFactory = registry.getSerializer(
46                 new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, SetConfigInput.class));
47     }
48
49     /**
50      * Testing of {@link SetConfigMessageFactory} for correct translation from POJO.
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(Uint16.TEN);
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      */
72     @Test
73     public void testSetConfigMessageV10() throws Exception {
74         SetConfigInputBuilder builder = new SetConfigInputBuilder();
75         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
76         SwitchConfigFlag flag = SwitchConfigFlag.OFPCFRAGDROP;
77         builder.setFlags(flag);
78         builder.setMissSendLen(Uint16.valueOf(85));
79         SetConfigInput message = builder.build();
80
81         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
82         setConfigFactory.serialize(message, out);
83
84         BufferHelper.checkHeaderV10(out, MESSAGE_TYPE, MESSAGE_LENGTH);
85         Assert.assertEquals("Wrong flags", SwitchConfigFlag.OFPCFRAGDROP.getIntValue(), out.readUnsignedShort());
86         Assert.assertEquals("Wrong missSendLen", 85, out.readUnsignedShort());
87     }
88 }