Bug 5540 - Remove ConvertorManager singleton
[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> allMeterStats = new ArrayList<>();
38         MeterStatsBuilder meterStatsBuilder = new MeterStatsBuilder();
39         for (int i = 0; i < PRESET_COUNT; i++) {
40             meterStatsBuilder.setByteInCount(BigInteger.valueOf(i));
41             meterStatsBuilder.setDurationNsec((long) 1000 * i);
42             meterStatsBuilder.setDurationSec((long) 10 * i);
43             meterStatsBuilder.setFlowCount((long) i);
44             MeterBandStatsBuilder meterBandStatsBuilder = new MeterBandStatsBuilder();
45             List<MeterBandStats> meterBandStatses = new ArrayList<>();
46             for (int j = 0; j < PRESET_COUNT; j++) {
47                 meterBandStatsBuilder.setByteBandCount(BigInteger.valueOf(j));
48                 meterBandStatsBuilder.setPacketBandCount(BigInteger.valueOf(j));
49                 meterBandStatses.add(meterBandStatsBuilder.build());
50             }
51             meterStatsBuilder.setMeterBandStats(meterBandStatses);
52             meterStatsBuilder.setMeterId(new MeterId((long) i));
53             meterStatsBuilder.setPacketInCount(BigInteger.valueOf(i));
54
55             allMeterStats.add(meterStatsBuilder.build());
56         }
57
58         return allMeterStats;
59     }
60
61     @Test
62     /**
63      * Test of basic mapping functionality of {@link MeterStatsResponseConvertor#convert(java.util.List)}
64      */
65     public void testToSALMeterStatsList() {
66         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
67         Optional<List<MeterStats>> meterStatsListOptional = convertorManager.convert(createMeterStatsLit(), new VersionConvertorData(OFConstants.OFP_VERSION_1_3));
68         List<MeterStats> meterStatsList = meterStatsListOptional.orElse(Collections.emptyList());
69         assertEquals(PRESET_COUNT, meterStatsList.size());
70
71         int cnt = 0;
72         for (MeterStats meterStats : meterStatsList) {
73             assertEquals((new MeterStatsKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId((long) cnt))).getMeterId(), meterStats.getKey().getMeterId());
74             assertEquals((BigInteger.valueOf(cnt)), meterStats.getByteInCount().getValue());
75             assertEquals(new Long(1000 * cnt), meterStats.getDuration().getNanosecond().getValue());
76             assertEquals(new Long(10 * cnt), meterStats.getDuration().getSecond().getValue());
77
78             assertEquals(new Long(cnt), meterStats.getFlowCount().getValue());
79             assertEquals(BigInteger.valueOf(cnt), meterStats.getByteInCount().getValue());
80
81             assertEquals(PRESET_COUNT, meterStats.getMeterBandStats().getBandStat().size());
82             int bandStatCount = 0;
83             for (BandStat bandStat : meterStats.getMeterBandStats().getBandStat()) {
84                 assertEquals(BigInteger.valueOf(bandStatCount), bandStat.getByteBandCount().getValue());
85                 assertEquals(BigInteger.valueOf(bandStatCount), bandStat.getPacketBandCount().getValue());
86                 bandStatCount++;
87             }
88             assertEquals((new MeterStatsKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId((long) cnt))).getMeterId(), meterStats.getMeterId());
89             assertEquals(BigInteger.valueOf(cnt), meterStats.getPacketInCount().getValue());
90             cnt++;
91         }
92     }
93 }