Fix checkstyle
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / ArpThaCodecTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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 package org.opendaylight.openflowjava.nx.codec.match;
9
10 import static org.junit.Assert.assertEquals;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufAllocator;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxArpTha;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.tha.grouping.ArpThaValuesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpThaCaseValue;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpThaCaseValueBuilder;
26
27 public class ArpThaCodecTest {
28
29     ArpThaCodec arpThaCodec;
30     ByteBuf buffer;
31     MatchEntry input;
32
33     private static final int VALUE_LENGTH = 6;
34     private static final int NXM_FIELD_CODE = 18;
35
36     private static final byte[] TEST_ADDR = new byte[VALUE_LENGTH];
37     private static final MacAddress TEST_ADDRESS = new MacAddress(ByteBufUtils.macAddressToString(TEST_ADDR));
38
39     @Before
40     public void setUp() {
41         arpThaCodec = new ArpThaCodec();
42         buffer = ByteBufAllocator.DEFAULT.buffer();
43     }
44
45     @Test
46     public void serializeTest() {
47         input = createMatchEntry();
48         arpThaCodec.serialize(input, buffer);
49
50         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
51         short fieldMask = buffer.readUnsignedByte();
52         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
53         assertEquals(0, fieldMask & 1);
54         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
55         assertEquals(TEST_ADDRESS, ByteBufUtils.readIetfMacAddress(buffer));
56     }
57
58     @Test
59     public void deserializeTest() {
60         createBuffer(buffer);
61         input = arpThaCodec.deserialize(buffer);
62
63         final ArpThaCaseValue result = (ArpThaCaseValue) input.getMatchEntryValue();
64
65         assertEquals(Nxm1Class.class, input.getOxmClass());
66         assertEquals(NxmNxArpTha.class, input.getOxmMatchField());
67         assertEquals(false, input.isHasMask());
68         assertEquals(TEST_ADDRESS, result.getArpThaValues().getMacAddress());
69     }
70
71     private MatchEntry createMatchEntry() {
72         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
73         final ArpThaCaseValueBuilder caseBuilder = new ArpThaCaseValueBuilder();
74         final ArpThaValuesBuilder valuesBuilder = new ArpThaValuesBuilder();
75
76         matchEntryBuilder.setOxmClass(Nxm1Class.class);
77         matchEntryBuilder.setOxmMatchField(NxmNxArpTha.class);
78         matchEntryBuilder.setHasMask(false);
79
80         byte[] address = new byte[VALUE_LENGTH];
81
82         valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
83
84         caseBuilder.setArpThaValues(valuesBuilder.build());
85         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
86         return matchEntryBuilder.build();
87     }
88
89     private void createBuffer(ByteBuf message) {
90         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
91
92         int fieldMask = NXM_FIELD_CODE << 1;
93         message.writeByte(fieldMask);
94         message.writeByte(VALUE_LENGTH);
95         message.writeBytes(TEST_ADDR);
96     }
97 }