2 * Copyright (c) 2014, 2015 IBM Corporation and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.test;
10 import static java.util.Objects.requireNonNull;
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;
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);
56 private final DataBroker dataBroker;
60 public OpenflowpluginStatsTestCommandProvider(@Reference final DataBroker dataBroker) {
61 this.dataBroker = requireNonNull(dataBroker);
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);
74 if (node.getNodeConnector() != null) {
75 Collection<NodeConnector> ports = node.nonnullNodeConnector().values();
77 for (NodeConnector nodeConnector2 : ports) {
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,
84 if (nodeConnector != null) {
85 FlowCapableNodeConnectorStatisticsData data = nodeConnector
86 .augmentation(FlowCapableNodeConnectorStatisticsData.class);
88 nodeConnectorStatsCount++;
96 if (nodeConnectorCount == nodeConnectorStatsCount) {
97 LOG.debug("portStats - Success");
99 LOG.debug("portStats - Failed");
100 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
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);
113 ReadTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction();
114 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
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++;
140 if (nodeConnectorCount == nodeConnectorDescStatsCount) {
141 LOG.debug("portDescStats - Success");
143 LOG.debug("portDescStats - Failed");
144 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
149 @SuppressFBWarnings("SLF4J_SIGN_ONLY_FORMAT")
150 public void _flowStats(final CommandInterpreter ci) {
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);
159 ReadTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction();
160 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
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);
171 if (table.getFlow() != null) {
172 Collection<Flow> flows = table.nonnullFlow().values();
173 for (Flow flow2 : flows) {
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);
181 FlowStatisticsData data = flow.augmentation(FlowStatisticsData.class);
184 LOG.debug("--------------------------------------------");
196 if (flowCount == flowStatsCount) {
197 LOG.debug("flowStats - Success");
199 LOG.debug("flowStats - Failed");
200 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
205 public void _tableStats(final CommandInterpreter ci) {
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);
214 ReadTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction();
215 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
217 Collection<Table> tables = node.nonnullTable().values();
218 for (Table table2 : tables) {
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);
226 FlowTableStatisticsData data = table.augmentation(FlowTableStatisticsData.class);
235 if (tableCount == tableStatsCount) {
236 LOG.debug("tableStats - Success");
238 LOG.debug("tableStats - Failed");
239 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
244 public void _groupStats(final CommandInterpreter ci) {
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);
256 if (node.getGroup() != null) {
257 Collection<Group> groups = node.nonnullGroup().values();
258 for (Group group2 : groups) {
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);
266 data = group.augmentation(NodeGroupStatistics.class);
277 if (groupCount == groupStatsCount) {
278 LOG.debug("---------------------groupStats - Success-------------------------------");
280 LOG.debug("------------------------------groupStats - Failed--------------------------");
281 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
285 public void _groupDescStats(final CommandInterpreter ci) {
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);
298 if (node.getGroup() != null) {
299 Collection<Group> groups = node.nonnullGroup().values();
300 for (Group group2 : groups) {
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);
308 data = group.augmentation(NodeGroupDescStats.class);
310 groupDescStatsCount++;
318 if (groupCount == groupDescStatsCount) {
319 LOG.debug("---------------------groupDescStats - Success-------------------------------");
321 LOG.debug("------------------------------groupDescStats - Failed--------------------------");
322 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
327 public void _meterStats(final CommandInterpreter ci) {
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);
339 if (node.getMeter() != null) {
340 Collection<Meter> meters = node.nonnullMeter().values();
341 for (Meter meter2 : meters) {
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);
349 data = meter.augmentation(NodeMeterStatistics.class);
360 if (meterCount == meterStatsCount) {
361 LOG.debug("---------------------------meterStats - Success-------------------------------------");
363 LOG.debug("----------------------------meterStats - Failed-------------------------------------");
364 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
368 public void _meterConfigStats(final CommandInterpreter ci) {
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);
380 if (node.getMeter() != null) {
381 Collection<Meter> meters = node.nonnullMeter().values();
382 for (Meter meter2 : meters) {
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);
390 data = meter.augmentation(NodeMeterConfigStats.class);
392 meterConfigStatsCount++;
401 if (meterCount == meterConfigStatsCount) {
402 LOG.debug("---------------------------meterConfigStats - Success-------------------------------------");
405 LOG.debug("----------------------------meterConfigStats - Failed-------------------------------------");
406 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
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);
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);
430 AggregateFlowStatisticsData data = table.augmentation(AggregateFlowStatisticsData.class);
432 aggerateFlowStatsCount++;
439 if (aggregateFlowCount == aggerateFlowStatsCount) {
440 LOG.debug("aggregateStats - Success");
442 LOG.debug("aggregateStats - Failed");
443 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
448 public void _descStats(final CommandInterpreter ci) {
450 int descStatsCount = 0;
451 Collection<Node> nodes = getNodes();
452 for (Node node2 : nodes) {
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);
460 if (null != node.getHardware() && null != node.getManufacturer() && null != node.getSoftware()) {
466 if (descCount == descStatsCount) {
467 LOG.debug("descStats - Success");
469 LOG.debug("descStats - Failed");
470 LOG.debug("System fetches stats data in 50 seconds interval, so please wait and try again.");
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);
480 throw new IllegalStateException("nodes are not found, pls add the node.");
482 return nodes.nonnullNode().values();
486 public String getHelp() {
487 StringBuilder help = new StringBuilder();
488 help.append("---MD-SAL Stats test module---\n");
489 return help.toString();