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.NxmNxArpSha;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.sha.grouping.ArpShaValuesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpShaCaseValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpShaCaseValueBuilder;
28 public class ArpShaCodecTest {
30 ArpShaCodec arpShaCodec;
34 private static final int VALUE_LENGTH = 6;
35 private static final int NXM_FIELD_CODE = 17;
37 private static byte[] resAddress = new byte[VALUE_LENGTH];
38 private static final MacAddress resultAddress = new MacAddress(ByteBufUtils.macAddressToString(resAddress));
43 arpShaCodec = new ArpShaCodec();
44 buffer = ByteBufAllocator.DEFAULT.buffer();
49 public void serializeTest() {
50 input = createMatchEntry();
51 arpShaCodec.serialize(input, buffer);
53 assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
54 short fieldMask = buffer.readUnsignedByte();
55 assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
56 assertEquals(0, fieldMask & 1);
57 assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
58 assertEquals(resultAddress, ByteBufUtils.readIetfMacAddress(buffer));
62 public void deserializeTest() {
65 input = arpShaCodec.deserialize(buffer);
67 ArpShaCaseValue result = ((ArpShaCaseValue) input.getMatchEntryValue());
69 assertEquals(Nxm1Class.class, input.getOxmClass());
70 assertEquals(NxmNxArpSha.class, input.getOxmMatchField());
71 assertEquals(false, input.isHasMask());
72 assertEquals(resultAddress, result.getArpShaValues().getMacAddress());
76 private MatchEntry createMatchEntry() {
77 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
78 ArpShaCaseValueBuilder caseBuilder = new ArpShaCaseValueBuilder();
79 ArpShaValuesBuilder valuesBuilder = new ArpShaValuesBuilder();
81 matchEntryBuilder.setOxmClass(Nxm1Class.class);
82 matchEntryBuilder.setOxmMatchField(NxmNxArpSha.class);
83 matchEntryBuilder.setHasMask(false);
86 byte[] address = new byte[VALUE_LENGTH];
88 valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
90 caseBuilder.setArpShaValues(valuesBuilder.build());
91 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
92 return matchEntryBuilder.build();
95 private void createBuffer(ByteBuf message) {
96 message.writeShort(OxmMatchConstants.NXM_1_CLASS);
98 int fieldMask = (NXM_FIELD_CODE << 1);
99 message.writeByte(fieldMask);
100 message.writeByte(VALUE_LENGTH);
101 message.writeBytes(resAddress);