3c04d436f252ad47f756f94c300f602d7f3076d7
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
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.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.api.ReadTransaction;
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             ReadTransaction 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             ReadTransaction 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     @SuppressFBWarnings("SLF4J_SIGN_ONLY_FORMAT")
147     public void _flowStats(CommandInterpreter ci) {
148         int flowCount = 0;
149         int flowStatsCount = 0;
150         List<Node> nodes = getNodes();
151         for (Node node2 : nodes) {
152             NodeKey nodeKey = node2.key();
153             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
154                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
155
156             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
157             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
158
159             if (node != null) {
160                 List<Table> tables = node.getTable();
161                 for (Table table2 : tables) {
162                     TableKey tableKey = table2.key();
163                     InstanceIdentifier<Table> tableRef = InstanceIdentifier.create(Nodes.class)
164                             .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
165                             .child(Table.class, tableKey);
166                     Table table = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, tableRef);
167                     if (table != null) {
168                         if (table.getFlow() != null) {
169                             List<Flow> flows = table.getFlow();
170                             for (Flow flow2 : flows) {
171                                 flowCount++;
172                                 FlowKey flowKey = flow2.key();
173                                 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.create(Nodes.class)
174                                         .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
175                                         .child(Table.class, tableKey).child(Flow.class, flowKey);
176                                 Flow flow = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, flowRef);
177                                 if (flow != null) {
178                                     FlowStatisticsData data = flow.augmentation(FlowStatisticsData.class);
179                                     if (null != data) {
180                                         flowStatsCount++;
181                                         LOG.debug("--------------------------------------------");
182                                         ci.print(data);
183                                     }
184                                 }
185
186                             }
187                         }
188                     }
189                 }
190             }
191         }
192
193         if (flowCount == flowStatsCount) {
194             LOG.debug("flowStats - Success");
195         } else {
196             LOG.debug("flowStats - Failed");
197             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
198         }
199
200     }
201
202     public void _tableStats(CommandInterpreter ci) {
203         int tableCount = 0;
204         int tableStatsCount = 0;
205         List<Node> nodes = getNodes();
206         for (Node node2 : nodes) {
207             NodeKey nodeKey = node2.key();
208             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
209                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
210
211             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
212             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
213             if (node != null) {
214                 List<Table> tables = node.getTable();
215                 for (Table table2 : tables) {
216                     tableCount++;
217                     TableKey tableKey = table2.key();
218                     InstanceIdentifier<Table> tableRef = InstanceIdentifier.create(Nodes.class)
219                             .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
220                             .child(Table.class, tableKey);
221                     Table table = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, tableRef);
222                     if (table != null) {
223                         FlowTableStatisticsData data = table.augmentation(FlowTableStatisticsData.class);
224                         if (null != data) {
225                             tableStatsCount++;
226                         }
227                     }
228                 }
229             }
230         }
231
232         if (tableCount == tableStatsCount) {
233             LOG.debug("tableStats - Success");
234         } else {
235             LOG.debug("tableStats - Failed");
236             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
237         }
238
239     }
240
241     public void _groupStats(CommandInterpreter ci) {
242         int groupCount = 0;
243         int groupStatsCount = 0;
244         NodeGroupStatistics data = null;
245         List<Node> nodes = getNodes();
246         for (Node node2 : nodes) {
247             NodeKey nodeKey = node2.key();
248             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
249                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
250             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
251             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
252             if (node != null) {
253                 if (node.getGroup() != null) {
254                     List<Group> groups = node.getGroup();
255                     for (Group group2 : groups) {
256                         groupCount++;
257                         GroupKey groupKey = group2.key();
258                         InstanceIdentifier<Group> groupRef = InstanceIdentifier.create(Nodes.class)
259                                 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
260                                 .child(Group.class, groupKey);
261                         Group group = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, groupRef);
262                         if (group != null) {
263                             data = group.augmentation(NodeGroupStatistics.class);
264                             if (null != data) {
265                                 groupStatsCount++;
266                             }
267                         }
268                     }
269                 }
270
271             }
272         }
273
274         if (groupCount == groupStatsCount) {
275             LOG.debug("---------------------groupStats - Success-------------------------------");
276         } else {
277             LOG.debug("------------------------------groupStats - Failed--------------------------");
278             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
279         }
280     }
281
282     public void _groupDescStats(CommandInterpreter ci) {
283         int groupCount = 0;
284         int groupDescStatsCount = 0;
285         NodeGroupDescStats data = null;
286         List<Node> nodes = getNodes();
287         for (Node node2 : nodes) {
288             NodeKey nodeKey = node2.key();
289             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
290                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
291             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
292             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
293
294             if (node != null) {
295                 if (node.getGroup() != null) {
296                     List<Group> groups = node.getGroup();
297                     for (Group group2 : groups) {
298                         groupCount++;
299                         GroupKey groupKey = group2.key();
300                         InstanceIdentifier<Group> groupRef = InstanceIdentifier.create(Nodes.class)
301                                 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
302                                 .child(Group.class, groupKey);
303                         Group group = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, groupRef);
304                         if (group != null) {
305                             data = group.augmentation(NodeGroupDescStats.class);
306                             if (null != data) {
307                                 groupDescStatsCount++;
308                             }
309                         }
310
311                     }
312                 }
313             }
314
315             if (groupCount == groupDescStatsCount) {
316                 LOG.debug("---------------------groupDescStats - Success-------------------------------");
317             } else {
318                 LOG.debug("------------------------------groupDescStats - Failed--------------------------");
319                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
320             }
321         }
322     }
323
324     public void _meterStats(CommandInterpreter ci) {
325         int meterCount = 0;
326         int meterStatsCount = 0;
327         NodeMeterStatistics data = null;
328         List<Node> nodes = getNodes();
329         for (Node node2 : nodes) {
330             NodeKey nodeKey = node2.key();
331             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
332                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
333             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
334             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
335             if (node != null) {
336                 if (node.getMeter() != null) {
337                     List<Meter> meters = node.getMeter();
338                     for (Meter meter2 : meters) {
339                         meterCount++;
340                         MeterKey meterKey = meter2.key();
341                         InstanceIdentifier<Meter> meterRef = InstanceIdentifier.create(Nodes.class)
342                                 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
343                                 .child(Meter.class, meterKey);
344                         Meter meter = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, meterRef);
345                         if (meter != null) {
346                             data = meter.augmentation(NodeMeterStatistics.class);
347                             if (null != data) {
348                                 meterStatsCount++;
349                             }
350                         }
351                     }
352
353                 }
354             }
355         }
356
357         if (meterCount == meterStatsCount) {
358             LOG.debug("---------------------------meterStats - Success-------------------------------------");
359         } else {
360             LOG.debug("----------------------------meterStats - Failed-------------------------------------");
361             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
362         }
363     }
364
365     public void _meterConfigStats(CommandInterpreter ci) {
366         int meterCount = 0;
367         int meterConfigStatsCount = 0;
368         NodeMeterConfigStats data = null;
369         List<Node> nodes = getNodes();
370         for (Node node2 : nodes) {
371             NodeKey nodeKey = node2.key();
372             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
373                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
374             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
375             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
376             if (node != null) {
377                 if (node.getMeter() != null) {
378                     List<Meter> meters = node.getMeter();
379                     for (Meter meter2 : meters) {
380                         meterCount++;
381                         MeterKey meterKey = meter2.key();
382                         InstanceIdentifier<Meter> meterRef = InstanceIdentifier.create(Nodes.class)
383                                 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
384                                 .child(Meter.class, meterKey);
385                         Meter meter = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, meterRef);
386                         if (meter != null) {
387                             data = meter.augmentation(NodeMeterConfigStats.class);
388                             if (null != data) {
389                                 meterConfigStatsCount++;
390                             }
391                         }
392                     }
393
394                 }
395             }
396         }
397
398         if (meterCount == meterConfigStatsCount) {
399             LOG.debug("---------------------------meterConfigStats - Success-------------------------------------");
400             ci.print(data);
401         } else {
402             LOG.debug("----------------------------meterConfigStats - Failed-------------------------------------");
403             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
404         }
405     }
406
407     public void _aggregateStats(CommandInterpreter ci) {
408         int aggregateFlowCount = 0;
409         int aggerateFlowStatsCount = 0;
410         List<Node> nodes = getNodes();
411         for (Node node2 : nodes) {
412             NodeKey nodeKey = node2.key();
413             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
414                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
415             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
416             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
417             if (node != null) {
418                 List<Table> tables = node.getTable();
419                 for (Table table2 : tables) {
420                     aggregateFlowCount++;
421                     TableKey tableKey = table2.key();
422                     InstanceIdentifier<Table> tableRef = InstanceIdentifier.create(Nodes.class)
423                             .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
424                             .child(Table.class, tableKey);
425                     Table table = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, tableRef);
426                     if (table != null) {
427                         AggregateFlowStatisticsData data = table.augmentation(AggregateFlowStatisticsData.class);
428                         if (null != data) {
429                             aggerateFlowStatsCount++;
430                         }
431                     }
432                 }
433             }
434         }
435
436         if (aggregateFlowCount == aggerateFlowStatsCount) {
437             LOG.debug("aggregateStats - Success");
438         } else {
439             LOG.debug("aggregateStats - Failed");
440             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
441         }
442
443     }
444
445     public void _descStats(CommandInterpreter ci) {
446         int descCount = 0;
447         int descStatsCount = 0;
448         List<Node> nodes = getNodes();
449         for (Node node2 : nodes) {
450             descCount++;
451             NodeKey nodeKey = node2.key();
452             InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
453                     .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
454             ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
455             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
456             if (node != null) {
457                 if (null != node.getHardware() && null != node.getManufacturer() && null != node.getSoftware()) {
458                     descStatsCount++;
459                 }
460             }
461         }
462
463         if (descCount == descStatsCount) {
464             LOG.debug("descStats - Success");
465         } else {
466             LOG.debug("descStats - Failed");
467             LOG.debug("System fetches stats data in 50 seconds interval, so please wait and try again.");
468         }
469
470     }
471
472     private List<Node> getNodes() {
473         ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
474         InstanceIdentifier<Nodes> nodesID = InstanceIdentifier.create(Nodes.class);
475         Nodes nodes = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodesID);
476         if (nodes == null) {
477             throw new RuntimeException("nodes are not found, pls add the node.");
478         }
479         return nodes.getNode();
480     }
481
482     @Override
483     public String getHelp() {
484         StringBuilder help = new StringBuilder();
485         help.append("---MD-SAL Stats test module---\n");
486         return help.toString();
487     }
488
489 }