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.IetfYangUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.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.aug.nx.match.ArpShaCaseValue;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.ArpShaCaseValueBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.sha.grouping.ArpShaValuesBuilder;
28 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 = IetfYangUtil.INSTANCE.macAddressFor(resAddress);
41 arpShaCodec = new ArpShaCodec();
42 buffer = ByteBufAllocator.DEFAULT.buffer();
47 public void serializeTest() {
48 input = createMatchEntry();
49 arpShaCodec.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(RESULT_ADDRESS, ByteBufUtils.readIetfMacAddress(buffer));
60 public void deserializeTest() {
63 input = arpShaCodec.deserialize(buffer);
65 final ArpShaCaseValue result = (ArpShaCaseValue) input.getMatchEntryValue();
67 assertEquals(Nxm1Class.VALUE, input.getOxmClass());
68 assertEquals(NxmNxArpSha.VALUE, input.getOxmMatchField());
69 assertEquals(false, input.getHasMask());
70 assertEquals(RESULT_ADDRESS, result.getArpShaValues().getMacAddress());
74 private static MatchEntry createMatchEntry() {
75 MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
76 final ArpShaCaseValueBuilder caseBuilder = new ArpShaCaseValueBuilder();
77 final ArpShaValuesBuilder valuesBuilder = new ArpShaValuesBuilder();
79 matchEntryBuilder.setOxmClass(Nxm1Class.VALUE);
80 matchEntryBuilder.setOxmMatchField(NxmNxArpSha.VALUE);
81 matchEntryBuilder.setHasMask(false);
84 valuesBuilder.setMacAddress(IetfYangUtil.INSTANCE.macAddressFor(new byte[VALUE_LENGTH]));
86 caseBuilder.setArpShaValues(valuesBuilder.build());
87 matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
88 return matchEntryBuilder.build();
91 private static void createBuffer(final ByteBuf message) {
92 message.writeShort(OxmMatchConstants.NXM_1_CLASS);
94 int fieldMask = NXM_FIELD_CODE << 1;
95 message.writeByte(fieldMask);
96 message.writeByte(VALUE_LENGTH);
97 message.writeBytes(resAddress);