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