Make methods static
[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
25 public class ArpSpaCodecTest {
26
27     ArpSpaCodec arpSpaCodec;
28     ByteBuf buffer;
29     MatchEntry input;
30
31     private static final int VALUE_LENGTH = 4;
32     private static final int NXM_FIELD_CODE = 16;
33
34
35     @Before
36     public void setUp() {
37         arpSpaCodec = new ArpSpaCodec();
38         buffer = ByteBufAllocator.DEFAULT.buffer();
39     }
40
41     @Test
42     public void serializeTest() {
43         input = createMatchEntry();
44         arpSpaCodec.serialize(input, buffer);
45
46         assertEquals(OxmMatchConstants.NXM_0_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(1, buffer.readUnsignedInt());
52     }
53
54     @Test
55     public void deserializeTest() {
56         createBuffer(buffer);
57         input = arpSpaCodec.deserialize(buffer);
58
59         final ArpSpaCaseValue result = (ArpSpaCaseValue) input.getMatchEntryValue();
60
61         assertEquals(Nxm0Class.class, input.getOxmClass());
62         assertEquals(NxmOfArpSpa.class, input.getOxmMatchField());
63         assertEquals(false, input.isHasMask());
64         assertEquals(2, result.getArpSpaValues().getValue().shortValue());
65     }
66
67     private static MatchEntry createMatchEntry() {
68         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
69         final ArpSpaCaseValueBuilder caseBuilder = new ArpSpaCaseValueBuilder();
70         final ArpSpaValuesBuilder valuesBuilder = new ArpSpaValuesBuilder();
71
72         matchEntryBuilder.setOxmClass(Nxm0Class.class);
73         matchEntryBuilder.setOxmMatchField(NxmOfArpSpa.class);
74         matchEntryBuilder.setHasMask(false);
75
76         valuesBuilder.setValue((long)1);
77
78         caseBuilder.setArpSpaValues(valuesBuilder.build());
79         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
80         return matchEntryBuilder.build();
81     }
82
83     private static void createBuffer(ByteBuf message) {
84         message.writeShort(OxmMatchConstants.NXM_0_CLASS);
85
86         int fieldMask = NXM_FIELD_CODE << 1;
87         message.writeByte(fieldMask);
88         message.writeByte(VALUE_LENGTH);
89         message.writeInt(2);
90     }
91 }