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