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.OpendaylightGroupStatisticsService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
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.meter.statistics.rev131111.GetAllMeterConfigStatisticsInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
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.opendaylight.yangtools.yang.common.RpcResult;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class StatisticsProvider implements AutoCloseable {
42 public final static Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
44 private DataProviderService dps;
46 private NotificationProviderService nps;
48 private OpendaylightGroupStatisticsService groupStatsService;
50 private OpendaylightMeterStatisticsService meterStatsService;
52 private Thread statisticsRequesterThread;
54 private final InstanceIdentifier<Nodes> nodesIdentifier = InstanceIdentifier.builder(Nodes.class).toInstance();
56 private final int STATS_THREAD_EXECUTION_TIME= 50000;
57 //Local caching of stats
59 private final ConcurrentMap<NodeId,NodeStatistics> statisticsCache =
60 new ConcurrentHashMap<NodeId,NodeStatistics>();
62 public DataProviderService getDataService() {
66 public void setDataService(final DataProviderService dataService) {
67 this.dps = dataService;
70 public NotificationProviderService getNotificationService() {
74 public void setNotificationService(final NotificationProviderService notificationService) {
75 this.nps = notificationService;
78 private final StatisticsUpdateCommiter updateCommiter = new StatisticsUpdateCommiter(StatisticsProvider.this);
80 private Registration<NotificationListener> listenerRegistration;
84 NotificationProviderService nps = this.getNotificationService();
85 Registration<NotificationListener> registerNotificationListener = nps.registerNotificationListener(this.updateCommiter);
86 this.listenerRegistration = registerNotificationListener;
88 // Get Group/Meter statistics service instance
89 groupStatsService = StatisticsManagerActivator.getProviderContext().
90 getRpcService(OpendaylightGroupStatisticsService.class);
92 meterStatsService = StatisticsManagerActivator.getProviderContext().
93 getRpcService(OpendaylightMeterStatisticsService.class);
95 statisticsRequesterThread = new Thread( new Runnable(){
101 statsRequestSender();
103 Thread.sleep(STATS_THREAD_EXECUTION_TIME);
104 }catch (Exception e){
105 spLogger.error("Exception occurred while sending stats request : {}",e);
111 spLogger.debug("Statistics requester thread started with timer interval : {}",STATS_THREAD_EXECUTION_TIME);
113 statisticsRequesterThread.start();
115 spLogger.info("Statistics Provider started.");
118 protected DataModificationTransaction startChange() {
120 DataProviderService dps = this.getDataService();
121 return dps.beginTransaction();
124 private void statsRequestSender(){
126 //Need to call API to receive all the nodes connected to controller.
127 List<Node> targetNodes = getAllConnectedNodes();
129 if(targetNodes == null)
132 for (Node targetNode : targetNodes){
133 spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
135 //We need to add check, so see if groups/meters are supported
136 //by the target node. Below check doesn't look good.
137 if(targetNode.getId().getValue().contains("openflow:")){
138 InstanceIdentifier<Node> targetInstanceId = InstanceIdentifier.builder(Nodes.class).child(Node.class,targetNode.getKey()).toInstance();
139 NodeRef targetNodeRef = new NodeRef(targetInstanceId);
142 sendAllGroupStatisticsRequest(targetNodeRef);
144 sendAllMeterStatisticsRequest(targetNodeRef);
146 sendGroupDescriptionRequest(targetNodeRef);
148 sendMeterConfigStatisticsRequest(targetNodeRef);
151 spLogger.error("Exception occured while sending statistics request : {}", e);
157 private void sendAllGroupStatisticsRequest(NodeRef targetNode){
159 final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
161 input.setNode(targetNode);
162 input.setNode(targetNode);
164 @SuppressWarnings("unused")
165 Future<RpcResult<GetAllGroupStatisticsOutput>> response =
166 groupStatsService.getAllGroupStatistics(input.build());
169 private void sendGroupDescriptionRequest(NodeRef targetNode){
170 final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
172 input.setNode(targetNode);
174 @SuppressWarnings("unused")
175 Future<RpcResult<GetGroupDescriptionOutput>> response =
176 groupStatsService.getGroupDescription(input.build());
179 private void sendAllMeterStatisticsRequest(NodeRef targetNode){
181 GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
183 input.setNode(targetNode);
185 @SuppressWarnings("unused")
186 Future<RpcResult<GetAllMeterStatisticsOutput>> response =
187 meterStatsService.getAllMeterStatistics(input.build());
190 private void sendMeterConfigStatisticsRequest(NodeRef targetNode){
192 GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
194 input.setNode(targetNode);
196 @SuppressWarnings("unused")
197 Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response =
198 meterStatsService.getAllMeterConfigStatistics(input.build());
202 public ConcurrentMap<NodeId, NodeStatistics> getStatisticsCache() {
203 return statisticsCache;
206 private List<Node> getAllConnectedNodes(){
208 Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
212 spLogger.info("Number of connected nodes : {}",nodes.getNode().size());
213 return nodes.getNode();
216 @SuppressWarnings("deprecation")
221 spLogger.info("Statistics Provider stopped.");
222 if (this.listenerRegistration != null) {
224 this.listenerRegistration.close();
226 this.statisticsRequesterThread.destroy();
229 } catch (Throwable e) {
230 throw Exceptions.sneakyThrow(e);