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.TimeUnit;
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;
51 import com.google.common.base.Preconditions;
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.
59 * 2) Invoke statistics ager thread, to clean up all the stale statistics data from
60 * operational data store.
62 * @author avishnoi@in.ibm.com
65 public class StatisticsProvider implements AutoCloseable {
66 public static final long STATS_COLLECTION_MILLIS = TimeUnit.SECONDS.toMillis(15);
68 private static final Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
70 private final ConcurrentMap<NodeId, NodeStatisticsHandler> handlers = new ConcurrentHashMap<>();
71 private final Timer timer = new Timer("statistics-manager", true);
72 private final DataProviderService dps;
74 private OpendaylightGroupStatisticsService groupStatsService;
76 private OpendaylightMeterStatisticsService meterStatsService;
78 private OpendaylightFlowStatisticsService flowStatsService;
80 private OpendaylightPortStatisticsService portStatsService;
82 private OpendaylightFlowTableStatisticsService flowTableStatsService;
84 private OpendaylightQueueStatisticsService queueStatsService;
86 private StatisticsUpdateHandler statsUpdateHandler;
88 public StatisticsProvider(final DataProviderService dataService) {
89 this.dps = Preconditions.checkNotNull(dataService);
92 private final StatisticsListener updateCommiter = new StatisticsListener(StatisticsProvider.this);
94 private Registration<NotificationListener> listenerRegistration;
96 private ListenerRegistration<DataChangeListener> flowCapableTrackerRegistration;
98 public void start(final DataBrokerService dbs, final NotificationProviderService nps, final RpcConsumerRegistry rpcRegistry) {
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);
108 // Start receiving notifications
109 this.listenerRegistration = nps.registerNotificationListener(this.updateCommiter);
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));
118 statsUpdateHandler = new StatisticsUpdateHandler(StatisticsProvider.this);
119 registerDataStoreUpdateListener(dbs);
121 timer.schedule(new TimerTask() {
125 // Send stats requests
126 for (NodeStatisticsHandler h : handlers.values()) {
127 h.requestPeriodicStatistics();
131 for(NodeStatisticsHandler nodeStatisticsAger : handlers.values()){
132 nodeStatisticsAger.cleanStaleStatistics();
135 } catch (RuntimeException e) {
136 spLogger.warn("Failed to request statistics", e);
139 }, 0, STATS_COLLECTION_MILLIS);
141 spLogger.debug("Statistics timer task with timer interval : {}ms", STATS_COLLECTION_MILLIS);
142 spLogger.info("Statistics Provider started.");
145 private void registerDataStoreUpdateListener(DataBrokerService dbs) {
146 // FIXME: the below should be broken out into StatisticsUpdateHandler
148 //Register for flow updates
149 InstanceIdentifier<? extends DataObject> pathFlow = InstanceIdentifier.builder(Nodes.class).child(Node.class)
150 .augmentation(FlowCapableNode.class)
152 .child(Flow.class).toInstance();
153 dbs.registerDataChangeListener(pathFlow, statsUpdateHandler);
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();
160 dbs.registerDataChangeListener(pathMeter, statsUpdateHandler);
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);
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);
176 protected DataModificationTransaction startChange() {
177 return dps.beginTransaction();
180 public void sendFlowStatsFromTableRequest(NodeKey node, Flow flow) {
181 final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
183 h.sendFlowStatsFromTableRequest(flow);
187 public void sendGroupDescriptionRequest(NodeKey node) {
188 final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
190 h.sendGroupDescriptionRequest();
194 public void sendMeterConfigStatisticsRequest(NodeKey node) {
195 final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
197 h.sendMeterConfigStatisticsRequest();
201 public void sendQueueStatsFromGivenNodeConnector(NodeKey node,NodeConnectorId nodeConnectorId, QueueId queueId) {
202 final NodeStatisticsHandler h = getStatisticsHandler(node.getId());
204 h.sendQueueStatsFromGivenNodeConnector(nodeConnectorId, queueId);
209 * Get the handler for a particular node.
211 * @param nodeId source node
212 * @return Node statistics handler for that node. Null if the statistics should
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);
225 public void close() {
227 if (this.listenerRegistration != null) {
228 this.listenerRegistration.close();
229 this.listenerRegistration = null;
231 if (this.flowCapableTrackerRegistration != null) {
232 this.flowCapableTrackerRegistration.close();
233 this.flowCapableTrackerRegistration = null;
236 } catch (Exception e) {
237 spLogger.warn("Failed to stop Statistics Provider completely", e);
239 spLogger.info("Statistics Provider stopped.");
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());
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);
255 spLogger.debug("Started node handler for {}", key.getId());
258 spLogger.debug("Prevented race on handler for {}", key.getId());
263 void stopNodeHandlers(final Collection<NodeKey> removedNodes) {
264 for (NodeKey key : removedNodes) {
265 final NodeStatisticsHandler s = handlers.remove(key.getId());
267 spLogger.debug("Stopping node handler for {}", key.getId());
270 spLogger.warn("Attempted to remove non-existing handler for {}, very strange", key.getId());