2 * Copyright (c) 2018 SUSE LINUX GmbH. 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;
11 import static org.junit.Assert.assertFalse;
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.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsh.ttl.grouping.NshTtlValues;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsh.ttl.grouping.NshTtlValuesBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.NxExpMatchEntryValue;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NshTtlCaseValue;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NshTtlCaseValueBuilder;
22 import org.opendaylight.yangtools.yang.common.Uint8;
24 public class NshTtlCodecTest {
26 private NshTtlCodec nshTtlCodec;
27 private ByteBuf buffer;
29 private static final Short TTL_VALUE = (short) 0xD8;
30 private static final Short TTL_MASK = (short) 0xFFL;
34 nshTtlCodec = new NshTtlCodec();
35 buffer = ByteBufAllocator.DEFAULT.buffer();
39 public void serializeTestNoMask() {
40 NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(TTL_VALUE, null);
42 nshTtlCodec.serializeValue(matchEntryValue, false, buffer);
44 assertEquals(TTL_VALUE.shortValue(), buffer.readUnsignedByte());
45 assertFalse(buffer.isReadable());
49 public void serializeTestMask() {
50 NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(TTL_VALUE, TTL_MASK);
52 nshTtlCodec.serializeValue(matchEntryValue, true, buffer);
54 assertEquals(TTL_VALUE.shortValue(), buffer.readUnsignedByte());
55 assertEquals(TTL_MASK.shortValue(), buffer.readUnsignedByte());
56 assertFalse(buffer.isReadable());
60 public void deserializeTestNoMask() {
61 writeBuffer(buffer, TTL_VALUE, null);
63 NxExpMatchEntryValue value = nshTtlCodec.deserializeValue(buffer, false);
65 assertEquals(Uint8.valueOf(TTL_VALUE), ((NshTtlCaseValue) value).getNshTtlValues().getNshTtl());
66 assertFalse(buffer.isReadable());
70 public void deserializeTestMask() {
71 writeBuffer(buffer, TTL_VALUE, TTL_MASK);
73 NxExpMatchEntryValue value = nshTtlCodec.deserializeValue(buffer, true);
75 assertEquals(Uint8.valueOf(TTL_VALUE), ((NshTtlCaseValue) value).getNshTtlValues().getNshTtl());
76 assertEquals(Uint8.valueOf(TTL_MASK), ((NshTtlCaseValue) value).getNshTtlValues().getMask());
77 assertFalse(buffer.isReadable());
80 private static NxExpMatchEntryValue createMatchEntryValue(final Short value, final Short mask) {
81 NshTtlValues nshTtlValues = new NshTtlValuesBuilder().setNshTtl(value).setMask(mask).build();
82 return new NshTtlCaseValueBuilder().setNshTtlValues(nshTtlValues).build();
85 private static void writeBuffer(final ByteBuf message, final Short value, final Short mask) {
86 message.writeByte(value.intValue());
88 message.writeByte(mask.intValue());