Bug 2756 - Match model update
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmPbbIsidSerializerTest.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.Assert;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.PbbIsid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.PbbIsidCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.pbb.isid._case.PbbIsidBuilder;
25
26 /**
27  * @author michal.polkorab
28  *
29  */
30 public class OxmPbbIsidSerializerTest {
31
32     OxmPbbIsidSerializer serializer = new OxmPbbIsidSerializer();
33
34     /**
35      * Test correct serialization
36      */
37     @Test
38     public void testSerializeWithMask() {
39         MatchEntryBuilder builder = preparePbbIsidMatchEntry(false, 12345);
40
41         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
42         serializer.serialize(builder.build(), buffer);
43
44         checkHeader(buffer, false);
45         assertEquals("Wrong value", 12345, buffer.readUnsignedMedium());
46         assertTrue("Unexpected data", buffer.readableBytes() == 0);
47     }
48
49     /**
50      * Test correct serialization
51      */
52     @Test
53     public void testSerializeWithoutMask() {
54         MatchEntryBuilder builder = preparePbbIsidMatchEntry(true, 6789);
55
56         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
57         serializer.serialize(builder.build(), buffer);
58
59         checkHeader(buffer, true);
60         assertEquals("Wrong value", 6789, buffer.readUnsignedMedium());
61         byte[] tmp = new byte[3];
62         buffer.readBytes(tmp);
63         Assert.assertArrayEquals("Wrong mask", new byte[]{0, 15, 10}, tmp);
64         assertTrue("Unexpected data", buffer.readableBytes() == 0);
65     }
66
67     /**
68      * Test correct header serialization
69      */
70     @Test
71     public void testSerializeHeaderWithoutMask() {
72         MatchEntryBuilder builder = preparePbbIsidHeader(false);
73
74         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
75         serializer.serializeHeader(builder.build(), buffer);
76
77         checkHeader(buffer, false);
78         assertTrue("Unexpected data", buffer.readableBytes() == 0);
79     }
80
81     /**
82      * Test correct header serialization
83      */
84     @Test
85     public void testSerializeHeaderWithMask() {
86         MatchEntryBuilder builder = preparePbbIsidHeader(true);
87
88         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
89         serializer.serializeHeader(builder.build(), buffer);
90
91         checkHeader(buffer, true);
92         assertTrue("Unexpected data", buffer.readableBytes() == 0);
93     }
94
95     /**
96      * Test correct oxm-class return value
97      */
98     @Test
99     public void testGetOxmClassCode() {
100         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
101     }
102
103     /**
104      * Test correct oxm-field return value
105      */
106     @Test
107     public void getOxmFieldCode() {
108         assertEquals("Wrong oxm-class", OxmMatchConstants.PBB_ISID, serializer.getOxmFieldCode());
109     }
110
111     /**
112      * Test correct value length return value
113      */
114     @Test
115     public void testGetValueLength() {
116         assertEquals("Wrong value length", EncodeConstants.SIZE_OF_3_BYTES, serializer.getValueLength());
117     }
118
119     private static MatchEntryBuilder preparePbbIsidMatchEntry(boolean hasMask, long value) {
120         MatchEntryBuilder builder = preparePbbIsidHeader(hasMask);
121         PbbIsidCaseBuilder casebuilder = new PbbIsidCaseBuilder();
122         PbbIsidBuilder valueBuilder = new PbbIsidBuilder();
123         if (hasMask) {
124             valueBuilder.setMask(new byte[]{0, 15, 10});
125         }
126         valueBuilder.setIsid(value);
127         casebuilder.setPbbIsid(valueBuilder.build());
128         builder.setMatchEntryValue(casebuilder.build());
129         return builder;
130     }
131
132     private static MatchEntryBuilder preparePbbIsidHeader(boolean hasMask) {
133         MatchEntryBuilder builder = new MatchEntryBuilder();
134         builder.setOxmClass(OpenflowBasicClass.class);
135         builder.setOxmMatchField(PbbIsid.class);
136         builder.setHasMask(hasMask);
137         return builder;
138     }
139
140     private static void checkHeader(ByteBuf buffer, boolean hasMask) {
141         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
142         short fieldAndMask = buffer.readUnsignedByte();
143         assertEquals("Wrong oxm-field", OxmMatchConstants.PBB_ISID, fieldAndMask >>> 1);
144         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
145         if (hasMask) {
146             assertEquals("Wrong length", 2 * EncodeConstants.SIZE_OF_3_BYTES, buffer.readUnsignedByte());
147         } else {
148             assertEquals("Wrong length", EncodeConstants.SIZE_OF_3_BYTES, buffer.readUnsignedByte());
149         }
150     }
151 }