Add Match entry serializers
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / match / TcpFlagsEntrySerializerTest.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.openflowplugin.impl.protocol.serialization.match;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
14 import org.junit.Test;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
17 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TcpFlagsMatchBuilder;
21
22 public class TcpFlagsEntrySerializerTest extends AbstractExperimenterMatchEntrySerializerTest {
23
24     @Test
25     public void testSerialize() throws Exception {
26         final int tcp = 8;
27         final byte[] tcpMask = new byte[] { 30, 30 };
28
29         final Match tcpFlagsMatch = new MatchBuilder()
30                 .setTcpFlagsMatch(new TcpFlagsMatchBuilder()
31                         .setTcpFlags(tcp)
32                         .setTcpFlagsMask(ByteUtil.bytesToUnsignedShort(tcpMask))
33                         .build())
34                 .build();
35
36         assertMatch(tcpFlagsMatch, true, (out) -> {
37             assertEquals(out.readUnsignedShort(), tcp);
38
39             byte[] mask = new byte[2];
40             out.readBytes(mask);
41             assertArrayEquals(mask, tcpMask);
42         });
43
44         final Match tcpFlagsMatchNoMask = new MatchBuilder()
45                 .setTcpFlagsMatch(new TcpFlagsMatchBuilder()
46                         .setTcpFlags(tcp)
47                         .build())
48                 .build();
49
50         assertMatch(tcpFlagsMatchNoMask, false, (out) -> assertEquals(out.readUnsignedShort(), tcp));
51     }
52
53     @Override
54     protected short getLength() {
55         return EncodeConstants.SIZE_OF_SHORT_IN_BYTES;
56     }
57
58     @Override
59     protected int getOxmFieldCode() {
60         return EncodeConstants.ONFOXM_ET_TCP_FLAGS;
61     }
62
63     @Override
64     protected int getOxmClassCode() {
65         return OxmMatchConstants.EXPERIMENTER_CLASS;
66     }
67
68     @Override
69     protected long getExperimenterId() {
70         return EncodeConstants.ONF_EXPERIMENTER_ID;
71     }
72 }