2 * Copyright (c) 2017 Red Hat, 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.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm0Class;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfMetadata;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.oxm.of.metadata.grouping.MetadataValuesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.OfMetadataCaseValueBuilder;
24 public class MetadataCodecTest {
26 MetadataCodec metadataCodec;
30 private static final int NXM_FIELD_CODE = 2;
31 private static final int VALUE_LENGTH = 8;
35 metadataCodec = new MetadataCodec();
36 buffer = ByteBufAllocator.DEFAULT.buffer();
40 public void serializeTest() {
41 input = createMatchEntry();
42 metadataCodec.serialize(input, buffer);
44 assertEquals(OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
45 short fieldMask = buffer.readUnsignedByte();
46 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
47 assertEquals(0, fieldMask & 1);
48 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
52 public void deserializeTest() {
54 input = metadataCodec.deserialize(buffer);
55 assertEquals(Nxm0Class.class, input.getOxmClass());
56 assertEquals(NxmOfMetadata.class, input.getOxmMatchField());
57 assertEquals(false, input.isHasMask());
60 private static MatchEntry createMatchEntry() {
61 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
62 matchEntryBuilder.setOxmClass(Nxm0Class.class);
63 matchEntryBuilder.setOxmMatchField(NxmOfMetadata.class);
64 matchEntryBuilder.setHasMask(false);
66 OfMetadataCaseValueBuilder caseBuilder = new OfMetadataCaseValueBuilder();
67 MetadataValuesBuilder valuesBuilder = new MetadataValuesBuilder();
69 caseBuilder.setMetadataValues(valuesBuilder.build());
70 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
71 return matchEntryBuilder.build();
74 private static void createBuffer(ByteBuf message) {
75 message.writeShort(OxmMatchConstants.NXM_1_CLASS);
76 int fieldMask = NXM_FIELD_CODE << 1;
77 message.writeByte(fieldMask);
78 message.writeByte(VALUE_LENGTH);