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