Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmIpv6NdSllSerializerTest.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.Ipv6NdSll;
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 OxmIpv6NdSllSerializerTest {
34
35     OxmIpv6NdSllSerializer serializer = new OxmIpv6NdSllSerializer();
36
37     /**
38      * Test correct serialization
39      */
40     @Test
41     public void testSerialize() {
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 header serialization
56      */
57     @Test
58     public void testSerializeHeader() {
59         MatchEntriesBuilder builder = prepareHeader(false);
60         
61         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
62         serializer.serializeHeader(builder.build(), buffer);
63
64         checkHeader(buffer, false);
65         assertTrue("Unexpected data", buffer.readableBytes() == 0);
66     }
67
68     /**
69      * Test correct oxm-class return value
70      */
71     @Test
72     public void testGetOxmClassCode() {
73         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
74     }
75
76     /**
77      * Test correct oxm-field return value
78      */
79     @Test
80     public void getOxmFieldCode() {
81         assertEquals("Wrong oxm-class", OxmMatchConstants.IPV6_ND_SLL, serializer.getOxmFieldCode());
82     }
83
84     /**
85      * Test correct value length return value
86      */
87     @Test
88     public void testGetValueLength() {
89         assertEquals("Wrong value length", EncodeConstants.MAC_ADDRESS_LENGTH, serializer.getValueLength());
90     }
91
92     private static MatchEntriesBuilder prepareMatchEntry(boolean hasMask, String value) {
93         MatchEntriesBuilder builder = prepareHeader(hasMask);
94         if (hasMask) {
95             MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
96             maskBuilder.setMask(new byte[]{15, 15, 0, 0, 10, 10});
97             builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
98         }
99         MacAddressMatchEntryBuilder macBuilder = new MacAddressMatchEntryBuilder();
100         macBuilder.setMacAddress(new MacAddress(value));
101         builder.addAugmentation(MacAddressMatchEntry.class, macBuilder.build());
102         return builder;
103     }
104
105     private static MatchEntriesBuilder prepareHeader(boolean hasMask) {
106         MatchEntriesBuilder builder = new MatchEntriesBuilder();
107         builder.setOxmClass(OpenflowBasicClass.class);
108         builder.setOxmMatchField(Ipv6NdSll.class);
109         builder.setHasMask(hasMask);
110         return builder;
111     }
112
113     private static void checkHeader(ByteBuf buffer, boolean hasMask) {
114         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
115         short fieldAndMask = buffer.readUnsignedByte();
116         assertEquals("Wrong oxm-field", OxmMatchConstants.IPV6_ND_SLL, fieldAndMask >>> 1);
117         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
118         assertEquals("Wrong length", EncodeConstants.MAC_ADDRESS_LENGTH, buffer.readUnsignedByte());
119     }
120 }