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.NxmNxArpSha;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.sha.grouping.ArpShaValuesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpShaCaseValue;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpShaCaseValueBuilder;
27 public class ArpShaCodecTest {
29 ArpShaCodec arpShaCodec;
33 private static final int VALUE_LENGTH = 6;
34 private static final int NXM_FIELD_CODE = 17;
36 private static byte[] resAddress = new byte[VALUE_LENGTH];
37 private static final MacAddress RESULT_ADDRESS = new MacAddress(ByteBufUtils.macAddressToString(resAddress));
42 arpShaCodec = new ArpShaCodec();
43 buffer = ByteBufAllocator.DEFAULT.buffer();
48 public void serializeTest() {
49 input = createMatchEntry();
50 arpShaCodec.serialize(input, buffer);
52 assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
53 short fieldMask = buffer.readUnsignedByte();
54 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
55 assertEquals(0, fieldMask & 1);
56 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
57 assertEquals(RESULT_ADDRESS, ByteBufUtils.readIetfMacAddress(buffer));
61 public void deserializeTest() {
64 input = arpShaCodec.deserialize(buffer);
66 final ArpShaCaseValue result = (ArpShaCaseValue) input.getMatchEntryValue();
68 assertEquals(Nxm1Class.class, input.getOxmClass());
69 assertEquals(NxmNxArpSha.class, input.getOxmMatchField());
70 assertEquals(false, input.isHasMask());
71 assertEquals(RESULT_ADDRESS, result.getArpShaValues().getMacAddress());
75 private MatchEntry createMatchEntry() {
76 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
77 final ArpShaCaseValueBuilder caseBuilder = new ArpShaCaseValueBuilder();
78 final ArpShaValuesBuilder valuesBuilder = new ArpShaValuesBuilder();
80 matchEntryBuilder.setOxmClass(Nxm1Class.class);
81 matchEntryBuilder.setOxmMatchField(NxmNxArpSha.class);
82 matchEntryBuilder.setHasMask(false);
85 byte[] address = new byte[VALUE_LENGTH];
87 valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
89 caseBuilder.setArpShaValues(valuesBuilder.build());
90 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
91 return matchEntryBuilder.build();
94 private void createBuffer(ByteBuf message) {
95 message.writeShort(OxmMatchConstants.NXM_1_CLASS);
97 int fieldMask = NXM_FIELD_CODE << 1;
98 message.writeByte(fieldMask);
99 message.writeByte(VALUE_LENGTH);
100 message.writeBytes(resAddress);