Switch to MD-SAL APIs
[openflowplugin.git] / test-provider / src / main / java / org / opendaylight / openflowplugin / test / OpenflowpluginStatsTestCommandProvider.java
1 /*
2  * Copyright (c) 2014, 2015 IBM Corporation 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 org.opendaylight.openflowplugin.test;
9
10 import java.util.List;
11 import org.eclipse.osgi.framework.console.CommandInterpreter;
12 import org.eclipse.osgi.framework.console.CommandProvider;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.binding.api.ReadTransaction;
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.meters.Meter;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.osgi.framework.BundleContext;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 @SuppressWarnings("checkstyle:MethodName")
44 public class OpenflowpluginStatsTestCommandProvider implements CommandProvider {
45
46     private static final Logger LOG = LoggerFactory.getLogger(OpenflowpluginStatsTestCommandProvider.class);
47     private final DataBroker dataProviderService;
48     private final BundleContext ctx;
49
50     public OpenflowpluginStatsTestCommandProvider(DataBroker dataProviderService, BundleContext ctx) {
51         this.dataProviderService = dataProviderService;
52         this.ctx = ctx;
53     }
54
55     public void init() {
56         ctx.registerService(CommandProvider.class.getName(), this, null);
57
58     }
59
60     public void _portStats(CommandInterpreter ci) {
61         int nodeConnectorCount = 0;
62         int nodeConnectorStatsCount = 0;
63         List<Node> nodes = getNodes();
64         for (Node node2 : nodes) {
65             NodeKey nodeKey = node2.key();
66             InstanceIdentifier<Node> nodeRef = InstanceIdentifier.create(Nodes.class).child(Node.class, nodeKey);
67             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
68             Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
69             if (node != null) {
70                 if (node.getNodeConnector() != null) {
71                     List<NodeConnector> ports = node.getNodeConnector();
72
73                     for (NodeConnector nodeConnector2 : ports) {
74                         nodeConnectorCount++;
75                         NodeConnectorKey nodeConnectorKey = nodeConnector2.key();
76                         InstanceIdentifier<NodeConnector> connectorRef = InstanceIdentifier.create(Nodes.class)
77                                 .child(Node.class, nodeKey).child(NodeConnector.class, nodeConnectorKey);
78                         NodeConnector nodeConnector = TestProviderTransactionUtil.getDataObject(readOnlyTransaction,
79                                 connectorRef);
80                         if (nodeConnector != null) {
81                             FlowCapableNodeConnectorStatisticsData data = nodeConnector
82                                     .augmentation(FlowCapableNodeConnectorStatisticsData.class);
83                             if (null != data) {
84                                 nodeConnectorStatsCount++;
85                             }
86                         }
87                     }
88                 }
89             }
90         }
91
92         if (nodeConnectorCount == nodeConnectorStatsCount) {
93             LOG.debug("portStats - Success");
94         } else {
95             LOG.debug("portStats - Failed");
96             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
97         }
98
99     }
100
101     public void _portDescStats(CommandInterpreter ci) {
102         int nodeConnectorCount = 0;
103         int nodeConnectorDescStatsCount = 0;
104         List<Node> nodes = getNodes();
105         for (Node node2 : nodes) {
106             NodeKey nodeKey = node2.key();
107             InstanceIdentifier<Node> nodeRef = InstanceIdentifier.create(Nodes.class).child(Node.class, nodeKey);
108
109             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
110             Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
111             if (node != null) {
112                 if (node.getNodeConnector() != null) {
113                     List<NodeConnector> ports = node.getNodeConnector();
114                     for (NodeConnector nodeConnector2 : ports) {
115                         nodeConnectorCount++;
116                         NodeConnectorKey nodeConnectorKey = nodeConnector2.key();
117                         InstanceIdentifier<FlowCapableNodeConnector> connectorRef = InstanceIdentifier
118                                 .create(Nodes.class).child(Node.class, nodeKey)
119                                 .child(NodeConnector.class, nodeConnectorKey)
120                                 .augmentation(FlowCapableNodeConnector.class);
121                         FlowCapableNodeConnector nodeConnector = TestProviderTransactionUtil
122                                 .getDataObject(readOnlyTransaction, connectorRef);
123                         if (nodeConnector != null) {
124                             if (null != nodeConnector.getName() && null != nodeConnector.getCurrentFeature()
125                                     && null != nodeConnector.getState() && null != nodeConnector.getHardwareAddress()
126                                     && null != nodeConnector.getPortNumber()) {
127                                 nodeConnectorDescStatsCount++;
128                             }
129                         }
130                     }
131                 }
132
133             }
134         }
135
136         if (nodeConnectorCount == nodeConnectorDescStatsCount) {
137             LOG.debug("portDescStats - Success");
138         } else {
139             LOG.debug("portDescStats - Failed");
140             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
141         }
142
143     }
144
145     public void _flowStats(CommandInterpreter ci) {
146         int flowCount = 0;
147         int flowStatsCount = 0;
148         List<Node> nodes = getNodes();
149         for (Node node2 : nodes) {
150             NodeKey nodeKey = node2.key();
151             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
152                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
153
154             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
155             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
156
157             if (node != null) {
158                 List<Table> tables = node.getTable();
159                 for (Table table2 : tables) {
160                     TableKey tableKey = table2.key();
161                     InstanceIdentifier<Table> tableRef = InstanceIdentifier.create(Nodes.class)
162                             .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
163                             .child(Table.class, tableKey);
164                     Table table = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, tableRef);
165                     if (table != null) {
166                         if (table.getFlow() != null) {
167                             List<Flow> flows = table.getFlow();
168                             for (Flow flow2 : flows) {
169                                 flowCount++;
170                                 FlowKey flowKey = flow2.key();
171                                 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.create(Nodes.class)
172                                         .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
173                                         .child(Table.class, tableKey).child(Flow.class, flowKey);
174                                 Flow flow = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, flowRef);
175                                 if (flow != null) {
176                                     FlowStatisticsData data = flow.augmentation(FlowStatisticsData.class);
177                                     if (null != data) {
178                                         flowStatsCount++;
179                                         LOG.debug("--------------------------------------------");
180                                         ci.print(data);
181                                     }
182                                 }
183
184                             }
185                         }
186                     }
187                 }
188             }
189         }
190
191         if (flowCount == flowStatsCount) {
192             LOG.debug("flowStats - Success");
193         } else {
194             LOG.debug("flowStats - Failed");
195             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
196         }
197
198     }
199
200     public void _tableStats(CommandInterpreter ci) {
201         int tableCount = 0;
202         int tableStatsCount = 0;
203         List<Node> nodes = getNodes();
204         for (Node node2 : nodes) {
205             NodeKey nodeKey = node2.key();
206             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
207                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
208
209             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
210             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
211             if (node != null) {
212                 List<Table> tables = node.getTable();
213                 for (Table table2 : tables) {
214                     tableCount++;
215                     TableKey tableKey = table2.key();
216                     InstanceIdentifier<Table> tableRef = InstanceIdentifier.create(Nodes.class)
217                             .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
218                             .child(Table.class, tableKey);
219                     Table table = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, tableRef);
220                     if (table != null) {
221                         FlowTableStatisticsData data = table.augmentation(FlowTableStatisticsData.class);
222                         if (null != data) {
223                             tableStatsCount++;
224                         }
225                     }
226                 }
227             }
228         }
229
230         if (tableCount == tableStatsCount) {
231             LOG.debug("tableStats - Success");
232         } else {
233             LOG.debug("tableStats - Failed");
234             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
235         }
236
237     }
238
239     public void _groupStats(CommandInterpreter ci) {
240         int groupCount = 0;
241         int groupStatsCount = 0;
242         NodeGroupStatistics data = null;
243         List<Node> nodes = getNodes();
244         for (Node node2 : nodes) {
245             NodeKey nodeKey = node2.key();
246             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
247                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
248             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
249             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
250             if (node != null) {
251                 if (node.getGroup() != null) {
252                     List<Group> groups = node.getGroup();
253                     for (Group group2 : groups) {
254                         groupCount++;
255                         GroupKey groupKey = group2.key();
256                         InstanceIdentifier<Group> groupRef = InstanceIdentifier.create(Nodes.class)
257                                 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
258                                 .child(Group.class, groupKey);
259                         Group group = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, groupRef);
260                         if (group != null) {
261                             data = group.augmentation(NodeGroupStatistics.class);
262                             if (null != data) {
263                                 groupStatsCount++;
264                             }
265                         }
266                     }
267                 }
268
269             }
270         }
271
272         if (groupCount == groupStatsCount) {
273             LOG.debug("---------------------groupStats - Success-------------------------------");
274         } else {
275             LOG.debug("------------------------------groupStats - Failed--------------------------");
276             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
277         }
278     }
279
280     public void _groupDescStats(CommandInterpreter ci) {
281         int groupCount = 0;
282         int groupDescStatsCount = 0;
283         NodeGroupDescStats data = null;
284         List<Node> nodes = getNodes();
285         for (Node node2 : nodes) {
286             NodeKey nodeKey = node2.key();
287             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
288                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
289             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
290             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
291
292             if (node != null) {
293                 if (node.getGroup() != null) {
294                     List<Group> groups = node.getGroup();
295                     for (Group group2 : groups) {
296                         groupCount++;
297                         GroupKey groupKey = group2.key();
298                         InstanceIdentifier<Group> groupRef = InstanceIdentifier.create(Nodes.class)
299                                 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
300                                 .child(Group.class, groupKey);
301                         Group group = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, groupRef);
302                         if (group != null) {
303                             data = group.augmentation(NodeGroupDescStats.class);
304                             if (null != data) {
305                                 groupDescStatsCount++;
306                             }
307                         }
308
309                     }
310                 }
311             }
312
313             if (groupCount == groupDescStatsCount) {
314                 LOG.debug("---------------------groupDescStats - Success-------------------------------");
315             } else {
316                 LOG.debug("------------------------------groupDescStats - Failed--------------------------");
317                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
318             }
319         }
320     }
321
322     public void _meterStats(CommandInterpreter ci) {
323         int meterCount = 0;
324         int meterStatsCount = 0;
325         NodeMeterStatistics data = null;
326         List<Node> nodes = getNodes();
327         for (Node node2 : nodes) {
328             NodeKey nodeKey = node2.key();
329             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
330                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
331             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
332             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
333             if (node != null) {
334                 if (node.getMeter() != null) {
335                     List<Meter> meters = node.getMeter();
336                     for (Meter meter2 : meters) {
337                         meterCount++;
338                         MeterKey meterKey = meter2.key();
339                         InstanceIdentifier<Meter> meterRef = InstanceIdentifier.create(Nodes.class)
340                                 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
341                                 .child(Meter.class, meterKey);
342                         Meter meter = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, meterRef);
343                         if (meter != null) {
344                             data = meter.augmentation(NodeMeterStatistics.class);
345                             if (null != data) {
346                                 meterStatsCount++;
347                             }
348                         }
349                     }
350
351                 }
352             }
353         }
354
355         if (meterCount == meterStatsCount) {
356             LOG.debug("---------------------------meterStats - Success-------------------------------------");
357         } else {
358             LOG.debug("----------------------------meterStats - Failed-------------------------------------");
359             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
360         }
361     }
362
363     public void _meterConfigStats(CommandInterpreter ci) {
364         int meterCount = 0;
365         int meterConfigStatsCount = 0;
366         NodeMeterConfigStats data = null;
367         List<Node> nodes = getNodes();
368         for (Node node2 : nodes) {
369             NodeKey nodeKey = node2.key();
370             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
371                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
372             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
373             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
374             if (node != null) {
375                 if (node.getMeter() != null) {
376                     List<Meter> meters = node.getMeter();
377                     for (Meter meter2 : meters) {
378                         meterCount++;
379                         MeterKey meterKey = meter2.key();
380                         InstanceIdentifier<Meter> meterRef = InstanceIdentifier.create(Nodes.class)
381                                 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
382                                 .child(Meter.class, meterKey);
383                         Meter meter = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, meterRef);
384                         if (meter != null) {
385                             data = meter.augmentation(NodeMeterConfigStats.class);
386                             if (null != data) {
387                                 meterConfigStatsCount++;
388                             }
389                         }
390                     }
391
392                 }
393             }
394         }
395
396         if (meterCount == meterConfigStatsCount) {
397             LOG.debug("---------------------------meterConfigStats - Success-------------------------------------");
398             ci.print(data);
399         } else {
400             LOG.debug("----------------------------meterConfigStats - Failed-------------------------------------");
401             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
402         }
403     }
404
405     public void _aggregateStats(CommandInterpreter ci) {
406         int aggregateFlowCount = 0;
407         int aggerateFlowStatsCount = 0;
408         List<Node> nodes = getNodes();
409         for (Node node2 : nodes) {
410             NodeKey nodeKey = node2.key();
411             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
412                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
413             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
414             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
415             if (node != null) {
416                 List<Table> tables = node.getTable();
417                 for (Table table2 : tables) {
418                     aggregateFlowCount++;
419                     TableKey tableKey = table2.key();
420                     InstanceIdentifier<Table> tableRef = InstanceIdentifier.create(Nodes.class)
421                             .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
422                             .child(Table.class, tableKey);
423                     Table table = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, tableRef);
424                     if (table != null) {
425                         AggregateFlowStatisticsData data = table.augmentation(AggregateFlowStatisticsData.class);
426                         if (null != data) {
427                             aggerateFlowStatsCount++;
428                         }
429                     }
430                 }
431             }
432         }
433
434         if (aggregateFlowCount == aggerateFlowStatsCount) {
435             LOG.debug("aggregateStats - Success");
436         } else {
437             LOG.debug("aggregateStats - Failed");
438             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
439         }
440
441     }
442
443     public void _descStats(CommandInterpreter ci) {
444         int descCount = 0;
445         int descStatsCount = 0;
446         List<Node> nodes = getNodes();
447         for (Node node2 : nodes) {
448             descCount++;
449             NodeKey nodeKey = node2.key();
450             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
451                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
452             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
453             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
454             if (node != null) {
455                 if (null != node.getHardware() && null != node.getManufacturer() && null != node.getSoftware()) {
456                     descStatsCount++;
457                 }
458             }
459         }
460
461         if (descCount == descStatsCount) {
462             LOG.debug("descStats - Success");
463         } else {
464             LOG.debug("descStats - Failed");
465             LOG.debug("System fetches stats data in 50 seconds interval, so please wait and try again.");
466         }
467
468     }
469
470     private List<Node> getNodes() {
471         ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
472         InstanceIdentifier<Nodes> nodesID = InstanceIdentifier.create(Nodes.class);
473         Nodes nodes = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodesID);
474         if (nodes == null) {
475             throw new RuntimeException("nodes are not found, pls add the node.");
476         }
477         return nodes.getNode();
478     }
479
480     @Override
481     public String getHelp() {
482         StringBuilder help = new StringBuilder();
483         help.append("---MD-SAL Stats test module---\n");
484         return help.toString();
485     }
486
487 }