Move statistics request methods into NodeStatisticsHandler
[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.TimerTask;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17
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.OpendaylightFlowStatisticsService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
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.OpendaylightMeterStatisticsService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.concepts.Registration;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.binding.NotificationListener;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 import com.google.common.base.Preconditions;
53
54 /**
55  * Following are main responsibilities of the class:
56  * 1) Invoke statistics request thread to send periodic statistics request to all the
57  * flow capable switch connected to the controller. It sends statistics request for
58  * Group,Meter,Table,Flow,Queue,Aggregate stats.
59  *
60  * 2) Invoke statistics ager thread, to clean up all the stale statistics data from
61  * operational data store.
62  *
63  * @author avishnoi@in.ibm.com
64  *
65  */
66 public class StatisticsProvider implements AutoCloseable {
67     public static final long STATS_COLLECTION_MILLIS = TimeUnit.SECONDS.toMillis(15);
68
69     private static final Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
70
71     private final ConcurrentMap<NodeId, NodeStatisticsHandler> handlers = new ConcurrentHashMap<>();
72     private final Timer timer = new Timer("statistics-manager", true);
73     private final DataProviderService dps;
74
75     private OpendaylightGroupStatisticsService groupStatsService;
76
77     private OpendaylightMeterStatisticsService meterStatsService;
78
79     private OpendaylightFlowStatisticsService flowStatsService;
80
81     private OpendaylightPortStatisticsService portStatsService;
82
83     private OpendaylightFlowTableStatisticsService flowTableStatsService;
84
85     private OpendaylightQueueStatisticsService queueStatsService;
86
87     private StatisticsUpdateHandler statsUpdateHandler;
88
89     public StatisticsProvider(final DataProviderService dataService) {
90         this.dps = Preconditions.checkNotNull(dataService);
91     }
92
93     private final StatisticsListener updateCommiter = new StatisticsListener(StatisticsProvider.this);
94
95     private Registration<NotificationListener> listenerRegistration;
96
97     private ListenerRegistration<DataChangeListener> flowCapableTrackerRegistration;
98
99     public void start(final DataBrokerService dbs, final NotificationProviderService nps, final RpcConsumerRegistry rpcRegistry) {
100
101         // Get Group/Meter statistics service instances
102         groupStatsService = rpcRegistry.getRpcService(OpendaylightGroupStatisticsService.class);
103         meterStatsService = rpcRegistry.getRpcService(OpendaylightMeterStatisticsService.class);
104         flowStatsService = rpcRegistry.getRpcService(OpendaylightFlowStatisticsService.class);
105         portStatsService = rpcRegistry.getRpcService(OpendaylightPortStatisticsService.class);
106         flowTableStatsService = rpcRegistry.getRpcService(OpendaylightFlowTableStatisticsService.class);
107         queueStatsService = rpcRegistry.getRpcService(OpendaylightQueueStatisticsService.class);
108
109         // Start receiving notifications
110         this.listenerRegistration = nps.registerNotificationListener(this.updateCommiter);
111
112         // Register for switch connect/disconnect notifications
113         final InstanceIdentifier<FlowCapableNode> fcnId = InstanceIdentifier.builder(Nodes.class)
114                 .child(Node.class).augmentation(FlowCapableNode.class).build();
115         spLogger.debug("Registering FlowCapable tracker to {}", fcnId);
116         this.flowCapableTrackerRegistration = dbs.registerDataChangeListener(fcnId,
117                 new FlowCapableTracker(this, fcnId));
118
119         statsUpdateHandler = new StatisticsUpdateHandler(StatisticsProvider.this);
120         registerDataStoreUpdateListener(dbs);
121
122         timer.schedule(new TimerTask() {
123             @Override
124             public void run() {
125                 try {
126                     // Send stats requests
127                     for (NodeStatisticsHandler h : handlers.values()) {
128                         h.requestPeriodicStatistics();
129                     }
130
131                     // Perform cleanup
132                     for(NodeStatisticsHandler nodeStatisticsAger : handlers.values()){
133                         nodeStatisticsAger.cleanStaleStatistics();
134                     }
135
136                 } catch (RuntimeException e) {
137                     spLogger.warn("Failed to request statistics", e);
138                 }
139             }
140         }, 0, STATS_COLLECTION_MILLIS);
141
142         spLogger.debug("Statistics timer task with timer interval : {}ms", STATS_COLLECTION_MILLIS);
143         spLogger.info("Statistics Provider started.");
144     }
145
146     private void registerDataStoreUpdateListener(DataBrokerService dbs) {
147         // FIXME: the below should be broken out into StatisticsUpdateHandler
148
149         //Register for flow updates
150         InstanceIdentifier<? extends DataObject> pathFlow = InstanceIdentifier.builder(Nodes.class).child(Node.class)
151                                                                     .augmentation(FlowCapableNode.class)
152                                                                     .child(Table.class)
153                                                                     .child(Flow.class).toInstance();
154         dbs.registerDataChangeListener(pathFlow, statsUpdateHandler);
155
156         //Register for meter updates
157         InstanceIdentifier<? extends DataObject> pathMeter = InstanceIdentifier.builder(Nodes.class).child(Node.class)
158                                                     .augmentation(FlowCapableNode.class)
159                                                     .child(Meter.class).toInstance();
160
161         dbs.registerDataChangeListener(pathMeter, statsUpdateHandler);
162
163         //Register for group updates
164         InstanceIdentifier<? extends DataObject> pathGroup = InstanceIdentifier.builder(Nodes.class).child(Node.class)
165                                                     .augmentation(FlowCapableNode.class)
166                                                     .child(Group.class).toInstance();
167         dbs.registerDataChangeListener(pathGroup, statsUpdateHandler);
168
169         //Register for queue updates
170         InstanceIdentifier<? extends DataObject> pathQueue = InstanceIdentifier.builder(Nodes.class).child(Node.class)
171                                                                     .child(NodeConnector.class)
172                                                                     .augmentation(FlowCapableNodeConnector.class)
173                                                                     .child(Queue.class).toInstance();
174         dbs.registerDataChangeListener(pathQueue, statsUpdateHandler);
175     }
176
177     protected DataModificationTransaction startChange() {
178         return dps.beginTransaction();
179     }
180
181     public void sendFlowStatsFromTableRequest(NodeKey node, Flow flow) throws InterruptedException, ExecutionException {
182         final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
183         if (h != null) {
184             h.sendFlowStatsFromTableRequest(flow);
185         }
186     }
187
188     public void sendGroupDescriptionRequest(NodeKey node) throws InterruptedException, ExecutionException{
189         final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
190         if (h != null) {
191             h.sendGroupDescriptionRequest();
192         }
193     }
194
195     public void sendMeterConfigStatisticsRequest(NodeKey node) throws InterruptedException, ExecutionException {
196         final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
197         if (h != null) {
198             h.sendMeterConfigStatisticsRequest();
199         }
200     }
201
202     public void sendQueueStatsFromGivenNodeConnector(NodeKey node,NodeConnectorId nodeConnectorId, QueueId queueId) throws InterruptedException, ExecutionException {
203         final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
204         if (h != null) {
205             h.sendQueueStatsFromGivenNodeConnector(nodeConnectorId, queueId);
206         }
207     }
208
209     /**
210      * Get the handler for a particular node.
211      *
212      * @param nodeId source node
213      * @return Node statistics handler for that node. Null if the statistics should
214      *         not handled.
215      */
216     public final NodeStatisticsHandler getStatisticsHandler(final NodeId nodeId) {
217         Preconditions.checkNotNull(nodeId);
218         NodeStatisticsHandler handler = handlers.get(nodeId);
219         if (handler == null) {
220             spLogger.info("Attempted to get non-existing handler for {}", nodeId);
221         }
222         return handler;
223     }
224
225     @Override
226     public void close() {
227         try {
228             if (this.listenerRegistration != null) {
229                 this.listenerRegistration.close();
230                 this.listenerRegistration = null;
231             }
232             if (this.flowCapableTrackerRegistration != null) {
233                 this.flowCapableTrackerRegistration.close();
234                 this.flowCapableTrackerRegistration = null;
235             }
236             timer.cancel();
237         } catch (Exception e) {
238             spLogger.warn("Failed to stop Statistics Provider completely", e);
239         } finally {
240             spLogger.info("Statistics Provider stopped.");
241         }
242     }
243
244     void startNodeHandlers(final Collection<NodeKey> addedNodes) {
245         for (NodeKey key : addedNodes) {
246             if (handlers.containsKey(key.getId())) {
247                 spLogger.warn("Attempted to start already-existing handler for {}, very strange", key.getId());
248                 continue;
249             }
250
251             final NodeStatisticsHandler h = new NodeStatisticsHandler(dps, key,
252                     flowStatsService, flowTableStatsService, groupStatsService,
253                     meterStatsService, portStatsService, queueStatsService);
254             final NodeStatisticsHandler old = handlers.putIfAbsent(key.getId(), h);
255             if (old == null) {
256                 spLogger.debug("Started node handler for {}", key.getId());
257                 h.start();
258             } else {
259                 spLogger.debug("Prevented race on handler for {}", key.getId());
260             }
261         }
262     }
263
264     void stopNodeHandlers(final Collection<NodeKey> removedNodes) {
265         for (NodeKey key : removedNodes) {
266             final NodeStatisticsHandler s = handlers.remove(key.getId());
267             if (s != null) {
268                 spLogger.debug("Stopping node handler for {}", key.getId());
269                 s.close();
270             } else {
271                 spLogger.warn("Attempted to remove non-existing handler for {}, very strange", key.getId());
272             }
273         }
274     }
275 }