Bug 2175 - Migrate frm, statistics and inventory manager to config subsystem
[controller.git] / opendaylight / md-sal / statistics-manager / src / test / java / test / mock / StatCollectorTest.java
1 package test.mock;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5
6 import com.google.common.base.Optional;
7 import java.util.concurrent.ExecutionException;
8 import org.junit.Test;
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.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityFlowStats;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityGroupStats;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortStats;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityQueueStats;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityTableStats;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeatures;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeatures;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
49 import org.opendaylight.yangtools.yang.binding.DataObject;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import test.mock.util.StatisticsManagerTest;
52
53 public class StatCollectorTest extends StatisticsManagerTest {
54     private final Object waitObject = new Object();
55
56     @Test(timeout = 200000)
57     public void getAllFlowStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
58         setupStatisticsManager();
59
60         addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityFlowStats.class);
61
62         final Flow flow = getFlow();
63
64         final InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
65                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(flow.getTableId()));
66
67         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
68                 tableII.child(Flow.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
69
70         synchronized (waitObject) {
71             waitObject.wait();
72         }
73
74         final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
75         final Optional<Table> tableOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
76                 .child(Node.class, s1Key).augmentation(FlowCapableNode.class)
77                 .child(Table.class, new TableKey(flow.getTableId()))).checkedGet();
78         assertTrue(tableOptional.isPresent());
79         final FlowStatisticsData flowStats = tableOptional.get().getFlow().get(0).getAugmentation(FlowStatisticsData.class);
80         assertTrue(flowStats != null);
81         assertEquals(COUNTER_64_TEST_VALUE, flowStats.getFlowStatistics().getByteCount());
82     }
83
84     @Test(timeout = 200000)
85     public void getAllGroupStatsFeatureNotAdvertisedTest() throws ExecutionException, InterruptedException {
86         setupStatisticsManager();
87
88         addFlowCapableNodeWithFeatures(s1Key, true);
89
90         final InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
91                 .augmentation(FlowCapableNode.class).child(Group.class, getGroup().getKey());
92         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
93                 groupII.augmentation(NodeGroupStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
94
95         synchronized (waitObject) {
96             waitObject.wait();
97         }
98
99         ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
100         final Optional<Group> optionalGroup = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
101                 .child(Node.class, s1Key).augmentation(FlowCapableNode.class)
102                 .child(Group.class, getGroup().getKey())).get();
103
104         assertTrue(optionalGroup.isPresent());
105         assertTrue(optionalGroup.get().getAugmentation(NodeGroupDescStats.class) != null);
106         final NodeGroupStatistics groupStats = optionalGroup.get().getAugmentation(NodeGroupStatistics.class);
107         assertTrue(groupStats != null);
108         assertEquals(COUNTER_64_TEST_VALUE, groupStats.getGroupStatistics().getByteCount());
109
110         readTx = getDataBroker().newReadOnlyTransaction();
111         final Optional<GroupFeatures> optionalGroupFeatures = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
112                 .child(Node.class, s1Key).augmentation(NodeGroupFeatures.class).child(GroupFeatures.class)).get();
113         assertTrue(optionalGroupFeatures.isPresent());
114         assertEquals(1, optionalGroupFeatures.get().getMaxGroups().size());
115         assertEquals(MAX_GROUPS_TEST_VALUE, optionalGroupFeatures.get().getMaxGroups().get(0));
116     }
117
118     @Test(timeout = 200000)
119     public void getAllGroupStatsFeatureAdvertisedTest() throws ExecutionException, InterruptedException {
120         setupStatisticsManager();
121
122         addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityGroupStats.class);
123
124         final InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
125                 .augmentation(FlowCapableNode.class).child(Group.class, getGroup().getKey());
126         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
127                 groupII.augmentation(NodeGroupStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
128
129         synchronized (waitObject) {
130             waitObject.wait();
131         }
132
133         ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
134         final Optional<Group> optionalGroup = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
135                 .child(Node.class, s1Key).augmentation(FlowCapableNode.class)
136                 .child(Group.class, getGroup().getKey())).get();
137
138         assertTrue(optionalGroup.isPresent());
139         assertTrue(optionalGroup.get().getAugmentation(NodeGroupDescStats.class) != null);
140         final NodeGroupStatistics groupStats = optionalGroup.get().getAugmentation(NodeGroupStatistics.class);
141         assertTrue(groupStats != null);
142         assertEquals(COUNTER_64_TEST_VALUE, groupStats.getGroupStatistics().getByteCount());
143
144         readTx = getDataBroker().newReadOnlyTransaction();
145         final Optional<GroupFeatures> optionalGroupFeatures = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
146                 .child(Node.class, s1Key).augmentation(NodeGroupFeatures.class).child(GroupFeatures.class)).get();
147         assertTrue(optionalGroupFeatures.isPresent());
148         assertEquals(1, optionalGroupFeatures.get().getMaxGroups().size());
149         assertEquals(MAX_GROUPS_TEST_VALUE, optionalGroupFeatures.get().getMaxGroups().get(0));
150     }
151
152     @Test(timeout = 200000)
153     public void getAllMeterStatsTest() throws ExecutionException, InterruptedException {
154         setupStatisticsManager();
155
156         addFlowCapableNodeWithFeatures(s1Key, true);
157
158         final InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
159                 .augmentation(FlowCapableNode.class).child(Meter.class, getMeter().getKey());
160         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
161                 meterII.augmentation(NodeMeterStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
162
163         synchronized (waitObject) {
164             waitObject.wait();
165         }
166
167         ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
168         final Optional<Meter> optionalMeter = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
169                 .child(Node.class, s1Key).augmentation(FlowCapableNode.class)
170                 .child(Meter.class, getMeter().getKey())).get();
171
172         assertTrue(optionalMeter.isPresent());
173         assertTrue(optionalMeter.get().getAugmentation(NodeMeterConfigStats.class) != null);
174         final NodeMeterStatistics meterStats = optionalMeter.get().getAugmentation(NodeMeterStatistics.class);
175         assertTrue(meterStats != null);
176         assertEquals(COUNTER_64_TEST_VALUE, meterStats.getMeterStatistics().getByteInCount());
177         assertEquals(COUNTER_64_TEST_VALUE, meterStats.getMeterStatistics().getPacketInCount());
178
179         readTx = getDataBroker().newReadOnlyTransaction();
180         final Optional<MeterFeatures> optionalMeterFeautures = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
181                 .child(Node.class, s1Key).augmentation(NodeMeterFeatures.class).child(MeterFeatures.class)).get();
182         assertTrue(optionalMeterFeautures.isPresent());
183         assertEquals(COUNTER_32_TEST_VALUE, optionalMeterFeautures.get().getMaxMeter());
184     }
185
186     @Test(timeout = 200000)
187     public void getAllQueueStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
188         setupStatisticsManager();
189
190         addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityQueueStats.class);
191
192         final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder();
193         final FlowCapableNodeConnectorBuilder fcncBuilder = new FlowCapableNodeConnectorBuilder();
194         ncBuilder.setKey(new NodeConnectorKey(getNodeConnectorId()));
195         ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcncBuilder.build());
196
197         final InstanceIdentifier<NodeConnector> nodeConnectorII = InstanceIdentifier.create(Nodes.class)
198                 .child(Node.class, s1Key)
199                 .child(NodeConnector.class, ncBuilder.getKey());
200
201         final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
202         writeTx.put(LogicalDatastoreType.OPERATIONAL, nodeConnectorII, ncBuilder.build());
203         final InstanceIdentifier<Queue> queueII = nodeConnectorII.augmentation(FlowCapableNodeConnector.class)
204                 .child(Queue.class, getQueue().getKey());
205         final QueueBuilder qBuilder = new QueueBuilder(getQueue());
206         writeTx.put(LogicalDatastoreType.OPERATIONAL, queueII, qBuilder.build());
207         assertCommit(writeTx.submit());
208
209         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
210                 queueII.augmentation(FlowCapableNodeConnectorQueueStatisticsData.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
211
212         synchronized (waitObject) {
213             waitObject.wait();
214         }
215
216         final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
217         final Optional<Queue> queueOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, queueII).checkedGet();
218         assertTrue(queueOptional.isPresent());
219         final FlowCapableNodeConnectorQueueStatisticsData queueStats =
220                 queueOptional.get().getAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
221         assertTrue(queueStats != null);
222         assertEquals(COUNTER_64_TEST_VALUE,
223                 queueStats.getFlowCapableNodeConnectorQueueStatistics().getTransmittedBytes());
224     }
225
226     @Test(timeout = 200000)
227     public void getAllPortStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
228         setupStatisticsManager();
229
230         addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityPortStats.class);
231
232         final InstanceIdentifier<NodeConnector> nodeConnectorII = InstanceIdentifier.create(Nodes.class)
233                 .child(Node.class, s1Key).child(NodeConnector.class, new NodeConnectorKey(getNodeConnectorId()));
234
235         NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder();
236         ncBuilder.setKey(new NodeConnectorKey(getNodeConnectorId()));
237         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
238         writeTx.put(LogicalDatastoreType.OPERATIONAL, nodeConnectorII, ncBuilder.build());
239         assertCommit(writeTx.submit());
240
241         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
242                 nodeConnectorII.augmentation(FlowCapableNodeConnectorStatisticsData.class),
243                 new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
244
245         synchronized (waitObject) {
246             waitObject.wait();
247         }
248
249         final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
250         final Optional<FlowCapableNodeConnectorStatisticsData> flowCapableNodeConnectorStatisticsDataOptional =
251                 readTx.read(LogicalDatastoreType.OPERATIONAL,
252                         nodeConnectorII.augmentation(FlowCapableNodeConnectorStatisticsData.class)).checkedGet();
253         assertTrue(flowCapableNodeConnectorStatisticsDataOptional.isPresent());
254         assertEquals(BIG_INTEGER_TEST_VALUE,
255                 flowCapableNodeConnectorStatisticsDataOptional.get().getFlowCapableNodeConnectorStatistics()
256                         .getReceiveDrops());
257         assertEquals(BIG_INTEGER_TEST_VALUE,
258                 flowCapableNodeConnectorStatisticsDataOptional.get().getFlowCapableNodeConnectorStatistics()
259                         .getCollisionCount());
260     }
261
262     @Test(timeout = 200000)
263     public void getAllTableStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
264         setupStatisticsManager();
265
266         addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityTableStats.class);
267
268         final TableId tableId = getTableId();
269         final InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
270                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId.getValue()));
271
272         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
273                 tableII.augmentation(FlowTableStatisticsData.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
274
275         synchronized (waitObject) {
276             waitObject.wait();
277         }
278
279         final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
280         final Optional<FlowTableStatisticsData> flowTableStatisticsDataOptional = readTx.read(
281                 LogicalDatastoreType.OPERATIONAL, tableII.augmentation(FlowTableStatisticsData.class)).checkedGet();
282         assertTrue(flowTableStatisticsDataOptional.isPresent());
283         assertEquals(COUNTER_32_TEST_VALUE,
284                 flowTableStatisticsDataOptional.get().getFlowTableStatistics().getActiveFlows());
285         assertEquals(COUNTER_64_TEST_VALUE,
286                 flowTableStatisticsDataOptional.get().getFlowTableStatistics().getPacketsLookedUp());
287     }
288
289     public class ChangeListener implements DataChangeListener {
290
291         @Override
292         public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
293             synchronized (waitObject) {
294                 waitObject.notify();
295             }
296         }
297     }
298 }