Merge "Initial implementation of netconf monitoring module according to http://tools...
[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.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
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.OpendaylightMeterStatisticsService;
34 import org.opendaylight.yangtools.concepts.Registration;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.NotificationListener;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class StatisticsProvider implements AutoCloseable {
42
43     public final static Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
44     
45     private DataProviderService dps;
46
47     private NotificationProviderService nps;
48     
49     private OpendaylightGroupStatisticsService groupStatsService;
50     
51     private OpendaylightMeterStatisticsService meterStatsService;
52     
53     private Thread statisticsRequesterThread;
54     
55     private final  InstanceIdentifier<Nodes> nodesIdentifier = InstanceIdentifier.builder(Nodes.class).toInstance();
56     
57     private final int STATS_THREAD_EXECUTION_TIME= 50000;
58     //Local caching of stats
59     
60     private final ConcurrentMap<NodeId,NodeStatistics> statisticsCache = 
61             new ConcurrentHashMap<NodeId,NodeStatistics>();
62     
63     public DataProviderService getDataService() {
64       return this.dps;
65     }
66     
67     public void setDataService(final DataProviderService dataService) {
68       this.dps = dataService;
69     }
70     
71     public NotificationProviderService getNotificationService() {
72       return this.nps;
73     }
74     
75     public void setNotificationService(final NotificationProviderService notificationService) {
76       this.nps = notificationService;
77     }
78
79     private final StatisticsUpdateCommiter updateCommiter = new StatisticsUpdateCommiter(StatisticsProvider.this);
80     
81     private Registration<NotificationListener> listenerRegistration;
82     
83     public void start() {
84         
85         NotificationProviderService nps = this.getNotificationService();
86         Registration<NotificationListener> registerNotificationListener = nps.registerNotificationListener(this.updateCommiter);
87         this.listenerRegistration = registerNotificationListener;
88         
89         // Get Group/Meter statistics service instance
90         groupStatsService = StatisticsManagerActivator.getProviderContext().
91                 getRpcService(OpendaylightGroupStatisticsService.class);
92         
93         meterStatsService = StatisticsManagerActivator.getProviderContext().
94                 getRpcService(OpendaylightMeterStatisticsService.class);
95
96         statisticsRequesterThread = new Thread( new Runnable(){
97
98             @Override
99             public void run() {
100                 while(true){
101                     try {
102                         statsRequestSender();
103                         
104                         Thread.sleep(STATS_THREAD_EXECUTION_TIME);
105                     }catch (Exception e){
106                         spLogger.error("Exception occurred while sending stats request : {}",e);
107                     }
108                 }
109             }
110         });
111         
112         spLogger.debug("Statistics requester thread started with timer interval : {}",STATS_THREAD_EXECUTION_TIME);
113         
114         statisticsRequesterThread.start();
115         
116         spLogger.info("Statistics Provider started.");
117     }
118     
119     protected DataModificationTransaction startChange() {
120         
121         DataProviderService dps = this.getDataService();
122         return dps.beginTransaction();
123     }
124     
125     private void statsRequestSender(){
126         
127         //Need to call API to receive all the nodes connected to controller.
128         List<Node> targetNodes = getAllConnectedNodes();
129         
130         if(targetNodes == null)
131             return;
132
133         for (Node targetNode : targetNodes){
134
135             if(targetNode.getAugmentation(FlowCapableNode.class) != null){
136
137                 spLogger.info("Send request for stats collection to node : {})",targetNode.getId());
138                 
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 }