Merge "Config save support for networkconfiguration.neutron"
[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.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
16
17 import org.eclipse.xtext.xbase.lib.Exceptions;
18 import org.opendaylight.controller.md.statistics.manager.MultipartMessageManager.StatsRequestType;
19 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
20 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
21 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsInputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
53 import org.opendaylight.yangtools.concepts.Registration;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.binding.NotificationListener;
56 import org.opendaylight.yangtools.yang.common.RpcResult;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 public class StatisticsProvider implements AutoCloseable {
61
62     public final static Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
63     
64     private DataProviderService dps;
65
66     private NotificationProviderService nps;
67     
68     private OpendaylightGroupStatisticsService groupStatsService;
69     
70     private OpendaylightMeterStatisticsService meterStatsService;
71     
72     private OpendaylightFlowStatisticsService flowStatsService;
73     
74     private OpendaylightPortStatisticsService portStatsService;
75
76     private OpendaylightFlowTableStatisticsService flowTableStatsService;
77
78     private OpendaylightQueueStatisticsService queueStatsService;
79
80     private final MultipartMessageManager multipartMessageManager = new MultipartMessageManager();
81     
82     private Thread statisticsRequesterThread;
83     
84     private final  InstanceIdentifier<Nodes> nodesIdentifier = InstanceIdentifier.builder(Nodes.class).toInstance();
85     
86     private final int STATS_THREAD_EXECUTION_TIME= 50000;
87     //Local caching of stats
88     
89     private final ConcurrentMap<NodeId,NodeStatistics> statisticsCache = 
90             new ConcurrentHashMap<NodeId,NodeStatistics>();
91     
92     public DataProviderService getDataService() {
93       return this.dps;
94     }
95     
96     public void setDataService(final DataProviderService dataService) {
97       this.dps = dataService;
98     }
99     
100     public NotificationProviderService getNotificationService() {
101       return this.nps;
102     }
103     
104     public void setNotificationService(final NotificationProviderService notificationService) {
105       this.nps = notificationService;
106     }
107
108     public MultipartMessageManager getMultipartMessageManager() {
109         return multipartMessageManager;
110     }
111
112     private final StatisticsUpdateCommiter updateCommiter = new StatisticsUpdateCommiter(StatisticsProvider.this);
113     
114     private Registration<NotificationListener> listenerRegistration;
115     
116     public void start() {
117         
118         NotificationProviderService nps = this.getNotificationService();
119         Registration<NotificationListener> registerNotificationListener = nps.registerNotificationListener(this.updateCommiter);
120         this.listenerRegistration = registerNotificationListener;
121         
122         // Get Group/Meter statistics service instance
123         groupStatsService = StatisticsManagerActivator.getProviderContext().
124                 getRpcService(OpendaylightGroupStatisticsService.class);
125         
126         meterStatsService = StatisticsManagerActivator.getProviderContext().
127                 getRpcService(OpendaylightMeterStatisticsService.class);
128         
129         flowStatsService = StatisticsManagerActivator.getProviderContext().
130                 getRpcService(OpendaylightFlowStatisticsService.class);
131
132         portStatsService = StatisticsManagerActivator.getProviderContext().
133                 getRpcService(OpendaylightPortStatisticsService.class);
134
135         flowTableStatsService = StatisticsManagerActivator.getProviderContext().
136                 getRpcService(OpendaylightFlowTableStatisticsService.class);
137         
138         queueStatsService = StatisticsManagerActivator.getProviderContext().
139                 getRpcService(OpendaylightQueueStatisticsService.class);
140         
141         statisticsRequesterThread = new Thread( new Runnable(){
142
143             @Override
144             public void run() {
145                 while(true){
146                     try {
147                         statsRequestSender();
148                         
149                         Thread.sleep(STATS_THREAD_EXECUTION_TIME);
150                     }catch (Exception e){
151                         spLogger.error("Exception occurred while sending stats request : {}",e);
152                     }
153                 }
154             }
155         });
156         
157         spLogger.debug("Statistics requester thread started with timer interval : {}",STATS_THREAD_EXECUTION_TIME);
158         
159         statisticsRequesterThread.start();
160         
161         spLogger.info("Statistics Provider started.");
162     }
163     
164     protected DataModificationTransaction startChange() {
165         
166         DataProviderService dps = this.getDataService();
167         return dps.beginTransaction();
168     }
169     
170     private void statsRequestSender(){
171         
172         List<Node> targetNodes = getAllConnectedNodes();
173         
174         if(targetNodes == null)
175             return;
176         
177
178         for (Node targetNode : targetNodes){
179             
180             if(targetNode.getAugmentation(FlowCapableNode.class) != null){
181
182                 spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
183                 
184                 InstanceIdentifier<Node> targetInstanceId = InstanceIdentifier.builder(Nodes.class).child(Node.class,targetNode.getKey()).toInstance();
185                 
186                 NodeRef targetNodeRef = new NodeRef(targetInstanceId);
187             
188                 try{
189                     sendAggregateFlowsStatsFromAllTablesRequest(targetNode.getKey());
190                 
191                     sendAllFlowsStatsFromAllTablesRequest(targetNodeRef);
192
193                     sendAllNodeConnectorsStatisticsRequest(targetNodeRef);
194                 
195                     sendAllFlowTablesStatisticsRequest(targetNodeRef);
196                 
197                     sendAllQueueStatsFromAllNodeConnector (targetNodeRef);
198
199                     sendAllGroupStatisticsRequest(targetNodeRef);
200                     
201                     sendAllMeterStatisticsRequest(targetNodeRef);
202                     
203                     sendGroupDescriptionRequest(targetNodeRef);
204                     
205                     sendMeterConfigStatisticsRequest(targetNodeRef);
206                 }catch(Exception e){
207                     spLogger.error("Exception occured while sending statistics requests : {}", e);
208                 }
209             }
210         }
211     }
212
213     private void sendAllFlowTablesStatisticsRequest(NodeRef targetNodeRef) throws InterruptedException, ExecutionException {
214         final GetFlowTablesStatisticsInputBuilder input = 
215                 new GetFlowTablesStatisticsInputBuilder();
216         
217         input.setNode(targetNodeRef);
218
219         Future<RpcResult<GetFlowTablesStatisticsOutput>> response = 
220                 flowTableStatsService.getFlowTablesStatistics(input.build());
221
222         this.multipartMessageManager.addTxIdToRequestTypeEntry(response.get().getResult().getTransactionId()
223                 , StatsRequestType.ALL_FLOW_TABLE);
224
225     }
226
227     private void sendAllFlowsStatsFromAllTablesRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
228         final GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input =
229                 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder();
230         
231         input.setNode(targetNode);
232         
233         Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> response = 
234                 flowStatsService.getAllFlowsStatisticsFromAllFlowTables(input.build());
235         
236         this.multipartMessageManager.addTxIdToRequestTypeEntry(response.get().getResult().getTransactionId()
237                 , StatsRequestType.ALL_FLOW);
238         
239     }
240     
241     private void sendAggregateFlowsStatsFromAllTablesRequest(NodeKey targetNodeKey) throws InterruptedException, ExecutionException{
242         
243         List<Short> tablesId = getTablesFromNode(targetNodeKey);
244         
245         if(tablesId.size() != 0){
246             for(Short id : tablesId){
247                 
248                 spLogger.info("Send aggregate stats request for flow table {} to node {}",id,targetNodeKey);
249                 GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input = 
250                         new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder();
251                 
252                 input.setNode(new NodeRef(InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey).toInstance()));
253                 input.setTableId(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId(id));
254                 Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> response = 
255                         flowStatsService.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());
256                 
257                 multipartMessageManager.setTxIdAndTableIdMapEntry(response.get().getResult().getTransactionId(), id);
258                 this.multipartMessageManager.addTxIdToRequestTypeEntry(response.get().getResult().getTransactionId()
259                         , StatsRequestType.AGGR_FLOW);
260             }
261         }else{
262             spLogger.debug("No details found in data store for flow tables associated with Node {}",targetNodeKey);
263         }
264     }
265
266     private void sendAllNodeConnectorsStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
267         
268         final GetAllNodeConnectorsStatisticsInputBuilder input = new GetAllNodeConnectorsStatisticsInputBuilder();
269         
270         input.setNode(targetNode);
271
272         Future<RpcResult<GetAllNodeConnectorsStatisticsOutput>> response = 
273                 portStatsService.getAllNodeConnectorsStatistics(input.build());
274         this.multipartMessageManager.addTxIdToRequestTypeEntry(response.get().getResult().getTransactionId()
275                 , StatsRequestType.ALL_PORT);
276
277     }
278
279     private void sendAllGroupStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
280         
281         final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
282         
283         input.setNode(targetNode);
284
285         Future<RpcResult<GetAllGroupStatisticsOutput>> response = 
286                 groupStatsService.getAllGroupStatistics(input.build());
287         
288         this.multipartMessageManager.addTxIdToRequestTypeEntry(response.get().getResult().getTransactionId()
289                 , StatsRequestType.ALL_GROUP);
290
291     }
292     
293     private void sendGroupDescriptionRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
294         final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
295         
296         input.setNode(targetNode);
297
298         Future<RpcResult<GetGroupDescriptionOutput>> response = 
299                 groupStatsService.getGroupDescription(input.build());
300
301         this.multipartMessageManager.addTxIdToRequestTypeEntry(response.get().getResult().getTransactionId()
302                 , StatsRequestType.GROUP_DESC);
303
304     }
305     
306     private void sendAllMeterStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
307         
308         GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
309         
310         input.setNode(targetNode);
311
312         Future<RpcResult<GetAllMeterStatisticsOutput>> response = 
313                 meterStatsService.getAllMeterStatistics(input.build());
314         
315         this.multipartMessageManager.addTxIdToRequestTypeEntry(response.get().getResult().getTransactionId()
316                 , StatsRequestType.ALL_METER);;
317
318     }
319     
320     private void sendMeterConfigStatisticsRequest(NodeRef targetNode) throws InterruptedException, ExecutionException{
321         
322         GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
323         
324         input.setNode(targetNode);
325
326         Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response = 
327                 meterStatsService.getAllMeterConfigStatistics(input.build());
328         
329         this.multipartMessageManager.addTxIdToRequestTypeEntry(response.get().getResult().getTransactionId()
330                 , StatsRequestType.METER_CONFIG);;
331
332     }
333     
334     private void sendAllQueueStatsFromAllNodeConnector(NodeRef targetNode) throws InterruptedException, ExecutionException {
335         GetAllQueuesStatisticsFromAllPortsInputBuilder input = new GetAllQueuesStatisticsFromAllPortsInputBuilder();
336         
337         input.setNode(targetNode);
338         
339         Future<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> response = 
340                 queueStatsService.getAllQueuesStatisticsFromAllPorts(input.build());
341         
342         this.multipartMessageManager.addTxIdToRequestTypeEntry(response.get().getResult().getTransactionId()
343                 , StatsRequestType.ALL_QUEUE_STATS);;
344
345     }
346
347     public ConcurrentMap<NodeId, NodeStatistics> getStatisticsCache() {
348         return statisticsCache;
349     }
350     
351     private List<Node> getAllConnectedNodes(){
352         
353         Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
354         if(nodes == null)
355             return null;
356         
357         spLogger.info("Number of connected nodes : {}",nodes.getNode().size());
358         return nodes.getNode();
359     }
360     
361     private List<Short> getTablesFromNode(NodeKey nodeKey){
362         InstanceIdentifier<FlowCapableNode> nodesIdentifier = InstanceIdentifier.builder(Nodes.class).child(Node.class,nodeKey).augmentation(FlowCapableNode.class).toInstance();
363         
364         FlowCapableNode node = (FlowCapableNode)dps.readOperationalData(nodesIdentifier);
365         List<Short> tablesId = new ArrayList<Short>();
366         if(node != null && node.getTable()!=null){
367             spLogger.info("Number of tables {} supported by node {}",node.getTable().size(),nodeKey);
368             for(Table table: node.getTable()){
369                 tablesId.add(table.getId());
370             }
371         }
372         return tablesId;
373     }
374
375     @SuppressWarnings("deprecation")
376     @Override
377     public void close(){
378         
379         try {
380             spLogger.info("Statistics Provider stopped.");
381             if (this.listenerRegistration != null) {
382               
383                 this.listenerRegistration.close();
384                 
385                 this.statisticsRequesterThread.destroy();
386             
387             }
388           } catch (Throwable e) {
389             throw Exceptions.sneakyThrow(e);
390           }
391     }
392
393 }