Walk the tables directly
[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.Collection;
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.opendaylight.controller.md.statistics.manager.MultipartMessageManager.StatsRequestType;
18 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
19 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
20 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
21 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
22 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
23 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsInputBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInputBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsOutput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsInputBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsOutput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortInputBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
67 import org.opendaylight.yangtools.concepts.ListenerRegistration;
68 import org.opendaylight.yangtools.concepts.Registration;
69 import org.opendaylight.yangtools.yang.binding.DataObject;
70 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
71 import org.opendaylight.yangtools.yang.binding.NotificationListener;
72 import org.opendaylight.yangtools.yang.common.RpcResult;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75
76 import com.google.common.base.Preconditions;
77
78 /**
79  * Following are main responsibilities of the class:
80  * 1) Invoke statistics request thread to send periodic statistics request to all the
81  * flow capable switch connected to the controller. It sends statistics request for
82  * Group,Meter,Table,Flow,Queue,Aggregate stats.
83  *
84  * 2) Invoke statistics ager thread, to clean up all the stale statistics data from
85  * operational data store.
86  *
87  * @author avishnoi@in.ibm.com
88  *
89  */
90 public class StatisticsProvider implements AutoCloseable {
91     public static final int STATS_THREAD_EXECUTION_TIME= 15000;
92
93     private static final Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
94
95     private final MultipartMessageManager multipartMessageManager = new MultipartMessageManager();
96     private final DataProviderService dps;
97
98     //Local caching of stats
99     private final ConcurrentMap<NodeId, NodeStatisticsHandler> handlers = new ConcurrentHashMap<>();
100
101     private OpendaylightGroupStatisticsService groupStatsService;
102
103     private OpendaylightMeterStatisticsService meterStatsService;
104
105     private OpendaylightFlowStatisticsService flowStatsService;
106
107     private OpendaylightPortStatisticsService portStatsService;
108
109     private OpendaylightFlowTableStatisticsService flowTableStatsService;
110
111     private OpendaylightQueueStatisticsService queueStatsService;
112
113     private StatisticsUpdateHandler statsUpdateHandler;
114
115     private Thread statisticsRequesterThread;
116
117     private Thread statisticsAgerThread;
118
119
120     public StatisticsProvider(final DataProviderService dataService) {
121         this.dps = Preconditions.checkNotNull(dataService);
122     }
123
124     public MultipartMessageManager getMultipartMessageManager() {
125         return multipartMessageManager;
126     }
127
128     private final StatisticsListener updateCommiter = new StatisticsListener(StatisticsProvider.this);
129
130     private Registration<NotificationListener> listenerRegistration;
131
132     private ListenerRegistration<DataChangeListener> flowCapableTrackerRegistration;
133
134     public void start(final DataBrokerService dbs, final NotificationProviderService nps, final RpcConsumerRegistry rpcRegistry) {
135
136         // Get Group/Meter statistics service instances
137         groupStatsService = rpcRegistry.getRpcService(OpendaylightGroupStatisticsService.class);
138         meterStatsService = rpcRegistry.getRpcService(OpendaylightMeterStatisticsService.class);
139         flowStatsService = rpcRegistry.getRpcService(OpendaylightFlowStatisticsService.class);
140         portStatsService = rpcRegistry.getRpcService(OpendaylightPortStatisticsService.class);
141         flowTableStatsService = rpcRegistry.getRpcService(OpendaylightFlowTableStatisticsService.class);
142         queueStatsService = rpcRegistry.getRpcService(OpendaylightQueueStatisticsService.class);
143
144         // Start receiving notifications
145         this.listenerRegistration = nps.registerNotificationListener(this.updateCommiter);
146
147         // Register for switch connect/disconnect notifications
148         final InstanceIdentifier<FlowCapableNode> fcnId = InstanceIdentifier.builder(Nodes.class)
149                 .child(Node.class).augmentation(FlowCapableNode.class).build();
150         this.flowCapableTrackerRegistration = dbs.registerDataChangeListener(fcnId,
151                 new FlowCapableTracker(this, fcnId));
152
153         statsUpdateHandler = new StatisticsUpdateHandler(StatisticsProvider.this);
154         registerDataStoreUpdateListener(dbs);
155
156         statisticsRequesterThread = new Thread( new Runnable(){
157
158             @Override
159             public void run() {
160                 while(true){
161                     try {
162                         statsRequestSender();
163
164                         Thread.sleep(STATS_THREAD_EXECUTION_TIME);
165                     }catch (Exception e){
166                         spLogger.error("Exception occurred while sending stats request : {}",e);
167                     }
168                 }
169             }
170         });
171
172         spLogger.debug("Statistics requester thread started with timer interval : {}",STATS_THREAD_EXECUTION_TIME);
173
174         statisticsRequesterThread.start();
175
176         statisticsAgerThread = new Thread( new Runnable(){
177
178             @Override
179             public void run() {
180                 while(true){
181                     try {
182                         for(NodeStatisticsHandler nodeStatisticsAger : handlers.values()){
183                             nodeStatisticsAger.cleanStaleStatistics();
184                         }
185                         multipartMessageManager.cleanStaleTransactionIds();
186
187                         Thread.sleep(STATS_THREAD_EXECUTION_TIME);
188                     }catch (Exception e){
189                         spLogger.error("Exception occurred while sending stats request : {}",e);
190                     }
191                 }
192             }
193         });
194
195         spLogger.debug("Statistics ager thread started with timer interval : {}",STATS_THREAD_EXECUTION_TIME);
196
197         statisticsAgerThread.start();
198
199         spLogger.info("Statistics Provider started.");
200     }
201
202     private void registerDataStoreUpdateListener(DataBrokerService dbs) {
203         // FIXME: the below should be broken out into StatisticsUpdateHandler
204
205         //Register for flow updates
206         InstanceIdentifier<? extends DataObject> pathFlow = InstanceIdentifier.builder(Nodes.class).child(Node.class)
207                                                                     .augmentation(FlowCapableNode.class)
208                                                                     .child(Table.class)
209                                                                     .child(Flow.class).toInstance();
210         dbs.registerDataChangeListener(pathFlow, statsUpdateHandler);
211
212         //Register for meter updates
213         InstanceIdentifier<? extends DataObject> pathMeter = InstanceIdentifier.builder(Nodes.class).child(Node.class)
214                                                     .augmentation(FlowCapableNode.class)
215                                                     .child(Meter.class).toInstance();
216
217         dbs.registerDataChangeListener(pathMeter, statsUpdateHandler);
218
219         //Register for group updates
220         InstanceIdentifier<? extends DataObject> pathGroup = InstanceIdentifier.builder(Nodes.class).child(Node.class)
221                                                     .augmentation(FlowCapableNode.class)
222                                                     .child(Group.class).toInstance();
223         dbs.registerDataChangeListener(pathGroup, statsUpdateHandler);
224
225         //Register for queue updates
226         InstanceIdentifier<? extends DataObject> pathQueue = InstanceIdentifier.builder(Nodes.class).child(Node.class)
227                                                                     .child(NodeConnector.class)
228                                                                     .augmentation(FlowCapableNodeConnector.class)
229                                                                     .child(Queue.class).toInstance();
230         dbs.registerDataChangeListener(pathQueue, statsUpdateHandler);
231     }
232
233     protected DataModificationTransaction startChange() {
234         return dps.beginTransaction();
235     }
236
237     private void statsRequestSender() {
238         for (NodeStatisticsHandler h : handlers.values()) {
239             sendStatisticsRequestsToNode(h.getTargetNodeKey());
240         }
241     }
242
243     private void sendStatisticsRequestsToNode(NodeKey targetNode){
244
245         spLogger.debug("Send requests for statistics collection to node : {})",targetNode.getId());
246
247         InstanceIdentifier<Node> targetInstanceId = InstanceIdentifier.builder(Nodes.class).child(Node.class,targetNode).toInstance();
248
249         NodeRef targetNodeRef = new NodeRef(targetInstanceId);
250
251         try{
252             if(flowStatsService != null){
253                 sendAggregateFlowsStatsFromAllTablesRequest(targetNode);
254                 sendAllFlowsStatsFromAllTablesRequest(targetNodeRef);
255             }
256             if(flowTableStatsService != null){
257                 sendAllFlowTablesStatisticsRequest(targetNodeRef);
258             }
259             if(portStatsService != null){
260                 sendAllNodeConnectorsStatisticsRequest(targetNodeRef);
261             }
262             if(groupStatsService != null){
263                 sendAllGroupStatisticsRequest(targetNodeRef);
264                 sendGroupDescriptionRequest(targetNodeRef);
265             }
266             if(meterStatsService != null){
267                 sendAllMeterStatisticsRequest(targetNodeRef);
268                 sendMeterConfigStatisticsRequest(targetNodeRef);
269             }
270             if(queueStatsService != null){
271                 sendAllQueueStatsFromAllNodeConnector (targetNodeRef);
272             }
273         }catch(Exception e){
274             spLogger.error("Exception occured while sending statistics requests : {}", e);
275         }
276     }
277
278
279     private void sendAllFlowTablesStatisticsRequest(NodeRef targetNodeRef) throws InterruptedException, ExecutionException {
280         final GetFlowTablesStatisticsInputBuilder input =
281                 new GetFlowTablesStatisticsInputBuilder();
282
283         input.setNode(targetNodeRef);
284
285         Future<RpcResult<GetFlowTablesStatisticsOutput>> response =
286                 flowTableStatsService.getFlowTablesStatistics(input.build());
287
288         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNodeRef),response.get().getResult().getTransactionId()
289                 , StatsRequestType.ALL_FLOW_TABLE);
290
291     }
292
293     private void sendAllFlowsStatsFromAllTablesRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
294         final GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input =
295                 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder();
296
297         input.setNode(targetNode);
298
299         Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> response =
300                 flowStatsService.getAllFlowsStatisticsFromAllFlowTables(input.build());
301
302         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
303                 , StatsRequestType.ALL_FLOW);
304
305     }
306
307     public void sendFlowStatsFromTableRequest(NodeRef targetNode,Flow flow) throws InterruptedException, ExecutionException{
308         final GetFlowStatisticsFromFlowTableInputBuilder input =
309                 new GetFlowStatisticsFromFlowTableInputBuilder();
310
311         input.setNode(targetNode);
312         input.fieldsFrom(flow);
313
314         Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> response =
315                 flowStatsService.getFlowStatisticsFromFlowTable(input.build());
316
317         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
318                 , StatsRequestType.ALL_FLOW);
319
320     }
321
322     private void sendAggregateFlowsStatsFromAllTablesRequest(final NodeKey nodeKey) throws InterruptedException, ExecutionException{
323         FlowCapableNode node = (FlowCapableNode)dps.readOperationalData(
324                 InstanceIdentifier.builder(Nodes.class).child(Node.class,nodeKey).augmentation(FlowCapableNode.class).build());
325         if (node != null) {
326             final List<Table> tables = node.getTable();
327             if (tables != null) {
328                 spLogger.debug("Node {} supports {} table(s)", nodeKey, tables.size());
329                 for(Table table : tables) {
330                     sendAggregateFlowsStatsFromTableRequest(nodeKey, table.getId());
331                 }
332             } else {
333                 spLogger.debug("Node {} has no associated tables", nodeKey);
334             }
335         } else {
336             spLogger.debug("Node {} not found", nodeKey);
337         }
338     }
339
340     private void sendAggregateFlowsStatsFromTableRequest(NodeKey targetNodeKey,Short tableId) throws InterruptedException, ExecutionException{
341
342         spLogger.debug("Send aggregate stats request for flow table {} to node {}",tableId,targetNodeKey);
343         GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input =
344                 new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder();
345
346         input.setNode(new NodeRef(InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey).toInstance()));
347         input.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(tableId));
348         Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> response =
349                 flowStatsService.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());
350
351         multipartMessageManager.setTxIdAndTableIdMapEntry(targetNodeKey.getId(), response.get().getResult().getTransactionId(), tableId);
352         this.multipartMessageManager.addTxIdToRequestTypeEntry(targetNodeKey.getId(), response.get().getResult().getTransactionId()
353                 , StatsRequestType.AGGR_FLOW);
354     }
355
356     private void sendAllNodeConnectorsStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
357
358         final GetAllNodeConnectorsStatisticsInputBuilder input = new GetAllNodeConnectorsStatisticsInputBuilder();
359
360         input.setNode(targetNode);
361
362         Future<RpcResult<GetAllNodeConnectorsStatisticsOutput>> response =
363                 portStatsService.getAllNodeConnectorsStatistics(input.build());
364         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
365                 , StatsRequestType.ALL_PORT);
366
367     }
368
369     private void sendAllGroupStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
370
371         final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
372
373         input.setNode(targetNode);
374
375         Future<RpcResult<GetAllGroupStatisticsOutput>> response =
376                 groupStatsService.getAllGroupStatistics(input.build());
377
378         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
379                 , StatsRequestType.ALL_GROUP);
380
381     }
382
383     public void sendGroupDescriptionRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
384         final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
385
386         input.setNode(targetNode);
387
388         Future<RpcResult<GetGroupDescriptionOutput>> response =
389                 groupStatsService.getGroupDescription(input.build());
390
391         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
392                 , StatsRequestType.GROUP_DESC);
393
394     }
395
396     private void sendAllMeterStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
397
398         GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
399
400         input.setNode(targetNode);
401
402         Future<RpcResult<GetAllMeterStatisticsOutput>> response =
403                 meterStatsService.getAllMeterStatistics(input.build());
404
405         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
406                 , StatsRequestType.ALL_METER);;
407
408     }
409
410     public void sendMeterConfigStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
411
412         GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
413
414         input.setNode(targetNode);
415
416         Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response =
417                 meterStatsService.getAllMeterConfigStatistics(input.build());
418
419         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
420                 , StatsRequestType.METER_CONFIG);;
421
422     }
423
424     private void sendAllQueueStatsFromAllNodeConnector(NodeRef targetNode) throws InterruptedException, ExecutionException {
425         GetAllQueuesStatisticsFromAllPortsInputBuilder input = new GetAllQueuesStatisticsFromAllPortsInputBuilder();
426
427         input.setNode(targetNode);
428
429         Future<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> response =
430                 queueStatsService.getAllQueuesStatisticsFromAllPorts(input.build());
431
432         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
433                 , StatsRequestType.ALL_QUEUE_STATS);;
434
435     }
436
437     public void sendQueueStatsFromGivenNodeConnector(NodeRef targetNode,NodeConnectorId nodeConnectorId, QueueId queueId) throws InterruptedException, ExecutionException {
438         GetQueueStatisticsFromGivenPortInputBuilder input = new GetQueueStatisticsFromGivenPortInputBuilder();
439
440         input.setNode(targetNode);
441         input.setNodeConnectorId(nodeConnectorId);
442         input.setQueueId(queueId);
443         Future<RpcResult<GetQueueStatisticsFromGivenPortOutput>> response =
444                 queueStatsService.getQueueStatisticsFromGivenPort(input.build());
445
446         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
447                 , StatsRequestType.ALL_QUEUE_STATS);;
448
449     }
450
451     /**
452      * Get the handler for a particular node.
453      *
454      * @param nodeId source node
455      * @return Node statistics handler for that node. Null if the statistics should
456      *         not handled.
457      */
458     public final NodeStatisticsHandler getStatisticsHandler(final NodeId nodeId) {
459         Preconditions.checkNotNull(nodeId);
460         NodeStatisticsHandler handler = handlers.get(nodeId);
461         if (handler == null) {
462             spLogger.info("Attempted to get non-existing handler for {}", nodeId);
463         }
464         return handler;
465     }
466
467     @SuppressWarnings("unchecked")
468     private NodeId getNodeId(NodeRef nodeRef){
469         InstanceIdentifier<Node> nodeII = (InstanceIdentifier<Node>) nodeRef.getValue();
470         NodeKey nodeKey = InstanceIdentifier.keyOf(nodeII);
471         return nodeKey.getId();
472     }
473
474     @Override
475     public void close() {
476         try {
477             if (this.listenerRegistration != null) {
478                 this.listenerRegistration.close();
479                 this.statisticsRequesterThread.destroy();
480                 this.statisticsAgerThread.destroy();
481             }
482             if (this.flowCapableTrackerRegistration != null) {
483                 this.flowCapableTrackerRegistration.close();
484                 this.flowCapableTrackerRegistration = null;
485             }
486         } catch (Exception e) {
487             spLogger.warn("Failed to stop Statistics Provider completely", e);
488         } finally {
489             spLogger.info("Statistics Provider stopped.");
490         }
491     }
492
493     synchronized void startNodeHandlers(final Collection<NodeKey> addedNodes) {
494         for (NodeKey key : addedNodes) {
495             if (handlers.containsKey(key.getId())) {
496                 spLogger.warn("Attempted to start already-existing handler for {}, very strange", key.getId());
497                 continue;
498             }
499
500             final NodeStatisticsHandler h = new NodeStatisticsHandler(this, key);
501             handlers.put(key.getId(), h);
502             spLogger.debug("Started node handler for {}", key.getId());
503
504             // FIXME: this should be in the NodeStatisticsHandler itself
505             sendStatisticsRequestsToNode(key);
506         }
507     }
508
509     synchronized void stopNodeHandlers(final Collection<NodeKey> removedNodes) {
510         for (NodeKey key : removedNodes) {
511             final NodeStatisticsHandler s = handlers.remove(key.getId());
512             if (s != null) {
513                 spLogger.debug("Stopping node handler for {}", key.getId());
514                 s.close();
515             } else {
516                 spLogger.warn("Attempted to remove non-existing handler for {}, very strange", key.getId());
517             }
518         }
519     }
520 }