blob: 7c879b9d1830058e88628c6d2d33a5743d3203fb [file] [log] [blame]
Jian Lie1d97c92016-03-22 10:21:44 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Lie1d97c92016-03-22 10:21:44 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.influxdbmetrics;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.onlab.util.Tools;
26import org.onosproject.cfg.ComponentConfigService;
27import org.onosproject.core.CoreService;
28import org.osgi.service.component.ComponentContext;
29import org.slf4j.Logger;
30
31import java.util.Dictionary;
32
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * A configuration service for InfluxDB metrics.
37 * Both InfluxDbMetrics Reporter and Retriever rely on this configuration service.
38 */
39@Component(immediate = true)
40public class InfluxDbMetricsConfig {
41
42 private final Logger log = getLogger(getClass());
43
44 private static final String DEFAULT_ADDRESS = "localhost";
45 private static final int DEFAULT_PORT = 8086;
Jian Lie1d97c92016-03-22 10:21:44 -070046 private static final String DEFAULT_DATABASE = "onos";
47 private static final String DEFAULT_USERNAME = "onos";
48 private static final String DEFAULT_PASSWORD = "onos.password";
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected CoreService coreService;
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected InfluxDbMetricsReporter influxDbMetricsReporter;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jian Libdfd37f2016-03-24 16:36:35 -070057 protected InfluxDbMetricsRetriever influxDbMetricsRetriever;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jian Lie1d97c92016-03-22 10:21:44 -070060 protected ComponentConfigService cfgService;
61
62 @Property(name = "address", value = DEFAULT_ADDRESS,
63 label = "IP address of influxDB server; default is localhost")
64 protected String address = DEFAULT_ADDRESS;
65
66 @Property(name = "port", intValue = DEFAULT_PORT,
67 label = "Port number of influxDB server; default is 8086")
68 protected int port = DEFAULT_PORT;
69
70 @Property(name = "database", value = DEFAULT_DATABASE,
71 label = "Database name of influxDB server; default is onos")
72 protected String database = DEFAULT_DATABASE;
73
74 @Property(name = "username", value = DEFAULT_USERNAME,
75 label = "Username of influxDB server; default is onos")
76 protected String username = DEFAULT_USERNAME;
77
78 @Property(name = "password", value = DEFAULT_PASSWORD,
79 label = "Password of influxDB server; default is onos.password")
80 protected String password = DEFAULT_PASSWORD;
81
82 @Activate
83 public void activate() {
84 cfgService.registerProperties(getClass());
85
86 coreService.registerApplication("org.onosproject.influxdbmetrics");
87
88 configReporter(influxDbMetricsReporter);
89 influxDbMetricsReporter.startReport();
90
Jian Libdfd37f2016-03-24 16:36:35 -070091 configRetriever(influxDbMetricsRetriever);
92
Jian Lie1d97c92016-03-22 10:21:44 -070093 log.info("Started");
94 }
95
96 @Deactivate
97 public void deactivate() {
98 cfgService.unregisterProperties(getClass(), false);
99
100 influxDbMetricsReporter.stopReport();
101 log.info("Stopped");
102 }
103
104 @Modified
105 public void modified(ComponentContext context) {
106 readComponentConfiguration(context);
107
108 configReporter(influxDbMetricsReporter);
109 influxDbMetricsReporter.restartReport();
Jian Libdfd37f2016-03-24 16:36:35 -0700110
111 configRetriever(influxDbMetricsRetriever);
Jian Lie1d97c92016-03-22 10:21:44 -0700112 }
113
114 private void configReporter(InfluxDbMetricsReporter reporter) {
115 reporter.config(address, port, database, username, password);
116 }
117
Jian Libdfd37f2016-03-24 16:36:35 -0700118 private void configRetriever(InfluxDbMetricsRetriever retriever) {
119 retriever.config(address, port, database, username, password);
120 }
121
Jian Lie1d97c92016-03-22 10:21:44 -0700122 /**
123 * Extracts properties from the component configuration context.
124 *
125 * @param context the component context
126 */
127 private void readComponentConfiguration(ComponentContext context) {
128 Dictionary<?, ?> properties = context.getProperties();
129
130 String addressStr = Tools.get(properties, "address");
131 address = addressStr != null ? addressStr : DEFAULT_ADDRESS;
132 log.info("Configured. InfluxDB server address is {}", address);
133
134 String databaseStr = Tools.get(properties, "database");
135 database = databaseStr != null ? databaseStr : DEFAULT_DATABASE;
136 log.info("Configured. InfluxDB server database is {}", database);
137
138 String usernameStr = Tools.get(properties, "username");
139 username = usernameStr != null ? usernameStr : DEFAULT_USERNAME;
140 log.info("Configured. InfluxDB server username is {}", username);
141
142 String passwordStr = Tools.get(properties, "password");
143 password = passwordStr != null ? passwordStr : DEFAULT_PASSWORD;
144 log.info("Configured. InfluxDB server password is {}", password);
145
146 Integer portConfigured = Tools.getIntegerProperty(properties, "port");
147 if (portConfigured == null) {
148 port = DEFAULT_PORT;
149 log.info("InfluxDB port is not configured, default value is {}", port);
150 } else {
151 port = portConfigured;
152 log.info("Configured. InfluxDB port is configured to {}", port);
153 }
154 }
155}