Merge "Added JSON and XML payloads tabs with RFC 8040 URL"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / NshTtlCodecTest.java
1 /*
2  * Copyright (c) 2018 SUSE LINUX GmbH.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.nx.codec.match;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12
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;
23
24 public class NshTtlCodecTest {
25
26     private NshTtlCodec nshTtlCodec;
27     private ByteBuf buffer;
28
29     private static final Short TTL_VALUE = (short) 0xD8;
30     private static final Short TTL_MASK = (short) 0xFFL;
31
32     @Before
33     public void setUp() {
34         nshTtlCodec = new NshTtlCodec();
35         buffer = ByteBufAllocator.DEFAULT.buffer();
36     }
37
38     @Test
39     public void serializeTestNoMask() {
40         NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(TTL_VALUE, null);
41
42         nshTtlCodec.serializeValue(matchEntryValue, false, buffer);
43
44         assertEquals(TTL_VALUE.shortValue(), buffer.readUnsignedByte());
45         assertFalse(buffer.isReadable());
46     }
47
48     @Test
49     public void serializeTestMask() {
50         NxExpMatchEntryValue matchEntryValue = createMatchEntryValue(TTL_VALUE, TTL_MASK);
51
52         nshTtlCodec.serializeValue(matchEntryValue, true, buffer);
53
54         assertEquals(TTL_VALUE.shortValue(), buffer.readUnsignedByte());
55         assertEquals(TTL_MASK.shortValue(), buffer.readUnsignedByte());
56         assertFalse(buffer.isReadable());
57     }
58
59     @Test
60     public void deserializeTestNoMask() {
61         writeBuffer(buffer, TTL_VALUE, null);
62
63         NxExpMatchEntryValue value = nshTtlCodec.deserializeValue(buffer, false);
64
65         assertEquals(Uint8.valueOf(TTL_VALUE), ((NshTtlCaseValue) value).getNshTtlValues().getNshTtl());
66         assertFalse(buffer.isReadable());
67     }
68
69     @Test
70     public void deserializeTestMask() {
71         writeBuffer(buffer, TTL_VALUE, TTL_MASK);
72
73         NxExpMatchEntryValue value = nshTtlCodec.deserializeValue(buffer, true);
74
75         assertEquals(Uint8.valueOf(TTL_VALUE), ((NshTtlCaseValue) value).getNshTtlValues().getNshTtl());
76         assertEquals(Uint8.valueOf(TTL_MASK), ((NshTtlCaseValue) value).getNshTtlValues().getMask());
77         assertFalse(buffer.isReadable());
78     }
79
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();
83     }
84
85     private static void writeBuffer(final ByteBuf message, final Short value, final Short mask) {
86         message.writeByte(value.intValue());
87         if (mask != null) {
88             message.writeByte(mask.intValue());
89         }
90     }
91 }