Merge "- netconf SSH bridge bundle - Implement bridge using socket - standard netconf...
[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.List;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
13 import java.util.concurrent.Future;
14
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;
43
44 public class StatisticsProvider implements AutoCloseable {
45
46     public final static Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
47     
48     private DataProviderService dps;
49
50     private NotificationProviderService nps;
51     
52     private OpendaylightGroupStatisticsService groupStatsService;
53     
54     private OpendaylightMeterStatisticsService meterStatsService;
55     
56     private Thread statisticsRequesterThread;
57     
58     private final  InstanceIdentifier<Nodes> nodesIdentifier = InstanceIdentifier.builder().node(Nodes.class).toInstance();
59     
60     //Local caching of stats
61     
62     private final ConcurrentMap<NodeId,NodeStatistics> statisticsCache = 
63             new ConcurrentHashMap<NodeId,NodeStatistics>();
64     
65     public DataProviderService getDataService() {
66       return this.dps;
67     }
68     
69     public void setDataService(final DataProviderService dataService) {
70       this.dps = dataService;
71     }
72     
73     public NotificationProviderService getNotificationService() {
74       return this.nps;
75     }
76     
77     public void setNotificationService(final NotificationProviderService notificationService) {
78       this.nps = notificationService;
79     }
80
81     private final StatisticsUpdateCommiter updateCommiter = new StatisticsUpdateCommiter(StatisticsProvider.this);
82     
83     private Registration<NotificationListener> listenerRegistration;
84     
85     public void start() {
86         
87         NotificationProviderService nps = this.getNotificationService();
88         Registration<NotificationListener> registerNotificationListener = nps.registerNotificationListener(this.updateCommiter);
89         this.listenerRegistration = registerNotificationListener;
90         
91         // Get Group/Meter statistics service instance
92         groupStatsService = StatisticsManagerActivator.getProviderContext().
93                 getRpcService(OpendaylightGroupStatisticsService.class);
94         
95         meterStatsService = StatisticsManagerActivator.getProviderContext().
96                 getRpcService(OpendaylightMeterStatisticsService.class);
97
98         statisticsRequesterThread = new Thread( new Runnable(){
99
100             @Override
101             public void run() {
102                 while(true){
103                     try {
104                         statsRequestSender();
105                         
106                         Thread.sleep(5000);
107                     }catch (Exception e){
108                         spLogger.error("Exception occurred while sending stats request : {}",e);
109                     }
110                 }
111             }
112         });
113         
114         spLogger.debug("Statistics requester thread started with timer interval : {}",5000);
115         
116         statisticsRequesterThread.start();
117         
118         spLogger.info("Statistics Provider started.");
119     }
120     
121     protected DataModificationTransaction startChange() {
122         
123         DataProviderService dps = this.getDataService();
124         return dps.beginTransaction();
125     }
126     
127     private void statsRequestSender(){
128         
129         //Need to call API to receive all the nodes connected to controller.
130         List<Node> targetNodes = getAllConnectedNodes();
131         
132         if(targetNodes == null)
133             return;
134
135         for (Node targetNode : targetNodes){
136             spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
137             
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);
143                 
144                 sendAllGroupStatisticsRequest(targetNodeRef);
145                 
146                 sendAllMeterStatisticsRequest(targetNodeRef);
147                 
148                 sendGroupDescriptionRequest(targetNodeRef);
149                 
150                 sendGroupFeaturesRequest(targetNodeRef);
151                 
152                 sendMeterConfigStatisticsRequest(targetNodeRef);
153                 
154                 sendMeterFeaturesRequest(targetNodeRef);
155             }
156         }
157     }
158     
159     private void sendAllGroupStatisticsRequest(NodeRef targetNode){
160         
161         final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
162         
163         input.setNode(targetNode);
164         input.setNode(targetNode);
165
166         @SuppressWarnings("unused")
167         Future<RpcResult<GetAllGroupStatisticsOutput>> response = 
168                 groupStatsService.getAllGroupStatistics(input.build());
169     }
170     
171     private void sendGroupDescriptionRequest(NodeRef targetNode){
172         final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
173         
174         input.setNode(targetNode);
175
176         @SuppressWarnings("unused")
177         Future<RpcResult<GetGroupDescriptionOutput>> response = 
178                 groupStatsService.getGroupDescription(input.build());
179     }
180     
181     private void sendGroupFeaturesRequest(NodeRef targetNode){
182         
183         GetGroupFeaturesInputBuilder input = new GetGroupFeaturesInputBuilder();
184         
185         input.setNode(targetNode);
186
187         @SuppressWarnings("unused")
188         Future<RpcResult<GetGroupFeaturesOutput>> response = 
189                 groupStatsService.getGroupFeatures(input.build());
190     }
191     
192     private void sendAllMeterStatisticsRequest(NodeRef targetNode){
193         
194         GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
195         
196         input.setNode(targetNode);
197
198         @SuppressWarnings("unused")
199         Future<RpcResult<GetAllMeterStatisticsOutput>> response = 
200                 meterStatsService.getAllMeterStatistics(input.build());
201     }
202     
203     private void sendMeterConfigStatisticsRequest(NodeRef targetNode){
204         
205         GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
206         
207         input.setNode(targetNode);
208
209         @SuppressWarnings("unused")
210         Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response = 
211                 meterStatsService.getAllMeterConfigStatistics(input.build());
212         
213     }
214     private void sendMeterFeaturesRequest(NodeRef targetNode){
215      
216         GetMeterFeaturesInputBuilder input = new GetMeterFeaturesInputBuilder();
217         
218         input.setNode(targetNode);
219
220         @SuppressWarnings("unused")
221         Future<RpcResult<GetMeterFeaturesOutput>> response = 
222                 meterStatsService.getMeterFeatures(input.build());
223     }
224     
225     public ConcurrentMap<NodeId, NodeStatistics> getStatisticsCache() {
226         return statisticsCache;
227     }
228     
229     private List<Node> getAllConnectedNodes(){
230         
231         Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
232         if(nodes == null)
233             return null;
234         
235         spLogger.info("Number of connected nodes : {}",nodes.getNode().size());
236         return nodes.getNode();
237     }
238
239     @SuppressWarnings("deprecation")
240     @Override
241     public void close(){
242         
243         try {
244             spLogger.info("Statistics Provider stopped.");
245             if (this.listenerRegistration != null) {
246               
247                 this.listenerRegistration.close();
248                 
249                 this.statisticsRequesterThread.destroy();
250             
251             }
252           } catch (Throwable e) {
253             throw Exceptions.sneakyThrow(e);
254           }
255     }
256
257 }