073d43ef60df3d944dda0da6512326267be114e5
[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 org.eclipse.xtext.xbase.lib.Exceptions;
4 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
5 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
6 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
7 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
9 import org.opendaylight.yangtools.concepts.Registration;
10 import org.opendaylight.yangtools.yang.binding.NotificationListener;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 public class StatisticsProvider implements AutoCloseable {
15
16     public final static Logger spLogger = LoggerFactory.getLogger(StatisticsProvider.class);
17     
18     private DataProviderService dps;
19
20     private NotificationProviderService nps;
21     
22     private OpendaylightGroupStatisticsService groupStatsService;
23     
24     private OpendaylightMeterStatisticsService meterStatsService;
25     
26     public DataProviderService getDataService() {
27       return this.dps;
28     }
29     
30     public void setDataService(final DataProviderService dataService) {
31       this.dps = dataService;
32     }
33     
34     public NotificationProviderService getNotificationService() {
35       return this.nps;
36     }
37     
38     public void setNotificationService(final NotificationProviderService notificationService) {
39       this.nps = notificationService;
40     }
41
42     private final StatisticsUpdateCommiter updateCommiter = new StatisticsUpdateCommiter(StatisticsProvider.this);
43     
44     private Registration<NotificationListener> listenerRegistration;
45     
46     public void start() {
47         
48         NotificationProviderService nps = this.getNotificationService();
49         Registration<NotificationListener> registerNotificationListener = nps.registerNotificationListener(this.updateCommiter);
50         this.listenerRegistration = registerNotificationListener;
51         
52         // Get Group/Meter statistics service instance
53         groupStatsService = StatisticsManagerActivator.getProviderContext().getRpcService(OpendaylightGroupStatisticsService.class);
54         
55         meterStatsService = StatisticsManagerActivator.getProviderContext().getRpcService(OpendaylightMeterStatisticsService.class);
56
57         spLogger.info("Statistics Provider started.");
58     }
59     
60     protected DataModificationTransaction startChange() {
61         
62         DataProviderService dps = this.getDataService();
63         return dps.beginTransaction();
64     }
65     
66     @Override
67     public void close(){
68         
69         try {
70             spLogger.info("Statistics Provider stopped.");
71             if (this.listenerRegistration != null) {
72               
73                 this.listenerRegistration.close();
74             
75             }
76           } catch (Throwable e) {
77             throw Exceptions.sneakyThrow(e);
78           }
79
80     }
81
82 }