2 * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.protocol.deserialization.messages;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.UnpooledByteBufAllocator;
16 import java.util.Iterator;
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;
29 public class MeterMessageDeserializerTest extends AbstractDeserializerTest {
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;
47 private ByteBuf buffer;
50 protected void init() {
51 buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
55 public void deserialize() {
56 buffer.writeByte(TYPE); // Message type
57 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
59 buffer.writeShort(COMMAND.getIntValue());
60 buffer.writeShort(ByteBufUtils.fillBitMask(0,
69 buffer.writeInt(DROP_RATE);
70 buffer.writeInt(DROP_BURST);
71 buffer.writeZero(PADDING_IN_METER_BAND_DROP_HEADER);
75 buffer.writeInt(DSCP_RATE);
76 buffer.writeInt(DSCP_BURST);
77 buffer.writeByte(DSCP_PREC);
78 buffer.writeZero(PADDING_IN_METER_BAND_DSCP_HEADER);
80 final MeterMessage message = (MeterMessage)getFactory().deserialize(buffer, EncodeConstants.OF13_VERSION_ID);
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);
90 final Map<MeterBandHeaderKey, MeterBandHeader> meterBandHeader =
91 message.getMeterBandHeaders().nonnullMeterBandHeader();
92 assertEquals(meterBandHeader.size(), 2);
93 final Iterator<MeterBandHeader> meterBandHeaderIt = meterBandHeader.values().iterator();
96 final MeterBandHeader dropHeader = meterBandHeaderIt.next();
97 assertEquals(Drop.class, dropHeader.getBandType().implementedInterface());
98 assertTrue(dropHeader.getMeterBandTypes().getFlags().isOfpmbtDrop());
100 final Drop drop = (Drop) dropHeader.getBandType();
101 assertEquals(DROP_RATE, drop.getDropRate().intValue());
102 assertEquals(DROP_BURST, drop.getDropBurstSize().intValue());
105 final MeterBandHeader dscpHeader = meterBandHeaderIt.next();
106 assertEquals(DscpRemark.class, dscpHeader.getBandType().implementedInterface());
107 assertTrue(dscpHeader.getMeterBandTypes().getFlags().isOfpmbtDscpRemark());
109 final DscpRemark dscpRemark = (DscpRemark) dscpHeader.getBandType();
110 assertEquals(DSCP_RATE, dscpRemark.getDscpRemarkRate().intValue());
111 assertEquals(DSCP_BURST, dscpRemark.getDscpRemarkBurstSize().intValue());
113 assertEquals(buffer.readableBytes(), 0);