Keep track of the tables we discover
[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.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15
16 import org.opendaylight.controller.md.statistics.manager.MultipartMessageManager.StatsRequestType;
17 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
18 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
19 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
20 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
21 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
22 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
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);
240         }
241     }
242
243     private void sendStatisticsRequestsToNode(NodeStatisticsHandler h) {
244         NodeKey targetNode = h.getTargetNodeKey();
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).build();
248
249         NodeRef targetNodeRef = new NodeRef(targetInstanceId);
250
251         try{
252             if(flowTableStatsService != null){
253                 sendAllFlowTablesStatisticsRequest(targetNodeRef);
254             }
255             if(flowStatsService != null){
256                 // FIXME: it does not make sense to trigger this before sendAllFlowTablesStatisticsRequest()
257                 //        comes back -- we do not have any tables anyway.
258                 sendAggregateFlowsStatsFromAllTablesRequest(h);
259
260                 sendAllFlowsStatsFromAllTablesRequest(targetNodeRef);
261             }
262             if(portStatsService != null){
263                 sendAllNodeConnectorsStatisticsRequest(targetNodeRef);
264             }
265             if(groupStatsService != null){
266                 sendAllGroupStatisticsRequest(targetNodeRef);
267                 sendGroupDescriptionRequest(targetNodeRef);
268             }
269             if(meterStatsService != null){
270                 sendAllMeterStatisticsRequest(targetNodeRef);
271                 sendMeterConfigStatisticsRequest(targetNodeRef);
272             }
273             if(queueStatsService != null){
274                 sendAllQueueStatsFromAllNodeConnector(targetNodeRef);
275             }
276         }catch(Exception e){
277             spLogger.error("Exception occured while sending statistics requests : {}", e);
278         }
279     }
280
281
282     private void sendAllFlowTablesStatisticsRequest(NodeRef targetNodeRef) throws InterruptedException, ExecutionException {
283         final GetFlowTablesStatisticsInputBuilder input =
284                 new GetFlowTablesStatisticsInputBuilder();
285
286         input.setNode(targetNodeRef);
287
288         Future<RpcResult<GetFlowTablesStatisticsOutput>> response =
289                 flowTableStatsService.getFlowTablesStatistics(input.build());
290
291         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNodeRef),response.get().getResult().getTransactionId()
292                 , StatsRequestType.ALL_FLOW_TABLE);
293
294     }
295
296     private void sendAllFlowsStatsFromAllTablesRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
297         final GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input =
298                 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder();
299
300         input.setNode(targetNode);
301
302         Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> response =
303                 flowStatsService.getAllFlowsStatisticsFromAllFlowTables(input.build());
304
305         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
306                 , StatsRequestType.ALL_FLOW);
307
308     }
309
310     public void sendFlowStatsFromTableRequest(NodeRef targetNode,Flow flow) throws InterruptedException, ExecutionException{
311         final GetFlowStatisticsFromFlowTableInputBuilder input =
312                 new GetFlowStatisticsFromFlowTableInputBuilder();
313
314         input.setNode(targetNode);
315         input.fieldsFrom(flow);
316
317         Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> response =
318                 flowStatsService.getFlowStatisticsFromFlowTable(input.build());
319
320         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
321                 , StatsRequestType.ALL_FLOW);
322
323     }
324
325     private void sendAggregateFlowsStatsFromAllTablesRequest(final NodeStatisticsHandler h) throws InterruptedException, ExecutionException{
326         final Collection<TableKey> tables = h.getKnownTables();
327         spLogger.debug("Node {} supports {} table(s)", h, tables.size());
328
329         for (TableKey key : h.getKnownTables()) {
330             sendAggregateFlowsStatsFromTableRequest(h.getTargetNodeKey(), key.getId().shortValue());
331         }
332     }
333
334     private void sendAggregateFlowsStatsFromTableRequest(NodeKey targetNodeKey,Short tableId) throws InterruptedException, ExecutionException{
335
336         spLogger.debug("Send aggregate stats request for flow table {} to node {}",tableId,targetNodeKey);
337         GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input =
338                 new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder();
339
340         input.setNode(new NodeRef(InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey).toInstance()));
341         input.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(tableId));
342         Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> response =
343                 flowStatsService.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());
344
345         multipartMessageManager.setTxIdAndTableIdMapEntry(targetNodeKey.getId(), response.get().getResult().getTransactionId(), tableId);
346         this.multipartMessageManager.addTxIdToRequestTypeEntry(targetNodeKey.getId(), response.get().getResult().getTransactionId()
347                 , StatsRequestType.AGGR_FLOW);
348     }
349
350     private void sendAllNodeConnectorsStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
351
352         final GetAllNodeConnectorsStatisticsInputBuilder input = new GetAllNodeConnectorsStatisticsInputBuilder();
353
354         input.setNode(targetNode);
355
356         Future<RpcResult<GetAllNodeConnectorsStatisticsOutput>> response =
357                 portStatsService.getAllNodeConnectorsStatistics(input.build());
358         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
359                 , StatsRequestType.ALL_PORT);
360
361     }
362
363     private void sendAllGroupStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
364
365         final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
366
367         input.setNode(targetNode);
368
369         Future<RpcResult<GetAllGroupStatisticsOutput>> response =
370                 groupStatsService.getAllGroupStatistics(input.build());
371
372         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
373                 , StatsRequestType.ALL_GROUP);
374
375     }
376
377     public void sendGroupDescriptionRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
378         final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
379
380         input.setNode(targetNode);
381
382         Future<RpcResult<GetGroupDescriptionOutput>> response =
383                 groupStatsService.getGroupDescription(input.build());
384
385         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
386                 , StatsRequestType.GROUP_DESC);
387
388     }
389
390     private void sendAllMeterStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
391
392         GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
393
394         input.setNode(targetNode);
395
396         Future<RpcResult<GetAllMeterStatisticsOutput>> response =
397                 meterStatsService.getAllMeterStatistics(input.build());
398
399         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
400                 , StatsRequestType.ALL_METER);;
401
402     }
403
404     public void sendMeterConfigStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
405
406         GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
407
408         input.setNode(targetNode);
409
410         Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response =
411                 meterStatsService.getAllMeterConfigStatistics(input.build());
412
413         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
414                 , StatsRequestType.METER_CONFIG);;
415
416     }
417
418     private void sendAllQueueStatsFromAllNodeConnector(NodeRef targetNode) throws InterruptedException, ExecutionException {
419         GetAllQueuesStatisticsFromAllPortsInputBuilder input = new GetAllQueuesStatisticsFromAllPortsInputBuilder();
420
421         input.setNode(targetNode);
422
423         Future<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> response =
424                 queueStatsService.getAllQueuesStatisticsFromAllPorts(input.build());
425
426         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
427                 , StatsRequestType.ALL_QUEUE_STATS);;
428
429     }
430
431     public void sendQueueStatsFromGivenNodeConnector(NodeRef targetNode,NodeConnectorId nodeConnectorId, QueueId queueId) throws InterruptedException, ExecutionException {
432         GetQueueStatisticsFromGivenPortInputBuilder input = new GetQueueStatisticsFromGivenPortInputBuilder();
433
434         input.setNode(targetNode);
435         input.setNodeConnectorId(nodeConnectorId);
436         input.setQueueId(queueId);
437         Future<RpcResult<GetQueueStatisticsFromGivenPortOutput>> response =
438                 queueStatsService.getQueueStatisticsFromGivenPort(input.build());
439
440         this.multipartMessageManager.addTxIdToRequestTypeEntry(getNodeId(targetNode), response.get().getResult().getTransactionId()
441                 , StatsRequestType.ALL_QUEUE_STATS);;
442
443     }
444
445     /**
446      * Get the handler for a particular node.
447      *
448      * @param nodeId source node
449      * @return Node statistics handler for that node. Null if the statistics should
450      *         not handled.
451      */
452     public final NodeStatisticsHandler getStatisticsHandler(final NodeId nodeId) {
453         Preconditions.checkNotNull(nodeId);
454         NodeStatisticsHandler handler = handlers.get(nodeId);
455         if (handler == null) {
456             spLogger.info("Attempted to get non-existing handler for {}", nodeId);
457         }
458         return handler;
459     }
460
461     @SuppressWarnings("unchecked")
462     private NodeId getNodeId(NodeRef nodeRef){
463         InstanceIdentifier<Node> nodeII = (InstanceIdentifier<Node>) nodeRef.getValue();
464         NodeKey nodeKey = InstanceIdentifier.keyOf(nodeII);
465         return nodeKey.getId();
466     }
467
468     @Override
469     public void close() {
470         try {
471             if (this.listenerRegistration != null) {
472                 this.listenerRegistration.close();
473                 this.statisticsRequesterThread.destroy();
474                 this.statisticsAgerThread.destroy();
475             }
476             if (this.flowCapableTrackerRegistration != null) {
477                 this.flowCapableTrackerRegistration.close();
478                 this.flowCapableTrackerRegistration = null;
479             }
480         } catch (Exception e) {
481             spLogger.warn("Failed to stop Statistics Provider completely", e);
482         } finally {
483             spLogger.info("Statistics Provider stopped.");
484         }
485     }
486
487     synchronized void startNodeHandlers(final Collection<NodeKey> addedNodes) {
488         for (NodeKey key : addedNodes) {
489             if (handlers.containsKey(key.getId())) {
490                 spLogger.warn("Attempted to start already-existing handler for {}, very strange", key.getId());
491                 continue;
492             }
493
494             final NodeStatisticsHandler h = new NodeStatisticsHandler(this, key);
495             handlers.put(key.getId(), h);
496             spLogger.debug("Started node handler for {}", key.getId());
497
498             // FIXME: this should be in the NodeStatisticsHandler itself
499             sendStatisticsRequestsToNode(h);
500         }
501     }
502
503     synchronized void stopNodeHandlers(final Collection<NodeKey> removedNodes) {
504         for (NodeKey key : removedNodes) {
505             final NodeStatisticsHandler s = handlers.remove(key.getId());
506             if (s != null) {
507                 spLogger.debug("Stopping node handler for {}", key.getId());
508                 s.close();
509             } else {
510                 spLogger.warn("Attempted to remove non-existing handler for {}, very strange", key.getId());
511             }
512         }
513     }
514 }