Fix javadocs and enable doclint
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / reporting / MetricsReporter.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  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.cluster.reporting;
9
10 import com.codahale.metrics.JmxReporter;
11 import com.codahale.metrics.MetricRegistry;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15
16 /**
17  * Maintains metrics registry that is provided to reporters.
18  * At the moment only one reporter exists {@code JmxReporter}.
19  * More reporters can be added.
20  *
21  * <p>
22  * The consumers of this class will only be interested in {@code MetricsRegistry}
23  * where metrics for that consumer gets stored.
24  */
25 public class MetricsReporter implements AutoCloseable {
26
27     private static LoadingCache<String, MetricsReporter> METRIC_REPORTERS = CacheBuilder.newBuilder().build(
28             new CacheLoader<String, MetricsReporter>() {
29                 @Override
30                 public MetricsReporter load(String domainName) {
31                     return new MetricsReporter(domainName);
32                 }
33             });
34
35     private final String domainName;
36     private final JmxReporter jmxReporter;
37     private final MetricRegistry metricRegistry = new MetricRegistry();
38
39     private MetricsReporter(String domainName) {
40         this.domainName = domainName;
41         jmxReporter = JmxReporter.forRegistry(metricRegistry).inDomain(domainName).build();
42         jmxReporter.start();
43     }
44
45     public static MetricsReporter getInstance(String domainName) {
46         return METRIC_REPORTERS.getUnchecked(domainName);
47     }
48
49     public MetricRegistry getMetricsRegistry() {
50         return metricRegistry;
51     }
52
53     @Override
54     public void close() {
55         jmxReporter.close();
56
57         METRIC_REPORTERS.invalidate(domainName);
58     }
59 }