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.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm0Class;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfEthType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.eth.type.grouping.EthTypeValuesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.EthTypeCaseValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.EthTypeCaseValueBuilder;
26 public class EthTypeCodecTest {
28 EthTypeCodec ethTypeCodec;
32 private static final int VALUE_LENGTH = 2;
33 private static final int NXM_FIELD_CODE = 3;
37 ethTypeCodec = new EthTypeCodec();
38 buffer = ByteBufAllocator.DEFAULT.buffer();
42 public void serializeTest() {
43 input = createMatchEntry();
44 ethTypeCodec.serialize(input, buffer);
46 assertEquals(OxmMatchConstants.NXM_0_CLASS, buffer.readUnsignedShort());
47 short fieldMask = buffer.readUnsignedByte();
48 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
49 assertEquals(0, fieldMask & 1);
50 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
51 assertEquals(1, buffer.readUnsignedShort());
55 public void deserializeTest() {
58 input = ethTypeCodec.deserialize(buffer);
60 EthTypeCaseValue result = ((EthTypeCaseValue) input.getMatchEntryValue());
62 assertEquals(Nxm0Class.class, input.getOxmClass());
63 assertEquals(NxmOfEthType.class, input.getOxmMatchField());
64 assertEquals(false, input.isHasMask());
65 assertEquals(2, result.getEthTypeValues().getValue().shortValue());
70 private MatchEntry createMatchEntry() {
71 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
72 EthTypeCaseValueBuilder caseBuilder = new EthTypeCaseValueBuilder();
73 EthTypeValuesBuilder valuesBuilder = new EthTypeValuesBuilder();
75 matchEntryBuilder.setOxmClass(Nxm0Class.class);
76 matchEntryBuilder.setOxmMatchField(NxmOfEthType.class);
77 matchEntryBuilder.setHasMask(false);
79 valuesBuilder.setValue(1);
81 caseBuilder.setEthTypeValues(valuesBuilder.build());
82 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
83 return matchEntryBuilder.build();
86 private void createBuffer(ByteBuf message) {
87 message.writeShort(OxmMatchConstants.NXM_0_CLASS);
89 int fieldMask = (NXM_FIELD_CODE << 1);
90 message.writeByte(fieldMask);
91 message.writeByte(VALUE_LENGTH);
92 message.writeShort(2);