1 package org.opendaylight.openflowplugin.test;
3 import java.util.Iterator;
5 import org.eclipse.osgi.framework.console.CommandInterpreter;
6 import org.eclipse.osgi.framework.console.CommandProvider;
7 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
8 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
9 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
10 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.osgi.framework.BundleContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class OpenflowpluginStatsTestCommandProvider implements CommandProvider {
41 private static final Logger LOG = LoggerFactory.getLogger(OpenflowpluginStatsTestCommandProvider.class);
42 private DataBroker dataProviderService;
43 private final BundleContext ctx;
45 public OpenflowpluginStatsTestCommandProvider(BundleContext ctx) {
50 public void onSessionInitiated(ProviderContext session) {
51 dataProviderService = session.getSALService(DataBroker.class);
52 ctx.registerService(CommandProvider.class.getName(), this, null);
56 public void _portStats(CommandInterpreter ci) {
57 int nodeConnectorCount = 0;
58 int nodeConnectorStatsCount = 0;
59 List<Node> nodes = getNodes();
60 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
61 NodeKey nodeKey = iterator.next().getKey();
62 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).toInstance();
64 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
65 Node node = (Node) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
67 List<NodeConnector> ports = node.getNodeConnector();
68 for (Iterator<NodeConnector> iterator2 = ports.iterator(); iterator2.hasNext(); ) {
70 NodeConnectorKey nodeConnectorKey = iterator2.next().getKey();
71 InstanceIdentifier<NodeConnector> connectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).child(NodeConnector.class, nodeConnectorKey).toInstance();
72 NodeConnector nodeConnector = (NodeConnector) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, connectorRef);
73 FlowCapableNodeConnectorStatisticsData data = nodeConnector.getAugmentation(FlowCapableNodeConnectorStatisticsData.class);
75 nodeConnectorStatsCount++;
80 if (nodeConnectorCount == nodeConnectorStatsCount) {
81 LOG.debug("portStats - Success");
83 LOG.debug("portStats - Failed");
84 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
90 public void _portDescStats(CommandInterpreter ci) {
91 int nodeConnectorCount = 0;
92 int nodeConnectorDescStatsCount = 0;
93 List<Node> nodes = getNodes();
94 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
95 NodeKey nodeKey = iterator.next().getKey();
96 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).toInstance();
98 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
99 Node node = (Node) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
101 List<NodeConnector> ports = node.getNodeConnector();
102 for (Iterator<NodeConnector> iterator2 = ports.iterator(); iterator2.hasNext(); ) {
103 nodeConnectorCount++;
104 NodeConnectorKey nodeConnectorKey = iterator2.next().getKey();
105 InstanceIdentifier<FlowCapableNodeConnector> connectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).child(NodeConnector.class, nodeConnectorKey).augmentation(FlowCapableNodeConnector.class).toInstance();
106 FlowCapableNodeConnector nodeConnector = (FlowCapableNodeConnector) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, connectorRef);
107 if (null != nodeConnector.getName() &&
108 null != nodeConnector.getCurrentFeature() &&
109 null != nodeConnector.getState() &&
110 null != nodeConnector.getHardwareAddress() &&
111 null != nodeConnector.getPortNumber()) {
112 nodeConnectorDescStatsCount++;
117 if (nodeConnectorCount == nodeConnectorDescStatsCount) {
118 LOG.debug("portDescStats - Success");
120 LOG.debug("portDescStats - Failed");
121 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
126 public void _flowStats(CommandInterpreter ci) {
128 int flowStatsCount = 0;
129 List<Node> nodes = getNodes();
130 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
131 NodeKey nodeKey = iterator.next().getKey();
132 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
134 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
135 FlowCapableNode node = (FlowCapableNode) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
137 List<Table> tables = node.getTable();
138 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext(); ) {
139 TableKey tableKey = iterator2.next().getKey();
140 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Table.class, tableKey).toInstance();
141 Table table = (Table) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, tableRef);
142 List<Flow> flows = table.getFlow();
143 for (Iterator<Flow> iterator3 = flows.iterator(); iterator3.hasNext(); ) {
145 FlowKey flowKey = iterator3.next().getKey();
146 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey).toInstance();
147 Flow flow = (Flow) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, flowRef);
148 FlowStatisticsData data = flow.getAugmentation(FlowStatisticsData.class);
151 LOG.debug("--------------------------------------------");
158 if (flowCount == flowStatsCount) {
159 LOG.debug("flowStats - Success");
161 LOG.debug("flowStats - Failed");
162 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
168 public void _tableStats(CommandInterpreter ci) {
170 int tableStatsCount = 0;
171 List<Node> nodes = getNodes();
172 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
173 NodeKey nodeKey = iterator.next().getKey();
174 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
176 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
177 FlowCapableNode node = (FlowCapableNode) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
179 List<Table> tables = node.getTable();
180 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext(); ) {
182 TableKey tableKey = iterator2.next().getKey();
183 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Table.class, tableKey).toInstance();
184 Table table = (Table) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, tableRef);
185 FlowTableStatisticsData data = table.getAugmentation(FlowTableStatisticsData.class);
192 if (tableCount == tableStatsCount) {
193 LOG.debug("tableStats - Success");
195 LOG.debug("tableStats - Failed");
196 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
201 public void _groupStats(CommandInterpreter ci) {
203 int groupStatsCount = 0;
204 NodeGroupStatistics data = null;
205 List<Node> nodes = getNodes();
206 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
207 NodeKey nodeKey = iterator.next().getKey();
208 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
209 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
210 FlowCapableNode node = (FlowCapableNode) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
212 List<Group> groups = node.getGroup();
213 for (Iterator<Group> iterator2 = groups.iterator(); iterator2.hasNext(); ) {
215 GroupKey groupKey = iterator2.next().getKey();
216 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Group.class, groupKey).toInstance();
217 Group group = (Group) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, groupRef);
218 data = group.getAugmentation(NodeGroupStatistics.class);
225 if (groupCount == groupStatsCount) {
226 LOG.debug("---------------------groupStats - Success-------------------------------");
228 LOG.debug("------------------------------groupStats - Failed--------------------------");
229 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
233 public void _groupDescStats(CommandInterpreter ci) {
235 int groupDescStatsCount = 0;
236 NodeGroupDescStats data = null;
237 List<Node> nodes = getNodes();
238 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
239 NodeKey nodeKey = iterator.next().getKey();
240 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
241 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
242 FlowCapableNode node = (FlowCapableNode) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
244 List<Group> groups = node.getGroup();
245 for (Iterator<Group> iterator2 = groups.iterator(); iterator2.hasNext(); ) {
247 GroupKey groupKey = iterator2.next().getKey();
248 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Group.class, groupKey).toInstance();
249 Group group = (Group) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, groupRef);
250 data = group.getAugmentation(NodeGroupDescStats.class);
252 groupDescStatsCount++;
257 if (groupCount == groupDescStatsCount) {
258 LOG.debug("---------------------groupDescStats - Success-------------------------------");
260 LOG.debug("------------------------------groupDescStats - Failed--------------------------");
261 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
265 public void _meterStats(CommandInterpreter ci) {
267 int meterStatsCount = 0;
268 NodeMeterStatistics data = null;
269 List<Node> nodes = getNodes();
270 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
271 NodeKey nodeKey = iterator.next().getKey();
272 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
273 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
274 FlowCapableNode node = (FlowCapableNode) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
276 List<Meter> meters = node.getMeter();
277 for (Iterator<Meter> iterator2 = meters.iterator(); iterator2.hasNext(); ) {
279 MeterKey meterKey = iterator2.next().getKey();
280 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Meter.class, meterKey).toInstance();
281 Meter meter = (Meter) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, meterRef);
282 data = meter.getAugmentation(NodeMeterStatistics.class);
289 if (meterCount == meterStatsCount) {
290 LOG.debug("---------------------------meterStats - Success-------------------------------------");
292 LOG.debug("----------------------------meterStats - Failed-------------------------------------");
293 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
297 public void _meterConfigStats(CommandInterpreter ci) {
299 int meterConfigStatsCount = 0;
300 NodeMeterConfigStats data = null;
301 List<Node> nodes = getNodes();
302 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
303 NodeKey nodeKey = iterator.next().getKey();
304 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
305 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
306 FlowCapableNode node = (FlowCapableNode) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
308 List<Meter> meters = node.getMeter();
309 for (Iterator<Meter> iterator2 = meters.iterator(); iterator2.hasNext(); ) {
311 MeterKey meterKey = iterator2.next().getKey();
312 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Meter.class, meterKey).toInstance();
313 Meter meter = (Meter) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, meterRef);
314 data = meter.getAugmentation(NodeMeterConfigStats.class);
316 meterConfigStatsCount++;
321 if (meterCount == meterConfigStatsCount) {
322 LOG.debug("---------------------------meterConfigStats - Success-------------------------------------");
325 LOG.debug("----------------------------meterConfigStats - Failed-------------------------------------");
326 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
331 public void _aggregateStats(CommandInterpreter ci) {
332 int aggregateFlowCount = 0;
333 int aggerateFlowStatsCount = 0;
334 List<Node> nodes = getNodes();
335 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
336 NodeKey nodeKey = iterator.next().getKey();
337 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
338 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
339 FlowCapableNode node = (FlowCapableNode) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
341 List<Table> tables = node.getTable();
342 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext(); ) {
343 aggregateFlowCount++;
344 TableKey tableKey = iterator2.next().getKey();
345 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Table.class, tableKey).toInstance();
346 Table table = (Table) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, tableRef);
347 AggregateFlowStatisticsData data = table.getAugmentation(AggregateFlowStatisticsData.class);
349 aggerateFlowStatsCount++;
354 if (aggregateFlowCount == aggerateFlowStatsCount) {
355 LOG.debug("aggregateStats - Success");
357 LOG.debug("aggregateStats - Failed");
358 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
363 public void _descStats(CommandInterpreter ci) {
365 int descStatsCount = 0;
366 List<Node> nodes = getNodes();
367 for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
369 NodeKey nodeKey = iterator.next().getKey();
370 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
371 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
372 FlowCapableNode node = (FlowCapableNode) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
373 if (null != node.getHardware() &&
374 null != node.getManufacturer() &&
375 null != node.getSoftware()) {
380 if (descCount == descStatsCount) {
381 LOG.debug("descStats - Success");
383 LOG.debug("descStats - Failed");
384 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
389 private List<Node> getNodes() {
390 ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
391 Nodes nodes = (Nodes) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(Nodes.class).toInstance());
393 throw new RuntimeException("nodes are not found, pls add the node.");
395 return nodes.getNode();
401 public String getHelp() {
402 StringBuffer help = new StringBuffer();
403 help.append("---MD-SAL Stats test module---\n");
404 return help.toString();