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