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