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 org.junit.Test;
15 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxCtZone;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.CtZoneCaseValue;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.CtZoneCaseValueBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.ct.zone.grouping.CtZoneValuesBuilder;
23 import org.opendaylight.yangtools.yang.common.Uint16;
25 public class CtZoneCodecTest {
26 private final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
27 private final CtZoneCodec ctZoneCodec = new CtZoneCodec();
29 private MatchEntry input;
31 private static final int VALUE_LENGTH = 2;
32 private static final int NXM_FIELD_CODE = 106;
35 public void serializeTest() {
36 input = createMatchEntry();
37 ctZoneCodec.serialize(input, buffer);
39 assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
40 short fieldMask = buffer.readUnsignedByte();
41 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
42 assertEquals(0, fieldMask & 1);
43 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
44 assertEquals(1, buffer.readUnsignedShort());
48 public void deserializeTest() {
50 input = ctZoneCodec.deserialize(buffer);
52 final CtZoneCaseValue result = (CtZoneCaseValue) input.getMatchEntryValue();
54 assertEquals(Nxm1Class.VALUE, input.getOxmClass());
55 assertEquals(NxmNxCtZone.VALUE, input.getOxmMatchField());
56 assertEquals(false, input.getHasMask());
57 assertEquals(2, result.getCtZoneValues().getCtZone().shortValue());
60 private static MatchEntry createMatchEntry() {
61 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
62 final CtZoneCaseValueBuilder caseBuilder = new CtZoneCaseValueBuilder();
63 final CtZoneValuesBuilder valuesBuilder = new CtZoneValuesBuilder();
65 matchEntryBuilder.setOxmClass(Nxm1Class.VALUE);
66 matchEntryBuilder.setOxmMatchField(NxmNxCtZone.VALUE);
67 matchEntryBuilder.setHasMask(false);
69 valuesBuilder.setCtZone(Uint16.ONE);
71 caseBuilder.setCtZoneValues(valuesBuilder.build());
72 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
73 return matchEntryBuilder.build();
76 private static void createBuffer(final ByteBuf message) {
77 message.writeShort(OxmMatchConstants.NXM_1_CLASS);
79 int fieldMask = NXM_FIELD_CODE << 1;
80 message.writeByte(fieldMask);
81 message.writeByte(VALUE_LENGTH);
82 message.writeShort(2);