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;
13 import org.eclipse.osgi.framework.console.CommandInterpreter;
14 import org.eclipse.osgi.framework.console.CommandProvider;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.osgi.framework.BundleContext;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class OpenflowpluginStatsTestCommandProvider implements CommandProvider {
48 private static final Logger LOG = LoggerFactory
49 .getLogger(OpenflowpluginStatsTestCommandProvider.class);
50 private DataBroker dataProviderService;
51 private final BundleContext ctx;
53 public OpenflowpluginStatsTestCommandProvider(BundleContext ctx) {
57 public void onSessionInitiated(ProviderContext session) {
58 dataProviderService = session.getSALService(DataBroker.class);
59 ctx.registerService(CommandProvider.class.getName(), this, null);
63 public void _portStats(CommandInterpreter ci) {
64 int nodeConnectorCount = 0;
65 int nodeConnectorStatsCount = 0;
66 List<Node> nodes = getNodes();
67 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
68 NodeKey nodeKey = iterator.next().getKey();
69 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.create(Nodes.class)
70 .child(Node.class, nodeKey);
71 ReadOnlyTransaction readOnlyTransaction = dataProviderService
72 .newReadOnlyTransaction();
73 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction,
76 if (node.getNodeConnector() != null) {
77 List<NodeConnector> ports = node.getNodeConnector();
79 for (Iterator<NodeConnector> iterator2 = ports.iterator(); iterator2
82 NodeConnectorKey nodeConnectorKey = iterator2.next().getKey();
83 InstanceIdentifier<NodeConnector> connectorRef = InstanceIdentifier
84 .create(Nodes.class).child(Node.class, nodeKey)
85 .child(NodeConnector.class, nodeConnectorKey);
86 NodeConnector nodeConnector = TestProviderTransactionUtil.getDataObject(
87 readOnlyTransaction, connectorRef);
88 if (nodeConnector != null) {
89 FlowCapableNodeConnectorStatisticsData data = nodeConnector
90 .getAugmentation(FlowCapableNodeConnectorStatisticsData.class);
92 nodeConnectorStatsCount++;
100 if (nodeConnectorCount == nodeConnectorStatsCount) {
101 LOG.debug("portStats - Success");
103 LOG.debug("portStats - Failed");
105 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
110 public void _portDescStats(CommandInterpreter ci) {
111 int nodeConnectorCount = 0;
112 int nodeConnectorDescStatsCount = 0;
113 List<Node> nodes = getNodes();
114 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
115 NodeKey nodeKey = iterator.next().getKey();
116 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.create(Nodes.class)
117 .child(Node.class, nodeKey);
119 ReadOnlyTransaction readOnlyTransaction = dataProviderService
120 .newReadOnlyTransaction();
121 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction,
124 if (node.getNodeConnector() != null) {
125 List<NodeConnector> ports = node.getNodeConnector();
126 for (Iterator<NodeConnector> iterator2 = ports.iterator(); iterator2
128 nodeConnectorCount++;
129 NodeConnectorKey nodeConnectorKey = iterator2.next().getKey();
130 InstanceIdentifier<FlowCapableNodeConnector> connectorRef = InstanceIdentifier
131 .create(Nodes.class).child(Node.class, nodeKey)
132 .child(NodeConnector.class, nodeConnectorKey)
133 .augmentation(FlowCapableNodeConnector.class);
134 FlowCapableNodeConnector nodeConnector = TestProviderTransactionUtil
135 .getDataObject(readOnlyTransaction, connectorRef);
136 if (nodeConnector != null) {
137 if (null != nodeConnector.getName()
138 && null != nodeConnector.getCurrentFeature()
139 && null != nodeConnector.getState()
140 && null != nodeConnector.getHardwareAddress()
141 && null != nodeConnector.getPortNumber()) {
142 nodeConnectorDescStatsCount++;
151 if (nodeConnectorCount == nodeConnectorDescStatsCount) {
152 LOG.debug("portDescStats - Success");
154 LOG.debug("portDescStats - Failed");
156 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
161 public void _flowStats(CommandInterpreter ci) {
163 int flowStatsCount = 0;
164 List<Node> nodes = getNodes();
165 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
166 NodeKey nodeKey = iterator.next().getKey();
167 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
168 .create(Nodes.class).child(Node.class, nodeKey)
169 .augmentation(FlowCapableNode.class);
171 ReadOnlyTransaction readOnlyTransaction = dataProviderService
172 .newReadOnlyTransaction();
173 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
174 readOnlyTransaction, nodeRef);
177 List<Table> tables = node.getTable();
178 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext();) {
179 TableKey tableKey = iterator2.next().getKey();
180 InstanceIdentifier<Table> tableRef = InstanceIdentifier
181 .create(Nodes.class).child(Node.class, nodeKey)
182 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
183 Table table = TestProviderTransactionUtil.getDataObject(
184 readOnlyTransaction, tableRef);
186 if (table.getFlow() != null) {
187 List<Flow> flows = table.getFlow();
188 for (Iterator<Flow> iterator3 = flows.iterator(); iterator3.hasNext();) {
190 FlowKey flowKey = iterator3.next().getKey();
191 InstanceIdentifier<Flow> flowRef = InstanceIdentifier
192 .create(Nodes.class).child(Node.class, nodeKey)
193 .augmentation(FlowCapableNode.class).child(Table.class, tableKey)
194 .child(Flow.class, flowKey);
195 Flow flow = TestProviderTransactionUtil.getDataObject(
196 readOnlyTransaction, flowRef);
198 FlowStatisticsData data = flow
199 .getAugmentation(FlowStatisticsData.class);
202 LOG.debug("--------------------------------------------");
214 if (flowCount == flowStatsCount) {
215 LOG.debug("flowStats - Success");
217 LOG.debug("flowStats - Failed");
219 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
224 public void _tableStats(CommandInterpreter ci) {
226 int tableStatsCount = 0;
227 List<Node> nodes = getNodes();
228 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
229 NodeKey nodeKey = iterator.next().getKey();
230 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
231 .create(Nodes.class).child(Node.class, nodeKey)
232 .augmentation(FlowCapableNode.class);
234 ReadOnlyTransaction readOnlyTransaction = dataProviderService
235 .newReadOnlyTransaction();
236 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
237 readOnlyTransaction, nodeRef);
239 List<Table> tables = node.getTable();
240 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext();) {
242 TableKey tableKey = iterator2.next().getKey();
243 InstanceIdentifier<Table> tableRef = InstanceIdentifier
244 .create(Nodes.class).child(Node.class, nodeKey)
245 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
246 Table table = TestProviderTransactionUtil.getDataObject(
247 readOnlyTransaction, tableRef);
249 FlowTableStatisticsData data = table
250 .getAugmentation(FlowTableStatisticsData.class);
259 if (tableCount == tableStatsCount) {
260 LOG.debug("tableStats - Success");
262 LOG.debug("tableStats - Failed");
264 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
269 public void _groupStats(CommandInterpreter ci) {
271 int groupStatsCount = 0;
272 NodeGroupStatistics data = null;
273 List<Node> nodes = getNodes();
274 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
275 NodeKey nodeKey = iterator.next().getKey();
276 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
277 .create(Nodes.class).child(Node.class, nodeKey)
278 .augmentation(FlowCapableNode.class);
279 ReadOnlyTransaction readOnlyTransaction = dataProviderService
280 .newReadOnlyTransaction();
281 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
282 readOnlyTransaction, nodeRef);
284 if (node.getGroup() != null) {
285 List<Group> groups = node.getGroup();
286 for (Iterator<Group> iterator2 = groups.iterator(); iterator2.hasNext();) {
288 GroupKey groupKey = iterator2.next().getKey();
289 InstanceIdentifier<Group> groupRef = InstanceIdentifier
290 .create(Nodes.class).child(Node.class, nodeKey)
291 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
292 Group group = TestProviderTransactionUtil.getDataObject(
293 readOnlyTransaction, groupRef);
295 data = group.getAugmentation(NodeGroupStatistics.class);
306 if (groupCount == groupStatsCount) {
308 .debug("---------------------groupStats - Success-------------------------------");
311 .debug("------------------------------groupStats - Failed--------------------------");
313 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
317 public void _groupDescStats(CommandInterpreter ci) {
319 int groupDescStatsCount = 0;
320 NodeGroupDescStats data = null;
321 List<Node> nodes = getNodes();
322 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
323 NodeKey nodeKey = iterator.next().getKey();
324 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
325 .create(Nodes.class).child(Node.class, nodeKey)
326 .augmentation(FlowCapableNode.class);
327 ReadOnlyTransaction readOnlyTransaction = dataProviderService
328 .newReadOnlyTransaction();
329 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
330 readOnlyTransaction, nodeRef);
333 if (node.getGroup() != null) {
334 List<Group> groups = node.getGroup();
335 for (Iterator<Group> iterator2 = groups.iterator(); iterator2.hasNext();) {
337 GroupKey groupKey = iterator2.next().getKey();
338 InstanceIdentifier<Group> groupRef = InstanceIdentifier
339 .create(Nodes.class).child(Node.class, nodeKey)
340 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
341 Group group = TestProviderTransactionUtil.getDataObject(
342 readOnlyTransaction, groupRef);
344 data = group.getAugmentation(NodeGroupDescStats.class);
346 groupDescStatsCount++;
354 if (groupCount == groupDescStatsCount) {
356 .debug("---------------------groupDescStats - Success-------------------------------");
359 .debug("------------------------------groupDescStats - Failed--------------------------");
361 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
366 public void _meterStats(CommandInterpreter ci) {
368 int meterStatsCount = 0;
369 NodeMeterStatistics data = null;
370 List<Node> nodes = getNodes();
371 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
372 NodeKey nodeKey = iterator.next().getKey();
373 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
374 .create(Nodes.class).child(Node.class, nodeKey)
375 .augmentation(FlowCapableNode.class);
376 ReadOnlyTransaction readOnlyTransaction = dataProviderService
377 .newReadOnlyTransaction();
378 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
379 readOnlyTransaction, nodeRef);
381 if (node.getMeter() != null) {
382 List<Meter> meters = node.getMeter();
383 for (Iterator<Meter> iterator2 = meters.iterator(); iterator2.hasNext();) {
385 MeterKey meterKey = iterator2.next().getKey();
386 InstanceIdentifier<Meter> meterRef = InstanceIdentifier
387 .create(Nodes.class).child(Node.class, nodeKey)
388 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
389 Meter meter = TestProviderTransactionUtil.getDataObject(
390 readOnlyTransaction, meterRef);
392 data = meter.getAugmentation(NodeMeterStatistics.class);
403 if (meterCount == meterStatsCount) {
405 .debug("---------------------------meterStats - Success-------------------------------------");
408 .debug("----------------------------meterStats - Failed-------------------------------------");
410 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
414 public void _meterConfigStats(CommandInterpreter ci) {
416 int meterConfigStatsCount = 0;
417 NodeMeterConfigStats data = null;
418 List<Node> nodes = getNodes();
419 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
420 NodeKey nodeKey = iterator.next().getKey();
421 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
422 .create(Nodes.class).child(Node.class, nodeKey)
423 .augmentation(FlowCapableNode.class);
424 ReadOnlyTransaction readOnlyTransaction = dataProviderService
425 .newReadOnlyTransaction();
426 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
427 readOnlyTransaction, nodeRef);
429 if (node.getMeter() != null) {
430 List<Meter> meters = node.getMeter();
431 for (Iterator<Meter> iterator2 = meters.iterator(); iterator2.hasNext();) {
433 MeterKey meterKey = iterator2.next().getKey();
434 InstanceIdentifier<Meter> meterRef = InstanceIdentifier
435 .create(Nodes.class).child(Node.class, nodeKey)
436 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
437 Meter meter = TestProviderTransactionUtil.getDataObject(
438 readOnlyTransaction, meterRef);
440 data = meter.getAugmentation(NodeMeterConfigStats.class);
442 meterConfigStatsCount++;
451 if (meterCount == meterConfigStatsCount) {
453 .debug("---------------------------meterConfigStats - Success-------------------------------------");
457 .debug("----------------------------meterConfigStats - Failed-------------------------------------");
459 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
463 public void _aggregateStats(CommandInterpreter ci) {
464 int aggregateFlowCount = 0;
465 int aggerateFlowStatsCount = 0;
466 List<Node> nodes = getNodes();
467 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
468 NodeKey nodeKey = iterator.next().getKey();
469 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
470 .create(Nodes.class).child(Node.class, nodeKey)
471 .augmentation(FlowCapableNode.class);
472 ReadOnlyTransaction readOnlyTransaction = dataProviderService
473 .newReadOnlyTransaction();
474 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
475 readOnlyTransaction, nodeRef);
477 List<Table> tables = node.getTable();
478 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext();) {
479 aggregateFlowCount++;
480 TableKey tableKey = iterator2.next().getKey();
481 InstanceIdentifier<Table> tableRef = InstanceIdentifier
482 .create(Nodes.class).child(Node.class, nodeKey)
483 .augmentation(FlowCapableNode.class).child(Table.class, tableKey);
484 Table table = TestProviderTransactionUtil.getDataObject(
485 readOnlyTransaction, tableRef);
487 AggregateFlowStatisticsData data = table
488 .getAugmentation(AggregateFlowStatisticsData.class);
490 aggerateFlowStatsCount++;
497 if (aggregateFlowCount == aggerateFlowStatsCount) {
498 LOG.debug("aggregateStats - Success");
500 LOG.debug("aggregateStats - Failed");
502 .debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
507 public void _descStats(CommandInterpreter ci) {
509 int descStatsCount = 0;
510 List<Node> nodes = getNodes();
511 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
513 NodeKey nodeKey = iterator.next().getKey();
514 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier
515 .create(Nodes.class).child(Node.class, nodeKey)
516 .augmentation(FlowCapableNode.class);
517 ReadOnlyTransaction readOnlyTransaction = dataProviderService
518 .newReadOnlyTransaction();
519 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(
520 readOnlyTransaction, nodeRef);
522 if (null != node.getHardware() && null != node.getManufacturer()
523 && null != node.getSoftware()) {
529 if (descCount == descStatsCount) {
530 LOG.debug("descStats - Success");
532 LOG.debug("descStats - Failed");
534 .debug("System fetches stats data in 50 seconds interval, so please wait and try again.");
539 private List<Node> getNodes() {
540 ReadOnlyTransaction readOnlyTransaction = dataProviderService
541 .newReadOnlyTransaction();
542 InstanceIdentifier<Nodes> nodesID = InstanceIdentifier.create(Nodes.class);
543 Nodes nodes = TestProviderTransactionUtil.getDataObject(readOnlyTransaction,
546 throw new RuntimeException("nodes are not found, pls add the node.");
548 return nodes.getNode();
552 public String getHelp() {
553 StringBuilder help = new StringBuilder();
554 help.append("---MD-SAL Stats test module---\n");
555 return help.toString();