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 java.math.BigInteger;
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.Nxm1Class;
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.NxmNxTunId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.TunIdCaseValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.TunIdCaseValueBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.tun.id.grouping.TunIdValuesBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint64;
26 public class TunIdCodecTest {
27 private final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
28 private final TunIdCodec tunIdCodec = new TunIdCodec();
30 private MatchEntry input;
32 private static final int VALUE_LENGTH = 8;
33 private static final int NXM_FIELD_CODE = 16;
36 public void serializeTest() {
37 input = createMatchEntry();
38 tunIdCodec.serialize(input, buffer);
40 assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
41 short fieldMask = buffer.readUnsignedByte();
42 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
43 assertEquals(0, fieldMask & 1);
44 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
45 assertEquals(0, buffer.readLong());
49 public void deserializeTest() {
52 input = tunIdCodec.deserialize(buffer);
54 final TunIdCaseValue result = (TunIdCaseValue) input.getMatchEntryValue();
56 assertEquals(Nxm1Class.class, input.getOxmClass());
57 assertEquals(NxmNxTunId.class, input.getOxmMatchField());
58 assertEquals(false, input.getHasMask());
59 assertEquals(0, result.getTunIdValues().getValue().longValue());
62 private static MatchEntry createMatchEntry() {
63 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
64 final TunIdCaseValueBuilder caseBuilder = new TunIdCaseValueBuilder();
65 final TunIdValuesBuilder valuesBuilder = new TunIdValuesBuilder();
67 matchEntryBuilder.setOxmClass(Nxm1Class.class);
68 matchEntryBuilder.setOxmMatchField(NxmNxTunId.class);
69 matchEntryBuilder.setHasMask(false);
71 valuesBuilder.setValue(Uint64.ZERO);
73 caseBuilder.setTunIdValues(valuesBuilder.build());
74 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
75 return matchEntryBuilder.build();
78 private static void createBuffer(final ByteBuf message) {
79 message.writeShort(OxmMatchConstants.NXM_1_CLASS);
81 int fieldMask = NXM_FIELD_CODE << 1;
82 message.writeByte(fieldMask);
83 message.writeByte(VALUE_LENGTH);
84 byte[] value = new byte[VALUE_LENGTH];
85 message.writeLong(new BigInteger(value).longValue());