d1053aaa2906e1ab5339d1eb5ce662c3481a56af
[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 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;
38
39 public class OpenflowpluginStatsTestCommandProvider implements CommandProvider {
40
41     private static final Logger LOG = LoggerFactory.getLogger(OpenflowpluginStatsTestCommandProvider.class);
42     private DataBroker dataProviderService;
43     private final BundleContext ctx;
44
45     public OpenflowpluginStatsTestCommandProvider(BundleContext ctx) {
46         this.ctx = ctx;
47     }
48
49
50     public void onSessionInitiated(ProviderContext session) {
51         dataProviderService = session.getSALService(DataBroker.class);
52         ctx.registerService(CommandProvider.class.getName(), this, null);
53
54     }
55
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();
63
64             ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
65             Node node = (Node) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
66
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) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, connectorRef);
73                 FlowCapableNodeConnectorStatisticsData data = nodeConnector.getAugmentation(FlowCapableNodeConnectorStatisticsData.class);
74                 if (null != data) {
75                     nodeConnectorStatsCount++;
76                 }
77             }
78         }
79
80         if (nodeConnectorCount == nodeConnectorStatsCount) {
81             LOG.debug("portStats - Success");
82         } else {
83             LOG.debug("portStats - Failed");
84             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
85         }
86
87     }
88
89
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();
97
98             ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
99             Node node = (Node) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
100
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++;
113                 }
114             }
115         }
116
117         if (nodeConnectorCount == nodeConnectorDescStatsCount) {
118             LOG.debug("portDescStats - Success");
119         } else {
120             LOG.debug("portDescStats - Failed");
121             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
122         }
123
124     }
125
126     public void _flowStats(CommandInterpreter ci) {
127         int flowCount = 0;
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();
133
134             ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
135             FlowCapableNode node = (FlowCapableNode) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
136
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(); ) {
144                     flowCount++;
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);
149                     if (null != data) {
150                         flowStatsCount++;
151                         LOG.debug("--------------------------------------------");
152                         ci.print(data);
153                     }
154                 }
155             }
156         }
157
158         if (flowCount == flowStatsCount) {
159             LOG.debug("flowStats - Success");
160         } else {
161             LOG.debug("flowStats - Failed");
162             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
163         }
164
165     }
166
167
168     public void _tableStats(CommandInterpreter ci) {
169         int tableCount = 0;
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();
175
176             ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
177             FlowCapableNode node = (FlowCapableNode) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeRef);
178
179             List<Table> tables = node.getTable();
180             for (Iterator<Table> iterator2 = tables.iterator(); iterator2.hasNext(); ) {
181                 tableCount++;
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);
186                 if (null != data) {
187                     tableStatsCount++;
188                 }
189             }
190         }
191
192         if (tableCount == tableStatsCount) {
193             LOG.debug("tableStats - Success");
194         } else {
195             LOG.debug("tableStats - Failed");
196             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
197         }
198
199     }
200
201     public void _groupStats(CommandInterpreter ci) {
202         int groupCount = 0;
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);
211
212             List<Group> groups = node.getGroup();
213             for (Iterator<Group> iterator2 = groups.iterator(); iterator2.hasNext(); ) {
214                 groupCount++;
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);
219                 if (null != data) {
220                     groupStatsCount++;
221                 }
222             }
223         }
224
225         if (groupCount == groupStatsCount) {
226             LOG.debug("---------------------groupStats - Success-------------------------------");
227         } else {
228             LOG.debug("------------------------------groupStats - Failed--------------------------");
229             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
230         }
231     }
232
233     public void _groupDescStats(CommandInterpreter ci) {
234         int groupCount = 0;
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);
243
244             List<Group> groups = node.getGroup();
245             for (Iterator<Group> iterator2 = groups.iterator(); iterator2.hasNext(); ) {
246                 groupCount++;
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);
251                 if (null != data) {
252                     groupDescStatsCount++;
253                 }
254             }
255         }
256
257         if (groupCount == groupDescStatsCount) {
258             LOG.debug("---------------------groupDescStats - Success-------------------------------");
259         } else {
260             LOG.debug("------------------------------groupDescStats - Failed--------------------------");
261             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
262         }
263     }
264
265     public void _meterStats(CommandInterpreter ci) {
266         int meterCount = 0;
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);
275
276             List<Meter> meters = node.getMeter();
277             for (Iterator<Meter> iterator2 = meters.iterator(); iterator2.hasNext(); ) {
278                 meterCount++;
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);
283                 if (null != data) {
284                     meterStatsCount++;
285                 }
286             }
287         }
288
289         if (meterCount == meterStatsCount) {
290             LOG.debug("---------------------------meterStats - Success-------------------------------------");
291         } else {
292             LOG.debug("----------------------------meterStats - Failed-------------------------------------");
293             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
294         }
295     }
296
297     public void _meterConfigStats(CommandInterpreter ci) {
298         int meterCount = 0;
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);
307
308             List<Meter> meters = node.getMeter();
309             for (Iterator<Meter> iterator2 = meters.iterator(); iterator2.hasNext(); ) {
310                 meterCount++;
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);
315                 if (null != data) {
316                     meterConfigStatsCount++;
317                 }
318             }
319         }
320
321         if (meterCount == meterConfigStatsCount) {
322             LOG.debug("---------------------------meterConfigStats - Success-------------------------------------");
323             ci.print(data);
324         } else {
325             LOG.debug("----------------------------meterConfigStats - Failed-------------------------------------");
326             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
327         }
328     }
329
330
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);
340
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);
348                 if (null != data) {
349                     aggerateFlowStatsCount++;
350                 }
351             }
352         }
353
354         if (aggregateFlowCount == aggerateFlowStatsCount) {
355             LOG.debug("aggregateStats - Success");
356         } else {
357             LOG.debug("aggregateStats - Failed");
358             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
359         }
360
361     }
362
363     public void _descStats(CommandInterpreter ci) {
364         int descCount = 0;
365         int descStatsCount = 0;
366         List<Node> nodes = getNodes();
367         for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
368             descCount++;
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()) {
376                 descStatsCount++;
377             }
378         }
379
380         if (descCount == descStatsCount) {
381             LOG.debug("descStats - Success");
382         } else {
383             LOG.debug("descStats - Failed");
384             LOG.debug("System fetchs stats data in 50 seconds interval, so pls wait and try again.");
385         }
386
387     }
388
389     private List<Node> getNodes() {
390         ReadOnlyTransaction readOnlyTransaction = dataProviderService.newReadOnlyTransaction();
391         Nodes nodes = (Nodes) readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(Nodes.class).toInstance());
392         if (null == nodes) {
393             throw new RuntimeException("nodes are not found, pls add the node.");
394         }
395         return nodes.getNode();
396
397     }
398
399
400     @Override
401     public String getHelp() {
402         StringBuffer help = new StringBuffer();
403         help.append("---MD-SAL Stats test module---\n");
404         return help.toString();
405     }
406
407
408 }