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