2 * Copyright IBM Corporation, 2013. All rights reserved.
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
8 package org.opendaylight.controller.md.statistics.manager;
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;
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;
52 import com.google.common.base.Preconditions;
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.
60 * 2) Invoke statistics ager thread, to clean up all the stale statistics data from
61 * operational data store.
63 * @author avishnoi@in.ibm.com
66 public class StatisticsProvider implements AutoCloseable {
67 public static final long STATS_COLLECTION_MILLIS = TimeUnit.SECONDS.toMillis(15);
69 private static final Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
71 private final ConcurrentMap<NodeId, NodeStatisticsHandler> handlers = new ConcurrentHashMap<>();
72 private final Timer timer = new Timer("statistics-manager", true);
73 private final DataProviderService dps;
75 private OpendaylightGroupStatisticsService groupStatsService;
77 private OpendaylightMeterStatisticsService meterStatsService;
79 private OpendaylightFlowStatisticsService flowStatsService;
81 private OpendaylightPortStatisticsService portStatsService;
83 private OpendaylightFlowTableStatisticsService flowTableStatsService;
85 private OpendaylightQueueStatisticsService queueStatsService;
87 private StatisticsUpdateHandler statsUpdateHandler;
89 public StatisticsProvider(final DataProviderService dataService) {
90 this.dps = Preconditions.checkNotNull(dataService);
93 private final StatisticsListener updateCommiter = new StatisticsListener(StatisticsProvider.this);
95 private Registration<NotificationListener> listenerRegistration;
97 private ListenerRegistration<DataChangeListener> flowCapableTrackerRegistration;
99 public void start(final DataBrokerService dbs, final NotificationProviderService nps, final RpcConsumerRegistry rpcRegistry) {
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);
109 // Start receiving notifications
110 this.listenerRegistration = nps.registerNotificationListener(this.updateCommiter);
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));
119 statsUpdateHandler = new StatisticsUpdateHandler(StatisticsProvider.this);
120 registerDataStoreUpdateListener(dbs);
122 timer.schedule(new TimerTask() {
126 // Send stats requests
127 for (NodeStatisticsHandler h : handlers.values()) {
128 h.requestPeriodicStatistics();
132 for(NodeStatisticsHandler nodeStatisticsAger : handlers.values()){
133 nodeStatisticsAger.cleanStaleStatistics();
136 } catch (RuntimeException e) {
137 spLogger.warn("Failed to request statistics", e);
140 }, 0, STATS_COLLECTION_MILLIS);
142 spLogger.debug("Statistics timer task with timer interval : {}ms", STATS_COLLECTION_MILLIS);
143 spLogger.info("Statistics Provider started.");
146 private void registerDataStoreUpdateListener(DataBrokerService dbs) {
147 // FIXME: the below should be broken out into StatisticsUpdateHandler
149 //Register for flow updates
150 InstanceIdentifier<? extends DataObject> pathFlow = InstanceIdentifier.builder(Nodes.class).child(Node.class)
151 .augmentation(FlowCapableNode.class)
153 .child(Flow.class).toInstance();
154 dbs.registerDataChangeListener(pathFlow, statsUpdateHandler);
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();
161 dbs.registerDataChangeListener(pathMeter, statsUpdateHandler);
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);
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);
177 protected DataModificationTransaction startChange() {
178 return dps.beginTransaction();
181 public void sendFlowStatsFromTableRequest(NodeKey node, Flow flow) throws InterruptedException, ExecutionException {
182 final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
184 h.sendFlowStatsFromTableRequest(flow);
188 public void sendGroupDescriptionRequest(NodeKey node) throws InterruptedException, ExecutionException{
189 final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
191 h.sendGroupDescriptionRequest();
195 public void sendMeterConfigStatisticsRequest(NodeKey node) throws InterruptedException, ExecutionException {
196 final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
198 h.sendMeterConfigStatisticsRequest();
202 public void sendQueueStatsFromGivenNodeConnector(NodeKey node,NodeConnectorId nodeConnectorId, QueueId queueId) throws InterruptedException, ExecutionException {
203 final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
205 h.sendQueueStatsFromGivenNodeConnector(nodeConnectorId, queueId);
210 * Get the handler for a particular node.
212 * @param nodeId source node
213 * @return Node statistics handler for that node. Null if the statistics should
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);
226 public void close() {
228 if (this.listenerRegistration != null) {
229 this.listenerRegistration.close();
230 this.listenerRegistration = null;
232 if (this.flowCapableTrackerRegistration != null) {
233 this.flowCapableTrackerRegistration.close();
234 this.flowCapableTrackerRegistration = null;
237 } catch (Exception e) {
238 spLogger.warn("Failed to stop Statistics Provider completely", e);
240 spLogger.info("Statistics Provider stopped.");
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());
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);
256 spLogger.debug("Started node handler for {}", key.getId());
259 spLogger.debug("Prevented race on handler for {}", key.getId());
264 void stopNodeHandlers(final Collection<NodeKey> removedNodes) {
265 for (NodeKey key : removedNodes) {
266 final NodeStatisticsHandler s = handlers.remove(key.getId());
268 spLogger.debug("Stopping node handler for {}", key.getId());
271 spLogger.warn("Attempted to remove non-existing handler for {}, very strange", key.getId());