/* * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.openflowjava.nx.codec.match; import static org.junit.Assert.assertEquals; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; import org.junit.Before; import org.junit.Test; import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants; import org.opendaylight.openflowjava.util.ByteBufUtils; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxArpTha; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.ArpThaCaseValue; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.ArpThaCaseValueBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.tha.grouping.ArpThaValuesBuilder; public class ArpThaCodecTest { ArpThaCodec arpThaCodec; ByteBuf buffer; MatchEntry input; private static final int VALUE_LENGTH = 6; private static final int NXM_FIELD_CODE = 18; private static final byte[] TEST_ADDR = new byte[VALUE_LENGTH]; private static final MacAddress TEST_ADDRESS = IetfYangUtil.macAddressFor(TEST_ADDR); @Before public void setUp() { arpThaCodec = new ArpThaCodec(); buffer = ByteBufAllocator.DEFAULT.buffer(); } @Test public void serializeTest() { input = createMatchEntry(); arpThaCodec.serialize(input, buffer); assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort()); short fieldMask = buffer.readUnsignedByte(); assertEquals(NXM_FIELD_CODE, fieldMask >> 1); assertEquals(0, fieldMask & 1); assertEquals(VALUE_LENGTH, buffer.readUnsignedByte()); assertEquals(TEST_ADDRESS, ByteBufUtils.readIetfMacAddress(buffer)); } @Test public void deserializeTest() { createBuffer(buffer); input = arpThaCodec.deserialize(buffer); final ArpThaCaseValue result = (ArpThaCaseValue) input.getMatchEntryValue(); assertEquals(Nxm1Class.VALUE, input.getOxmClass()); assertEquals(NxmNxArpTha.VALUE, input.getOxmMatchField()); assertEquals(false, input.getHasMask()); assertEquals(TEST_ADDRESS, result.getArpThaValues().getMacAddress()); } private static MatchEntry createMatchEntry() { MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder(); final ArpThaCaseValueBuilder caseBuilder = new ArpThaCaseValueBuilder(); final ArpThaValuesBuilder valuesBuilder = new ArpThaValuesBuilder(); matchEntryBuilder.setOxmClass(Nxm1Class.VALUE); matchEntryBuilder.setOxmMatchField(NxmNxArpTha.VALUE); matchEntryBuilder.setHasMask(false); valuesBuilder.setMacAddress(IetfYangUtil.macAddressFor(new byte[VALUE_LENGTH])); caseBuilder.setArpThaValues(valuesBuilder.build()); matchEntryBuilder.setMatchEntryValue(caseBuilder.build()); return matchEntryBuilder.build(); } private static void createBuffer(final ByteBuf message) { message.writeShort(OxmMatchConstants.NXM_1_CLASS); int fieldMask = NXM_FIELD_CODE << 1; message.writeByte(fieldMask); message.writeByte(VALUE_LENGTH); message.writeBytes(TEST_ADDR); } }