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