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