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