Merge "Fix a modernizer warning"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / Reg3CodecTest.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 org.junit.Before;
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.NxmNxReg3;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.reg.grouping.RegValuesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValue;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValueBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint32;
25
26 public class Reg3CodecTest {
27
28     Reg3Codec reg3Codec;
29     ByteBuf buffer;
30     MatchEntry input;
31
32     private static final int NXM_FIELD_CODE = 3;
33     private static final int VALUE_LENGTH = 4;
34
35     @Before
36     public void setUp() {
37         reg3Codec = new Reg3Codec();
38         buffer = ByteBufAllocator.DEFAULT.buffer();
39     }
40
41
42     @Test
43     public void serializeTest() {
44         input = createMatchEntry();
45         reg3Codec.serialize(input, buffer);
46
47         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
48         short fieldMask = buffer.readUnsignedByte();
49         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
50         assertEquals(0, fieldMask & 1);
51         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
52         assertEquals(1, buffer.readUnsignedInt());
53     }
54
55     @Test
56     public void deserializeTest() {
57         createBuffer(buffer);
58
59         input = reg3Codec.deserialize(buffer);
60
61         final RegCaseValue result = (RegCaseValue) input.getMatchEntryValue();
62
63         assertEquals(Nxm1Class.class, input.getOxmClass());
64         assertEquals(NxmNxReg3.class, input.getOxmMatchField());
65         assertEquals(false, input.isHasMask());
66         assertEquals(1, result.getRegValues().getValue().intValue());
67     }
68
69     private static MatchEntry createMatchEntry() {
70         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
71         final RegCaseValueBuilder caseBuilder = new RegCaseValueBuilder();
72         final RegValuesBuilder valuesBuilder = new RegValuesBuilder();
73
74         matchEntryBuilder.setOxmClass(Nxm1Class.class);
75         matchEntryBuilder.setOxmMatchField(NxmNxReg3.class);
76         matchEntryBuilder.setHasMask(false);
77
78         valuesBuilder.setValue(Uint32.ONE);
79
80         caseBuilder.setRegValues(valuesBuilder.build());
81         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
82         return matchEntryBuilder.build();
83     }
84
85     private static void createBuffer(ByteBuf message) {
86         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
87
88         int fieldMask = NXM_FIELD_CODE << 1;
89         message.writeByte(fieldMask);
90         message.writeByte(VALUE_LENGTH);
91         message.writeInt(1);
92     }
93 }