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.openflowjava.util.ByteBufUtils;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
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.NxmNxArpTha;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.tha.grouping.ArpThaValuesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpThaCaseValue;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpThaCaseValueBuilder;
27 public class ArpThaCodecTest {
29 ArpThaCodec arpThaCodec;
33 private static final int VALUE_LENGTH = 6;
34 private static final int NXM_FIELD_CODE = 18;
36 private static final byte[] TEST_ADDR = new byte[VALUE_LENGTH];
37 private static final MacAddress TEST_ADDRESS = new MacAddress(ByteBufUtils.macAddressToString(TEST_ADDR));
41 arpThaCodec = new ArpThaCodec();
42 buffer = ByteBufAllocator.DEFAULT.buffer();
46 public void serializeTest() {
47 input = createMatchEntry();
48 arpThaCodec.serialize(input, buffer);
50 assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
51 short fieldMask = buffer.readUnsignedByte();
52 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
53 assertEquals(0, fieldMask & 1);
54 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
55 assertEquals(TEST_ADDRESS, ByteBufUtils.readIetfMacAddress(buffer));
59 public void deserializeTest() {
61 input = arpThaCodec.deserialize(buffer);
63 final ArpThaCaseValue result = (ArpThaCaseValue) input.getMatchEntryValue();
65 assertEquals(Nxm1Class.class, input.getOxmClass());
66 assertEquals(NxmNxArpTha.class, input.getOxmMatchField());
67 assertEquals(false, input.isHasMask());
68 assertEquals(TEST_ADDRESS, result.getArpThaValues().getMacAddress());
71 private static MatchEntry createMatchEntry() {
72 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
73 final ArpThaCaseValueBuilder caseBuilder = new ArpThaCaseValueBuilder();
74 final ArpThaValuesBuilder valuesBuilder = new ArpThaValuesBuilder();
76 matchEntryBuilder.setOxmClass(Nxm1Class.class);
77 matchEntryBuilder.setOxmMatchField(NxmNxArpTha.class);
78 matchEntryBuilder.setHasMask(false);
80 byte[] address = new byte[VALUE_LENGTH];
82 valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
84 caseBuilder.setArpThaValues(valuesBuilder.build());
85 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
86 return matchEntryBuilder.build();
89 private static void createBuffer(ByteBuf message) {
90 message.writeShort(OxmMatchConstants.NXM_1_CLASS);
92 int fieldMask = NXM_FIELD_CODE << 1;
93 message.writeByte(fieldMask);
94 message.writeByte(VALUE_LENGTH);
95 message.writeBytes(TEST_ADDR);