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.Iterator;
12 import java.util.List;
14 import org.eclipse.osgi.framework.console.CommandInterpreter;
15 import org.eclipse.osgi.framework.console.CommandProvider;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.osgi.framework.BundleContext;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import com.google.common.base.Preconditions;
49 public class OpenflowpluginStatsTestCommandProvider implements CommandProvider {
51 private static final Logger LOG = LoggerFactory
52 .getLogger(OpenflowpluginStatsTestCommandProvider.class);
53 private DataBroker dataProviderService;
54 private final BundleContext ctx;
56 public OpenflowpluginStatsTestCommandProvider(BundleContext ctx) {
60 public void onSessionInitiated(ProviderContext session) {
61 dataProviderService = session.getSALService(DataBroker.class);
62 ctx.registerService(CommandProvider.class.getName(), this, null);
66 public void _portStats(CommandInterpreter ci) {
67 int nodeConnectorCount = 0;
68 int nodeConnectorStatsCount = 0;
69 List<Node> nodes = getNodes();
70 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
71 NodeKey nodeKey = iterator.next().getKey();
72 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.create(Nodes.class)
73 .child(Node.class, nodeKey);
74 ReadOnlyTransaction readOnlyTransaction = dataProviderService
75 .newReadOnlyTransaction();
76 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction,
79 if (node.getNodeConnector() != null) {
80 List<NodeConnector> ports = node.getNodeConnector();
82 for (Iterator<NodeConnector> iterator2 = ports.iterator(); iterator2
85 NodeConnectorKey nodeConnectorKey = iterator2.next().getKey();
86 InstanceIdentifier<NodeConnector> connectorRef = InstanceIdentifier
87 .create(Nodes.class).child(Node.class, nodeKey)
88 .child(NodeConnector.class, nodeConnectorKey);
89 NodeConnector nodeConnector = TestProviderTransactionUtil.getDataObject(
90 readOnlyTransaction, connectorRef);
91 if (nodeConnector != null) {
92 FlowCapableNodeConnectorStatisticsData data = nodeConnector
93 .getAugmentation(FlowCapableNodeConnectorStatisticsData.class);
95 nodeConnectorStatsCount++;
103 if (nodeConnectorCount == nodeConnectorStatsCount) {
104 LOG.debug("portStats - Success");
106 LOG.debug("portStats - Failed");
108 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
113 public void _portDescStats(CommandInterpreter ci) {
114 int nodeConnectorCount = 0;
115 int nodeConnectorDescStatsCount = 0;
116 List<Node> nodes = getNodes();
117 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
118 NodeKey nodeKey = iterator.next().getKey();
119 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.create(Nodes.class)
120 .child(Node.class, nodeKey);
122 ReadOnlyTransaction readOnlyTransaction = dataProviderService
123 .newReadOnlyTransaction();
124 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction,
127 if (node.getNodeConnector() != null) {
128 List<NodeConnector> ports = node.getNodeConnector();
129 for (Iterator<NodeConnector> iterator2 = ports.iterator(); iterator2
131 nodeConnectorCount++;
132 NodeConnectorKey nodeConnectorKey = iterator2.next().getKey();
133 InstanceIdentifier<FlowCapableNodeConnector> connectorRef = InstanceIdentifier
134 .create(Nodes.class).child(Node.class, nodeKey)
135 .child(NodeConnector.class, nodeConnectorKey)
136 .augmentation(FlowCapableNodeConnector.class);
137 FlowCapableNodeConnector nodeConnector = TestProviderTransactionUtil
138 .getDataObject(readOnlyTransaction, connectorRef);
139 if (nodeConnector != null) {
140 if (null != nodeConnector.getName()
141 && null != nodeConnector.getCurrentFeature()
142 && null != nodeConnector.getState()
143 && null != nodeConnector.getHardwareAddress()
144 && null != nodeConnector.getPortNumber()) {
145 nodeConnectorDescStatsCount++;
154 if (nodeConnectorCount == nodeConnectorDescStatsCount) {
155 LOG.debug("portDescStats - Success");
157 LOG.debug("portDescStats - Failed");
159 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
164 public void _flowStats(CommandInterpreter ci) {
166 int flowStatsCount = 0;
167 List<Node> nodes = getNodes();
168 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
169 NodeKey nodeKey = iterator.next().getKey();
170 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
171 .create(Nodes.class).child(Node.class, nodeKey)
172 .augmentation(FlowCapableNode.class);
174 ReadOnlyTransaction readOnlyTransaction = dataProviderService
175 .newReadOnlyTransaction();
176 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
177 readOnlyTransaction, nodeRef);
180 List<Table> tables = node.getTable();
181 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext();) {
182 TableKey tableKey = iterator2.next().getKey();
183 InstanceIdentifier<Table> tableRef = InstanceIdentifier
184 .create(Nodes.class).child(Node.class, nodeKey)
185 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
186 Table table = TestProviderTransactionUtil.getDataObject(
187 readOnlyTransaction, tableRef);
189 if (table.getFlow() != null) {
190 List<Flow> flows = table.getFlow();
191 for (Iterator<Flow> iterator3 = flows.iterator(); iterator3.hasNext();) {
193 FlowKey flowKey = iterator3.next().getKey();
194 InstanceIdentifier<Flow> flowRef = InstanceIdentifier
195 .create(Nodes.class).child(Node.class, nodeKey)
196 .augmentation(FlowCapableNode.class).child(Table.class, tableKey)
197 .child(Flow.class, flowKey);
198 Flow flow = TestProviderTransactionUtil.getDataObject(
199 readOnlyTransaction, flowRef);
201 FlowStatisticsData data = flow
202 .getAugmentation(FlowStatisticsData.class);
205 LOG.debug("--------------------------------------------");
217 if (flowCount == flowStatsCount) {
218 LOG.debug("flowStats - Success");
220 LOG.debug("flowStats - Failed");
222 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
227 public void _tableStats(CommandInterpreter ci) {
229 int tableStatsCount = 0;
230 List<Node> nodes = getNodes();
231 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
232 NodeKey nodeKey = iterator.next().getKey();
233 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
234 .create(Nodes.class).child(Node.class, nodeKey)
235 .augmentation(FlowCapableNode.class);
237 ReadOnlyTransaction readOnlyTransaction = dataProviderService
238 .newReadOnlyTransaction();
239 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
240 readOnlyTransaction, nodeRef);
242 List<Table> tables = node.getTable();
243 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext();) {
245 TableKey tableKey = iterator2.next().getKey();
246 InstanceIdentifier<Table> tableRef = InstanceIdentifier
247 .create(Nodes.class).child(Node.class, nodeKey)
248 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
249 Table table = TestProviderTransactionUtil.getDataObject(
250 readOnlyTransaction, tableRef);
252 FlowTableStatisticsData data = table
253 .getAugmentation(FlowTableStatisticsData.class);
262 if (tableCount == tableStatsCount) {
263 LOG.debug("tableStats - Success");
265 LOG.debug("tableStats - Failed");
267 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
272 public void _groupStats(CommandInterpreter ci) {
274 int groupStatsCount = 0;
275 NodeGroupStatistics data = null;
276 List<Node> nodes = getNodes();
277 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
278 NodeKey nodeKey = iterator.next().getKey();
279 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
280 .create(Nodes.class).child(Node.class, nodeKey)
281 .augmentation(FlowCapableNode.class);
282 ReadOnlyTransaction readOnlyTransaction = dataProviderService
283 .newReadOnlyTransaction();
284 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
285 readOnlyTransaction, nodeRef);
287 if (node.getGroup() != null) {
288 List<Group> groups = node.getGroup();
289 for (Iterator<Group> iterator2 = groups.iterator(); iterator2.hasNext();) {
291 GroupKey groupKey = iterator2.next().getKey();
292 InstanceIdentifier<Group> groupRef = InstanceIdentifier
293 .create(Nodes.class).child(Node.class, nodeKey)
294 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
295 Group group = TestProviderTransactionUtil.getDataObject(
296 readOnlyTransaction, groupRef);
298 data = group.getAugmentation(NodeGroupStatistics.class);
309 if (groupCount == groupStatsCount) {
311 .debug("---------------------groupStats - Success-------------------------------");
314 .debug("------------------------------groupStats - Failed--------------------------");
316 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
320 public void _groupDescStats(CommandInterpreter ci) {
322 int groupDescStatsCount = 0;
323 NodeGroupDescStats data = null;
324 List<Node> nodes = getNodes();
325 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
326 NodeKey nodeKey = iterator.next().getKey();
327 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
328 .create(Nodes.class).child(Node.class, nodeKey)
329 .augmentation(FlowCapableNode.class);
330 ReadOnlyTransaction readOnlyTransaction = dataProviderService
331 .newReadOnlyTransaction();
332 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
333 readOnlyTransaction, nodeRef);
336 if (node.getGroup() != null) {
337 List<Group> groups = node.getGroup();
338 for (Iterator<Group> iterator2 = groups.iterator(); iterator2.hasNext();) {
340 GroupKey groupKey = iterator2.next().getKey();
341 InstanceIdentifier<Group> groupRef = InstanceIdentifier
342 .create(Nodes.class).child(Node.class, nodeKey)
343 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
344 Group group = TestProviderTransactionUtil.getDataObject(
345 readOnlyTransaction, groupRef);
347 data = group.getAugmentation(NodeGroupDescStats.class);
349 groupDescStatsCount++;
357 if (groupCount == groupDescStatsCount) {
359 .debug("---------------------groupDescStats - Success-------------------------------");
362 .debug("------------------------------groupDescStats - Failed--------------------------");
364 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
369 public void _meterStats(CommandInterpreter ci) {
371 int meterStatsCount = 0;
372 NodeMeterStatistics data = null;
373 List<Node> nodes = getNodes();
374 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
375 NodeKey nodeKey = iterator.next().getKey();
376 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
377 .create(Nodes.class).child(Node.class, nodeKey)
378 .augmentation(FlowCapableNode.class);
379 ReadOnlyTransaction readOnlyTransaction = dataProviderService
380 .newReadOnlyTransaction();
381 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
382 readOnlyTransaction, nodeRef);
384 if (node.getMeter() != null) {
385 List<Meter> meters = node.getMeter();
386 for (Iterator<Meter> iterator2 = meters.iterator(); iterator2.hasNext();) {
388 MeterKey meterKey = iterator2.next().getKey();
389 InstanceIdentifier<Meter> meterRef = InstanceIdentifier
390 .create(Nodes.class).child(Node.class, nodeKey)
391 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
392 Meter meter = TestProviderTransactionUtil.getDataObject(
393 readOnlyTransaction, meterRef);
395 data = meter.getAugmentation(NodeMeterStatistics.class);
406 if (meterCount == meterStatsCount) {
408 .debug("---------------------------meterStats - Success-------------------------------------");
411 .debug("----------------------------meterStats - Failed-------------------------------------");
413 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
417 public void _meterConfigStats(CommandInterpreter ci) {
419 int meterConfigStatsCount = 0;
420 NodeMeterConfigStats data = null;
421 List<Node> nodes = getNodes();
422 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
423 NodeKey nodeKey = iterator.next().getKey();
424 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
425 .create(Nodes.class).child(Node.class, nodeKey)
426 .augmentation(FlowCapableNode.class);
427 ReadOnlyTransaction readOnlyTransaction = dataProviderService
428 .newReadOnlyTransaction();
429 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
430 readOnlyTransaction, nodeRef);
432 if (node.getMeter() != null) {
433 List<Meter> meters = node.getMeter();
434 for (Iterator<Meter> iterator2 = meters.iterator(); iterator2.hasNext();) {
436 MeterKey meterKey = iterator2.next().getKey();
437 InstanceIdentifier<Meter> meterRef = InstanceIdentifier
438 .create(Nodes.class).child(Node.class, nodeKey)
439 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
440 Meter meter = TestProviderTransactionUtil.getDataObject(
441 readOnlyTransaction, meterRef);
443 data = meter.getAugmentation(NodeMeterConfigStats.class);
445 meterConfigStatsCount++;
454 if (meterCount == meterConfigStatsCount) {
456 .debug("---------------------------meterConfigStats - Success-------------------------------------");
460 .debug("----------------------------meterConfigStats - Failed-------------------------------------");
462 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
466 public void _aggregateStats(CommandInterpreter ci) {
467 int aggregateFlowCount = 0;
468 int aggerateFlowStatsCount = 0;
469 List<Node> nodes = getNodes();
470 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
471 NodeKey nodeKey = iterator.next().getKey();
472 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
473 .create(Nodes.class).child(Node.class, nodeKey)
474 .augmentation(FlowCapableNode.class);
475 ReadOnlyTransaction readOnlyTransaction = dataProviderService
476 .newReadOnlyTransaction();
477 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
478 readOnlyTransaction, nodeRef);
480 List<Table> tables = node.getTable();
481 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext();) {
482 aggregateFlowCount++;
483 TableKey tableKey = iterator2.next().getKey();
484 InstanceIdentifier<Table> tableRef = InstanceIdentifier
485 .create(Nodes.class).child(Node.class, nodeKey)
486 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
487 Table table = TestProviderTransactionUtil.getDataObject(
488 readOnlyTransaction, tableRef);
490 AggregateFlowStatisticsData data = table
491 .getAugmentation(AggregateFlowStatisticsData.class);
493 aggerateFlowStatsCount++;
500 if (aggregateFlowCount == aggerateFlowStatsCount) {
501 LOG.debug("aggregateStats - Success");
503 LOG.debug("aggregateStats - Failed");
505 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
510 public void _descStats(CommandInterpreter ci) {
512 int descStatsCount = 0;
513 List<Node> nodes = getNodes();
514 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
516 NodeKey nodeKey = iterator.next().getKey();
517 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
518 .create(Nodes.class).child(Node.class, nodeKey)
519 .augmentation(FlowCapableNode.class);
520 ReadOnlyTransaction readOnlyTransaction = dataProviderService
521 .newReadOnlyTransaction();
522 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
523 readOnlyTransaction, nodeRef);
525 if (null != node.getHardware() && null != node.getManufacturer()
526 && null != node.getSoftware()) {
532 if (descCount == descStatsCount) {
533 LOG.debug("descStats - Success");
535 LOG.debug("descStats - Failed");
537 .debug("System fetches stats data in 50 seconds interval, so please wait and try again.");
542 private List<Node> getNodes() {
543 ReadOnlyTransaction readOnlyTransaction = dataProviderService
544 .newReadOnlyTransaction();
545 InstanceIdentifier<Nodes> nodesID = InstanceIdentifier.create(Nodes.class);
546 Nodes nodes = TestProviderTransactionUtil.getDataObject(readOnlyTransaction,
549 throw new RuntimeException("nodes are not found, pls add the node.");
551 return nodes.getNode();
555 public String getHelp() {
556 StringBuilder help = new StringBuilder();
557 help.append("---MD-SAL Stats test module---\n");
558 return help.toString();