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 java.math.BigInteger;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
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.NxmNxTunId;
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.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TunIdCaseValue;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TunIdCaseValueBuilder;
27 public class TunIdCodecTest {
29 TunIdCodec tunIdCodec;
33 private static final int VALUE_LENGTH = 8;
34 private static final int NXM_FIELD_CODE = 16;
38 tunIdCodec = new TunIdCodec();
39 buffer = ByteBufAllocator.DEFAULT.buffer();
43 public void serializeTest() {
44 input = createMatchEntry();
45 tunIdCodec.serialize(input, buffer);
47 assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
48 short fieldMask = buffer.readUnsignedByte();
49 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
50 assertEquals(0, fieldMask & 1);
51 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
52 assertEquals(0, buffer.readLong());
56 public void deserializeTest() {
59 input = tunIdCodec.deserialize(buffer);
61 TunIdCaseValue result = ((TunIdCaseValue) input.getMatchEntryValue());
63 assertEquals(Nxm1Class.class, input.getOxmClass());
64 assertEquals(NxmNxTunId.class, input.getOxmMatchField());
65 assertEquals(false, input.isHasMask());
66 assertEquals(0, result.getTunIdValues().getValue().longValue());
71 private MatchEntry createMatchEntry() {
72 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
73 TunIdCaseValueBuilder caseBuilder = new TunIdCaseValueBuilder();
74 TunIdValuesBuilder valuesBuilder = new TunIdValuesBuilder();
76 matchEntryBuilder.setOxmClass(Nxm1Class.class);
77 matchEntryBuilder.setOxmMatchField(NxmNxTunId.class);
78 matchEntryBuilder.setHasMask(false);
80 byte[] value = new byte[VALUE_LENGTH];
81 valuesBuilder.setValue(new BigInteger(value));
83 caseBuilder.setTunIdValues(valuesBuilder.build());
84 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
85 return matchEntryBuilder.build();
88 private void createBuffer(ByteBuf message) {
89 message.writeShort(OxmMatchConstants.NXM_1_CLASS);
91 int fieldMask = (NXM_FIELD_CODE << 1);
92 message.writeByte(fieldMask);
93 message.writeByte(VALUE_LENGTH);
94 byte[] value = new byte[VALUE_LENGTH];
95 message.writeLong(new BigInteger(value).longValue());