Update MRI projects for Aluminium
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / multipart / MultipartReplyMeterConfigDeserializerTest.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.deserialization.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.util.ByteBufUtils;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyMeterConfig;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterFlags;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.band.type.band.type.Drop;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.band.type.band.type.DscpRemark;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
22
23 public class MultipartReplyMeterConfigDeserializerTest extends AbstractMultipartDeserializerTest {
24     private static final byte PADDING_IN_METER_BAND_DROP_HEADER = 4;
25     private static final byte PADDING_IN_METER_BAND_DSCP_HEADER = 3;
26     private static final int OFPMBTDROP = 1;
27     private static final int OFPMBTDSCP = 2;
28     private static final short ITEM_LENGTH = 16;
29     private static final boolean MF_KBPS = true;
30     private static final boolean MF_PKTPS = false;
31     private static final boolean MF_BURST = true;
32     private static final boolean MF_STATS = true;
33     private static final MeterFlags FLAGS = new MeterFlags(MF_KBPS, MF_PKTPS, MF_BURST, MF_STATS);
34     private static final int METER_ID = 1;
35     private static final short SUB_ITEM = 8;
36     private static final int DROP_RATE = 2;
37     private static final int DROP_BURST_SIZE = 3;
38     private static final int DSCP_REMARK_RATE = 3;
39     private static final int DSCP_REMARK_BURST_SIZE = 3;
40     private static final byte PREC_LEVEL = 3;
41
42     @Test
43     public void deserializeDrop() {
44         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
45         writeCommonAtributes(buffer);
46         buffer.writeShort(OFPMBTDROP);
47         buffer.writeShort(SUB_ITEM);
48         buffer.writeInt(DROP_RATE);
49         buffer.writeInt(DROP_BURST_SIZE);
50         buffer.writeZero(PADDING_IN_METER_BAND_DROP_HEADER);
51
52         final MultipartReplyMeterConfig reply = (MultipartReplyMeterConfig) deserializeMultipart(buffer);
53
54         assertEquals(METER_ID,
55             reply.nonnullMeterConfigStats().values().iterator().next().getMeterId().getValue().intValue());
56         assertEquals(FLAGS, reply.nonnullMeterConfigStats().values().iterator().next().getFlags());
57         final Drop drop = (Drop) reply.nonnullMeterConfigStats().values().iterator().next()
58                 .getMeterBandHeaders().nonnullMeterBandHeader().values().iterator().next().getBandType();
59         assertEquals(DROP_RATE, drop.getDropRate().intValue());
60         assertEquals(DROP_BURST_SIZE, drop.getDropBurstSize().intValue());
61     }
62
63     @Test
64     public void deserializeDscp() {
65         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
66         writeCommonAtributes(buffer);
67         buffer.writeShort(OFPMBTDSCP);
68         buffer.writeShort(SUB_ITEM);
69         buffer.writeInt(DSCP_REMARK_RATE);
70         buffer.writeInt(DSCP_REMARK_BURST_SIZE);
71         buffer.writeByte(PREC_LEVEL);
72         buffer.writeZero(PADDING_IN_METER_BAND_DSCP_HEADER);
73
74         final MultipartReplyMeterConfig reply = (MultipartReplyMeterConfig) deserializeMultipart(buffer);
75
76         final DscpRemark dscpRemark = (DscpRemark) reply.nonnullMeterConfigStats().values().iterator().next()
77                 .getMeterBandHeaders().nonnullMeterBandHeader().values().iterator().next().getBandType();
78         assertEquals(DSCP_REMARK_RATE, dscpRemark.getDscpRemarkRate().intValue());
79         assertEquals(DSCP_REMARK_BURST_SIZE, dscpRemark.getDscpRemarkBurstSize().intValue());
80         assertEquals(PREC_LEVEL, dscpRemark.getPrecLevel().byteValue());
81     }
82
83     @Override
84     protected int getType() {
85         return MultipartType.OFPMPMETERCONFIG.getIntValue();
86     }
87
88     private void writeCommonAtributes(final ByteBuf buffer) {
89         buffer.writeShort(ITEM_LENGTH);
90         buffer.writeShort(ByteBufUtils.fillBitMask(0,
91                 FLAGS.isMeterKbps(),
92                 FLAGS.isMeterPktps(),
93                 FLAGS.isMeterBurst(),
94                 FLAGS.isMeterStats()));
95         buffer.writeInt(METER_ID);
96     }
97 }