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.List;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
13 import java.util.concurrent.Future;
15 import org.eclipse.xtext.xbase.lib.Exceptions;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
17 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
18 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
36 import org.opendaylight.yangtools.concepts.Registration;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.NotificationListener;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class StatisticsProvider implements AutoCloseable {
45 public final static Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
47 private DataProviderService dps;
49 private NotificationProviderService nps;
51 private OpendaylightGroupStatisticsService groupStatsService;
53 private OpendaylightMeterStatisticsService meterStatsService;
55 private Thread statisticsRequesterThread;
57 private final InstanceIdentifier<Nodes> nodesIdentifier = InstanceIdentifier.builder().node(Nodes.class).toInstance();
59 //Local caching of stats
61 private final ConcurrentMap<NodeId,NodeStatistics> statisticsCache =
62 new ConcurrentHashMap<NodeId,NodeStatistics>();
64 public DataProviderService getDataService() {
68 public void setDataService(final DataProviderService dataService) {
69 this.dps = dataService;
72 public NotificationProviderService getNotificationService() {
76 public void setNotificationService(final NotificationProviderService notificationService) {
77 this.nps = notificationService;
80 private final StatisticsUpdateCommiter updateCommiter = new StatisticsUpdateCommiter(StatisticsProvider.this);
82 private Registration<NotificationListener> listenerRegistration;
86 NotificationProviderService nps = this.getNotificationService();
87 Registration<NotificationListener> registerNotificationListener = nps.registerNotificationListener(this.updateCommiter);
88 this.listenerRegistration = registerNotificationListener;
90 // Get Group/Meter statistics service instance
91 groupStatsService = StatisticsManagerActivator.getProviderContext().
92 getRpcService(OpendaylightGroupStatisticsService.class);
94 meterStatsService = StatisticsManagerActivator.getProviderContext().
95 getRpcService(OpendaylightMeterStatisticsService.class);
97 statisticsRequesterThread = new Thread( new Runnable(){
103 spLogger.info("Statistics requester thread started with timer interval : {}",5000);
105 statsRequestSender();
108 }catch (Exception e){
109 spLogger.error("Exception occurred while sending stats request : {}",e);
114 spLogger.info("Statistics Provider started.");
117 protected DataModificationTransaction startChange() {
119 DataProviderService dps = this.getDataService();
120 return dps.beginTransaction();
123 private void statsRequestSender(){
125 //Need to call API to receive all the nodes connected to controller.
126 List<Node> targetNodes = getAllConnectedNodes();
128 for (Node targetNode : targetNodes){
129 spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
131 //We need to add check, so see if groups/meters are supported
132 //by the target node. Below check doesn't look good.
133 if(targetNode.getId().getValue().contains("openflow:")){
134 sendAllGroupStatisticsRequest(targetNode);
136 sendAllMeterStatisticsRequest(targetNode);
138 sendGroupDescriptionRequest(targetNode);
140 sendGroupFeaturesRequest(targetNode);
142 sendMeterConfigStatisticsRequest(targetNode);
144 sendMeterFeaturesRequest(targetNode);
149 private void sendAllGroupStatisticsRequest(Node targetNode){
151 final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
153 input.setId(targetNode.getId());
155 Future<RpcResult<GetAllGroupStatisticsOutput>> response =
156 groupStatsService.getAllGroupStatistics(input.build());
159 private void sendGroupDescriptionRequest(Node targetNode){
160 final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
162 input.setId(targetNode.getId());
164 Future<RpcResult<GetGroupDescriptionOutput>> response =
165 groupStatsService.getGroupDescription(input.build());
168 private void sendGroupFeaturesRequest(Node targetNode){
170 GetGroupFeaturesInputBuilder input = new GetGroupFeaturesInputBuilder();
172 input.setId(targetNode.getId());
174 Future<RpcResult<GetGroupFeaturesOutput>> response =
175 groupStatsService.getGroupFeatures(input.build());
178 private void sendAllMeterStatisticsRequest(Node targetNode){
180 GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
182 input.setId(targetNode.getId());
184 Future<RpcResult<GetAllMeterStatisticsOutput>> response =
185 meterStatsService.getAllMeterStatistics(input.build());
188 private void sendMeterConfigStatisticsRequest(Node targetNode){
190 GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
192 input.setId(targetNode.getId());
194 Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response =
195 meterStatsService.getAllMeterConfigStatistics(input.build());
198 private void sendMeterFeaturesRequest(Node targetNode){
200 GetMeterFeaturesInputBuilder input = new GetMeterFeaturesInputBuilder();
202 input.setId(targetNode.getId());
204 Future<RpcResult<GetMeterFeaturesOutput>> response =
205 meterStatsService.getMeterFeatures(input.build());
208 public ConcurrentMap<NodeId, NodeStatistics> getStatisticsCache() {
209 return statisticsCache;
212 private List<Node> getAllConnectedNodes(){
214 Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
215 spLogger.info("Number of connected nodes : {}",nodes.getNode().size());
216 return nodes.getNode();
219 @SuppressWarnings("deprecation")
224 spLogger.info("Statistics Provider stopped.");
225 if (this.listenerRegistration != null) {
227 this.listenerRegistration.close();
229 this.statisticsRequesterThread.destroy();
232 } catch (Throwable e) {
233 throw Exceptions.sneakyThrow(e);