Merge "changed the pom.xml to include proper integration test"
[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                         spLogger.info("Statistics requester thread started with timer interval : {}",5000);
104                         
105                         statsRequestSender();
106                         
107                         Thread.sleep(5000);
108                     }catch (Exception e){
109                         spLogger.error("Exception occurred while sending stats request : {}",e);
110                     }
111                 }
112             }
113         });
114         spLogger.info("Statistics Provider started.");
115     }
116     
117     protected DataModificationTransaction startChange() {
118         
119         DataProviderService dps = this.getDataService();
120         return dps.beginTransaction();
121     }
122     
123     private void statsRequestSender(){
124         
125         //Need to call API to receive all the nodes connected to controller.
126         List<Node> targetNodes = getAllConnectedNodes();
127
128         for (Node targetNode : targetNodes){
129             spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
130             
131             //We need to add check, so see if groups/meters are supported
132             //by the target node. Below check doesn't look good.
133             if(targetNode.getId().getValue().contains("openflow:")){
134                 sendAllGroupStatisticsRequest(targetNode);
135                 
136                 sendAllMeterStatisticsRequest(targetNode);
137                 
138                 sendGroupDescriptionRequest(targetNode);
139                 
140                 sendGroupFeaturesRequest(targetNode);
141                 
142                 sendMeterConfigStatisticsRequest(targetNode);
143                 
144                 sendMeterFeaturesRequest(targetNode);
145             }
146         }
147     }
148     
149     private void sendAllGroupStatisticsRequest(Node targetNode){
150         
151         final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
152         
153         input.setId(targetNode.getId());
154
155         Future<RpcResult<GetAllGroupStatisticsOutput>> response = 
156                 groupStatsService.getAllGroupStatistics(input.build());
157     }
158     
159     private void sendGroupDescriptionRequest(Node targetNode){
160         final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
161         
162         input.setId(targetNode.getId());
163         
164         Future<RpcResult<GetGroupDescriptionOutput>> response = 
165                 groupStatsService.getGroupDescription(input.build());
166     }
167     
168     private void sendGroupFeaturesRequest(Node targetNode){
169         
170         GetGroupFeaturesInputBuilder input = new GetGroupFeaturesInputBuilder();
171         
172         input.setId(targetNode.getId());
173         
174         Future<RpcResult<GetGroupFeaturesOutput>> response = 
175                 groupStatsService.getGroupFeatures(input.build());
176     }
177     
178     private void sendAllMeterStatisticsRequest(Node targetNode){
179         
180         GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
181         
182         input.setId(targetNode.getId());
183         
184         Future<RpcResult<GetAllMeterStatisticsOutput>> response = 
185                 meterStatsService.getAllMeterStatistics(input.build());
186     }
187     
188     private void sendMeterConfigStatisticsRequest(Node targetNode){
189         
190         GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
191         
192         input.setId(targetNode.getId());
193         
194         Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response = 
195                 meterStatsService.getAllMeterConfigStatistics(input.build());
196         
197     }
198     private void sendMeterFeaturesRequest(Node targetNode){
199      
200         GetMeterFeaturesInputBuilder input = new GetMeterFeaturesInputBuilder();
201         
202         input.setId(targetNode.getId());
203         
204         Future<RpcResult<GetMeterFeaturesOutput>> response = 
205                 meterStatsService.getMeterFeatures(input.build());
206     }
207     
208     public ConcurrentMap<NodeId, NodeStatistics> getStatisticsCache() {
209         return statisticsCache;
210     }
211     
212     private List<Node> getAllConnectedNodes(){
213         
214         Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
215         spLogger.info("Number of connected nodes : {}",nodes.getNode().size());
216         return nodes.getNode();
217     }
218
219     @SuppressWarnings("deprecation")
220     @Override
221     public void close(){
222         
223         try {
224             spLogger.info("Statistics Provider stopped.");
225             if (this.listenerRegistration != null) {
226               
227                 this.listenerRegistration.close();
228                 
229                 this.statisticsRequesterThread.destroy();
230             
231             }
232           } catch (Throwable e) {
233             throw Exceptions.sneakyThrow(e);
234           }
235
236     }
237
238 }