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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
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.NxmOfTcpDst;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.tcp.dst.grouping.TcpDstValuesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpDstCaseValue;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpDstCaseValueBuilder;
27 public class TcpDstCodecTest {
29 TcpDstCodec tcpDstCodec;
33 private static final int VALUE_LENGTH = 4;
34 private static final int NXM_FIELD_CODE = 10;
38 tcpDstCodec = new TcpDstCodec();
39 buffer = ByteBufAllocator.DEFAULT.buffer();
43 public void serializeTest() {
44 input = createMatchEntry();
45 tcpDstCodec.serialize(input, buffer);
47 assertEquals(OxmMatchConstants.NXM_0_CLASS, buffer.readUnsignedShort());
48 short fieldMask = buffer.readUnsignedByte();
49 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
50 assertEquals(1, fieldMask & 1);
51 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
52 assertEquals(1, buffer.readUnsignedShort());
53 assertEquals(0xffff, buffer.readUnsignedShort());
57 public void deserializeTest() {
60 input = tcpDstCodec.deserialize(buffer);
62 TcpDstCaseValue result = ((TcpDstCaseValue) input.getMatchEntryValue());
64 assertEquals(Nxm0Class.class, input.getOxmClass());
65 assertEquals(NxmOfTcpDst.class, input.getOxmMatchField());
66 assertEquals(true, input.isHasMask());
67 assertEquals(1, result.getTcpDstValues().getPort().getValue().shortValue());
68 assertEquals(0xffff, result.getTcpDstValues().getMask().shortValue() & 0xffff);
72 private MatchEntry createMatchEntry() {
73 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
74 TcpDstCaseValueBuilder caseBuilder = new TcpDstCaseValueBuilder();
75 TcpDstValuesBuilder valuesBuilder = new TcpDstValuesBuilder();
77 matchEntryBuilder.setOxmClass(Nxm0Class.class);
78 matchEntryBuilder.setOxmMatchField(NxmOfTcpDst.class);
79 matchEntryBuilder.setHasMask(true);
81 valuesBuilder.setPort(new PortNumber(1));
82 valuesBuilder.setMask(0xffff);
84 caseBuilder.setTcpDstValues(valuesBuilder.build());
85 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
86 return matchEntryBuilder.build();
89 private void createBuffer(ByteBuf message) {
90 message.writeShort(OxmMatchConstants.NXM_0_CLASS);
92 int fieldMask = (NXM_FIELD_CODE << 1);
93 message.writeByte(fieldMask);
94 message.writeByte(VALUE_LENGTH);
96 message.writeShort(1);
97 message.writeShort(0xffff);