Update MRI upstreams for Phosphorus
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / Reg4CodecTest.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.Test;
15 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.RegCaseValue;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.RegCaseValueBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.reg.grouping.RegValuesBuilder;
23 import org.opendaylight.yangtools.yang.common.Uint32;
24
25 public class Reg4CodecTest {
26     private final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
27     private final Reg4Codec reg4Codec = new Reg4Codec();
28
29     private MatchEntry input;
30
31     private static final int NXM_FIELD_CODE = 4;
32     private static final int VALUE_LENGTH = 4;
33
34     @Test
35     public void serializeTest() {
36         input = createMatchEntry();
37         reg4Codec.serialize(input, buffer);
38
39         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
40         short fieldMask = buffer.readUnsignedByte();
41         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
42         assertEquals(0, fieldMask & 1);
43         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
44         assertEquals(1, buffer.readUnsignedInt());
45     }
46
47     @Test
48     public void deserializeTest() {
49         createBuffer(buffer);
50
51         input = reg4Codec.deserialize(buffer);
52
53         final RegCaseValue result = (RegCaseValue) input.getMatchEntryValue();
54
55         assertEquals(Nxm1Class.class, input.getOxmClass());
56         assertEquals(NxmNxReg4.class, input.getOxmMatchField());
57         assertEquals(false, input.getHasMask());
58         assertEquals(1, result.getRegValues().getValue().intValue());
59     }
60
61     private static MatchEntry createMatchEntry() {
62         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
63         final RegCaseValueBuilder caseBuilder = new RegCaseValueBuilder();
64         final RegValuesBuilder valuesBuilder = new RegValuesBuilder();
65
66         matchEntryBuilder.setOxmClass(Nxm1Class.class);
67         matchEntryBuilder.setOxmMatchField(NxmNxReg4.class);
68         matchEntryBuilder.setHasMask(false);
69
70         valuesBuilder.setValue(Uint32.ONE);
71
72         caseBuilder.setRegValues(valuesBuilder.build());
73         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
74         return matchEntryBuilder.build();
75     }
76
77     private static void createBuffer(final ByteBuf message) {
78         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
79
80         int fieldMask = NXM_FIELD_CODE << 1;
81         message.writeByte(fieldMask);
82         message.writeByte(VALUE_LENGTH);
83         message.writeInt(1);
84     }
85 }