Disconnect node statistics tracking
[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.Timer;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14
15 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
16 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
17 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
18 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.concepts.Registration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.NotificationListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.base.Preconditions;
38
39 /**
40  * Following are main responsibilities of the class:
41  * 1) Invoke statistics request thread to send periodic statistics request to all the
42  * flow capable switch connected to the controller. It sends statistics request for
43  * Group,Meter,Table,Flow,Queue,Aggregate stats.
44  *
45  * 2) Invoke statistics ager thread, to clean up all the stale statistics data from
46  * operational data store.
47  *
48  * @author avishnoi@in.ibm.com
49  *
50  */
51 public class StatisticsProvider implements AutoCloseable {
52     private static final Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
53
54     private final ConcurrentMap<NodeId, NodeStatisticsHandler> handlers = new ConcurrentHashMap<>();
55     private final Timer timer = new Timer("statistics-manager", true);
56     private final DataProviderService dps;
57
58     private OpendaylightGroupStatisticsService groupStatsService;
59
60     private OpendaylightMeterStatisticsService meterStatsService;
61
62     private OpendaylightFlowStatisticsService flowStatsService;
63
64     private OpendaylightPortStatisticsService portStatsService;
65
66     private OpendaylightFlowTableStatisticsService flowTableStatsService;
67
68     private OpendaylightQueueStatisticsService queueStatsService;
69
70     public StatisticsProvider(final DataProviderService dataService) {
71         this.dps = Preconditions.checkNotNull(dataService);
72     }
73
74     private final StatisticsListener updateCommiter = new StatisticsListener(StatisticsProvider.this);
75
76     private Registration<NotificationListener> listenerRegistration;
77
78     private ListenerRegistration<DataChangeListener> flowCapableTrackerRegistration;
79
80     public void start(final NotificationProviderService nps, final RpcConsumerRegistry rpcRegistry) {
81
82         // Get Group/Meter statistics service instances
83         groupStatsService = rpcRegistry.getRpcService(OpendaylightGroupStatisticsService.class);
84         meterStatsService = rpcRegistry.getRpcService(OpendaylightMeterStatisticsService.class);
85         flowStatsService = rpcRegistry.getRpcService(OpendaylightFlowStatisticsService.class);
86         portStatsService = rpcRegistry.getRpcService(OpendaylightPortStatisticsService.class);
87         flowTableStatsService = rpcRegistry.getRpcService(OpendaylightFlowTableStatisticsService.class);
88         queueStatsService = rpcRegistry.getRpcService(OpendaylightQueueStatisticsService.class);
89
90         // Start receiving notifications
91         this.listenerRegistration = nps.registerNotificationListener(this.updateCommiter);
92
93         // Register for switch connect/disconnect notifications
94         final InstanceIdentifier<FlowCapableNode> fcnId = InstanceIdentifier.builder(Nodes.class)
95                 .child(Node.class).augmentation(FlowCapableNode.class).build();
96         spLogger.debug("Registering FlowCapable tracker to {}", fcnId);
97         this.flowCapableTrackerRegistration = dps.registerDataChangeListener(fcnId,
98                 new FlowCapableTracker(this, fcnId));
99
100         spLogger.info("Statistics Provider started.");
101     }
102
103     /**
104      * Get the handler for a particular node.
105      *
106      * @param nodeId source node
107      * @return Node statistics handler for that node. Null if the statistics should
108      *         not handled.
109      */
110     public final NodeStatisticsHandler getStatisticsHandler(final NodeId nodeId) {
111         Preconditions.checkNotNull(nodeId);
112         NodeStatisticsHandler handler = handlers.get(nodeId);
113         if (handler == null) {
114             spLogger.info("Attempted to get non-existing handler for {}", nodeId);
115         }
116         return handler;
117     }
118
119     @Override
120     public void close() {
121         try {
122             if (this.listenerRegistration != null) {
123                 this.listenerRegistration.close();
124                 this.listenerRegistration = null;
125             }
126             if (this.flowCapableTrackerRegistration != null) {
127                 this.flowCapableTrackerRegistration.close();
128                 this.flowCapableTrackerRegistration = null;
129             }
130             timer.cancel();
131         } catch (Exception e) {
132             spLogger.warn("Failed to stop Statistics Provider completely", e);
133         } finally {
134             spLogger.info("Statistics Provider stopped.");
135         }
136     }
137
138     void startNodeHandlers(final Collection<NodeKey> addedNodes) {
139         for (NodeKey key : addedNodes) {
140             if (handlers.containsKey(key.getId())) {
141                 spLogger.warn("Attempted to start already-existing handler for {}, very strange", key.getId());
142                 continue;
143             }
144
145             final NodeStatisticsHandler h = new NodeStatisticsHandler(dps, key,
146                     flowStatsService, flowTableStatsService, groupStatsService,
147                     meterStatsService, portStatsService, queueStatsService);
148             final NodeStatisticsHandler old = handlers.putIfAbsent(key.getId(), h);
149             if (old == null) {
150                 spLogger.debug("Started node handler for {}", key.getId());
151                 h.start(timer);
152             } else {
153                 spLogger.debug("Prevented race on handler for {}", key.getId());
154             }
155         }
156     }
157
158     void stopNodeHandlers(final Collection<NodeKey> removedNodes) {
159         for (NodeKey key : removedNodes) {
160             final NodeStatisticsHandler s = handlers.remove(key.getId());
161             if (s != null) {
162                 spLogger.debug("Stopping node handler for {}", key.getId());
163                 s.close();
164             } else {
165                 spLogger.warn("Attempted to remove non-existing handler for {}, very strange", key.getId());
166             }
167         }
168     }
169 }