Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmMplsBosSerializerTest.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.BosMatchEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.BosMatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MplsBos;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
24
25 /**
26  * @author michal.polkorab
27  *
28  */
29 public class OxmMplsBosSerializerTest {
30
31     OxmMplsBosSerializer serializer = new OxmMplsBosSerializer();
32
33     /**
34      * Test correct serialization
35      */
36     @Test
37     public void testSerialize() {
38         MatchEntriesBuilder builder = prepareMplsBosMatchEntry(true);
39         
40         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
41         serializer.serialize(builder.build(), buffer);
42
43         checkHeader(buffer, false);
44         assertEquals("Wrong value", 1, buffer.readUnsignedByte());
45         assertTrue("Unexpected data", buffer.readableBytes() == 0);
46     }
47
48     /**
49      * Test correct header serialization
50      */
51     @Test
52     public void testSerializeHeader() {
53         MatchEntriesBuilder builder = prepareMplsBosHeader(false);
54         
55         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
56         serializer.serializeHeader(builder.build(), buffer);
57
58         checkHeader(buffer, false);
59         assertTrue("Unexpected data", buffer.readableBytes() == 0);
60     }
61
62     /**
63      * Test correct oxm-class return value
64      */
65     @Test
66     public void testGetOxmClassCode() {
67         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
68     }
69
70     /**
71      * Test correct oxm-field return value
72      */
73     @Test
74     public void getOxmFieldCode() {
75         assertEquals("Wrong oxm-class", OxmMatchConstants.MPLS_BOS, serializer.getOxmFieldCode());
76     }
77
78     /**
79      * Test correct value length return value
80      */
81     @Test
82     public void testGetValueLength() {
83         assertEquals("Wrong value length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, serializer.getValueLength());
84     }
85
86     
87     private static MatchEntriesBuilder prepareMplsBosMatchEntry(boolean bos) {
88         MatchEntriesBuilder builder = prepareMplsBosHeader(false);
89         BosMatchEntryBuilder bosBuilder = new BosMatchEntryBuilder();
90         bosBuilder.setBos(bos);
91         builder.addAugmentation(BosMatchEntry.class, bosBuilder.build());
92         return builder;
93     }
94
95     private static MatchEntriesBuilder prepareMplsBosHeader(boolean hasMask) {
96         MatchEntriesBuilder builder = new MatchEntriesBuilder();
97         builder.setOxmClass(OpenflowBasicClass.class);
98         builder.setOxmMatchField(MplsBos.class);
99         builder.setHasMask(hasMask);
100         return builder;
101     }
102
103     private static void checkHeader(ByteBuf buffer, boolean hasMask) {
104         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
105         short fieldAndMask = buffer.readUnsignedByte();
106         assertEquals("Wrong oxm-field", OxmMatchConstants.MPLS_BOS, fieldAndMask >>> 1);
107         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
108         assertEquals("Wrong length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, buffer.readUnsignedByte());
109     }
110 }