Bug 5540 - MeterStatsResponseConvertor, MeterConfigStatsResponseConvertor
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / MeterStatsResponseConvertorTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.openflow.md.core.sal.convertor;
10
11 import static org.junit.Assert.assertEquals;
12
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Optional;
18 import org.junit.Test;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.meter.band.stats.BandStat;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStatsKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.MeterStatsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.meter.stats.MeterBandStats;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.meter.stats.MeterBandStatsBuilder;
26
27 public class MeterStatsResponseConvertorTest {
28     private static final int PRESET_COUNT = 7;
29
30
31     private static List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply
32             .multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.MeterStats> createMeterStatsLit() {
33
34         List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply
35                 .multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.MeterStats> allMeterStats = new ArrayList<>();
36         MeterStatsBuilder meterStatsBuilder = new MeterStatsBuilder();
37         for (int i = 0; i < PRESET_COUNT; i++) {
38             meterStatsBuilder.setByteInCount(BigInteger.valueOf(i));
39             meterStatsBuilder.setDurationNsec((long) 1000 * i);
40             meterStatsBuilder.setDurationSec((long) 10 * i);
41             meterStatsBuilder.setFlowCount((long) i);
42             MeterBandStatsBuilder meterBandStatsBuilder = new MeterBandStatsBuilder();
43             List<MeterBandStats> meterBandStatses = new ArrayList<>();
44             for (int j = 0; j < PRESET_COUNT; j++) {
45                 meterBandStatsBuilder.setByteBandCount(BigInteger.valueOf(j));
46                 meterBandStatsBuilder.setPacketBandCount(BigInteger.valueOf(j));
47                 meterBandStatses.add(meterBandStatsBuilder.build());
48             }
49             meterStatsBuilder.setMeterBandStats(meterBandStatses);
50             meterStatsBuilder.setMeterId(new MeterId((long) i));
51             meterStatsBuilder.setPacketInCount(BigInteger.valueOf(i));
52
53             allMeterStats.add(meterStatsBuilder.build());
54         }
55
56         return allMeterStats;
57     }
58
59     @Test
60     /**
61      * Test of basic mapping functionality of {@link MeterStatsResponseConvertor#convert(java.util.List)}
62      */
63     public void testToSALMeterStatsList() {
64
65         Optional<List<MeterStats>> meterStatsListOptional = ConvertorManager.getInstance().convert(createMeterStatsLit());
66         List<MeterStats> meterStatsList = meterStatsListOptional.orElse(Collections.emptyList());
67         assertEquals(PRESET_COUNT, meterStatsList.size());
68
69         int cnt = 0;
70         for (MeterStats meterStats : meterStatsList) {
71             assertEquals((new MeterStatsKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId((long) cnt))).getMeterId(), meterStats.getKey().getMeterId());
72             assertEquals((BigInteger.valueOf(cnt)), meterStats.getByteInCount().getValue());
73             assertEquals(new Long(1000 * cnt), meterStats.getDuration().getNanosecond().getValue());
74             assertEquals(new Long(10 * cnt), meterStats.getDuration().getSecond().getValue());
75
76             assertEquals(new Long(cnt), meterStats.getFlowCount().getValue());
77             assertEquals(BigInteger.valueOf(cnt), meterStats.getByteInCount().getValue());
78
79             assertEquals(PRESET_COUNT, meterStats.getMeterBandStats().getBandStat().size());
80             int bandStatCount = 0;
81             for (BandStat bandStat : meterStats.getMeterBandStats().getBandStat()) {
82                 assertEquals(BigInteger.valueOf(bandStatCount), bandStat.getByteBandCount().getValue());
83                 assertEquals(BigInteger.valueOf(bandStatCount), bandStat.getPacketBandCount().getValue());
84                 bandStatCount++;
85             }
86             assertEquals((new MeterStatsKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId((long) cnt))).getMeterId(), meterStats.getMeterId());
87             assertEquals(BigInteger.valueOf(cnt), meterStats.getPacketInCount().getValue());
88             cnt++;
89         }
90     }
91 }