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