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