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.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
37 import org.opendaylight.yangtools.concepts.Registration;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.NotificationListener;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class StatisticsProvider implements AutoCloseable {
46 public final static Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
48 private DataProviderService dps;
50 private NotificationProviderService nps;
52 private OpendaylightGroupStatisticsService groupStatsService;
54 private OpendaylightMeterStatisticsService meterStatsService;
56 private Thread statisticsRequesterThread;
58 private final InstanceIdentifier<Nodes> nodesIdentifier = InstanceIdentifier.builder().node(Nodes.class).toInstance();
60 //Local caching of stats
62 private final ConcurrentMap<NodeId,NodeStatistics> statisticsCache =
63 new ConcurrentHashMap<NodeId,NodeStatistics>();
65 public DataProviderService getDataService() {
69 public void setDataService(final DataProviderService dataService) {
70 this.dps = dataService;
73 public NotificationProviderService getNotificationService() {
77 public void setNotificationService(final NotificationProviderService notificationService) {
78 this.nps = notificationService;
81 private final StatisticsUpdateCommiter updateCommiter = new StatisticsUpdateCommiter(StatisticsProvider.this);
83 private Registration<NotificationListener> listenerRegistration;
87 NotificationProviderService nps = this.getNotificationService();
88 Registration<NotificationListener> registerNotificationListener = nps.registerNotificationListener(this.updateCommiter);
89 this.listenerRegistration = registerNotificationListener;
91 // Get Group/Meter statistics service instance
92 groupStatsService = StatisticsManagerActivator.getProviderContext().
93 getRpcService(OpendaylightGroupStatisticsService.class);
95 meterStatsService = StatisticsManagerActivator.getProviderContext().
96 getRpcService(OpendaylightMeterStatisticsService.class);
98 statisticsRequesterThread = new Thread( new Runnable(){
104 statsRequestSender();
107 }catch (Exception e){
108 spLogger.error("Exception occurred while sending stats request : {}",e);
114 spLogger.debug("Statistics requester thread started with timer interval : {}",5000);
116 //statisticsRequesterThread.start();
118 spLogger.info("Statistics Provider started.");
121 protected DataModificationTransaction startChange() {
123 DataProviderService dps = this.getDataService();
124 return dps.beginTransaction();
127 private void statsRequestSender(){
129 //Need to call API to receive all the nodes connected to controller.
130 List<Node> targetNodes = getAllConnectedNodes();
132 if(targetNodes == null)
135 for (Node targetNode : targetNodes){
136 spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
138 //We need to add check, so see if groups/meters are supported
139 //by the target node. Below check doesn't look good.
140 if(targetNode.getId().getValue().contains("openflow:")){
141 InstanceIdentifier<Node> targetInstanceId = InstanceIdentifier.builder(Nodes.class).child(Node.class,targetNode.getKey()).toInstance();
142 NodeRef targetNodeRef = new NodeRef(targetInstanceId);
144 sendAllGroupStatisticsRequest(targetNodeRef);
146 sendAllMeterStatisticsRequest(targetNodeRef);
148 sendGroupDescriptionRequest(targetNodeRef);
150 sendGroupFeaturesRequest(targetNodeRef);
152 sendMeterConfigStatisticsRequest(targetNodeRef);
154 sendMeterFeaturesRequest(targetNodeRef);
159 private void sendAllGroupStatisticsRequest(NodeRef targetNode){
161 final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
163 input.setNode(targetNode);
164 input.setNode(targetNode);
166 @SuppressWarnings("unused")
167 Future<RpcResult<GetAllGroupStatisticsOutput>> response =
168 groupStatsService.getAllGroupStatistics(input.build());
171 private void sendGroupDescriptionRequest(NodeRef targetNode){
172 final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
174 input.setNode(targetNode);
176 @SuppressWarnings("unused")
177 Future<RpcResult<GetGroupDescriptionOutput>> response =
178 groupStatsService.getGroupDescription(input.build());
181 private void sendGroupFeaturesRequest(NodeRef targetNode){
183 GetGroupFeaturesInputBuilder input = new GetGroupFeaturesInputBuilder();
185 input.setNode(targetNode);
187 @SuppressWarnings("unused")
188 Future<RpcResult<GetGroupFeaturesOutput>> response =
189 groupStatsService.getGroupFeatures(input.build());
192 private void sendAllMeterStatisticsRequest(NodeRef targetNode){
194 GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
196 input.setNode(targetNode);
198 @SuppressWarnings("unused")
199 Future<RpcResult<GetAllMeterStatisticsOutput>> response =
200 meterStatsService.getAllMeterStatistics(input.build());
203 private void sendMeterConfigStatisticsRequest(NodeRef targetNode){
205 GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
207 input.setNode(targetNode);
209 @SuppressWarnings("unused")
210 Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response =
211 meterStatsService.getAllMeterConfigStatistics(input.build());
214 private void sendMeterFeaturesRequest(NodeRef targetNode){
216 GetMeterFeaturesInputBuilder input = new GetMeterFeaturesInputBuilder();
218 input.setNode(targetNode);
220 @SuppressWarnings("unused")
221 Future<RpcResult<GetMeterFeaturesOutput>> response =
222 meterStatsService.getMeterFeatures(input.build());
225 public ConcurrentMap<NodeId, NodeStatistics> getStatisticsCache() {
226 return statisticsCache;
229 private List<Node> getAllConnectedNodes(){
231 Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
235 spLogger.info("Number of connected nodes : {}",nodes.getNode().size());
236 return nodes.getNode();
239 @SuppressWarnings("deprecation")
244 spLogger.info("Statistics Provider stopped.");
245 if (this.listenerRegistration != null) {
247 this.listenerRegistration.close();
249 this.statisticsRequesterThread.destroy();
252 } catch (Throwable e) {
253 throw Exceptions.sneakyThrow(e);