Statistics loadable / installable in Karaf
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / yang / gen / v1 / urn / opendaylight / params / xml / ns / yang / openflow / _switch / connection / provider / impl / rev140328 / StatisticsCollectionModule.java
1 package org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.provider.impl.rev140328;
2
3 import org.opendaylight.openflowjava.protocol.api.connection.StatisticsConfiguration;
4 import org.opendaylight.openflowjava.protocol.spi.statistics.StatisticsHandler;
5 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 /**
10 * This is the definition of statistics collection module identity.
11 */
12 public class StatisticsCollectionModule extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.provider.impl.rev140328.AbstractStatisticsCollectionModule {
13
14     private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsCollectionModule.class);
15
16     public StatisticsCollectionModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
17         super(identifier, dependencyResolver);
18     }
19
20     public StatisticsCollectionModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.provider.impl.rev140328.StatisticsCollectionModule oldModule, java.lang.AutoCloseable oldInstance) {
21         super(identifier, dependencyResolver, oldModule, oldInstance);
22     }
23
24     @Override
25     public void customValidation() {
26         // add custom validation form module attributes here.
27     }
28
29     @Override
30     public java.lang.AutoCloseable createInstance() {
31         final Statistics statistics = getStatistics();
32         final StatisticsCounters statsCounter = StatisticsCounters.getInstance();
33         StatisticsConfiguration statsConfig = null;
34         if (statistics != null) {
35             statsConfig = new StatisticsConfiguration() {
36
37                 @Override
38                 public boolean getStatisticsCollect() {
39                     if (statistics.getStatisticsCollect() != null) {
40                         return statistics.getStatisticsCollect().booleanValue();
41                     }
42                     return false;
43                 }
44
45                 @Override
46                 public int getLogReportDelay() {
47                     if (statistics.getLogReportDelay() != null) {
48                         return statistics.getLogReportDelay().intValue();
49                     }
50                     return 0;
51                 }
52             };
53         }
54         if (statsConfig != null) {
55             statsCounter.startCounting(statsConfig.getStatisticsCollect(), statsConfig.getLogReportDelay());
56         } else {
57             LOGGER.debug("Unable to start StatisticCounter - wrong configuration");
58         }
59
60         /* Internal MXBean implementation */
61         final StatisticsCollectionRuntimeMXBean collectionBean = new StatisticsCollectionRuntimeMXBean() {
62
63             @Override
64             public String printOfjavaStatistics() {
65                 if (statsCounter != null) {
66                     return statsCounter.printStatistics();
67                 }
68                 return "Statistics collection is not avaliable.";
69             }
70             @Override
71             public String getMsgStatistics() {
72                 return printOfjavaStatistics();
73             }
74             @Override
75             public String resetOfjavaStatistics() {
76                 statsCounter.resetCounters();
77                 return "Statistics have been reset";
78             }
79         };
80
81         /* MXBean registration */
82         final StatisticsCollectionRuntimeRegistration runtimeReg =
83                 getRootRuntimeBeanRegistratorWrapper().register(collectionBean);
84
85         /* Internal StatisticsCollectionService implementation */
86         final class AutoClosableStatisticsCollection implements StatisticsHandler, AutoCloseable {
87
88             @Override
89             public void close() {
90                 if (runtimeReg != null) {
91                     try {
92                         runtimeReg.close();
93                     }
94                     catch (Exception e) {
95                         String errMsg = "Error by stoping StatisticsCollectionService.";
96                         LOGGER.error(errMsg, e);
97                         throw new IllegalStateException(errMsg, e);
98                     }
99                 }
100                 LOGGER.info("StatisticsCollection Service consumer (instance {} turn down.)", this);
101             }
102
103             @Override
104             public void resetCounters() {
105                 statsCounter.resetCounters();
106             }
107
108             @Override
109             public String printStatistics() {
110                 return statsCounter.printStatistics();
111             }
112         }
113
114         AutoCloseable ret = new AutoClosableStatisticsCollection();
115         LOGGER.info("StatisticsCollection service (instance {}) initialized.", ret);
116         return ret;
117     }
118 }