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
8 package org.opendaylight.openflowjava.nx.codec.match;
10 import static org.junit.Assert.assertEquals;
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;
27 public class EthDstCodecTest {
29 EthDstCodec ethDstCodec;
33 private static final int VALUE_LENGTH = 6;
34 private static final int NXM_FIELD_CODE = 1;
36 private static final byte[] TEST_ADDR = new byte[VALUE_LENGTH];
37 private static final MacAddress TEST_ADDRESS = new MacAddress(ByteBufUtils.macAddressToString(TEST_ADDR));
41 ethDstCodec = new EthDstCodec();
42 buffer = ByteBufAllocator.DEFAULT.buffer();
46 public void serializeTest() {
47 input = createMatchEntry();
48 ethDstCodec.serialize(input, buffer);
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));
59 public void deserializeTest() {
62 input = ethDstCodec.deserialize(buffer);
64 final EthDstCaseValue result = (EthDstCaseValue) input.getMatchEntryValue();
66 assertEquals(Nxm0Class.class, input.getOxmClass());
67 assertEquals(NxmOfEthDst.class, input.getOxmMatchField());
68 assertEquals(false, input.isHasMask());
69 assertEquals(TEST_ADDRESS, result.getEthDstValues().getMacAddress());
72 private static MatchEntry createMatchEntry() {
73 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
74 final EthDstCaseValueBuilder caseBuilder = new EthDstCaseValueBuilder();
75 final EthDstValuesBuilder valuesBuilder = new EthDstValuesBuilder();
77 matchEntryBuilder.setOxmClass(Nxm0Class.class);
78 matchEntryBuilder.setOxmMatchField(NxmOfEthDst.class);
79 matchEntryBuilder.setHasMask(false);
81 byte[] address = new byte[VALUE_LENGTH];
83 valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
85 caseBuilder.setEthDstValues(valuesBuilder.build());
86 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
87 return matchEntryBuilder.build();
90 private static void createBuffer(ByteBuf message) {
91 message.writeShort(OxmMatchConstants.NXM_0_CLASS);
93 int fieldMask = NXM_FIELD_CODE << 1;
94 message.writeByte(fieldMask);
95 message.writeByte(VALUE_LENGTH);
96 message.writeBytes(TEST_ADDR);