Merge "Add INFO.yaml for openflowplugin"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / UdpDstCodecTest.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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm0Class;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfUdpDst;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.udp.dst.grouping.UdpDstValuesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.UdpDstCaseValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.UdpDstCaseValueBuilder;
25
26 public class UdpDstCodecTest {
27
28     UdpDstCodec udpDstCodec;
29     ByteBuf buffer;
30     MatchEntry input;
31
32     private static final int VALUE_LENGTH = 4;
33     private static final int NXM_FIELD_CODE = 12;
34
35     @Before
36     public void setUp() {
37         udpDstCodec = new UdpDstCodec();
38         buffer = ByteBufAllocator.DEFAULT.buffer();
39     }
40
41     @Test
42     public void serializeTest() {
43         input = createMatchEntry();
44         udpDstCodec.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(1, fieldMask & 1);
50         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
51         assertEquals(1, buffer.readUnsignedShort());
52         assertEquals(0xffff, buffer.readUnsignedShort());
53     }
54
55     @Test
56     public void deserializeTest() {
57         createBuffer(buffer);
58
59         input = udpDstCodec.deserialize(buffer);
60
61         final UdpDstCaseValue result = (UdpDstCaseValue) input.getMatchEntryValue();
62
63         assertEquals(Nxm0Class.class, input.getOxmClass());
64         assertEquals(NxmOfUdpDst.class, input.getOxmMatchField());
65         assertEquals(true, input.isHasMask());
66         assertEquals(1, result.getUdpDstValues().getPort().getValue().shortValue());
67         assertEquals(0xffff, result.getUdpDstValues().getMask().shortValue() & 0xffff);
68     }
69
70     private static MatchEntry createMatchEntry() {
71         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
72         final UdpDstCaseValueBuilder caseBuilder = new UdpDstCaseValueBuilder();
73         final UdpDstValuesBuilder valuesBuilder = new UdpDstValuesBuilder();
74
75         matchEntryBuilder.setOxmClass(Nxm0Class.class);
76         matchEntryBuilder.setOxmMatchField(NxmOfUdpDst.class);
77         matchEntryBuilder.setHasMask(true);
78
79         valuesBuilder.setPort(new PortNumber(1));
80         valuesBuilder.setMask(0xffff);
81
82         caseBuilder.setUdpDstValues(valuesBuilder.build());
83         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
84         return matchEntryBuilder.build();
85     }
86
87     private static void createBuffer(ByteBuf message) {
88         message.writeShort(OxmMatchConstants.NXM_0_CLASS);
89
90         int fieldMask = NXM_FIELD_CODE << 1;
91         message.writeByte(fieldMask);
92         message.writeByte(VALUE_LENGTH);
93         //Port num = 1
94         message.writeShort(1);
95         message.writeShort(0xffff);
96     }
97 }