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