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