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;
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.nsp.grouping.NspValues;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsp.grouping.NspValuesBuilder;
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.NspCaseValue;
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.NspCaseValueBuilder;
22 import org.opendaylight.yangtools.yang.common.Uint32;
24 public class NspCodecTest {
26 private NspCodec nspCodec;
27 private ByteBuf buffer;
29 private static final Long NSP_VALUE = 10L;
30 private static final Long NSP_MASK = 0xFL;
34 nspCodec = new NspCodec();
35 buffer = ByteBufAllocator.DEFAULT.buffer();
39 public void serializeTestNoMask() {
40 NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(NSP_VALUE, null);
42 nspCodec.serializeValue(matchEntryValue, false, buffer);
44 assertEquals(NSP_VALUE.longValue(), buffer.readUnsignedInt());
45 assertFalse(buffer.isReadable());
49 public void serializeTestMask() {
50 NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(NSP_VALUE, NSP_MASK);
52 nspCodec.serializeValue(matchEntryValue, true, buffer);
54 assertEquals(NSP_VALUE.longValue(), buffer.readUnsignedInt());
55 assertEquals(NSP_MASK.longValue(), buffer.readUnsignedInt());
56 assertFalse(buffer.isReadable());
60 public void deserializeTestNoMask() {
61 writeBuffer(buffer, NSP_VALUE, null);
63 NxExpMatchEntryValue value = nspCodec.deserializeValue(buffer, false);
65 assertEquals(Uint32.valueOf(NSP_VALUE), ((NspCaseValue) value).getNspValues().getNsp());
66 assertFalse(buffer.isReadable());
70 public void deserializeTestMask() {
71 writeBuffer(buffer, NSP_VALUE, NSP_MASK);
73 NxExpMatchEntryValue value = nspCodec.deserializeValue(buffer, true);
75 assertEquals(Uint32.valueOf(NSP_VALUE), ((NspCaseValue) value).getNspValues().getNsp());
76 assertEquals(Uint32.valueOf(NSP_MASK), ((NspCaseValue) value).getNspValues().getMask());
77 assertFalse(buffer.isReadable());
81 private NxExpMatchEntryValue createMatchEntryValue(final Long value, final Long mask) {
82 NspValues nspValues = new NspValuesBuilder().setNsp(value).setMask(mask).build();
83 return new NspCaseValueBuilder().setNspValues(nspValues).build();
86 private void writeBuffer(final ByteBuf message, final Long value, final Long mask) {
87 message.writeInt(value.intValue());
89 message.writeInt(mask.intValue());