OSGi property annotations for graphite metrics app
Change-Id: Ia92d6f2d8352f170115c792739703f5da1e6d327
diff --git a/apps/graphitemetrics/src/main/java/org/onosproject/graphitemetrics/DefaultGraphiteMetricsReporter.java b/apps/graphitemetrics/src/main/java/org/onosproject/graphitemetrics/DefaultGraphiteMetricsReporter.java
index 61dd14c..518c1ff 100644
--- a/apps/graphitemetrics/src/main/java/org/onosproject/graphitemetrics/DefaultGraphiteMetricsReporter.java
+++ b/apps/graphitemetrics/src/main/java/org/onosproject/graphitemetrics/DefaultGraphiteMetricsReporter.java
@@ -37,24 +37,39 @@
import java.util.Dictionary;
import java.util.concurrent.TimeUnit;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.ADDRESS;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.ADDRESS_DEFAULT;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.METRIC_NAMES;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.METRIC_NAMES_DEFAULT;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.METRIC_NAME_PREFIX;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.METRIC_NAME_PREFIX_DEFAULT;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.MONITOR_ALL;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.MONITOR_ALL_DEFAULT;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.PORT;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.PORT_DEFAULT;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.REPORT_PERIOD;
+import static org.onosproject.graphitemetrics.OsgiPropertyConstants.REPORT_PERIOD_DEFAULT;
import static org.slf4j.LoggerFactory.getLogger;
/**
* A metric report that reports all metrics value to graphite monitoring server.
*/
-@Component(immediate = true)
+@Component(
+ immediate = true,
+ property = {
+ MONITOR_ALL + ":Boolean=" + MONITOR_ALL_DEFAULT,
+ METRIC_NAMES + "=" + METRIC_NAMES_DEFAULT,
+ ADDRESS + "=" + ADDRESS_DEFAULT,
+ PORT + ":Integer=" + PORT_DEFAULT,
+ REPORT_PERIOD + ":Integer=" + REPORT_PERIOD_DEFAULT,
+ METRIC_NAME_PREFIX + "=" + METRIC_NAME_PREFIX_DEFAULT
+ }
+)
public class DefaultGraphiteMetricsReporter implements GraphiteMetricsReporter {
private final Logger log = getLogger(getClass());
private static final TimeUnit REPORT_TIME_UNIT = TimeUnit.MINUTES;
- private static final int DEFAULT_REPORT_PERIOD = 1;
-
- private static final String DEFAULT_METRIC_NAMES = "default";
- private static final String DEFAULT_ADDRESS = "localhost";
- private static final int DEFAULT_PORT = 2003;
- private static final String DEFAULT_METRIC_NAME_PREFIX = "onos";
-
@Reference(cardinality = ReferenceCardinality.MANDATORY)
protected CoreService coreService;
@@ -65,29 +80,23 @@
@Reference(cardinality = ReferenceCardinality.MANDATORY)
protected ComponentConfigService cfgService;
- //@Property(name = "monitorAll", boolValue = true,
- // label = "Enable to monitor all of metrics stored in metric registry default is true")
- protected boolean monitorAll = true;
+ /** Enable to monitor all of metrics stored in metric registry default is true. */
+ protected boolean monitorAll = MONITOR_ALL_DEFAULT;
- //@Property(name = "metricNames", value = DEFAULT_METRIC_NAMES,
- // label = "Names of metric to be monitored; default metric names are 'default'")
- protected String metricNames = DEFAULT_METRIC_NAMES;
+ /** Names of metric to be monitored; default metric names are 'default'. */
+ protected String metricNames = METRIC_NAMES_DEFAULT;
- //@Property(name = "address", value = DEFAULT_ADDRESS,
- // label = "IP address of graphite monitoring server; default is localhost")
- protected String address = DEFAULT_ADDRESS;
+ /** IP address of graphite monitoring server; default is localhost. */
+ protected String address = ADDRESS_DEFAULT;
- //@Property(name = "port", intValue = DEFAULT_PORT,
- // label = "Port number of graphite monitoring server; default is 2003")
- protected int port = DEFAULT_PORT;
+ /** Port number of graphite monitoring server; default is 2003. */
+ protected int port = PORT_DEFAULT;
- //@Property(name = "reportPeriod", intValue = DEFAULT_REPORT_PERIOD,
- // label = "Reporting period of graphite monitoring server; default is 1")
- protected int reportPeriod = DEFAULT_REPORT_PERIOD;
+ /** Reporting period of graphite monitoring server; default is 1. */
+ protected int reportPeriod = REPORT_PERIOD_DEFAULT;
- //@Property(name = "metricNamePrefix", value = DEFAULT_METRIC_NAME_PREFIX,
- // label = "Prefix of metric name for graphite back-end server; default is 'onos'")
- protected String metricNamePrefix = DEFAULT_METRIC_NAME_PREFIX;
+ /** Prefix of metric name for graphite back-end server; default is 'onos'. */
+ protected String metricNamePrefix = METRIC_NAME_PREFIX_DEFAULT;
private Graphite graphite;
private GraphiteReporter graphiteReporter;
@@ -205,7 +214,7 @@
private void readComponentConfiguration(ComponentContext context) {
Dictionary<?, ?> properties = context.getProperties();
- Boolean newMonitorAll = Tools.isPropertyEnabled(properties, "monitorAll");
+ Boolean newMonitorAll = Tools.isPropertyEnabled(properties, MONITOR_ALL);
if (newMonitorAll == null) {
log.info("Monitor all metrics is not configured, " +
"using current value of {}", monitorAll);
@@ -215,26 +224,26 @@
monitorAll ? "enabled" : "disabled");
}
- String newMetricNames = Tools.get(properties, "metricNames");
- metricNames = newMetricNames != null ? newMetricNames : DEFAULT_METRIC_NAMES;
+ String newMetricNames = Tools.get(properties, METRIC_NAMES);
+ metricNames = newMetricNames != null ? newMetricNames : METRIC_NAMES_DEFAULT;
log.info("Configured. Metric name is {}", metricNames);
- String newAddress = Tools.get(properties, "address");
- address = newAddress != null ? newAddress : DEFAULT_ADDRESS;
+ String newAddress = Tools.get(properties, ADDRESS);
+ address = newAddress != null ? newAddress : ADDRESS_DEFAULT;
log.info("Configured. Graphite monitoring server address is {}", address);
- Integer newPort = Tools.getIntegerProperty(properties, "port");
+ Integer newPort = Tools.getIntegerProperty(properties, PORT);
if (newPort == null) {
- port = DEFAULT_PORT;
+ port = PORT_DEFAULT;
log.info("Graphite port is not configured, default value is {}", port);
} else {
port = newPort;
log.info("Configured. Graphite port is configured to {}", port);
}
- Integer newReportPeriod = Tools.getIntegerProperty(properties, "reportPeriod");
+ Integer newReportPeriod = Tools.getIntegerProperty(properties, REPORT_PERIOD);
if (newReportPeriod == null) {
- reportPeriod = DEFAULT_REPORT_PERIOD;
+ reportPeriod = REPORT_PERIOD_DEFAULT;
log.info("Report period of graphite server is not configured, " +
"default value is {}", reportPeriod);
} else {
@@ -243,9 +252,9 @@
" is configured to {}", reportPeriod);
}
- String newMetricNamePrefix = Tools.get(properties, "metricNamePrefix");
+ String newMetricNamePrefix = Tools.get(properties, METRIC_NAME_PREFIX);
metricNamePrefix = newMetricNamePrefix != null ?
- newMetricNamePrefix : DEFAULT_METRIC_NAME_PREFIX;
+ newMetricNamePrefix : METRIC_NAME_PREFIX_DEFAULT;
}
diff --git a/apps/graphitemetrics/src/main/java/org/onosproject/graphitemetrics/OsgiPropertyConstants.java b/apps/graphitemetrics/src/main/java/org/onosproject/graphitemetrics/OsgiPropertyConstants.java
new file mode 100644
index 0000000..89aecc0
--- /dev/null
+++ b/apps/graphitemetrics/src/main/java/org/onosproject/graphitemetrics/OsgiPropertyConstants.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.graphitemetrics;
+
+public final class OsgiPropertyConstants {
+ private OsgiPropertyConstants() {
+ }
+
+ static final String MONITOR_ALL = "monitorAll";
+ static final boolean MONITOR_ALL_DEFAULT = true;
+
+ static final String METRIC_NAMES = "metricNames";
+ static final String METRIC_NAMES_DEFAULT = "default";
+
+ static final String ADDRESS = "address";
+ static final String ADDRESS_DEFAULT = "localhost";
+
+ static final String PORT = "port";
+ static final int PORT_DEFAULT = 2003;
+
+ static final String REPORT_PERIOD = "reportPeriod";
+ static final int REPORT_PERIOD_DEFAULT = 1;
+
+ static final String METRIC_NAME_PREFIX = "metricNamePrefix";
+ static final String METRIC_NAME_PREFIX_DEFAULT = "onos";
+}