MD-SAL Statistics Manager - Minor bug fixing
[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         for (Node targetNode : targetNodes){
132             spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
133             
134             //We need to add check, so see if groups/meters are supported
135             //by the target node. Below check doesn't look good.
136             if(targetNode.getId().getValue().contains("openflow:")){
137                 sendAllGroupStatisticsRequest(targetNode);
138                 
139                 sendAllMeterStatisticsRequest(targetNode);
140                 
141                 sendGroupDescriptionRequest(targetNode);
142                 
143                 sendGroupFeaturesRequest(targetNode);
144                 
145                 sendMeterConfigStatisticsRequest(targetNode);
146                 
147                 sendMeterFeaturesRequest(targetNode);
148             }
149         }
150     }
151     
152     private void sendAllGroupStatisticsRequest(Node targetNode){
153         
154         final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
155         
156         input.setId(targetNode.getId());
157
158         Future<RpcResult<GetAllGroupStatisticsOutput>> response = 
159                 groupStatsService.getAllGroupStatistics(input.build());
160     }
161     
162     private void sendGroupDescriptionRequest(Node targetNode){
163         final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
164         
165         input.setId(targetNode.getId());
166         
167         Future<RpcResult<GetGroupDescriptionOutput>> response = 
168                 groupStatsService.getGroupDescription(input.build());
169     }
170     
171     private void sendGroupFeaturesRequest(Node targetNode){
172         
173         GetGroupFeaturesInputBuilder input = new GetGroupFeaturesInputBuilder();
174         
175         input.setId(targetNode.getId());
176         
177         Future<RpcResult<GetGroupFeaturesOutput>> response = 
178                 groupStatsService.getGroupFeatures(input.build());
179     }
180     
181     private void sendAllMeterStatisticsRequest(Node targetNode){
182         
183         GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
184         
185         input.setId(targetNode.getId());
186         
187         Future<RpcResult<GetAllMeterStatisticsOutput>> response = 
188                 meterStatsService.getAllMeterStatistics(input.build());
189     }
190     
191     private void sendMeterConfigStatisticsRequest(Node targetNode){
192         
193         GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
194         
195         input.setId(targetNode.getId());
196         
197         Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response = 
198                 meterStatsService.getAllMeterConfigStatistics(input.build());
199         
200     }
201     private void sendMeterFeaturesRequest(Node targetNode){
202      
203         GetMeterFeaturesInputBuilder input = new GetMeterFeaturesInputBuilder();
204         
205         input.setId(targetNode.getId());
206         
207         Future<RpcResult<GetMeterFeaturesOutput>> response = 
208                 meterStatsService.getMeterFeatures(input.build());
209     }
210     
211     public ConcurrentMap<NodeId, NodeStatistics> getStatisticsCache() {
212         return statisticsCache;
213     }
214     
215     private List<Node> getAllConnectedNodes(){
216         
217         Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
218         spLogger.info("Number of connected nodes : {}",nodes.getNode().size());
219         return nodes.getNode();
220     }
221
222     @SuppressWarnings("deprecation")
223     @Override
224     public void close(){
225         
226         try {
227             spLogger.info("Statistics Provider stopped.");
228             if (this.listenerRegistration != null) {
229               
230                 this.listenerRegistration.close();
231                 
232                 this.statisticsRequesterThread.destroy();
233             
234             }
235           } catch (Throwable e) {
236             throw Exceptions.sneakyThrow(e);
237           }
238
239     }
240
241 }