Merge "Fix a modernizer warning"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / ArpSpaCodecTest.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.Nxm0Class;
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.NxmOfArpSpa;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.arp.spa.grouping.ArpSpaValuesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpSpaCaseValue;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpSpaCaseValueBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint32;
25
26 public class ArpSpaCodecTest {
27
28     ArpSpaCodec arpSpaCodec;
29     ByteBuf buffer;
30     MatchEntry input;
31
32     private static final int VALUE_LENGTH = 4;
33     private static final int NXM_FIELD_CODE = 16;
34
35
36     @Before
37     public void setUp() {
38         arpSpaCodec = new ArpSpaCodec();
39         buffer = ByteBufAllocator.DEFAULT.buffer();
40     }
41
42     @Test
43     public void serializeTest() {
44         input = createMatchEntry();
45         arpSpaCodec.serialize(input, buffer);
46
47         assertEquals(OxmMatchConstants.NXM_0_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         input = arpSpaCodec.deserialize(buffer);
59
60         final ArpSpaCaseValue result = (ArpSpaCaseValue) input.getMatchEntryValue();
61
62         assertEquals(Nxm0Class.class, input.getOxmClass());
63         assertEquals(NxmOfArpSpa.class, input.getOxmMatchField());
64         assertEquals(false, input.isHasMask());
65         assertEquals(2, result.getArpSpaValues().getValue().shortValue());
66     }
67
68     private static MatchEntry createMatchEntry() {
69         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
70         final ArpSpaCaseValueBuilder caseBuilder = new ArpSpaCaseValueBuilder();
71         final ArpSpaValuesBuilder valuesBuilder = new ArpSpaValuesBuilder();
72
73         matchEntryBuilder.setOxmClass(Nxm0Class.class);
74         matchEntryBuilder.setOxmMatchField(NxmOfArpSpa.class);
75         matchEntryBuilder.setHasMask(false);
76
77         valuesBuilder.setValue(Uint32.ONE);
78
79         caseBuilder.setArpSpaValues(valuesBuilder.build());
80         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
81         return matchEntryBuilder.build();
82     }
83
84     private static void createBuffer(ByteBuf message) {
85         message.writeShort(OxmMatchConstants.NXM_0_CLASS);
86
87         int fieldMask = NXM_FIELD_CODE << 1;
88         message.writeByte(fieldMask);
89         message.writeByte(VALUE_LENGTH);
90         message.writeInt(2);
91     }
92 }