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 / TunIdCodecTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  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
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufAllocator;
14 import java.math.BigInteger;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxTunId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.tun.id.grouping.TunIdValuesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TunIdCaseValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TunIdCaseValueBuilder;
25
26 public class TunIdCodecTest {
27
28     TunIdCodec tunIdCodec;
29     ByteBuf buffer;
30     MatchEntry input;
31
32     private static final int VALUE_LENGTH = 8;
33     private static final int NXM_FIELD_CODE = 16;
34
35     @Before
36     public void setUp() {
37         tunIdCodec = new TunIdCodec();
38         buffer = ByteBufAllocator.DEFAULT.buffer();
39     }
40
41     @Test
42     public void serializeTest() {
43         input = createMatchEntry();
44         tunIdCodec.serialize(input, buffer);
45
46         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
47         short fieldMask = buffer.readUnsignedByte();
48         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
49         assertEquals(0, fieldMask & 1);
50         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
51         assertEquals(0, buffer.readLong());
52     }
53
54     @Test
55     public void deserializeTest() {
56         createBuffer(buffer);
57
58         input = tunIdCodec.deserialize(buffer);
59
60         final TunIdCaseValue result = (TunIdCaseValue) input.getMatchEntryValue();
61
62         assertEquals(Nxm1Class.class, input.getOxmClass());
63         assertEquals(NxmNxTunId.class, input.getOxmMatchField());
64         assertEquals(false, input.isHasMask());
65         assertEquals(0, result.getTunIdValues().getValue().longValue());
66     }
67
68
69
70     private MatchEntry createMatchEntry() {
71         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
72         final TunIdCaseValueBuilder caseBuilder = new TunIdCaseValueBuilder();
73         final TunIdValuesBuilder valuesBuilder = new TunIdValuesBuilder();
74
75         matchEntryBuilder.setOxmClass(Nxm1Class.class);
76         matchEntryBuilder.setOxmMatchField(NxmNxTunId.class);
77         matchEntryBuilder.setHasMask(false);
78
79         byte[] value = new byte[VALUE_LENGTH];
80         valuesBuilder.setValue(new BigInteger(value));
81
82         caseBuilder.setTunIdValues(valuesBuilder.build());
83         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
84         return matchEntryBuilder.build();
85     }
86
87     private void createBuffer(ByteBuf message) {
88         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
89
90         int fieldMask = NXM_FIELD_CODE << 1;
91         message.writeByte(fieldMask);
92         message.writeByte(VALUE_LENGTH);
93         byte[] value = new byte[VALUE_LENGTH];
94         message.writeLong(new BigInteger(value).longValue());
95     }
96 }