2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.nx.codec.match;
11 import static org.junit.Assert.assertEquals;
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.rev130715.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;
29 public class EthSrcCodecTest {
31 EthSrcCodec ethSrcCodec;
35 private static final int VALUE_LENGTH = 6;
36 private static final int NXM_FIELD_CODE = 2;
38 private static final byte[] TEST_ADDR = new byte[VALUE_LENGTH];
39 private static final MacAddress TEST_ADDRESS = new MacAddress(ByteBufUtils.macAddressToString(TEST_ADDR));
43 ethSrcCodec = new EthSrcCodec();
44 buffer = ByteBufAllocator.DEFAULT.buffer();
48 public void serializeTest() {
49 input = createMatchEntry();
50 ethSrcCodec.serialize(input, buffer);
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(TEST_ADDRESS, ByteBufUtils.readIetfMacAddress(buffer));
61 public void deserializeTest() {
64 input = ethSrcCodec.deserialize(buffer);
66 final EthSrcCaseValue result = (EthSrcCaseValue) input.getMatchEntryValue();
68 assertEquals(Nxm0Class.class, input.getOxmClass());
69 assertEquals(NxmOfEthSrc.class, input.getOxmMatchField());
70 assertEquals(false, input.isHasMask());
71 assertEquals(TEST_ADDRESS, result.getEthSrcValues().getMacAddress());
76 private MatchEntry createMatchEntry() {
77 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
78 final EthSrcCaseValueBuilder caseBuilder = new EthSrcCaseValueBuilder();
79 final EthSrcValuesBuilder valuesBuilder = new EthSrcValuesBuilder();
81 matchEntryBuilder.setOxmClass(Nxm0Class.class);
82 matchEntryBuilder.setOxmMatchField(NxmOfEthDst.class);
83 matchEntryBuilder.setHasMask(false);
86 byte[] address = new byte[VALUE_LENGTH];
88 valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
90 caseBuilder.setEthSrcValues(valuesBuilder.build());
91 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
92 return matchEntryBuilder.build();
95 private void createBuffer(ByteBuf message) {
96 message.writeShort(OxmMatchConstants.NXM_0_CLASS);
98 int fieldMask = NXM_FIELD_CODE << 1;
99 message.writeByte(fieldMask);
100 message.writeByte(VALUE_LENGTH);
101 message.writeBytes(TEST_ADDR);