dd6eb4bc64e69791b2846fe1542c75697db19c40
[openflowplugin.git] / test-provider / src / main / java / org / opendaylight / openflowplugin / test / OpenflowpluginStatsTestCommandProvider.java
1 /*
2  * Copyright (c) 2014, 2015 IBM Corporation and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.test;
10
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;
44
45 @SuppressWarnings("checkstyle:MethodName")
46 public class OpenflowpluginStatsTestCommandProvider implements CommandProvider {
47
48     private static final Logger LOG = LoggerFactory.getLogger(OpenflowpluginStatsTestCommandProvider.class);
49     private DataBroker dataProviderService;
50     private final BundleContext ctx;
51
52     public OpenflowpluginStatsTestCommandProvider(BundleContext ctx) {
53         this.ctx = ctx;
54     }
55
56     public void onSessionInitiated(ProviderContext session) {
57         dataProviderService = session.getSALService(DataBroker.class);
58         ctx.registerService(CommandProvider.class.getName(), this, null);
59
60     }
61
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);
71             if (node != null) {
72                 if (node.getNodeConnector() != null) {
73                     List<NodeConnector> ports = node.getNodeConnector();
74
75                     for (NodeConnector nodeConnector2 : ports) {
76                         nodeConnectorCount++;
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,
81                                 connectorRef);
82                         if (nodeConnector != null) {
83                             FlowCapableNodeConnectorStatisticsData data = nodeConnector
84                                     .getAugmentation(FlowCapableNodeConnectorStatisticsData.class);
85                             if (null != data) {
86                                 nodeConnectorStatsCount++;
87                             }
88                         }
89                     }
90                 }
91             }
92         }
93
94         if (nodeConnectorCount == nodeConnectorStatsCount) {
95             LOG.debug("portStats - Success");
96         } else {
97             LOG.debug("portStats - Failed");
98             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
99         }
100
101     }
102
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);
110
111             ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
112             Node node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
113             if (node != null) {
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++;
130                             }
131                         }
132                     }
133                 }
134
135             }
136         }
137
138         if (nodeConnectorCount == nodeConnectorDescStatsCount) {
139             LOG.debug("portDescStats - Success");
140         } else {
141             LOG.debug("portDescStats - Failed");
142             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
143         }
144
145     }
146
147     public void _flowStats(CommandInterpreter ci) {
148         int flowCount = 0;
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);
155
156             ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
157             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
158
159             if (node != null) {
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);
167                     if (table != null) {
168                         if (table.getFlow() != null) {
169                             List<Flow> flows = table.getFlow();
170                             for (Flow flow2 : flows) {
171                                 flowCount++;
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);
177                                 if (flow != null) {
178                                     FlowStatisticsData data = flow.getAugmentation(FlowStatisticsData.class);
179                                     if (null != data) {
180                                         flowStatsCount++;
181                                         LOG.debug("--------------------------------------------");
182                                         ci.print(data);
183                                     }
184                                 }
185
186                             }
187                         }
188                     }
189                 }
190             }
191         }
192
193         if (flowCount == flowStatsCount) {
194             LOG.debug("flowStats - Success");
195         } else {
196             LOG.debug("flowStats - Failed");
197             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
198         }
199
200     }
201
202     public void _tableStats(CommandInterpreter ci) {
203         int tableCount = 0;
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);
210
211             ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
212             FlowCapableNode node = TestProviderTransactionUtil.getDataObject(readOnlyTransaction, nodeRef);
213             if (node != null) {
214                 List<Table> tables = node.getTable();
215                 for (Table table2 : tables) {
216                     tableCount++;
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);
222                     if (table != null) {
223                         FlowTableStatisticsData data = table.getAugmentation(FlowTableStatisticsData.class);
224                         if (null != data) {
225                             tableStatsCount++;
226                         }
227                     }
228                 }
229             }
230         }
231
232         if (tableCount == tableStatsCount) {
233             LOG.debug("tableStats - Success");
234         } else {
235             LOG.debug("tableStats - Failed");
236             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
237         }
238
239     }
240
241     public void _groupStats(CommandInterpreter ci) {
242         int groupCount = 0;
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);
252             if (node != null) {
253                 if (node.getGroup() != null) {
254                     List<Group> groups = node.getGroup();
255                     for (Group group2 : groups) {
256                         groupCount++;
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);
262                         if (group != null) {
263                             data = group.getAugmentation(NodeGroupStatistics.class);
264                             if (null != data) {
265                                 groupStatsCount++;
266                             }
267                         }
268                     }
269                 }
270
271             }
272         }
273
274         if (groupCount == groupStatsCount) {
275             LOG.debug("---------------------groupStats - Success-------------------------------");
276         } else {
277             LOG.debug("------------------------------groupStats - Failed--------------------------");
278             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
279         }
280     }
281
282     public void _groupDescStats(CommandInterpreter ci) {
283         int groupCount = 0;
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);
293
294             if (node != null) {
295                 if (node.getGroup() != null) {
296                     List<Group> groups = node.getGroup();
297                     for (Group group2 : groups) {
298                         groupCount++;
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);
304                         if (group != null) {
305                             data = group.getAugmentation(NodeGroupDescStats.class);
306                             if (null != data) {
307                                 groupDescStatsCount++;
308                             }
309                         }
310
311                     }
312                 }
313             }
314
315             if (groupCount == groupDescStatsCount) {
316                 LOG.debug("---------------------groupDescStats - Success-------------------------------");
317             } else {
318                 LOG.debug("------------------------------groupDescStats - Failed--------------------------");
319                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
320             }
321         }
322     }
323
324     public void _meterStats(CommandInterpreter ci) {
325         int meterCount = 0;
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);
335             if (node != null) {
336                 if (node.getMeter() != null) {
337                     List<Meter> meters = node.getMeter();
338                     for (Meter meter2 : meters) {
339                         meterCount++;
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);
345                         if (meter != null) {
346                             data = meter.getAugmentation(NodeMeterStatistics.class);
347                             if (null != data) {
348                                 meterStatsCount++;
349                             }
350                         }
351                     }
352
353                 }
354             }
355         }
356
357         if (meterCount == meterStatsCount) {
358             LOG.debug("---------------------------meterStats - Success-------------------------------------");
359         } else {
360             LOG.debug("----------------------------meterStats - Failed-------------------------------------");
361             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
362         }
363     }
364
365     public void _meterConfigStats(CommandInterpreter ci) {
366         int meterCount = 0;
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);
376             if (node != null) {
377                 if (node.getMeter() != null) {
378                     List<Meter> meters = node.getMeter();
379                     for (Meter meter2 : meters) {
380                         meterCount++;
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);
386                         if (meter != null) {
387                             data = meter.getAugmentation(NodeMeterConfigStats.class);
388                             if (null != data) {
389                                 meterConfigStatsCount++;
390                             }
391                         }
392                     }
393
394                 }
395             }
396         }
397
398         if (meterCount == meterConfigStatsCount) {
399             LOG.debug("---------------------------meterConfigStats - Success-------------------------------------");
400             ci.print(data);
401         } else {
402             LOG.debug("----------------------------meterConfigStats - Failed-------------------------------------");
403             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
404         }
405     }
406
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);
417             if (node != null) {
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);
426                     if (table != null) {
427                         AggregateFlowStatisticsData data = table.getAugmentation(AggregateFlowStatisticsData.class);
428                         if (null != data) {
429                             aggerateFlowStatsCount++;
430                         }
431                     }
432                 }
433             }
434         }
435
436         if (aggregateFlowCount == aggerateFlowStatsCount) {
437             LOG.debug("aggregateStats - Success");
438         } else {
439             LOG.debug("aggregateStats - Failed");
440             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
441         }
442
443     }
444
445     public void _descStats(CommandInterpreter ci) {
446         int descCount = 0;
447         int descStatsCount = 0;
448         List<Node> nodes = getNodes();
449         for (Node node2 : nodes) {
450             descCount++;
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);
456             if (node != null) {
457                 if (null != node.getHardware() && null != node.getManufacturer() && null != node.getSoftware()) {
458                     descStatsCount++;
459                 }
460             }
461         }
462
463         if (descCount == descStatsCount) {
464             LOG.debug("descStats - Success");
465         } else {
466             LOG.debug("descStats - Failed");
467             LOG.debug("System fetches stats data in 50 seconds interval, so please wait and try again.");
468         }
469
470     }
471
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);
476         if (nodes == null) {
477             throw new RuntimeException("nodes are not found, pls add the node.");
478         }
479         return nodes.getNode();
480     }
481
482     @Override
483     public String getHelp() {
484         StringBuilder help = new StringBuilder();
485         help.append("---MD-SAL Stats test module---\n");
486         return help.toString();
487     }
488
489 }