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.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
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.NodeRef;
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.OpendaylightMeterStatisticsService;
34 import org.opendaylight.yangtools.concepts.Registration;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.NotificationListener;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class StatisticsProvider implements AutoCloseable {
43 public final static Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
45 private DataProviderService dps;
47 private NotificationProviderService nps;
49 private OpendaylightGroupStatisticsService groupStatsService;
51 private OpendaylightMeterStatisticsService meterStatsService;
53 private Thread statisticsRequesterThread;
55 private final InstanceIdentifier<Nodes> nodesIdentifier = InstanceIdentifier.builder(Nodes.class).toInstance();
57 private final int STATS_THREAD_EXECUTION_TIME= 50000;
58 //Local caching of stats
60 private final ConcurrentMap<NodeId,NodeStatistics> statisticsCache =
61 new ConcurrentHashMap<NodeId,NodeStatistics>();
63 public DataProviderService getDataService() {
67 public void setDataService(final DataProviderService dataService) {
68 this.dps = dataService;
71 public NotificationProviderService getNotificationService() {
75 public void setNotificationService(final NotificationProviderService notificationService) {
76 this.nps = notificationService;
79 private final StatisticsUpdateCommiter updateCommiter = new StatisticsUpdateCommiter(StatisticsProvider.this);
81 private Registration<NotificationListener> listenerRegistration;
85 NotificationProviderService nps = this.getNotificationService();
86 Registration<NotificationListener> registerNotificationListener = nps.registerNotificationListener(this.updateCommiter);
87 this.listenerRegistration = registerNotificationListener;
89 // Get Group/Meter statistics service instance
90 groupStatsService = StatisticsManagerActivator.getProviderContext().
91 getRpcService(OpendaylightGroupStatisticsService.class);
93 meterStatsService = StatisticsManagerActivator.getProviderContext().
94 getRpcService(OpendaylightMeterStatisticsService.class);
96 statisticsRequesterThread = new Thread( new Runnable(){
102 statsRequestSender();
104 Thread.sleep(STATS_THREAD_EXECUTION_TIME);
105 }catch (Exception e){
106 spLogger.error("Exception occurred while sending stats request : {}",e);
112 spLogger.debug("Statistics requester thread started with timer interval : {}",STATS_THREAD_EXECUTION_TIME);
114 statisticsRequesterThread.start();
116 spLogger.info("Statistics Provider started.");
119 protected DataModificationTransaction startChange() {
121 DataProviderService dps = this.getDataService();
122 return dps.beginTransaction();
125 private void statsRequestSender(){
127 //Need to call API to receive all the nodes connected to controller.
128 List<Node> targetNodes = getAllConnectedNodes();
130 if(targetNodes == null)
133 for (Node targetNode : targetNodes){
135 if(targetNode.getAugmentation(FlowCapableNode.class) != null){
137 spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
139 InstanceIdentifier<Node> targetInstanceId = InstanceIdentifier.builder(Nodes.class).child(Node.class,targetNode.getKey()).toInstance();
140 NodeRef targetNodeRef = new NodeRef(targetInstanceId);
143 sendAllGroupStatisticsRequest(targetNodeRef);
145 sendAllMeterStatisticsRequest(targetNodeRef);
147 sendGroupDescriptionRequest(targetNodeRef);
149 sendMeterConfigStatisticsRequest(targetNodeRef);
152 spLogger.error("Exception occured while sending statistics request : {}", e);
158 private void sendAllGroupStatisticsRequest(NodeRef targetNode){
160 final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
162 input.setNode(targetNode);
163 input.setNode(targetNode);
165 @SuppressWarnings("unused")
166 Future<RpcResult<GetAllGroupStatisticsOutput>> response =
167 groupStatsService.getAllGroupStatistics(input.build());
170 private void sendGroupDescriptionRequest(NodeRef targetNode){
171 final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
173 input.setNode(targetNode);
175 @SuppressWarnings("unused")
176 Future<RpcResult<GetGroupDescriptionOutput>> response =
177 groupStatsService.getGroupDescription(input.build());
180 private void sendAllMeterStatisticsRequest(NodeRef targetNode){
182 GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
184 input.setNode(targetNode);
186 @SuppressWarnings("unused")
187 Future<RpcResult<GetAllMeterStatisticsOutput>> response =
188 meterStatsService.getAllMeterStatistics(input.build());
191 private void sendMeterConfigStatisticsRequest(NodeRef targetNode){
193 GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
195 input.setNode(targetNode);
197 @SuppressWarnings("unused")
198 Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response =
199 meterStatsService.getAllMeterConfigStatistics(input.build());
203 public ConcurrentMap<NodeId, NodeStatistics> getStatisticsCache() {
204 return statisticsCache;
207 private List<Node> getAllConnectedNodes(){
209 Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
213 spLogger.info("Number of connected nodes : {}",nodes.getNode().size());
214 return nodes.getNode();
217 @SuppressWarnings("deprecation")
222 spLogger.info("Statistics Provider stopped.");
223 if (this.listenerRegistration != null) {
225 this.listenerRegistration.close();
227 this.statisticsRequesterThread.destroy();
230 } catch (Throwable e) {
231 throw Exceptions.sneakyThrow(e);