Merge "Modify the java-client to use the build-helper-maven-plugin"
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / StatisticsProvider.java
1 package org.opendaylight.controller.md.statistics.manager;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.concurrent.ConcurrentHashMap;
6 import java.util.concurrent.ConcurrentMap;
7 import java.util.concurrent.Future;
8
9 import org.eclipse.xtext.xbase.lib.Exceptions;
10 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
11 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
12 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInputBuilder;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInputBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesInputBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
30 import org.opendaylight.yangtools.concepts.Registration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.binding.NotificationListener;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class StatisticsProvider implements AutoCloseable {
38
39     public final static Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
40     
41     private DataProviderService dps;
42
43     private NotificationProviderService nps;
44     
45     private OpendaylightGroupStatisticsService groupStatsService;
46     
47     private OpendaylightMeterStatisticsService meterStatsService;
48     
49     private Thread statisticsRequesterThread;
50     
51     private final  InstanceIdentifier<Nodes> nodesIdentifier = InstanceIdentifier.builder().node(Nodes.class).toInstance();
52     
53     //Local caching of stats
54     
55     private final ConcurrentMap<NodeRef,NodeStatistics> statisticsCache = 
56             new ConcurrentHashMap<NodeRef,NodeStatistics>();
57     
58     public DataProviderService getDataService() {
59       return this.dps;
60     }
61     
62     public void setDataService(final DataProviderService dataService) {
63       this.dps = dataService;
64     }
65     
66     public NotificationProviderService getNotificationService() {
67       return this.nps;
68     }
69     
70     public void setNotificationService(final NotificationProviderService notificationService) {
71       this.nps = notificationService;
72     }
73
74     private final StatisticsUpdateCommiter updateCommiter = new StatisticsUpdateCommiter(StatisticsProvider.this);
75     
76     private Registration<NotificationListener> listenerRegistration;
77     
78     public void start() {
79         
80         NotificationProviderService nps = this.getNotificationService();
81         Registration<NotificationListener> registerNotificationListener = nps.registerNotificationListener(this.updateCommiter);
82         this.listenerRegistration = registerNotificationListener;
83         
84         // Get Group/Meter statistics service instance
85         groupStatsService = StatisticsManagerActivator.getProviderContext().
86                 getRpcService(OpendaylightGroupStatisticsService.class);
87         
88         meterStatsService = StatisticsManagerActivator.getProviderContext().
89                 getRpcService(OpendaylightMeterStatisticsService.class);
90
91         statisticsRequesterThread = new Thread( new Runnable(){
92
93             @Override
94             public void run() {
95                 while(true){
96                     try {
97                         statsRequestSender();
98                         
99                         Thread.sleep(5000);
100                     }catch (Exception e){
101                         spLogger.error("Exception occurred while sending stats request : {}",e.getMessage());
102                         e.printStackTrace();
103                     }
104                 }
105             }
106         });
107         spLogger.info("Statistics Provider started.");
108     }
109     
110     protected DataModificationTransaction startChange() {
111         
112         DataProviderService dps = this.getDataService();
113         return dps.beginTransaction();
114     }
115     
116     private void statsRequestSender(){
117         
118         //Need to call API to receive all the nodes connected to controller.
119         
120         List<NodeRef> targetNodes = new ArrayList<NodeRef>();
121         
122         for (NodeRef targetNode : targetNodes){
123             
124             sendAllGroupStatisticsRequest(targetNode);
125             
126             sendAllMeterStatisticsRequest(targetNode);
127
128             //We need to add check, so see if groups/meters are supported
129             //by the target node.
130             sendGroupDescriptionRequest(targetNode);
131             
132             sendGroupFeaturesRequest(targetNode);
133             
134             sendMeterConfigStatisticsRequest(targetNode);
135             
136             sendMeterFeaturesRequest(targetNode);
137         }
138     }
139     
140     private void sendAllGroupStatisticsRequest(NodeRef targetNode){
141         
142         GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
143         
144         input.setNode(targetNode);
145
146         Future<RpcResult<GetAllGroupStatisticsOutput>> response = 
147                 groupStatsService.getAllGroupStatistics(input.build());
148     }
149     
150     private void sendGroupDescriptionRequest(NodeRef targetNode){
151         GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
152         
153         input.setNode(targetNode);
154         
155         Future<RpcResult<GetGroupDescriptionOutput>> response = 
156                 groupStatsService.getGroupDescription(input.build());
157     }
158     
159     private void sendGroupFeaturesRequest(NodeRef targetNode){
160         
161         GetGroupFeaturesInputBuilder input = new GetGroupFeaturesInputBuilder();
162         
163         input.setNode(targetNode);
164         
165         Future<RpcResult<GetGroupFeaturesOutput>> response = 
166                 groupStatsService.getGroupFeatures(input.build());
167     }
168     
169     private void sendAllMeterStatisticsRequest(NodeRef targenetNode){
170         
171         GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
172         
173         input.setNode(targenetNode);
174         
175         Future<RpcResult<GetAllMeterStatisticsOutput>> response = 
176                 meterStatsService.getAllMeterStatistics(input.build());
177     }
178     
179     private void sendMeterConfigStatisticsRequest(NodeRef targetNode){
180         
181         GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
182         
183         input.setNode(targetNode);
184         
185         Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response = 
186                 meterStatsService.getAllMeterConfigStatistics(input.build());
187         
188     }
189     private void sendMeterFeaturesRequest(NodeRef targetNode){
190      
191         GetMeterFeaturesInputBuilder input = new GetMeterFeaturesInputBuilder();
192         
193         input.setNode(targetNode);
194         
195         Future<RpcResult<GetMeterFeaturesOutput>> response = 
196                 meterStatsService.getMeterFeatures(input.build());
197     }
198     
199     public ConcurrentMap<NodeRef, NodeStatistics> getStatisticsCache() {
200         return statisticsCache;
201     }
202     
203     private List<Node> getAllConnectedNodes(){
204         
205         Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
206         return nodes.getNode();
207     }
208
209     @SuppressWarnings("deprecation")
210     @Override
211     public void close(){
212         
213         try {
214             spLogger.info("Statistics Provider stopped.");
215             if (this.listenerRegistration != null) {
216               
217                 this.listenerRegistration.close();
218                 
219                 this.statisticsRequesterThread.destroy();
220             
221             }
222           } catch (Throwable e) {
223             throw Exceptions.sneakyThrow(e);
224           }
225
226     }
227
228 }