MD-SAL Statistics Manager: Added support for queue statistics collection
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / StatisticsProvider.java
1 /*
2  * Copyright IBM Corporation, 2013.  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 package org.opendaylight.controller.md.statistics.manager;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
16
17 import org.eclipse.xtext.xbase.lib.Exceptions;
18 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
19 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
20 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllPortsStatisticsInputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllPortsStatisticsOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
52 import org.opendaylight.yangtools.concepts.Registration;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.opendaylight.yangtools.yang.binding.NotificationListener;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public class StatisticsProvider implements AutoCloseable {
60
61     public final static Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
62     
63     private DataProviderService dps;
64
65     private NotificationProviderService nps;
66     
67     private OpendaylightGroupStatisticsService groupStatsService;
68     
69     private OpendaylightMeterStatisticsService meterStatsService;
70     
71     private OpendaylightFlowStatisticsService flowStatsService;
72     
73     private OpendaylightPortStatisticsService portStatsService;
74
75     private OpendaylightFlowTableStatisticsService flowTableStatsService;
76
77     private OpendaylightQueueStatisticsService queueStatsService;
78
79     private final MultipartMessageManager multipartMessageManager = new MultipartMessageManager();
80     
81     private Thread statisticsRequesterThread;
82     
83     private final  InstanceIdentifier<Nodes> nodesIdentifier = InstanceIdentifier.builder(Nodes.class).toInstance();
84     
85     private final int STATS_THREAD_EXECUTION_TIME= 50000;
86     //Local caching of stats
87     
88     private final ConcurrentMap<NodeId,NodeStatistics> statisticsCache = 
89             new ConcurrentHashMap<NodeId,NodeStatistics>();
90     
91     public DataProviderService getDataService() {
92       return this.dps;
93     }
94     
95     public void setDataService(final DataProviderService dataService) {
96       this.dps = dataService;
97     }
98     
99     public NotificationProviderService getNotificationService() {
100       return this.nps;
101     }
102     
103     public void setNotificationService(final NotificationProviderService notificationService) {
104       this.nps = notificationService;
105     }
106
107     public MultipartMessageManager getMultipartMessageManager() {
108         return multipartMessageManager;
109     }
110
111     private final StatisticsUpdateCommiter updateCommiter = new StatisticsUpdateCommiter(StatisticsProvider.this);
112     
113     private Registration<NotificationListener> listenerRegistration;
114     
115     public void start() {
116         
117         NotificationProviderService nps = this.getNotificationService();
118         Registration<NotificationListener> registerNotificationListener = nps.registerNotificationListener(this.updateCommiter);
119         this.listenerRegistration = registerNotificationListener;
120         
121         // Get Group/Meter statistics service instance
122         groupStatsService = StatisticsManagerActivator.getProviderContext().
123                 getRpcService(OpendaylightGroupStatisticsService.class);
124         
125         meterStatsService = StatisticsManagerActivator.getProviderContext().
126                 getRpcService(OpendaylightMeterStatisticsService.class);
127         
128         flowStatsService = StatisticsManagerActivator.getProviderContext().
129                 getRpcService(OpendaylightFlowStatisticsService.class);
130
131         portStatsService = StatisticsManagerActivator.getProviderContext().
132                 getRpcService(OpendaylightPortStatisticsService.class);
133
134         flowTableStatsService = StatisticsManagerActivator.getProviderContext().
135                 getRpcService(OpendaylightFlowTableStatisticsService.class);
136         
137         queueStatsService = StatisticsManagerActivator.getProviderContext().
138                 getRpcService(OpendaylightQueueStatisticsService.class);
139         
140         statisticsRequesterThread = new Thread( new Runnable(){
141
142             @Override
143             public void run() {
144                 while(true){
145                     try {
146                         statsRequestSender();
147                         
148                         Thread.sleep(STATS_THREAD_EXECUTION_TIME);
149                     }catch (Exception e){
150                         spLogger.error("Exception occurred while sending stats request : {}",e);
151                     }
152                 }
153             }
154         });
155         
156         spLogger.debug("Statistics requester thread started with timer interval : {}",STATS_THREAD_EXECUTION_TIME);
157         
158         statisticsRequesterThread.start();
159         
160         spLogger.info("Statistics Provider started.");
161     }
162     
163     protected DataModificationTransaction startChange() {
164         
165         DataProviderService dps = this.getDataService();
166         return dps.beginTransaction();
167     }
168     
169     private void statsRequestSender(){
170         
171         List<Node> targetNodes = getAllConnectedNodes();
172         
173         if(targetNodes == null)
174             return;
175         
176
177         for (Node targetNode : targetNodes){
178             
179             InstanceIdentifier<Node> targetInstanceId = InstanceIdentifier.builder(Nodes.class).child(Node.class,targetNode.getKey()).toInstance();
180             NodeRef targetNodeRef = new NodeRef(targetInstanceId);
181             
182             try {
183                 
184                 sendAggregateFlowsStatsFromAllTablesRequest(targetNode.getKey());
185
186                 sendAllFlowsStatsFromAllTablesRequest(targetNodeRef);
187
188                 sendAllPortStatisticsRequest(targetNodeRef);
189                 
190                 sendAllFlowTablesStatisticsRequest(targetNodeRef);
191                 
192                 sendAllQueueStatsFromAllNodeConnector (targetNodeRef);
193
194             }catch(Exception e){
195                 spLogger.error("Exception occured while sending statistics requests : {}",e);
196             }
197
198             if(targetNode.getAugmentation(FlowCapableNode.class) != null){
199
200                 spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
201
202                 try{
203                   sendAllGroupStatisticsRequest(targetNodeRef);
204                   Thread.sleep(1000);
205                   sendAllMeterStatisticsRequest(targetNodeRef);
206                   Thread.sleep(1000);
207                   sendGroupDescriptionRequest(targetNodeRef);
208                   Thread.sleep(1000);
209                   sendMeterConfigStatisticsRequest(targetNodeRef);
210                   Thread.sleep(1000);
211                 }catch(Exception e){
212                     spLogger.error("Exception occured while sending statistics requests : {}", e);
213                 }
214             }
215         }
216     }
217
218     private void sendAllFlowTablesStatisticsRequest(NodeRef targetNodeRef) {
219         final GetFlowTablesStatisticsInputBuilder input = 
220                 new GetFlowTablesStatisticsInputBuilder();
221         
222         input.setNode(targetNodeRef);
223
224         @SuppressWarnings("unused")
225         Future<RpcResult<GetFlowTablesStatisticsOutput>> response = 
226                 flowTableStatsService.getFlowTablesStatistics(input.build());
227     }
228
229     private void sendAllFlowsStatsFromAllTablesRequest(NodeRef targetNode){
230         final GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input =
231                 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder();
232         
233         input.setNode(targetNode);
234         
235         @SuppressWarnings("unused")
236         Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> response = 
237                 flowStatsService.getAllFlowsStatisticsFromAllFlowTables(input.build());
238         
239     }
240     
241     private void sendAggregateFlowsStatsFromAllTablesRequest(NodeKey targetNodeKey) throws InterruptedException, ExecutionException{
242         
243         List<Short> tablesId = getTablesFromNode(targetNodeKey);
244         
245         if(tablesId.size() != 0){
246             for(Short id : tablesId){
247                 
248                 spLogger.info("Send aggregate stats request for flow table {} to node {}",id,targetNodeKey);
249                 GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input = 
250                         new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder();
251                 
252                 input.setNode(new NodeRef(InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey).toInstance()));
253                 input.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(id));
254                 Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> response = 
255                         flowStatsService.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());
256                 
257                 multipartMessageManager.setTxIdAndTableIdMapEntry(response.get().getResult().getTransactionId(), id);
258             }
259         }
260         
261         //Note: Just for testing, because i am not able to fetch table list from datastore
262         // Bug-225 is raised for investigation.
263         
264 //                spLogger.info("Send aggregate stats request for flow table {} to node {}",1,targetNodeKey);
265 //                GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input = 
266 //                        new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder();
267 //                
268 //                input.setNode(new NodeRef(InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey).toInstance()));
269 //                input.setTableId(new TableId((short)1));
270 //                Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> response = 
271 //                        flowStatsService.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());`
272 //                
273 //                multipartMessageManager.setTxIdAndTableIdMapEntry(response.get().getResult().getTransactionId(), (short)1);
274     }
275
276     private void sendAllPortStatisticsRequest(NodeRef targetNode){
277         
278         final GetAllPortsStatisticsInputBuilder input = new GetAllPortsStatisticsInputBuilder();
279         
280         input.setNode(targetNode);
281
282         @SuppressWarnings("unused")
283         Future<RpcResult<GetAllPortsStatisticsOutput>> response = 
284                 portStatsService.getAllPortsStatistics(input.build());
285     }
286
287     private void sendAllGroupStatisticsRequest(NodeRef targetNode){
288         
289         final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
290         
291         input.setNode(targetNode);
292
293         @SuppressWarnings("unused")
294         Future<RpcResult<GetAllGroupStatisticsOutput>> response = 
295                 groupStatsService.getAllGroupStatistics(input.build());
296     }
297     
298     private void sendGroupDescriptionRequest(NodeRef targetNode){
299         final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
300         
301         input.setNode(targetNode);
302
303         @SuppressWarnings("unused")
304         Future<RpcResult<GetGroupDescriptionOutput>> response = 
305                 groupStatsService.getGroupDescription(input.build());
306     }
307     
308     private void sendAllMeterStatisticsRequest(NodeRef targetNode){
309         
310         GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
311         
312         input.setNode(targetNode);
313
314         @SuppressWarnings("unused")
315         Future<RpcResult<GetAllMeterStatisticsOutput>> response = 
316                 meterStatsService.getAllMeterStatistics(input.build());
317     }
318     
319     private void sendMeterConfigStatisticsRequest(NodeRef targetNode){
320         
321         GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
322         
323         input.setNode(targetNode);
324
325         @SuppressWarnings("unused")
326         Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response = 
327                 meterStatsService.getAllMeterConfigStatistics(input.build());
328     }
329     
330     private void sendAllQueueStatsFromAllNodeConnector(NodeRef targetNode) {
331         GetAllQueuesStatisticsFromAllPortsInputBuilder input = new GetAllQueuesStatisticsFromAllPortsInputBuilder();
332         
333         input.setNode(targetNode);
334         
335         @SuppressWarnings("unused")
336         Future<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> response = 
337                 queueStatsService.getAllQueuesStatisticsFromAllPorts(input.build());
338     }
339
340     public ConcurrentMap<NodeId, NodeStatistics> getStatisticsCache() {
341         return statisticsCache;
342     }
343     
344     private List<Node> getAllConnectedNodes(){
345         
346         Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
347         if(nodes == null)
348             return null;
349         
350         spLogger.info("Number of connected nodes : {}",nodes.getNode().size());
351         return nodes.getNode();
352     }
353     
354     private List<Short> getTablesFromNode(NodeKey nodeKey){
355         InstanceIdentifier<FlowCapableNode> nodesIdentifier = InstanceIdentifier.builder(Nodes.class).child(Node.class,nodeKey).augmentation(FlowCapableNode.class).toInstance();
356         
357         FlowCapableNode node = (FlowCapableNode)dps.readConfigurationData(nodesIdentifier);
358         List<Short> tablesId = new ArrayList<Short>();
359         if(node != null && node.getTable()!=null){
360             spLogger.info("Number of tables {} supported by node {}",node.getTable().size(),nodeKey);
361             for(Table table: node.getTable()){
362                 tablesId.add(table.getId());
363             }
364         }
365         return tablesId;
366     }
367
368     @SuppressWarnings("deprecation")
369     @Override
370     public void close(){
371         
372         try {
373             spLogger.info("Statistics Provider stopped.");
374             if (this.listenerRegistration != null) {
375               
376                 this.listenerRegistration.close();
377                 
378                 this.statisticsRequesterThread.destroy();
379             
380             }
381           } catch (Throwable e) {
382             throw Exceptions.sneakyThrow(e);
383           }
384     }
385
386 }