Merge "Update docs header to Magnesium in the master"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / EthDstCodecTest.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.ofj.nxm.of.match.eth.dst.grouping.EthDstValuesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.EthDstCaseValue;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.EthDstCaseValueBuilder;
26
27 public class EthDstCodecTest {
28
29     EthDstCodec ethDstCodec;
30     ByteBuf buffer;
31     MatchEntry input;
32
33     private static final int VALUE_LENGTH = 6;
34     private static final int NXM_FIELD_CODE = 1;
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         ethDstCodec = new EthDstCodec();
42         buffer = ByteBufAllocator.DEFAULT.buffer();
43     }
44
45     @Test
46     public void serializeTest() {
47         input = createMatchEntry();
48         ethDstCodec.serialize(input, buffer);
49
50         assertEquals(OxmMatchConstants.NXM_0_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
62         input = ethDstCodec.deserialize(buffer);
63
64         final EthDstCaseValue result = (EthDstCaseValue) input.getMatchEntryValue();
65
66         assertEquals(Nxm0Class.class, input.getOxmClass());
67         assertEquals(NxmOfEthDst.class, input.getOxmMatchField());
68         assertEquals(false, input.isHasMask());
69         assertEquals(TEST_ADDRESS, result.getEthDstValues().getMacAddress());
70     }
71
72
73
74     private MatchEntry createMatchEntry() {
75         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
76         final EthDstCaseValueBuilder caseBuilder = new EthDstCaseValueBuilder();
77         final EthDstValuesBuilder valuesBuilder = new EthDstValuesBuilder();
78
79         matchEntryBuilder.setOxmClass(Nxm0Class.class);
80         matchEntryBuilder.setOxmMatchField(NxmOfEthDst.class);
81         matchEntryBuilder.setHasMask(false);
82
83         byte[] address = new byte[VALUE_LENGTH];
84
85         valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
86
87         caseBuilder.setEthDstValues(valuesBuilder.build());
88         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
89         return matchEntryBuilder.build();
90     }
91
92     private void createBuffer(ByteBuf message) {
93         message.writeShort(OxmMatchConstants.NXM_0_CLASS);
94
95         int fieldMask = NXM_FIELD_CODE << 1;
96         message.writeByte(fieldMask);
97         message.writeByte(VALUE_LENGTH);
98         message.writeBytes(TEST_ADDR);
99     }
100 }