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 java.util.List;
11 import org.eclipse.osgi.framework.console.CommandInterpreter;
12 import org.eclipse.osgi.framework.console.CommandProvider;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.binding.api.ReadTransaction;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.osgi.framework.BundleContext;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 @SuppressWarnings("checkstyle:MethodName")
44 public class OpenflowpluginStatsTestCommandProvider implements CommandProvider {
46 private static final Logger LOG = LoggerFactory.getLogger(OpenflowpluginStatsTestCommandProvider.class);
47 private final DataBroker dataProviderService;
48 private final BundleContext ctx;
50 public OpenflowpluginStatsTestCommandProvider(DataBroker dataProviderService, BundleContext ctx) {
51 this.dataProviderService = dataProviderService;
56 ctx.registerService(CommandProvider.class.getName(), this, null);
60 public void _portStats(CommandInterpreter ci) {
61 int nodeConnectorCount = 0;
62 int nodeConnectorStatsCount = 0;
63 List<Node> nodes = getNodes();
64 for (Node node2 : nodes) {
65 NodeKey nodeKey = node2.key();
66 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.create(Nodes.class).child(Node.class, nodeKey);
67 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
68 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
70 if (node.getNodeConnector() != null) {
71 List<NodeConnector> ports = node.getNodeConnector();
73 for (NodeConnector nodeConnector2 : ports) {
75 NodeConnectorKey nodeConnectorKey = nodeConnector2.key();
76 InstanceIdentifier<NodeConnector> connectorRef = InstanceIdentifier.create(Nodes.class)
77 .child(Node.class, nodeKey).child(NodeConnector.class, nodeConnectorKey);
78 NodeConnector nodeConnector = TestProviderTransactionUtil.getDataObject(readOnlyTransaction,
80 if (nodeConnector != null) {
81 FlowCapableNodeConnectorStatisticsData data = nodeConnector
82 .augmentation(FlowCapableNodeConnectorStatisticsData.class);
84 nodeConnectorStatsCount++;
92 if (nodeConnectorCount == nodeConnectorStatsCount) {
93 LOG.debug("portStats - Success");
95 LOG.debug("portStats - Failed");
96 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
101 public void _portDescStats(CommandInterpreter ci) {
102 int nodeConnectorCount = 0;
103 int nodeConnectorDescStatsCount = 0;
104 List<Node> nodes = getNodes();
105 for (Node node2 : nodes) {
106 NodeKey nodeKey = node2.key();
107 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.create(Nodes.class).child(Node.class, nodeKey);
109 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
110 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
112 if (node.getNodeConnector() != null) {
113 List<NodeConnector> ports = node.getNodeConnector();
114 for (NodeConnector nodeConnector2 : ports) {
115 nodeConnectorCount++;
116 NodeConnectorKey nodeConnectorKey = nodeConnector2.key();
117 InstanceIdentifier<FlowCapableNodeConnector> connectorRef = InstanceIdentifier
118 .create(Nodes.class).child(Node.class, nodeKey)
119 .child(NodeConnector.class, nodeConnectorKey)
120 .augmentation(FlowCapableNodeConnector.class);
121 FlowCapableNodeConnector nodeConnector = TestProviderTransactionUtil
122 .getDataObject(readOnlyTransaction, connectorRef);
123 if (nodeConnector != null) {
124 if (null != nodeConnector.getName() && null != nodeConnector.getCurrentFeature()
125 && null != nodeConnector.getState() && null != nodeConnector.getHardwareAddress()
126 && null != nodeConnector.getPortNumber()) {
127 nodeConnectorDescStatsCount++;
136 if (nodeConnectorCount == nodeConnectorDescStatsCount) {
137 LOG.debug("portDescStats - Success");
139 LOG.debug("portDescStats - Failed");
140 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
145 public void _flowStats(CommandInterpreter ci) {
147 int flowStatsCount = 0;
148 List<Node> nodes = getNodes();
149 for (Node node2 : nodes) {
150 NodeKey nodeKey = node2.key();
151 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
152 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
154 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
155 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
158 List<Table> tables = node.getTable();
159 for (Table table2 : tables) {
160 TableKey tableKey = table2.key();
161 InstanceIdentifier<Table> tableRef = InstanceIdentifier.create(Nodes.class)
162 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
163 .child(Table.class, tableKey);
164 Table table = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, tableRef);
166 if (table.getFlow() != null) {
167 List<Flow> flows = table.getFlow();
168 for (Flow flow2 : flows) {
170 FlowKey flowKey = flow2.key();
171 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.create(Nodes.class)
172 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
173 .child(Table.class, tableKey).child(Flow.class, flowKey);
174 Flow flow = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, flowRef);
176 FlowStatisticsData data = flow.augmentation(FlowStatisticsData.class);
179 LOG.debug("--------------------------------------------");
191 if (flowCount == flowStatsCount) {
192 LOG.debug("flowStats - Success");
194 LOG.debug("flowStats - Failed");
195 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
200 public void _tableStats(CommandInterpreter ci) {
202 int tableStatsCount = 0;
203 List<Node> nodes = getNodes();
204 for (Node node2 : nodes) {
205 NodeKey nodeKey = node2.key();
206 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
207 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
209 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
210 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
212 List<Table> tables = node.getTable();
213 for (Table table2 : tables) {
215 TableKey tableKey = table2.key();
216 InstanceIdentifier<Table> tableRef = InstanceIdentifier.create(Nodes.class)
217 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
218 .child(Table.class, tableKey);
219 Table table = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, tableRef);
221 FlowTableStatisticsData data = table.augmentation(FlowTableStatisticsData.class);
230 if (tableCount == tableStatsCount) {
231 LOG.debug("tableStats - Success");
233 LOG.debug("tableStats - Failed");
234 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
239 public void _groupStats(CommandInterpreter ci) {
241 int groupStatsCount = 0;
242 NodeGroupStatistics data = null;
243 List<Node> nodes = getNodes();
244 for (Node node2 : nodes) {
245 NodeKey nodeKey = node2.key();
246 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
247 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
248 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
249 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
251 if (node.getGroup() != null) {
252 List<Group> groups = node.getGroup();
253 for (Group group2 : groups) {
255 GroupKey groupKey = group2.key();
256 InstanceIdentifier<Group> groupRef = InstanceIdentifier.create(Nodes.class)
257 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
258 .child(Group.class, groupKey);
259 Group group = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, groupRef);
261 data = group.augmentation(NodeGroupStatistics.class);
272 if (groupCount == groupStatsCount) {
273 LOG.debug("---------------------groupStats - Success-------------------------------");
275 LOG.debug("------------------------------groupStats - Failed--------------------------");
276 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
280 public void _groupDescStats(CommandInterpreter ci) {
282 int groupDescStatsCount = 0;
283 NodeGroupDescStats data = null;
284 List<Node> nodes = getNodes();
285 for (Node node2 : nodes) {
286 NodeKey nodeKey = node2.key();
287 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
288 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
289 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
290 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
293 if (node.getGroup() != null) {
294 List<Group> groups = node.getGroup();
295 for (Group group2 : groups) {
297 GroupKey groupKey = group2.key();
298 InstanceIdentifier<Group> groupRef = InstanceIdentifier.create(Nodes.class)
299 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
300 .child(Group.class, groupKey);
301 Group group = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, groupRef);
303 data = group.augmentation(NodeGroupDescStats.class);
305 groupDescStatsCount++;
313 if (groupCount == groupDescStatsCount) {
314 LOG.debug("---------------------groupDescStats - Success-------------------------------");
316 LOG.debug("------------------------------groupDescStats - Failed--------------------------");
317 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
322 public void _meterStats(CommandInterpreter ci) {
324 int meterStatsCount = 0;
325 NodeMeterStatistics data = null;
326 List<Node> nodes = getNodes();
327 for (Node node2 : nodes) {
328 NodeKey nodeKey = node2.key();
329 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
330 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
331 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
332 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
334 if (node.getMeter() != null) {
335 List<Meter> meters = node.getMeter();
336 for (Meter meter2 : meters) {
338 MeterKey meterKey = meter2.key();
339 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.create(Nodes.class)
340 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
341 .child(Meter.class, meterKey);
342 Meter meter = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, meterRef);
344 data = meter.augmentation(NodeMeterStatistics.class);
355 if (meterCount == meterStatsCount) {
356 LOG.debug("---------------------------meterStats - Success-------------------------------------");
358 LOG.debug("----------------------------meterStats - Failed-------------------------------------");
359 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
363 public void _meterConfigStats(CommandInterpreter ci) {
365 int meterConfigStatsCount = 0;
366 NodeMeterConfigStats data = null;
367 List<Node> nodes = getNodes();
368 for (Node node2 : nodes) {
369 NodeKey nodeKey = node2.key();
370 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
371 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
372 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
373 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
375 if (node.getMeter() != null) {
376 List<Meter> meters = node.getMeter();
377 for (Meter meter2 : meters) {
379 MeterKey meterKey = meter2.key();
380 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.create(Nodes.class)
381 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
382 .child(Meter.class, meterKey);
383 Meter meter = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, meterRef);
385 data = meter.augmentation(NodeMeterConfigStats.class);
387 meterConfigStatsCount++;
396 if (meterCount == meterConfigStatsCount) {
397 LOG.debug("---------------------------meterConfigStats - Success-------------------------------------");
400 LOG.debug("----------------------------meterConfigStats - Failed-------------------------------------");
401 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
405 public void _aggregateStats(CommandInterpreter ci) {
406 int aggregateFlowCount = 0;
407 int aggerateFlowStatsCount = 0;
408 List<Node> nodes = getNodes();
409 for (Node node2 : nodes) {
410 NodeKey nodeKey = node2.key();
411 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
412 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
413 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
414 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
416 List<Table> tables = node.getTable();
417 for (Table table2 : tables) {
418 aggregateFlowCount++;
419 TableKey tableKey = table2.key();
420 InstanceIdentifier<Table> tableRef = InstanceIdentifier.create(Nodes.class)
421 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class)
422 .child(Table.class, tableKey);
423 Table table = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, tableRef);
425 AggregateFlowStatisticsData data = table.augmentation(AggregateFlowStatisticsData.class);
427 aggerateFlowStatsCount++;
434 if (aggregateFlowCount == aggerateFlowStatsCount) {
435 LOG.debug("aggregateStats - Success");
437 LOG.debug("aggregateStats - Failed");
438 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
443 public void _descStats(CommandInterpreter ci) {
445 int descStatsCount = 0;
446 List<Node> nodes = getNodes();
447 for (Node node2 : nodes) {
449 NodeKey nodeKey = node2.key();
450 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
451 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
452 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
453 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
455 if (null != node.getHardware() && null != node.getManufacturer() && null != node.getSoftware()) {
461 if (descCount == descStatsCount) {
462 LOG.debug("descStats - Success");
464 LOG.debug("descStats - Failed");
465 LOG.debug("System fetches stats data in 50 seconds interval, so please wait and try again.");
470 private List<Node> getNodes() {
471 ReadTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
472 InstanceIdentifier<Nodes> nodesID = InstanceIdentifier.create(Nodes.class);
473 Nodes nodes = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodesID);
475 throw new RuntimeException("nodes are not found, pls add the node.");
477 return nodes.getNode();
481 public String getHelp() {
482 StringBuilder help = new StringBuilder();
483 help.append("---MD-SAL Stats test module---\n");
484 return help.toString();