Fix checkstyle
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / ArpShaCodecTest.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.NxmNxArpSha;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.sha.grouping.ArpShaValuesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpShaCaseValue;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpShaCaseValueBuilder;
26
27 public class ArpShaCodecTest {
28
29     ArpShaCodec arpShaCodec;
30     ByteBuf buffer;
31     MatchEntry input;
32
33     private static final int VALUE_LENGTH = 6;
34     private static final int NXM_FIELD_CODE = 17;
35
36     private static byte[] resAddress = new byte[VALUE_LENGTH];
37     private static final MacAddress RESULT_ADDRESS = new MacAddress(ByteBufUtils.macAddressToString(resAddress));
38
39
40     @Before
41     public void setUp() {
42         arpShaCodec = new ArpShaCodec();
43         buffer = ByteBufAllocator.DEFAULT.buffer();
44     }
45
46
47     @Test
48     public void serializeTest() {
49         input = createMatchEntry();
50         arpShaCodec.serialize(input, buffer);
51
52         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
53         short fieldMask = buffer.readUnsignedByte();
54         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
55         assertEquals(0, fieldMask & 1);
56         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
57         assertEquals(RESULT_ADDRESS, ByteBufUtils.readIetfMacAddress(buffer));
58     }
59
60     @Test
61     public void deserializeTest() {
62         createBuffer(buffer);
63
64         input = arpShaCodec.deserialize(buffer);
65
66         final ArpShaCaseValue result = (ArpShaCaseValue) input.getMatchEntryValue();
67
68         assertEquals(Nxm1Class.class, input.getOxmClass());
69         assertEquals(NxmNxArpSha.class, input.getOxmMatchField());
70         assertEquals(false, input.isHasMask());
71         assertEquals(RESULT_ADDRESS, result.getArpShaValues().getMacAddress());
72     }
73
74
75     private MatchEntry createMatchEntry() {
76         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
77         final ArpShaCaseValueBuilder caseBuilder = new ArpShaCaseValueBuilder();
78         final ArpShaValuesBuilder valuesBuilder = new ArpShaValuesBuilder();
79
80         matchEntryBuilder.setOxmClass(Nxm1Class.class);
81         matchEntryBuilder.setOxmMatchField(NxmNxArpSha.class);
82         matchEntryBuilder.setHasMask(false);
83
84
85         byte[] address = new byte[VALUE_LENGTH];
86
87         valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
88
89         caseBuilder.setArpShaValues(valuesBuilder.build());
90         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
91         return matchEntryBuilder.build();
92     }
93
94     private void createBuffer(ByteBuf message) {
95         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
96
97         int fieldMask = NXM_FIELD_CODE << 1;
98         message.writeByte(fieldMask);
99         message.writeByte(VALUE_LENGTH);
100         message.writeBytes(resAddress);
101     }
102 }