Merge "BUG-614: introduce AbstractRuntimeCodeGenerator"
[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.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14
15 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
16 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
17 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
18 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.concepts.Registration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.NotificationListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.base.Preconditions;
38
39 /**
40  * Following are main responsibilities of the class:
41  * 1) Invoke statistics request thread to send periodic statistics request to all the
42  * flow capable switch connected to the controller. It sends statistics request for
43  * Group,Meter,Table,Flow,Queue,Aggregate stats.
44  *
45  * 2) Invoke statistics ager thread, to clean up all the stale statistics data from
46  * operational data store.
47  *
48  * @author avishnoi@in.ibm.com
49  *
50  */
51 public class StatisticsProvider implements AutoCloseable {
52     private static final Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
53
54     private final ConcurrentMap<NodeId, NodeStatisticsHandler> handlers = new ConcurrentHashMap<>();
55     private final Timer timer = new Timer("statistics-manager", true);
56     private final DataProviderService dps;
57
58     private OpendaylightGroupStatisticsService groupStatsService;
59
60     private OpendaylightMeterStatisticsService meterStatsService;
61
62     private OpendaylightFlowStatisticsService flowStatsService;
63
64     private OpendaylightPortStatisticsService portStatsService;
65
66     private OpendaylightFlowTableStatisticsService flowTableStatsService;
67
68     private OpendaylightQueueStatisticsService queueStatsService;
69
70     private final StatisticsRequestScheduler srScheduler;
71
72     public StatisticsProvider(final DataProviderService dataService) {
73         this.dps = Preconditions.checkNotNull(dataService);
74         this.srScheduler = new StatisticsRequestScheduler();
75     }
76
77     private final StatisticsListener updateCommiter = new StatisticsListener(StatisticsProvider.this);
78
79     private Registration<NotificationListener> listenerRegistration;
80
81     private ListenerRegistration<DataChangeListener> flowCapableTrackerRegistration;
82
83     public void start(final NotificationProviderService nps, final RpcConsumerRegistry rpcRegistry) {
84
85         // Get Group/Meter statistics service instances
86         groupStatsService = rpcRegistry.getRpcService(OpendaylightGroupStatisticsService.class);
87         meterStatsService = rpcRegistry.getRpcService(OpendaylightMeterStatisticsService.class);
88         flowStatsService = rpcRegistry.getRpcService(OpendaylightFlowStatisticsService.class);
89         portStatsService = rpcRegistry.getRpcService(OpendaylightPortStatisticsService.class);
90         flowTableStatsService = rpcRegistry.getRpcService(OpendaylightFlowTableStatisticsService.class);
91         queueStatsService = rpcRegistry.getRpcService(OpendaylightQueueStatisticsService.class);
92         this.srScheduler.start();
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         spLogger.info("Statistics Provider started.");
105     }
106
107     /**
108      * Get the handler for a particular node.
109      *
110      * @param nodeId source node
111      * @return Node statistics handler for that node. Null if the statistics should
112      *         not handled.
113      */
114     public final NodeStatisticsHandler getStatisticsHandler(final NodeId nodeId) {
115         Preconditions.checkNotNull(nodeId);
116         NodeStatisticsHandler handler = handlers.get(nodeId);
117         if (handler == null) {
118             spLogger.info("Attempted to get non-existing handler for {}", nodeId);
119         }
120         return handler;
121     }
122
123     @Override
124     public void close() {
125         try {
126             if (this.listenerRegistration != null) {
127                 this.listenerRegistration.close();
128                 this.listenerRegistration = null;
129             }
130             if (this.flowCapableTrackerRegistration != null) {
131                 this.flowCapableTrackerRegistration.close();
132                 this.flowCapableTrackerRegistration = null;
133             }
134             timer.cancel();
135         } catch (Exception e) {
136             spLogger.warn("Failed to stop Statistics Provider completely", e);
137         } finally {
138             spLogger.info("Statistics Provider stopped.");
139         }
140     }
141
142     void startNodeHandlers(final Collection<NodeKey> addedNodes) {
143         for (NodeKey key : addedNodes) {
144             if (handlers.containsKey(key.getId())) {
145                 spLogger.warn("Attempted to start already-existing handler for {}, very strange", key.getId());
146                 continue;
147             }
148
149             final NodeStatisticsHandler h = new NodeStatisticsHandler(dps, key,
150                     flowStatsService, flowTableStatsService, groupStatsService,
151                     meterStatsService, portStatsService, queueStatsService,srScheduler);
152             final NodeStatisticsHandler old = handlers.putIfAbsent(key.getId(), h);
153             if (old == null) {
154                 spLogger.debug("Started node handler for {}", key.getId());
155                 h.start(timer);
156             } else {
157                 spLogger.debug("Prevented race on handler for {}", key.getId());
158             }
159         }
160     }
161
162     void stopNodeHandlers(final Collection<NodeKey> removedNodes) {
163         for (NodeKey key : removedNodes) {
164             final NodeStatisticsHandler s = handlers.remove(key.getId());
165             if (s != null) {
166                 spLogger.debug("Stopping node handler for {}", key.getId());
167                 s.close();
168             } else {
169                 spLogger.warn("Attempted to remove non-existing handler for {}, very strange", key.getId());
170             }
171         }
172     }
173 }