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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.util.Collection;
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;
44 @SuppressWarnings("checkstyle:MethodName")
45 public class OpenflowpluginStatsTestCommandProvider implements CommandProvider {
47 private static final Logger LOG = LoggerFactory.getLogger(OpenflowpluginStatsTestCommandProvider.class);
48 private final DataBroker dataProviderService;
49 private final BundleContext ctx;
51 public OpenflowpluginStatsTestCommandProvider(DataBroker dataProviderService, BundleContext ctx) {
52 this.dataProviderService = dataProviderService;
57 ctx.registerService(CommandProvider.class.getName(), this, null);
61 public void _portStats(CommandInterpreter ci) {
62 int nodeConnectorCount = 0;
63 int nodeConnectorStatsCount = 0;
64 Collection<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);
71 if (node.getNodeConnector() != null) {
72 Collection<NodeConnector> ports = node.nonnullNodeConnector().values();
74 for (NodeConnector nodeConnector2 : ports) {
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,
81 if (nodeConnector != null) {
82 FlowCapableNodeConnectorStatisticsData data = nodeConnector
83 .augmentation(FlowCapableNodeConnectorStatisticsData.class);
85 nodeConnectorStatsCount++;
93 if (nodeConnectorCount == nodeConnectorStatsCount) {
94 LOG.debug("portStats - Success");
96 LOG.debug("portStats - Failed");
97 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
102 public void _portDescStats(CommandInterpreter ci) {
103 int nodeConnectorCount = 0;
104 int nodeConnectorDescStatsCount = 0;
105 Collection<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);
110 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
111 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
113 if (node.getNodeConnector() != null) {
114 Collection<NodeConnector> ports = node.nonnullNodeConnector().values();
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++;
137 if (nodeConnectorCount == nodeConnectorDescStatsCount) {
138 LOG.debug("portDescStats - Success");
140 LOG.debug("portDescStats - Failed");
141 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
146 @SuppressFBWarnings("SLF4J_SIGN_ONLY_FORMAT")
147 public void _flowStats(CommandInterpreter ci) {
149 int flowStatsCount = 0;
150 Collection<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);
156 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
157 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
160 Collection<Table> tables = node.nonnullTable().values();
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);
168 if (table.getFlow() != null) {
169 Collection<Flow> flows = table.nonnullFlow().values();
170 for (Flow flow2 : flows) {
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);
178 FlowStatisticsData data = flow.augmentation(FlowStatisticsData.class);
181 LOG.debug("--------------------------------------------");
193 if (flowCount == flowStatsCount) {
194 LOG.debug("flowStats - Success");
196 LOG.debug("flowStats - Failed");
197 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
202 public void _tableStats(CommandInterpreter ci) {
204 int tableStatsCount = 0;
205 Collection<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);
211 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
212 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
214 Collection<Table> tables = node.nonnullTable().values();
215 for (Table table2 : tables) {
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);
223 FlowTableStatisticsData data = table.augmentation(FlowTableStatisticsData.class);
232 if (tableCount == tableStatsCount) {
233 LOG.debug("tableStats - Success");
235 LOG.debug("tableStats - Failed");
236 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
241 public void _groupStats(CommandInterpreter ci) {
243 int groupStatsCount = 0;
244 NodeGroupStatistics data = null;
245 Collection<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);
253 if (node.getGroup() != null) {
254 Collection<Group> groups = node.nonnullGroup().values();
255 for (Group group2 : groups) {
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);
263 data = group.augmentation(NodeGroupStatistics.class);
274 if (groupCount == groupStatsCount) {
275 LOG.debug("---------------------groupStats - Success-------------------------------");
277 LOG.debug("------------------------------groupStats - Failed--------------------------");
278 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
282 public void _groupDescStats(CommandInterpreter ci) {
284 int groupDescStatsCount = 0;
285 NodeGroupDescStats data = null;
286 Collection<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);
295 if (node.getGroup() != null) {
296 Collection<Group> groups = node.nonnullGroup().values();
297 for (Group group2 : groups) {
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);
305 data = group.augmentation(NodeGroupDescStats.class);
307 groupDescStatsCount++;
315 if (groupCount == groupDescStatsCount) {
316 LOG.debug("---------------------groupDescStats - Success-------------------------------");
318 LOG.debug("------------------------------groupDescStats - Failed--------------------------");
319 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
324 public void _meterStats(CommandInterpreter ci) {
326 int meterStatsCount = 0;
327 NodeMeterStatistics data = null;
328 Collection<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);
336 if (node.getMeter() != null) {
337 Collection<Meter> meters = node.nonnullMeter().values();
338 for (Meter meter2 : meters) {
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);
346 data = meter.augmentation(NodeMeterStatistics.class);
357 if (meterCount == meterStatsCount) {
358 LOG.debug("---------------------------meterStats - Success-------------------------------------");
360 LOG.debug("----------------------------meterStats - Failed-------------------------------------");
361 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
365 public void _meterConfigStats(CommandInterpreter ci) {
367 int meterConfigStatsCount = 0;
368 NodeMeterConfigStats data = null;
369 Collection<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);
377 if (node.getMeter() != null) {
378 Collection<Meter> meters = node.nonnullMeter().values();
379 for (Meter meter2 : meters) {
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);
387 data = meter.augmentation(NodeMeterConfigStats.class);
389 meterConfigStatsCount++;
398 if (meterCount == meterConfigStatsCount) {
399 LOG.debug("---------------------------meterConfigStats - Success-------------------------------------");
402 LOG.debug("----------------------------meterConfigStats - Failed-------------------------------------");
403 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
407 public void _aggregateStats(CommandInterpreter ci) {
408 int aggregateFlowCount = 0;
409 int aggerateFlowStatsCount = 0;
410 Collection<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);
418 Collection<Table> tables = node.nonnullTable().values();
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);
427 AggregateFlowStatisticsData data = table.augmentation(AggregateFlowStatisticsData.class);
429 aggerateFlowStatsCount++;
436 if (aggregateFlowCount == aggerateFlowStatsCount) {
437 LOG.debug("aggregateStats - Success");
439 LOG.debug("aggregateStats - Failed");
440 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
445 public void _descStats(CommandInterpreter ci) {
447 int descStatsCount = 0;
448 Collection<Node> nodes = getNodes();
449 for (Node node2 : nodes) {
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);
457 if (null != node.getHardware() && null != node.getManufacturer() && null != node.getSoftware()) {
463 if (descCount == descStatsCount) {
464 LOG.debug("descStats - Success");
466 LOG.debug("descStats - Failed");
467 LOG.debug("System fetches stats data in 50 seconds interval, so please wait and try again.");
472 private Collection<Node> getNodes() {
473 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
474 InstanceIdentifier<Nodes> nodesID = InstanceIdentifier.create(Nodes.class);
475 Nodes nodes = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodesID);
477 throw new RuntimeException("nodes are not found, pls add the node.");
479 return nodes.nonnullNode().values();
483 public String getHelp() {
484 StringBuilder help = new StringBuilder();
485 help.append("---MD-SAL Stats test module---\n");
486 return help.toString();