Migrate openflowplugim-impl tests to uint types
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / multipart / MultipartRequestFlowStatsSerializerTest.java
1 /*
2  * Copyright (c) 2017 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.multipart;
10
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.UnpooledByteBufAllocator;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
19 import org.opendaylight.openflowplugin.impl.protocol.serialization.AbstractSerializerTest;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.multipart.request.multipart.request.body.MultipartRequestFlowStats;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.multipart.request.multipart.request.body.MultipartRequestFlowStatsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.multipart.request.multipart.request.body.multipart.request.flow.stats.FlowStatsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
27 import org.opendaylight.yangtools.yang.common.Uint32;
28 import org.opendaylight.yangtools.yang.common.Uint64;
29 import org.opendaylight.yangtools.yang.common.Uint8;
30
31 public class MultipartRequestFlowStatsSerializerTest extends AbstractSerializerTest {
32     private static final byte PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_01 = 3;
33     private static final byte PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_02 = 4;
34     private static final Uint8 TABLE_ID = Uint8.valueOf(42);
35     private static final Uint64 OUT_PORT = Uint64.ONE;
36     private static final Uint32 OUT_GROUP = Uint32.TEN;
37     private static final FlowCookie COOKIE = new FlowCookie(Uint64.valueOf(8));
38     private static final FlowCookie COOKIE_MASK = new FlowCookie(Uint64.TEN);
39     private static final Uint8 IP_PROTOCOL_MATCH = Uint8.valueOf(17);
40     private static final Match MATCH = new MatchBuilder()
41             .setIpMatch(new IpMatchBuilder()
42                     .setIpProtocol(IP_PROTOCOL_MATCH)
43                     .build())
44             .build();
45     private static final MultipartRequestFlowStats BODY = new MultipartRequestFlowStatsBuilder()
46             .setFlowStats(new FlowStatsBuilder()
47                 .setTableId(TABLE_ID)
48                 .setOutPort(OUT_PORT)
49                 .setOutGroup(OUT_GROUP)
50                 .setCookie(COOKIE)
51                 .setCookieMask(COOKIE_MASK)
52                 .setMatch(MATCH)
53                 .build()).build();
54
55     private MultipartRequestFlowStatsSerializer serializer;
56
57     @Override
58     protected void init() {
59         serializer = getRegistry().getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID,
60                 MultipartRequestFlowStats.class)) ;
61     }
62
63     @Test
64     public void testSerialize() {
65         final ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
66         serializer.serialize(BODY, out);
67
68         assertEquals(out.readUnsignedByte(), TABLE_ID.shortValue());
69         out.skipBytes(PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_01);
70         assertEquals(out.readUnsignedInt(), OUT_PORT.longValue());
71         assertEquals(out.readUnsignedInt(), OUT_GROUP.longValue());
72         out.skipBytes(PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_02);
73         assertEquals(out.readLong(), COOKIE.getValue().longValue());
74         assertEquals(out.readLong(), COOKIE_MASK.getValue().longValue());
75
76         int matchLength = 9;
77         assertEquals(out.readShort(), 1); // OXM match type
78         assertEquals(out.readUnsignedShort(), matchLength); // OXM match length
79
80         assertEquals(out.readUnsignedShort(), OxmMatchConstants.OPENFLOW_BASIC_CLASS);
81         assertEquals(out.readUnsignedByte(), OxmMatchConstants.IP_PROTO << 1);
82         assertEquals(out.readUnsignedByte(), Byte.BYTES);
83         assertEquals(out.readUnsignedByte(), IP_PROTOCOL_MATCH.shortValue());
84
85         int paddingRemainder = matchLength % EncodeConstants.PADDING;
86         if (paddingRemainder != 0) {
87             out.skipBytes(EncodeConstants.PADDING - paddingRemainder);
88         }
89
90         assertEquals(out.readableBytes(), 0);
91     }
92
93 }