3de6d6443c7aea8d1fcdde2bb397695d06dd361b
[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, reply.getMeterConfigStats().get(0).getMeterId().getValue().intValue());
55         assertEquals(FLAGS, reply.getMeterConfigStats().get(0).getFlags());
56         final Drop drop = (Drop) reply.getMeterConfigStats().get(0)
57                 .getMeterBandHeaders().getMeterBandHeader().get(0).getBandType();
58         assertEquals(DROP_RATE, drop.getDropRate().intValue());
59         assertEquals(DROP_BURST_SIZE, drop.getDropBurstSize().intValue());
60     }
61
62     @Test
63     public void deserializeDscp() {
64         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
65         writeCommonAtributes(buffer);
66         buffer.writeShort(OFPMBTDSCP);
67         buffer.writeShort(SUB_ITEM);
68         buffer.writeInt(DSCP_REMARK_RATE);
69         buffer.writeInt(DSCP_REMARK_BURST_SIZE);
70         buffer.writeByte(PREC_LEVEL);
71         buffer.writeZero(PADDING_IN_METER_BAND_DSCP_HEADER);
72
73         final MultipartReplyMeterConfig reply = (MultipartReplyMeterConfig) deserializeMultipart(buffer);
74
75         final DscpRemark dscpRemark = (DscpRemark) reply.getMeterConfigStats().get(0)
76                 .getMeterBandHeaders().getMeterBandHeader().get(0).getBandType();
77         assertEquals(DSCP_REMARK_RATE, dscpRemark.getDscpRemarkRate().intValue());
78         assertEquals(DSCP_REMARK_BURST_SIZE, dscpRemark.getDscpRemarkBurstSize().intValue());
79         assertEquals(PREC_LEVEL, dscpRemark.getPrecLevel().byteValue());
80     }
81
82     @Override
83     protected int getType() {
84         return MultipartType.OFPMPMETERCONFIG.getIntValue();
85     }
86
87     private void writeCommonAtributes(ByteBuf buffer) {
88         buffer.writeShort(ITEM_LENGTH);
89         buffer.writeShort(ByteBufUtils.fillBitMask(0,
90                 FLAGS.isMeterKbps(),
91                 FLAGS.isMeterPktps(),
92                 FLAGS.isMeterBurst(),
93                 FLAGS.isMeterStats()));
94         buffer.writeInt(METER_ID);
95     }
96 }