Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmEthTypeSerializerTest.java
1 /*
2  * Copyright (c) 2014 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.match;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.PooledByteBufAllocator;
15
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
25
26 /**
27  * @author michal.polkorab
28  *
29  */
30 public class OxmEthTypeSerializerTest {
31
32     OxmEthTypeSerializer serializer = new OxmEthTypeSerializer();
33
34     /**
35      * Test correct serialization
36      */
37     @Test
38     public void testSerialize() {
39         MatchEntriesBuilder builder = prepareEthTypeMatchEntry(65535);
40         
41         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
42         serializer.serialize(builder.build(), buffer);
43
44         checkHeader(buffer, false);
45         assertEquals("Wrong value", 65535, buffer.readUnsignedShort());
46         assertTrue("Unexpected data", buffer.readableBytes() == 0);
47     }
48
49     /**
50      * Test correct header serialization
51      */
52     @Test
53     public void testSerializeHeader() {
54         MatchEntriesBuilder builder = prepareEthTypeHeader(false);
55         
56         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
57         serializer.serializeHeader(builder.build(), buffer);
58
59         checkHeader(buffer, false);
60         assertTrue("Unexpected data", buffer.readableBytes() == 0);
61     }
62
63     /**
64      * Test correct oxm-class return value
65      */
66     @Test
67     public void testGetOxmClassCode() {
68         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
69     }
70
71     /**
72      * Test correct oxm-field return value
73      */
74     @Test
75     public void getOxmFieldCode() {
76         assertEquals("Wrong oxm-class", OxmMatchConstants.ETH_TYPE, serializer.getOxmFieldCode());
77     }
78
79     /**
80      * Test correct value length return value
81      */
82     @Test
83     public void testGetValueLength() {
84         assertEquals("Wrong value length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, serializer.getValueLength());
85     }
86
87     
88     private static MatchEntriesBuilder prepareEthTypeMatchEntry(int type) {
89         MatchEntriesBuilder builder = prepareEthTypeHeader(false);
90         EthTypeMatchEntryBuilder typeBuilder = new EthTypeMatchEntryBuilder();
91         typeBuilder.setEthType(new EtherType(type));
92         builder.addAugmentation(EthTypeMatchEntry.class, typeBuilder.build());
93         return builder;
94     }
95
96     private static MatchEntriesBuilder prepareEthTypeHeader(boolean hasMask) {
97         MatchEntriesBuilder builder = new MatchEntriesBuilder();
98         builder.setOxmClass(OpenflowBasicClass.class);
99         builder.setOxmMatchField(EthType.class);
100         builder.setHasMask(hasMask);
101         return builder;
102     }
103
104     private static void checkHeader(ByteBuf buffer, boolean hasMask) {
105         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
106         short fieldAndMask = buffer.readUnsignedByte();
107         assertEquals("Wrong oxm-field", OxmMatchConstants.ETH_TYPE, fieldAndMask >>> 1);
108         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
109         assertEquals("Wrong length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, buffer.readUnsignedByte());
110     }
111 }