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
9 package org.opendaylight.openflowplugin.test;
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;
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 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);
71 if (node.getNodeConnector() != null) {
72 List<NodeConnector> ports = node.getNodeConnector();
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 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);
110 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
111 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
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++;
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 public void _flowStats(CommandInterpreter ci) {
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);
155 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
156 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
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);
167 if (table.getFlow() != null) {
168 List<Flow> flows = table.getFlow();
169 for (Flow flow2 : flows) {
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);
177 FlowStatisticsData data = flow.augmentation(FlowStatisticsData.class);
180 LOG.debug("--------------------------------------------");
192 if (flowCount == flowStatsCount) {
193 LOG.debug("flowStats - Success");
195 LOG.debug("flowStats - Failed");
196 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
201 public void _tableStats(CommandInterpreter ci) {
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);
210 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
211 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
213 List<Table> tables = node.getTable();
214 for (Table table2 : tables) {
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);
222 FlowTableStatisticsData data = table.augmentation(FlowTableStatisticsData.class);
231 if (tableCount == tableStatsCount) {
232 LOG.debug("tableStats - Success");
234 LOG.debug("tableStats - Failed");
235 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
240 public void _groupStats(CommandInterpreter ci) {
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);
252 if (node.getGroup() != null) {
253 List<Group> groups = node.getGroup();
254 for (Group group2 : groups) {
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);
262 data = group.augmentation(NodeGroupStatistics.class);
273 if (groupCount == groupStatsCount) {
274 LOG.debug("---------------------groupStats - Success-------------------------------");
276 LOG.debug("------------------------------groupStats - Failed--------------------------");
277 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
281 public void _groupDescStats(CommandInterpreter ci) {
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);
294 if (node.getGroup() != null) {
295 List<Group> groups = node.getGroup();
296 for (Group group2 : groups) {
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);
304 data = group.augmentation(NodeGroupDescStats.class);
306 groupDescStatsCount++;
314 if (groupCount == groupDescStatsCount) {
315 LOG.debug("---------------------groupDescStats - Success-------------------------------");
317 LOG.debug("------------------------------groupDescStats - Failed--------------------------");
318 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
323 public void _meterStats(CommandInterpreter ci) {
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);
335 if (node.getMeter() != null) {
336 List<Meter> meters = node.getMeter();
337 for (Meter meter2 : meters) {
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);
345 data = meter.augmentation(NodeMeterStatistics.class);
356 if (meterCount == meterStatsCount) {
357 LOG.debug("---------------------------meterStats - Success-------------------------------------");
359 LOG.debug("----------------------------meterStats - Failed-------------------------------------");
360 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
364 public void _meterConfigStats(CommandInterpreter ci) {
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);
376 if (node.getMeter() != null) {
377 List<Meter> meters = node.getMeter();
378 for (Meter meter2 : meters) {
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);
386 data = meter.augmentation(NodeMeterConfigStats.class);
388 meterConfigStatsCount++;
397 if (meterCount == meterConfigStatsCount) {
398 LOG.debug("---------------------------meterConfigStats - Success-------------------------------------");
401 LOG.debug("----------------------------meterConfigStats - Failed-------------------------------------");
402 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
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);
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);
426 AggregateFlowStatisticsData data = table.augmentation(AggregateFlowStatisticsData.class);
428 aggerateFlowStatsCount++;
435 if (aggregateFlowCount == aggerateFlowStatsCount) {
436 LOG.debug("aggregateStats - Success");
438 LOG.debug("aggregateStats - Failed");
439 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
444 public void _descStats(CommandInterpreter ci) {
446 int descStatsCount = 0;
447 List<Node> nodes = getNodes();
448 for (Node node2 : nodes) {
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);
456 if (null != node.getHardware() && null != node.getManufacturer() && null != node.getSoftware()) {
462 if (descCount == descStatsCount) {
463 LOG.debug("descStats - Success");
465 LOG.debug("descStats - Failed");
466 LOG.debug("System fetches stats data in 50 seconds interval, so please wait and try again.");
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);
476 throw new RuntimeException("nodes are not found, pls add the node.");
478 return nodes.getNode();
482 public String getHelp() {
483 StringBuilder help = new StringBuilder();
484 help.append("---MD-SAL Stats test module---\n");
485 return help.toString();