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.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.osgi.framework.BundleContext;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 @SuppressWarnings("checkstyle:MethodName")
46 public class OpenflowpluginStatsTestCommandProvider implements CommandProvider {
48 private static final Logger LOG = LoggerFactory.getLogger(OpenflowpluginStatsTestCommandProvider.class);
49 private DataBroker dataProviderService;
50 private final BundleContext ctx;
52 public OpenflowpluginStatsTestCommandProvider(BundleContext ctx) {
56 public void onSessionInitiated(ProviderContext session) {
57 dataProviderService = session.getSALService(DataBroker.class);
58 ctx.registerService(CommandProvider.class.getName(), this, null);
62 public void _portStats(CommandInterpreter ci) {
63 int nodeConnectorCount = 0;
64 int nodeConnectorStatsCount = 0;
65 List<Node> nodes = getNodes();
66 for (Node node2 : nodes) {
67 NodeKey nodeKey = node2.getKey();
68 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.create(Nodes.class).child(Node.class, nodeKey);
69 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
70 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
72 if (node.getNodeConnector() != null) {
73 List<NodeConnector> ports = node.getNodeConnector();
75 for (NodeConnector nodeConnector2 : ports) {
77 NodeConnectorKey nodeConnectorKey = nodeConnector2.getKey();
78 InstanceIdentifier<NodeConnector> connectorRef = InstanceIdentifier.create(Nodes.class)
79 .child(Node.class, nodeKey).child(NodeConnector.class, nodeConnectorKey);
80 NodeConnector nodeConnector = TestProviderTransactionUtil.getDataObject(readOnlyTransaction,
82 if (nodeConnector != null) {
83 FlowCapableNodeConnectorStatisticsData data = nodeConnector
84 .getAugmentation(FlowCapableNodeConnectorStatisticsData.class);
86 nodeConnectorStatsCount++;
94 if (nodeConnectorCount == nodeConnectorStatsCount) {
95 LOG.debug("portStats - Success");
97 LOG.debug("portStats - Failed");
98 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
103 public void _portDescStats(CommandInterpreter ci) {
104 int nodeConnectorCount = 0;
105 int nodeConnectorDescStatsCount = 0;
106 List<Node> nodes = getNodes();
107 for (Node node2 : nodes) {
108 NodeKey nodeKey = node2.getKey();
109 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.create(Nodes.class).child(Node.class, nodeKey);
111 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
112 Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
114 if (node.getNodeConnector() != null) {
115 List<NodeConnector> ports = node.getNodeConnector();
116 for (NodeConnector nodeConnector2 : ports) {
117 nodeConnectorCount++;
118 NodeConnectorKey nodeConnectorKey = nodeConnector2.getKey();
119 InstanceIdentifier<FlowCapableNodeConnector> connectorRef = InstanceIdentifier
120 .create(Nodes.class).child(Node.class, nodeKey)
121 .child(NodeConnector.class, nodeConnectorKey)
122 .augmentation(FlowCapableNodeConnector.class);
123 FlowCapableNodeConnector nodeConnector = TestProviderTransactionUtil
124 .getDataObject(readOnlyTransaction, connectorRef);
125 if (nodeConnector != null) {
126 if (null != nodeConnector.getName() && null != nodeConnector.getCurrentFeature()
127 && null != nodeConnector.getState() && null != nodeConnector.getHardwareAddress()
128 && null != nodeConnector.getPortNumber()) {
129 nodeConnectorDescStatsCount++;
138 if (nodeConnectorCount == nodeConnectorDescStatsCount) {
139 LOG.debug("portDescStats - Success");
141 LOG.debug("portDescStats - Failed");
142 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
147 public void _flowStats(CommandInterpreter ci) {
149 int flowStatsCount = 0;
150 List<Node> nodes = getNodes();
151 for (Node node2 : nodes) {
152 NodeKey nodeKey = node2.getKey();
153 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
154 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
156 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
157 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
160 List<Table> tables = node.getTable();
161 for (Table table2 : tables) {
162 TableKey tableKey = table2.getKey();
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 List<Flow> flows = table.getFlow();
170 for (Flow flow2 : flows) {
172 FlowKey flowKey = flow2.getKey();
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.getAugmentation(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 List<Node> nodes = getNodes();
206 for (Node node2 : nodes) {
207 NodeKey nodeKey = node2.getKey();
208 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
209 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
211 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
212 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
214 List<Table> tables = node.getTable();
215 for (Table table2 : tables) {
217 TableKey tableKey = table2.getKey();
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.getAugmentation(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 List<Node> nodes = getNodes();
246 for (Node node2 : nodes) {
247 NodeKey nodeKey = node2.getKey();
248 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
249 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
250 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
251 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
253 if (node.getGroup() != null) {
254 List<Group> groups = node.getGroup();
255 for (Group group2 : groups) {
257 GroupKey groupKey = group2.getKey();
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.getAugmentation(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 List<Node> nodes = getNodes();
287 for (Node node2 : nodes) {
288 NodeKey nodeKey = node2.getKey();
289 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
290 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
291 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
292 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
295 if (node.getGroup() != null) {
296 List<Group> groups = node.getGroup();
297 for (Group group2 : groups) {
299 GroupKey groupKey = group2.getKey();
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.getAugmentation(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 List<Node> nodes = getNodes();
329 for (Node node2 : nodes) {
330 NodeKey nodeKey = node2.getKey();
331 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
332 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
333 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
334 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
336 if (node.getMeter() != null) {
337 List<Meter> meters = node.getMeter();
338 for (Meter meter2 : meters) {
340 MeterKey meterKey = meter2.getKey();
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.getAugmentation(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 List<Node> nodes = getNodes();
370 for (Node node2 : nodes) {
371 NodeKey nodeKey = node2.getKey();
372 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
373 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
374 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
375 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
377 if (node.getMeter() != null) {
378 List<Meter> meters = node.getMeter();
379 for (Meter meter2 : meters) {
381 MeterKey meterKey = meter2.getKey();
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.getAugmentation(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 List<Node> nodes = getNodes();
411 for (Node node2 : nodes) {
412 NodeKey nodeKey = node2.getKey();
413 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
414 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
415 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
416 FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
418 List<Table> tables = node.getTable();
419 for (Table table2 : tables) {
420 aggregateFlowCount++;
421 TableKey tableKey = table2.getKey();
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.getAugmentation(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 List<Node> nodes = getNodes();
449 for (Node node2 : nodes) {
451 NodeKey nodeKey = node2.getKey();
452 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.create(Nodes.class)
453 .child(Node.class, nodeKey).augmentation(FlowCapableNode.class);
454 ReadOnlyTransaction 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 List<Node> getNodes() {
473 ReadOnlyTransaction 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.getNode();
483 public String getHelp() {
484 StringBuilder help = new StringBuilder();
485 help.append("---MD-SAL Stats test module---\n");
486 return help.toString();