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