Fix checkstyle warnings in netty-threadgroup-config.
[controller.git] / opendaylight / md-sal / statistics-manager / src / test / java / test / mock / MeterStatisticsTest.java
1 package test.mock;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6
7 import java.util.concurrent.ExecutionException;
8
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;
27
28 import test.mock.util.StatisticsManagerTest;
29
30 import com.google.common.base.Optional;
31
32 public class MeterStatisticsTest extends StatisticsManagerTest {
33     private final Object waitObject = new Object();
34
35 //    @Test(timeout = 5000)
36     public void addedMeterOnDemandStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
37         final StatisticsManagerActivator activator = new StatisticsManagerActivator();
38         activator.onSessionInitiated(providerContext);
39
40         addFlowCapableNode(s1Key);
41
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());
45
46         final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
47         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
48         writeTx.put(LogicalDatastoreType.OPERATIONAL, meterII, meter);
49         assertCommit(writeTx.submit());
50
51         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
52                 meterII.augmentation(NodeMeterStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
53
54         synchronized (waitObject) {
55             waitObject.wait();
56         }
57
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());
64     }
65
66 //    @Test(timeout = 5000)
67     public void deletedMeterStatsRemovalTest() throws ExecutionException, InterruptedException, ReadFailedException {
68         final StatisticsManagerActivator activator = new StatisticsManagerActivator();
69         activator.onSessionInitiated(providerContext);
70
71         addFlowCapableNode(s1Key);
72
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());
76
77         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
78         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
79         writeTx.put(LogicalDatastoreType.OPERATIONAL, meterII, meter);
80         assertCommit(writeTx.submit());
81
82         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
83                 meterII.augmentation(NodeMeterStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
84
85         synchronized (waitObject) {
86             waitObject.wait();
87         }
88
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());
95
96         writeTx = getDataBroker().newWriteOnlyTransaction();
97         writeTx.delete(LogicalDatastoreType.CONFIGURATION, meterII);
98         assertCommit(writeTx.submit());
99
100         readTx = getDataBroker().newReadOnlyTransaction();
101         final Optional<Meter> meterOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, meterII).checkedGet();
102         assertFalse(meterOptional.isPresent());
103     }
104
105 //    @Test(timeout = 23000)
106     public void getAllStatsFromConnectedNodeTest() throws ExecutionException, InterruptedException {
107         final StatisticsManagerActivator activator = new StatisticsManagerActivator();
108         activator.onSessionInitiated(providerContext);
109
110         addFlowCapableNodeWithFeatures(s1Key, true);
111
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);
116
117         synchronized (waitObject) {
118             waitObject.wait();
119         }
120
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();
125
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());
132
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());
138     }
139
140     private class ChangeListener implements DataChangeListener {
141
142         @Override
143         public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
144             synchronized (waitObject) {
145                 waitObject.notify();
146             }
147         }
148     }
149 }
150