56d82f0989743f2daa72180a5dbb1f16e8dc110b
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / messages / MeterMessageDeserializerTest.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.deserialization.messages;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.UnpooledByteBufAllocator;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.util.ByteBufUtils;
20 import org.opendaylight.openflowplugin.impl.protocol.deserialization.AbstractDeserializerTest;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.band.type.band.type.Drop;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.band.type.band.type.DscpRemark;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.meter.band.headers.MeterBandHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterModCommand;
26
27 public class MeterMessageDeserializerTest extends AbstractDeserializerTest {
28
29     private static final byte PADDING_IN_METER_BAND_DROP_HEADER = 4;
30     private static final byte PADDING_IN_METER_BAND_DSCP_HEADER = 3;
31     private static final int TYPE = 29;
32     private static final int XID = 42;
33     private static final MeterModCommand COMMAND = MeterModCommand.OFPMCADD;
34     private static final boolean IS_KBPS = true;
35     private static final boolean IS_PKTPS = true;
36     private static final boolean IS_BURST = false;
37     private static final boolean IS_STATS = false;
38     private static final int ID = 10;
39     private static final int DROP_RATE = 15;
40     private static final int DROP_BURST = 16;
41     private static final int DSCP_RATE = 17;
42     private static final int DSCP_BURST = 18;
43     private static final int DSCP_PREC = 19;
44
45     private ByteBuf buffer;
46
47     @Override
48     protected void init() {
49         buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
50     }
51
52     @Test
53     public void deserialize() {
54         buffer.writeByte(TYPE); // Message type
55         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
56         buffer.writeInt(XID);
57         buffer.writeShort(COMMAND.getIntValue());
58         buffer.writeShort(ByteBufUtils.fillBitMask(0,
59                 IS_KBPS,
60                 IS_PKTPS,
61                 IS_BURST,
62                 IS_STATS));
63         buffer.writeInt(ID);
64
65         // Drop band
66         buffer.writeShort(1);
67         buffer.writeInt(DROP_RATE);
68         buffer.writeInt(DROP_BURST);
69         buffer.writeZero(PADDING_IN_METER_BAND_DROP_HEADER);
70
71         // Dscp remark band
72         buffer.writeShort(2);
73         buffer.writeInt(DSCP_RATE);
74         buffer.writeInt(DSCP_BURST);
75         buffer.writeByte(DSCP_PREC);
76         buffer.writeZero(PADDING_IN_METER_BAND_DSCP_HEADER);
77
78         final MeterMessage message = (MeterMessage)getFactory().deserialize(buffer, EncodeConstants.OF13_VERSION_ID);
79
80         assertEquals(message.getXid().intValue(), XID);
81         assertEquals(message.getCommand().getIntValue(), COMMAND.getIntValue());
82         assertEquals(message.getFlags().isMeterBurst(), IS_BURST);
83         assertEquals(message.getFlags().isMeterKbps(), IS_KBPS);
84         assertEquals(message.getFlags().isMeterPktps(), IS_PKTPS);
85         assertEquals(message.getFlags().isMeterStats(), IS_STATS);
86         assertEquals(message.getMeterId().getValue().intValue(), ID);
87
88         final List<MeterBandHeader> meterBandHeader = message.getMeterBandHeaders().getMeterBandHeader();
89         assertEquals(meterBandHeader.size(), 2);
90
91         // Drop band
92         final MeterBandHeader dropHeader = meterBandHeader.get(0);
93         assertEquals(Drop.class, dropHeader.getBandType().implementedInterface());
94         assertTrue(dropHeader.getMeterBandTypes().getFlags().isOfpmbtDrop());
95
96         final Drop drop = (Drop) dropHeader.getBandType();
97         assertEquals(DROP_RATE, drop.getDropRate().intValue());
98         assertEquals(DROP_BURST, drop.getDropBurstSize().intValue());
99
100         // Dscp band
101         final MeterBandHeader dscpHeader = meterBandHeader.get(1);
102         assertEquals(DscpRemark.class, dscpHeader.getBandType().implementedInterface());
103         assertTrue(dscpHeader.getMeterBandTypes().getFlags().isOfpmbtDscpRemark());
104
105         final DscpRemark dscpRemark = (DscpRemark) dscpHeader.getBandType();
106         assertEquals(DSCP_RATE, dscpRemark.getDscpRemarkRate().intValue());
107         assertEquals(DSCP_BURST, dscpRemark.getDscpRemarkBurstSize().intValue());
108
109         assertEquals(buffer.readableBytes(), 0);
110     }
111
112 }