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