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.NxmOfTcpSrc;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.tcp.src.grouping.TcpSrcValuesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpSrcCaseValue;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpSrcCaseValueBuilder;
27 public class TcpSrcCodecTest {
29 TcpSrcCodec tcpSrcCodec;
33 private static final int VALUE_LENGTH = 4;
34 private static final int NXM_FIELD_CODE = 9;
38 tcpSrcCodec = new TcpSrcCodec();
39 buffer = ByteBufAllocator.DEFAULT.buffer();
43 public void serializeTest() {
44 input = createMatchEntry();
45 tcpSrcCodec.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(0, fieldMask & 1);
51 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
52 assertEquals(1, buffer.readUnsignedShort());
56 public void deserializeTest() {
59 input = tcpSrcCodec.deserialize(buffer);
61 TcpSrcCaseValue result = ((TcpSrcCaseValue) input.getMatchEntryValue());
63 assertEquals(Nxm0Class.class, input.getOxmClass());
64 assertEquals(NxmOfTcpSrc.class, input.getOxmMatchField());
65 assertEquals(false, input.isHasMask());
66 assertEquals(1, result.getTcpSrcValues().getPort().getValue().shortValue());
69 private MatchEntry createMatchEntry() {
70 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
71 TcpSrcCaseValueBuilder caseBuilder = new TcpSrcCaseValueBuilder();
72 TcpSrcValuesBuilder valuesBuilder = new TcpSrcValuesBuilder();
74 matchEntryBuilder.setOxmClass(Nxm0Class.class);
75 matchEntryBuilder.setOxmMatchField(NxmOfTcpSrc.class);
76 matchEntryBuilder.setHasMask(false);
78 valuesBuilder.setPort(new PortNumber(1));
80 caseBuilder.setTcpSrcValues(valuesBuilder.build());
81 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
82 return matchEntryBuilder.build();
85 private void createBuffer(ByteBuf message) {
86 message.writeShort(OxmMatchConstants.NXM_0_CLASS);
88 int fieldMask = (NXM_FIELD_CODE << 1);
89 message.writeByte(fieldMask);
90 message.writeByte(VALUE_LENGTH);
92 message.writeShort(1);