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