Merge "BUG-5888: moving the reconciliation process into a different thread to prevent...
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / ArpShaCodecTest.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
9 package org.opendaylight.openflowjava.nx.codec.match;
10
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufAllocator;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.openflowjava.util.ByteBufUtils;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxArpSha;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.arp.sha.grouping.ArpShaValuesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpShaCaseValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.ArpShaCaseValueBuilder;
27
28 public class ArpShaCodecTest {
29
30     ArpShaCodec arpShaCodec;
31     ByteBuf buffer;
32     MatchEntry input;
33
34     private static final int VALUE_LENGTH = 6;
35     private static final int NXM_FIELD_CODE = 17;
36
37     private static byte[] resAddress = new byte[VALUE_LENGTH];
38     private static final MacAddress resultAddress = new MacAddress(ByteBufUtils.macAddressToString(resAddress));
39
40
41     @Before
42     public void setUp() {
43         arpShaCodec = new ArpShaCodec();
44         buffer = ByteBufAllocator.DEFAULT.buffer();
45     }
46
47
48     @Test
49     public void serializeTest() {
50         input = createMatchEntry();
51         arpShaCodec.serialize(input, buffer);
52
53         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
54         short fieldMask = buffer.readUnsignedByte();
55         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
56         assertEquals(0, fieldMask & 1);
57         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
58         assertEquals(resultAddress, ByteBufUtils.readIetfMacAddress(buffer));
59     }
60
61     @Test
62     public void deserializeTest() {
63         createBuffer(buffer);
64
65         input = arpShaCodec.deserialize(buffer);
66
67         ArpShaCaseValue result = ((ArpShaCaseValue) input.getMatchEntryValue());
68
69         assertEquals(Nxm1Class.class, input.getOxmClass());
70         assertEquals(NxmNxArpSha.class, input.getOxmMatchField());
71         assertEquals(false, input.isHasMask());
72         assertEquals(resultAddress, result.getArpShaValues().getMacAddress());
73     }
74
75
76     private MatchEntry createMatchEntry() {
77         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
78         ArpShaCaseValueBuilder caseBuilder = new ArpShaCaseValueBuilder();
79         ArpShaValuesBuilder valuesBuilder = new ArpShaValuesBuilder();
80
81         matchEntryBuilder.setOxmClass(Nxm1Class.class);
82         matchEntryBuilder.setOxmMatchField(NxmNxArpSha.class);
83         matchEntryBuilder.setHasMask(false);
84
85
86         byte[] address = new byte[VALUE_LENGTH];
87
88         valuesBuilder.setMacAddress(new MacAddress(ByteBufUtils.macAddressToString(address)));
89
90         caseBuilder.setArpShaValues(valuesBuilder.build());
91         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
92         return matchEntryBuilder.build();
93     }
94
95     private void createBuffer(ByteBuf message) {
96         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
97
98         int fieldMask = (NXM_FIELD_CODE << 1);
99         message.writeByte(fieldMask);
100         message.writeByte(VALUE_LENGTH);
101         message.writeBytes(resAddress);
102     }
103
104 }