Group and Meter desc statistics testing
[openflowplugin.git] / test-provider / src / main / java / org / opendaylight / openflowplugin / test / OpenflowpluginStatsTestCommandProvider.java
1 package org.opendaylight.openflowplugin.test;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 import org.eclipse.osgi.framework.console.CommandInterpreter;
7 import org.eclipse.osgi.framework.console.CommandProvider;
8 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
9 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatistics;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.osgi.framework.BundleContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class OpenflowpluginStatsTestCommandProvider implements CommandProvider {
43
44         private static final Logger LOG = LoggerFactory.getLogger(OpenflowpluginStatsTestCommandProvider.class);
45     private DataProviderService dataProviderService;
46     private final BundleContext ctx;
47
48     public OpenflowpluginStatsTestCommandProvider(BundleContext ctx) {
49         this.ctx = ctx;
50     }
51     
52
53     public void onSessionInitiated(ProviderContext session) {
54         dataProviderService = session.getSALService(DataProviderService.class);
55         ctx.registerService(CommandProvider.class.getName(), this, null);
56       
57     }
58
59     public void _portStats(CommandInterpreter ci) {
60         int nodeConnectorCount = 0;
61         int nodeConnectorStatsCount = 0;
62             List<Node> nodes = getNodes();
63             for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
64                 NodeKey nodeKey =  iterator.next().getKey();
65                 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).toInstance();
66                 Node node = (Node)dataProviderService.readOperationalData(nodeRef);
67                 List<NodeConnector> ports =  node.getNodeConnector();
68                 for (Iterator<NodeConnector> iterator2 = ports.iterator(); iterator2.hasNext();) {
69                     nodeConnectorCount++;
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)dataProviderService.readOperationalData(connectorRef);
73                     FlowCapableNodeConnectorStatisticsData data = nodeConnector.getAugmentation(FlowCapableNodeConnectorStatisticsData.class);
74                     if(null != data)
75                     {
76                         nodeConnectorStatsCount++;
77                     }
78                 }
79             }
80             
81             if(nodeConnectorCount == nodeConnectorStatsCount)
82             {
83                 LOG.debug("portStats - Success");
84             }
85             else
86             {
87                 LOG.debug("portStats - Failed");
88                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
89             }
90                            
91        }
92
93     
94     public void _portDescStats(CommandInterpreter ci) {
95         int nodeConnectorCount = 0;
96         int nodeConnectorDescStatsCount = 0;
97             List<Node> nodes = getNodes();
98             for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
99                 NodeKey nodeKey =  iterator.next().getKey();
100                 InstanceIdentifier<Node> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).toInstance();
101                 Node node = (Node)dataProviderService.readOperationalData(nodeRef);
102
103                List<NodeConnector> ports =  node.getNodeConnector();
104                 for (Iterator<NodeConnector> iterator2 = ports.iterator(); iterator2.hasNext();) {
105                     nodeConnectorCount++;
106                     NodeConnectorKey nodeConnectorKey = iterator2.next().getKey();
107                     InstanceIdentifier<FlowCapableNodeConnector> connectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).child(NodeConnector.class, nodeConnectorKey).augmentation(FlowCapableNodeConnector.class).toInstance();
108                     FlowCapableNodeConnector nodeConnector = (FlowCapableNodeConnector)dataProviderService.readOperationalData(connectorRef);
109                     if(null != nodeConnector.getName() &&
110                             null != nodeConnector.getCurrentFeature() &&
111                             null != nodeConnector.getState() &&
112                             null != nodeConnector.getHardwareAddress() &&
113                             null != nodeConnector.getPortNumber())
114                     {
115                         nodeConnectorDescStatsCount++;
116                     }
117                 }
118             }
119             
120             if(nodeConnectorCount == nodeConnectorDescStatsCount)
121             {
122                 LOG.debug("portDescStats - Success");
123             }
124             else
125             {
126                 LOG.debug("portDescStats - Failed");
127                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
128             }
129                            
130        }
131
132     public void _flowStats(CommandInterpreter ci) {
133         int flowCount = 0;
134         int flowStatsCount = 0;
135             List<Node> nodes = getNodes();
136             for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
137                 NodeKey nodeKey =  iterator.next().getKey();
138                 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
139                 FlowCapableNode node = (FlowCapableNode)dataProviderService.readOperationalData(nodeRef);
140                 List<Table> tables =  node.getTable();
141                 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext();) {
142                     TableKey tableKey = iterator2.next().getKey();
143                     InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Table.class, tableKey).toInstance();
144                     Table table = (Table)dataProviderService.readOperationalData(tableRef);
145                     List<Flow> flows = table.getFlow();
146                     for (Iterator<Flow> iterator3 = flows.iterator(); iterator3.hasNext();) {
147                         flowCount++;
148                         FlowKey flowKey =  iterator3.next().getKey();
149                         InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Table.class, tableKey).child(Flow.class, flowKey).toInstance();
150                         Flow flow = (Flow)dataProviderService.readOperationalData(flowRef);
151                         FlowStatisticsData data = flow.getAugmentation(FlowStatisticsData.class);
152                         if(null != data)
153                         {
154                             flowStatsCount++;
155                             LOG.debug("--------------------------------------------");
156                             ci.print(data);
157                         }
158                     }                    
159                 }
160             }
161             
162             if(flowCount == flowStatsCount)
163             {
164                 LOG.debug("flowStats - Success");
165             }
166             else
167             {
168                 LOG.debug("flowStats - Failed");
169                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
170             }
171                            
172        }
173
174    
175  
176
177     public void _tableStats(CommandInterpreter ci) {
178         int tableCount = 0;
179         int tableStatsCount = 0;
180             List<Node> nodes = getNodes();
181             for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
182                 NodeKey nodeKey =  iterator.next().getKey();
183                 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
184                 FlowCapableNode node = (FlowCapableNode)dataProviderService.readOperationalData(nodeRef);
185
186                List<Table> tables =  node.getTable();
187                 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext();) {
188                     tableCount++;
189                     TableKey tableKey = iterator2.next().getKey();
190                     InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Table.class, tableKey).toInstance();
191                     Table table = (Table)dataProviderService.readOperationalData(tableRef);
192                     FlowTableStatisticsData data = table.getAugmentation(FlowTableStatisticsData.class);
193                     if(null != data)
194                     {
195                         tableStatsCount++;
196                     }
197                 }
198             }
199             
200             if(tableCount == tableStatsCount)
201             {
202                 LOG.debug("tableStats - Success");
203             }
204             else
205             {
206                 LOG.debug("tableStats - Failed");
207                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
208             }
209                            
210        }
211
212     public void _groupStats(CommandInterpreter ci) {
213         int groupCount = 0;
214         int groupStatsCount = 0;
215         NodeGroupStatistics data = null;
216             List<Node> nodes = getNodes();
217             for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
218                 NodeKey nodeKey =  iterator.next().getKey();
219                 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
220                 FlowCapableNode node = (FlowCapableNode)dataProviderService.readOperationalData(nodeRef);
221
222                List<Group> groups =  node.getGroup();
223                 for (Iterator<Group> iterator2 = groups.iterator(); iterator2.hasNext();) {
224                     groupCount++;
225                     GroupKey groupKey = iterator2.next().getKey();
226                     InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Group.class, groupKey).toInstance();
227                     Group group = (Group)dataProviderService.readOperationalData(groupRef);
228                     data = group.getAugmentation(NodeGroupStatistics.class);
229                     if(null != data)
230                     {
231                         groupStatsCount++;
232                     }
233                 }
234             }
235             
236             if(groupCount == groupStatsCount)
237             {
238                 LOG.debug("---------------------groupStats - Success-------------------------------");           
239             }
240             else
241             {
242                 LOG.debug("------------------------------groupStats - Failed--------------------------");
243                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
244             }                        
245        }
246     
247     public void _groupDescStats(CommandInterpreter ci) {
248         int groupCount = 0;
249         int groupDescStatsCount = 0;
250         NodeGroupDescStats data = null;
251             List<Node> nodes = getNodes();
252             for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
253                 NodeKey nodeKey =  iterator.next().getKey();
254                 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
255                 FlowCapableNode node = (FlowCapableNode)dataProviderService.readOperationalData(nodeRef);
256
257                List<Group> groups =  node.getGroup();
258                 for (Iterator<Group> iterator2 = groups.iterator(); iterator2.hasNext();) {
259                     groupCount++;
260                     GroupKey groupKey = iterator2.next().getKey();
261                     InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Group.class, groupKey).toInstance();
262                     Group group = (Group)dataProviderService.readOperationalData(groupRef);
263                     data = group.getAugmentation(NodeGroupDescStats.class);
264                     if(null != data)
265                     {
266                         groupDescStatsCount++;
267                     }
268                 }
269             }
270             
271             if(groupCount == groupDescStatsCount)
272             {
273                 LOG.debug("---------------------groupDescStats - Success-------------------------------");
274             }
275             else
276             {
277                 LOG.debug("------------------------------groupDescStats - Failed--------------------------");
278                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
279             }                        
280        }
281     
282     public void _meterStats(CommandInterpreter ci) {
283         int meterCount = 0;
284         int meterStatsCount = 0;
285         NodeMeterStatistics data = null;
286             List<Node> nodes = getNodes();
287             for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
288                 NodeKey nodeKey =  iterator.next().getKey();
289                 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
290                 FlowCapableNode node = (FlowCapableNode)dataProviderService.readOperationalData(nodeRef);
291
292                List<Meter> meters =  node.getMeter();
293                 for (Iterator<Meter> iterator2 = meters.iterator(); iterator2.hasNext();) {
294                     meterCount++;
295                     MeterKey meterKey = iterator2.next().getKey();
296                     InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Meter.class, meterKey).toInstance();
297                     Meter meter = (Meter)dataProviderService.readOperationalData(meterRef);
298                     data = meter.getAugmentation(NodeMeterStatistics.class);
299                     if(null != data)
300                     {
301                         meterStatsCount++;
302                     }
303                 }
304             }
305             
306             if(meterCount == meterStatsCount)
307             {
308                 LOG.debug("---------------------------meterStats - Success-------------------------------------");
309             }
310             else
311             {
312                 LOG.debug("----------------------------meterStats - Failed-------------------------------------");
313                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
314             }                        
315        }
316  
317     public void _meterConfigStats(CommandInterpreter ci) {
318         int meterCount = 0;
319         int meterConfigStatsCount = 0;
320         NodeMeterConfigStats 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.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
325                 FlowCapableNode node = (FlowCapableNode)dataProviderService.readOperationalData(nodeRef);
326
327                List<Meter> meters =  node.getMeter();
328                 for (Iterator<Meter> iterator2 = meters.iterator(); iterator2.hasNext();) {
329                     meterCount++;
330                     MeterKey meterKey = iterator2.next().getKey();
331                     InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Meter.class, meterKey).toInstance();
332                     Meter meter = (Meter)dataProviderService.readOperationalData(meterRef);
333                     data = meter.getAugmentation(NodeMeterConfigStats.class);
334                     if(null != data)
335                     {
336                         meterConfigStatsCount++;
337                     }
338                 }
339             }
340             
341             if(meterCount == meterConfigStatsCount)
342             {
343                 LOG.debug("---------------------------meterConfigStats - Success-------------------------------------");
344                 ci.print(data);
345             }
346             else
347             {
348                 LOG.debug("----------------------------meterConfigStats - Failed-------------------------------------");
349                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
350             }                        
351        }
352  
353     
354     public void _aggregateStats(CommandInterpreter ci) {
355         int aggregateFlowCount = 0;
356         int aggerateFlowStatsCount = 0;
357             List<Node> nodes = getNodes();
358             for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
359                 NodeKey nodeKey =  iterator.next().getKey();
360                 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
361                 FlowCapableNode node = (FlowCapableNode)dataProviderService.readOperationalData(nodeRef);
362
363                List<Table> tables =  node.getTable();
364                 for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext();) {
365                     aggregateFlowCount++;
366                     TableKey tableKey = iterator2.next().getKey();
367                     InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).child(Table.class, tableKey).toInstance();
368                     Table table = (Table)dataProviderService.readOperationalData(tableRef);
369                     AggregateFlowStatisticsData data = table.getAugmentation(AggregateFlowStatisticsData.class);
370                     if(null != data)
371                     {
372                         aggerateFlowStatsCount++;
373                     }
374                 }
375             }
376             
377             if(aggregateFlowCount == aggerateFlowStatsCount)
378             {
379                 LOG.debug("aggregateStats - Success");
380             }
381             else
382             {
383                 LOG.debug("aggregateStats - Failed");
384                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
385             }
386                            
387        }
388
389     public void _descStats(CommandInterpreter ci) {
390         int descCount = 0;
391         int descStatsCount = 0;
392             List<Node> nodes = getNodes();
393             for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
394                 descCount++;
395                 NodeKey nodeKey =  iterator.next().getKey();
396                 InstanceIdentifier<FlowCapableNode> nodeRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey).augmentation(FlowCapableNode.class).toInstance();
397                 FlowCapableNode node = (FlowCapableNode)dataProviderService.readOperationalData(nodeRef);
398                     if(null != node.getHardware() &&
399                             null != node.getManufacturer() &&
400                             null != node.getSoftware())
401                     {
402                         descStatsCount++;
403                     }
404             }
405             
406             if(descCount == descStatsCount)
407             {
408                 LOG.debug("descStats - Success");
409             }
410             else
411             {
412                 LOG.debug("descStats - Failed");
413                 LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
414             }
415                            
416        }    
417     
418     private List<Node> getNodes()
419     {
420         Nodes nodes = (Nodes)dataProviderService.readOperationalData(InstanceIdentifier.builder(Nodes.class).toInstance());
421         if(null == nodes)
422         {
423             throw new RuntimeException("nodes are not found, pls add the node.");
424         }
425         return nodes.getNode();
426         
427     }
428     
429  
430     @Override
431     public String getHelp() {
432         StringBuffer help = new StringBuffer();
433         help.append("---MD-SAL Stats test module---\n");
434         return help.toString();
435     }
436
437  
438 }