3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
7 import java.util.concurrent.ExecutionException;
9 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
10 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
11 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
16 import org.opendaylight.controller.md.statistics.manager.StatisticsManagerActivator;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeatures;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import test.mock.util.StatisticsManagerTest;
30 import com.google.common.base.Optional;
32 public class MeterStatisticsTest extends StatisticsManagerTest {
33 private final Object waitObject = new Object();
35 // @Test(timeout = 5000)
36 public void addedMeterOnDemandStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
37 final StatisticsManagerActivator activator = new StatisticsManagerActivator();
38 activator.onSessionInitiated(providerContext);
40 addFlowCapableNode(s1Key);
42 final Meter meter = getMeter();
43 final InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
44 .augmentation(FlowCapableNode.class).child(Meter.class, meter.getKey());
46 final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
47 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
48 writeTx.put(LogicalDatastoreType.OPERATIONAL, meterII, meter);
49 assertCommit(writeTx.submit());
51 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
52 meterII.augmentation(NodeMeterStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
54 synchronized (waitObject) {
58 final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
59 final Optional<NodeMeterStatistics> meterStatsOptional = readTx.read(LogicalDatastoreType.OPERATIONAL,
60 meterII.augmentation(NodeMeterStatistics.class)).checkedGet();
61 assertTrue(meterStatsOptional.isPresent());
62 assertEquals(COUNTER_64_TEST_VALUE, meterStatsOptional.get().getMeterStatistics().getByteInCount());
63 assertEquals(COUNTER_64_TEST_VALUE, meterStatsOptional.get().getMeterStatistics().getPacketInCount());
66 // @Test(timeout = 5000)
67 public void deletedMeterStatsRemovalTest() throws ExecutionException, InterruptedException, ReadFailedException {
68 final StatisticsManagerActivator activator = new StatisticsManagerActivator();
69 activator.onSessionInitiated(providerContext);
71 addFlowCapableNode(s1Key);
73 final Meter meter = getMeter();
74 final InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
75 .augmentation(FlowCapableNode.class).child(Meter.class, meter.getKey());
77 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
78 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
79 writeTx.put(LogicalDatastoreType.OPERATIONAL, meterII, meter);
80 assertCommit(writeTx.submit());
82 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
83 meterII.augmentation(NodeMeterStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
85 synchronized (waitObject) {
89 ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
90 final Optional<NodeMeterStatistics> meterStatsOptional = readTx.read(LogicalDatastoreType.OPERATIONAL,
91 meterII.augmentation(NodeMeterStatistics.class)).checkedGet();
92 assertTrue(meterStatsOptional.isPresent());
93 assertEquals(COUNTER_64_TEST_VALUE, meterStatsOptional.get().getMeterStatistics().getByteInCount());
94 assertEquals(COUNTER_64_TEST_VALUE, meterStatsOptional.get().getMeterStatistics().getPacketInCount());
96 writeTx = getDataBroker().newWriteOnlyTransaction();
97 writeTx.delete(LogicalDatastoreType.CONFIGURATION, meterII);
98 assertCommit(writeTx.submit());
100 readTx = getDataBroker().newReadOnlyTransaction();
101 final Optional<Meter> meterOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, meterII).checkedGet();
102 assertFalse(meterOptional.isPresent());
105 // @Test(timeout = 23000)
106 public void getAllStatsFromConnectedNodeTest() throws ExecutionException, InterruptedException {
107 final StatisticsManagerActivator activator = new StatisticsManagerActivator();
108 activator.onSessionInitiated(providerContext);
110 addFlowCapableNodeWithFeatures(s1Key, true);
112 final InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
113 .augmentation(FlowCapableNode.class).child(Meter.class, getMeter().getKey());
114 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
115 meterII.augmentation(NodeMeterStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
117 synchronized (waitObject) {
121 ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
122 final Optional<Meter> optionalMeter = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
123 .child(Node.class, s1Key).augmentation(FlowCapableNode.class)
124 .child(Meter.class, getMeter().getKey())).get();
126 assertTrue(optionalMeter.isPresent());
127 assertTrue(optionalMeter.get().getAugmentation(NodeMeterConfigStats.class) != null);
128 final NodeMeterStatistics meterStats = optionalMeter.get().getAugmentation(NodeMeterStatistics.class);
129 assertTrue(meterStats != null);
130 assertEquals(COUNTER_64_TEST_VALUE, meterStats.getMeterStatistics().getByteInCount());
131 assertEquals(COUNTER_64_TEST_VALUE, meterStats.getMeterStatistics().getPacketInCount());
133 readTx = getDataBroker().newReadOnlyTransaction();
134 final Optional<MeterFeatures> optionalMeterFeautures = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
135 .child(Node.class, s1Key).augmentation(NodeMeterFeatures.class).child(MeterFeatures.class)).get();
136 assertTrue(optionalMeterFeautures.isPresent());
137 assertEquals(COUNTER_32_TEST_VALUE, optionalMeterFeautures.get().getMaxMeter());
140 private class ChangeListener implements DataChangeListener {
143 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
144 synchronized (waitObject) {