Update MRI upstreams for Phosphorus
[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.Test;
15 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
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.NxmOfUdpDst;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.UdpDstCaseValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.UdpDstCaseValueBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.udp.dst.grouping.UdpDstValuesBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint16;
25
26 public class UdpDstCodecTest {
27     private final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
28     private final UdpDstCodec udpDstCodec = new UdpDstCodec();
29
30     private MatchEntry input;
31
32     private static final int VALUE_LENGTH = 4;
33     private static final int NXM_FIELD_CODE = 12;
34
35     @Test
36     public void serializeTest() {
37         input = createMatchEntry();
38         udpDstCodec.serialize(input, buffer);
39
40         assertEquals(OxmMatchConstants.NXM_0_CLASS, buffer.readUnsignedShort());
41         short fieldMask = buffer.readUnsignedByte();
42         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
43         assertEquals(1, fieldMask & 1);
44         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
45         assertEquals(1, buffer.readUnsignedShort());
46         assertEquals(0xffff, buffer.readUnsignedShort());
47     }
48
49     @Test
50     public void deserializeTest() {
51         createBuffer(buffer);
52
53         input = udpDstCodec.deserialize(buffer);
54
55         final UdpDstCaseValue result = (UdpDstCaseValue) input.getMatchEntryValue();
56
57         assertEquals(Nxm0Class.class, input.getOxmClass());
58         assertEquals(NxmOfUdpDst.class, input.getOxmMatchField());
59         assertEquals(true, input.getHasMask());
60         assertEquals(1, result.getUdpDstValues().getPort().getValue().shortValue());
61         assertEquals(0xffff, result.getUdpDstValues().getMask().shortValue() & 0xffff);
62     }
63
64     private static MatchEntry createMatchEntry() {
65         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
66         final UdpDstCaseValueBuilder caseBuilder = new UdpDstCaseValueBuilder();
67         final UdpDstValuesBuilder valuesBuilder = new UdpDstValuesBuilder();
68
69         matchEntryBuilder.setOxmClass(Nxm0Class.class);
70         matchEntryBuilder.setOxmMatchField(NxmOfUdpDst.class);
71         matchEntryBuilder.setHasMask(true);
72
73         valuesBuilder.setPort(new PortNumber(Uint16.ONE));
74         valuesBuilder.setMask(Uint16.MAX_VALUE);
75
76         caseBuilder.setUdpDstValues(valuesBuilder.build());
77         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
78         return matchEntryBuilder.build();
79     }
80
81     private static void createBuffer(final ByteBuf message) {
82         message.writeShort(OxmMatchConstants.NXM_0_CLASS);
83
84         int fieldMask = NXM_FIELD_CODE << 1;
85         message.writeByte(fieldMask);
86         message.writeByte(VALUE_LENGTH);
87         //Port num = 1
88         message.writeShort(1);
89         message.writeShort(0xffff);
90     }
91 }