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.flags.grouping.NshFlagsValues;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsh.flags.grouping.NshFlagsValuesBuilder;
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.NshFlagsCaseValue;
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.NshFlagsCaseValueBuilder;
22 import org.opendaylight.yangtools.yang.common.Uint8;
24 public class NshFlagsCodecTest {
26 private NshFlagsCodec nshFlagsCodec;
27 private ByteBuf buffer;
29 private static final Uint8 FLAGS_VALUE = Uint8.valueOf(0xA1);
30 private static final Uint8 FLAGS_MASK = Uint8.MAX_VALUE;
34 nshFlagsCodec = new NshFlagsCodec();
35 buffer = ByteBufAllocator.DEFAULT.buffer();
39 public void serializeTestNoMask() {
40 NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(FLAGS_VALUE, null);
42 nshFlagsCodec.serializeValue(matchEntryValue, false, buffer);
44 assertEquals(FLAGS_VALUE.shortValue(), buffer.readUnsignedByte());
45 assertFalse(buffer.isReadable());
49 public void serializeTestMask() {
50 NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(FLAGS_VALUE, FLAGS_MASK);
52 nshFlagsCodec.serializeValue(matchEntryValue, true, buffer);
54 assertEquals(FLAGS_VALUE.shortValue(), buffer.readUnsignedByte());
55 assertEquals(FLAGS_MASK.shortValue(), buffer.readUnsignedByte());
56 assertFalse(buffer.isReadable());
60 public void deserializeTestNoMask() {
61 writeBuffer(buffer, FLAGS_VALUE, null);
63 NxExpMatchEntryValue value = nshFlagsCodec.deserializeValue(buffer, false);
65 assertEquals(FLAGS_VALUE, ((NshFlagsCaseValue) value).getNshFlagsValues().getNshFlags());
66 assertFalse(buffer.isReadable());
70 public void deserializeTestMask() {
71 writeBuffer(buffer, FLAGS_VALUE, FLAGS_MASK);
73 NxExpMatchEntryValue value = nshFlagsCodec.deserializeValue(buffer, true);
75 assertEquals(FLAGS_VALUE, ((NshFlagsCaseValue) value).getNshFlagsValues().getNshFlags());
76 assertEquals(FLAGS_MASK, ((NshFlagsCaseValue) value).getNshFlagsValues().getMask());
77 assertFalse(buffer.isReadable());
80 private static NxExpMatchEntryValue createMatchEntryValue(Uint8 value, Uint8 mask) {
81 NshFlagsValues nshFlagsValues = new NshFlagsValuesBuilder().setNshFlags(value).setMask(mask).build();
82 return new NshFlagsCaseValueBuilder().setNshFlagsValues(nshFlagsValues).build();
85 private static void writeBuffer(ByteBuf message, Uint8 value, Uint8 mask) {
86 message.writeByte(value.intValue());
88 message.writeByte(mask.intValue());