MD-SAL Statistics Manager - Minor change in group/meter type/capabilities
[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             spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
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                 InstanceIdentifier<Node> targetInstanceId = InstanceIdentifier.builder(Nodes.class).child(Node.class,targetNode.getKey()).toInstance();
139                 NodeRef targetNodeRef = new NodeRef(targetInstanceId);
140                 
141                 try{
142                   sendAllGroupStatisticsRequest(targetNodeRef);
143                   Thread.sleep(1000);
144                   sendAllMeterStatisticsRequest(targetNodeRef);
145                   Thread.sleep(1000);
146                   sendGroupDescriptionRequest(targetNodeRef);
147                   Thread.sleep(1000);
148                   sendMeterConfigStatisticsRequest(targetNodeRef);
149                   Thread.sleep(1000);
150                 }catch(Exception e){
151                     spLogger.error("Exception occured while sending statistics request : {}", e);
152                 }
153             }
154         }
155     }
156     
157     private void sendAllGroupStatisticsRequest(NodeRef targetNode){
158         
159         final GetAllGroupStatisticsInputBuilder input = new GetAllGroupStatisticsInputBuilder();
160         
161         input.setNode(targetNode);
162         input.setNode(targetNode);
163
164         @SuppressWarnings("unused")
165         Future<RpcResult<GetAllGroupStatisticsOutput>> response = 
166                 groupStatsService.getAllGroupStatistics(input.build());
167     }
168     
169     private void sendGroupDescriptionRequest(NodeRef targetNode){
170         final GetGroupDescriptionInputBuilder input = new GetGroupDescriptionInputBuilder();
171         
172         input.setNode(targetNode);
173
174         @SuppressWarnings("unused")
175         Future<RpcResult<GetGroupDescriptionOutput>> response = 
176                 groupStatsService.getGroupDescription(input.build());
177     }
178     
179     private void sendAllMeterStatisticsRequest(NodeRef targetNode){
180         
181         GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
182         
183         input.setNode(targetNode);
184
185         @SuppressWarnings("unused")
186         Future<RpcResult<GetAllMeterStatisticsOutput>> response = 
187                 meterStatsService.getAllMeterStatistics(input.build());
188     }
189     
190     private void sendMeterConfigStatisticsRequest(NodeRef targetNode){
191         
192         GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder();
193         
194         input.setNode(targetNode);
195
196         @SuppressWarnings("unused")
197         Future<RpcResult<GetAllMeterConfigStatisticsOutput>> response = 
198                 meterStatsService.getAllMeterConfigStatistics(input.build());
199         
200     }
201     
202     public ConcurrentMap<NodeId, NodeStatistics> getStatisticsCache() {
203         return statisticsCache;
204     }
205     
206     private List<Node> getAllConnectedNodes(){
207         
208         Nodes nodes = (Nodes) dps.readOperationalData(nodesIdentifier);
209         if(nodes == null)
210             return null;
211         
212         spLogger.info("Number of connected nodes : {}",nodes.getNode().size());
213         return nodes.getNode();
214     }
215
216     @SuppressWarnings("deprecation")
217     @Override
218     public void close(){
219         
220         try {
221             spLogger.info("Statistics Provider stopped.");
222             if (this.listenerRegistration != null) {
223               
224                 this.listenerRegistration.close();
225                 
226                 this.statisticsRequesterThread.destroy();
227             
228             }
229           } catch (Throwable e) {
230             throw Exceptions.sneakyThrow(e);
231           }
232     }
233
234 }