Merge "Bug 5543 - Bo: Update JUnit tests part_6"
[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
9 package org.opendaylight.openflowjava.nx.codec.match;
10
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufAllocator;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.openflowjava.util.ByteBufUtils;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm0Class;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfEthDst;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfEthSrc;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.eth.src.grouping.EthSrcValuesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.EthSrcCaseValue;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.EthSrcCaseValueBuilder;
28
29 public class EthSrcCodecTest {
30
31     EthSrcCodec ethSrcCodec;
32     ByteBuf buffer;
33     MatchEntry input;
34
35     private static final int VALUE_LENGTH = 6;
36     private static final int NXM_FIELD_CODE = 2;
37
38     private static final byte[] testAddr = new byte[VALUE_LENGTH];
39     private static final MacAddress testAddress = new MacAddress(ByteBufUtils.macAddressToString(testAddr));
40
41     @Before
42     public void setUp() {
43         ethSrcCodec = new EthSrcCodec();
44         buffer = ByteBufAllocator.DEFAULT.buffer();
45     }
46
47     @Test
48     public void serializeTest() {
49         input = createMatchEntry();
50         ethSrcCodec.serialize(input, buffer);
51
52         assertEquals(OxmMatchConstants.NXM_0_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(testAddress, ByteBufUtils.readIetfMacAddress(buffer));
58     }
59
60     @Test
61     public void deserializeTest() {
62         createBuffer(buffer);
63
64         input = ethSrcCodec.deserialize(buffer);
65
66         EthSrcCaseValue result = ((EthSrcCaseValue) input.getMatchEntryValue());
67
68         assertEquals(Nxm0Class.class, input.getOxmClass());
69         assertEquals(NxmOfEthSrc.class, input.getOxmMatchField());
70         assertEquals(false, input.isHasMask());
71         assertEquals(testAddress, result.getEthSrcValues().getMacAddress());
72     }
73
74
75
76     private MatchEntry createMatchEntry() {
77         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
78         EthSrcCaseValueBuilder caseBuilder = new EthSrcCaseValueBuilder();
79         EthSrcValuesBuilder valuesBuilder = new EthSrcValuesBuilder();
80
81         matchEntryBuilder.setOxmClass(Nxm0Class.class);
82         matchEntryBuilder.setOxmMatchField(NxmOfEthDst.class);
83         matchEntryBuilder.setHasMask(false);
84
85
86         byte[] address = new byte[VALUE_LENGTH];
87
88         valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
89
90         caseBuilder.setEthSrcValues(valuesBuilder.build());
91         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
92         return matchEntryBuilder.build();
93     }
94
95     private void createBuffer(ByteBuf message) {
96         message.writeShort(OxmMatchConstants.NXM_0_CLASS);
97
98         int fieldMask = (NXM_FIELD_CODE << 1);
99         message.writeByte(fieldMask);
100         message.writeByte(VALUE_LENGTH);
101         message.writeBytes(testAddr);
102     }
103
104
105 }