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.Before;
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.NxmNxReg5;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.reg.grouping.RegValuesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValue;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValueBuilder;
25 public class Reg5CodecTest {
31 private static final int NXM_FIELD_CODE = 5;
32 private static final int VALUE_LENGTH = 4;
36 reg5Codec = new Reg5Codec();
37 buffer = ByteBufAllocator.DEFAULT.buffer();
42 public void serializeTest() {
43 input = createMatchEntry();
44 reg5Codec.serialize(input, buffer);
46 assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
47 short fieldMask = buffer.readUnsignedByte();
48 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
49 assertEquals(0, fieldMask & 1);
50 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
51 assertEquals(1, buffer.readUnsignedInt());
55 public void deserializeTest() {
58 input = reg5Codec.deserialize(buffer);
60 final RegCaseValue result = (RegCaseValue) input.getMatchEntryValue();
62 assertEquals(Nxm1Class.class, input.getOxmClass());
63 assertEquals(NxmNxReg5.class, input.getOxmMatchField());
64 assertEquals(false, input.isHasMask());
65 assertEquals(1, result.getRegValues().getValue().intValue());
68 private static MatchEntry createMatchEntry() {
69 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
70 final RegCaseValueBuilder caseBuilder = new RegCaseValueBuilder();
71 final RegValuesBuilder valuesBuilder = new RegValuesBuilder();
73 matchEntryBuilder.setOxmClass(Nxm1Class.class);
74 matchEntryBuilder.setOxmMatchField(NxmNxReg5.class);
75 matchEntryBuilder.setHasMask(false);
77 valuesBuilder.setValue((long)1);
79 caseBuilder.setRegValues(valuesBuilder.build());
80 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
81 return matchEntryBuilder.build();
84 private static void createBuffer(ByteBuf message) {
85 message.writeShort(OxmMatchConstants.NXM_1_CLASS);
87 int fieldMask = NXM_FIELD_CODE << 1;
88 message.writeByte(fieldMask);
89 message.writeByte(VALUE_LENGTH);