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