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.openflowjava.util.ByteBufUtils;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxArpTha;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.tha.grouping.ArpThaValuesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpThaCaseValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpThaCaseValueBuilder;
28 public class ArpThaCodecTest {
30 ArpThaCodec arpThaCodec;
34 private static final int VALUE_LENGTH = 6;
35 private static final int NXM_FIELD_CODE = 18;
37 private static final byte[] testAddr = new byte[VALUE_LENGTH];
38 private static final MacAddress testAddress = new MacAddress(ByteBufUtils.macAddressToString(testAddr));
42 arpThaCodec = new ArpThaCodec();
43 buffer = ByteBufAllocator.DEFAULT.buffer();
47 public void serializeTest() {
48 input = createMatchEntry();
49 arpThaCodec.serialize(input, buffer);
51 assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
52 short fieldMask = buffer.readUnsignedByte();
53 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
54 assertEquals(0, fieldMask & 1);
55 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
56 assertEquals(testAddress, ByteBufUtils.readIetfMacAddress(buffer));
60 public void deserializeTest() {
62 input = arpThaCodec.deserialize(buffer);
64 ArpThaCaseValue result = ((ArpThaCaseValue) input.getMatchEntryValue());
66 assertEquals(Nxm1Class.class, input.getOxmClass());
67 assertEquals(NxmNxArpTha.class, input.getOxmMatchField());
68 assertEquals(false, input.isHasMask());
69 assertEquals(testAddress, result.getArpThaValues().getMacAddress());
72 private MatchEntry createMatchEntry() {
73 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
74 ArpThaCaseValueBuilder caseBuilder = new ArpThaCaseValueBuilder();
75 ArpThaValuesBuilder valuesBuilder = new ArpThaValuesBuilder();
77 matchEntryBuilder.setOxmClass(Nxm1Class.class);
78 matchEntryBuilder.setOxmMatchField(NxmNxArpTha.class);
79 matchEntryBuilder.setHasMask(false);
81 byte[] address = new byte[VALUE_LENGTH];
83 valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
85 caseBuilder.setArpThaValues(valuesBuilder.build());
86 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
87 return matchEntryBuilder.build();
90 private void createBuffer(ByteBuf message) {
91 message.writeShort(OxmMatchConstants.NXM_1_CLASS);
93 int fieldMask = (NXM_FIELD_CODE << 1);
94 message.writeByte(fieldMask);
95 message.writeByte(VALUE_LENGTH);
96 message.writeBytes(testAddr);