blob: c5301df8a5dea476cdbecdcb72a81803fcf02605 [file] [log] [blame]
Jian Lie1d97c92016-03-22 10:21:44 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
Jian Lie1d97c92016-03-22 10:21:44 -070018import org.onlab.util.Tools;
19import org.onosproject.cfg.ComponentConfigService;
20import org.onosproject.core.CoreService;
21import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.osgi.service.component.annotations.Activate;
23import org.osgi.service.component.annotations.Component;
24import org.osgi.service.component.annotations.Deactivate;
25import org.osgi.service.component.annotations.Modified;
26import org.osgi.service.component.annotations.Reference;
27import org.osgi.service.component.annotations.ReferenceCardinality;
Jian Lie1d97c92016-03-22 10:21:44 -070028import org.slf4j.Logger;
29
30import java.util.Dictionary;
31
32import static org.slf4j.LoggerFactory.getLogger;
33
34/**
35 * A configuration service for InfluxDB metrics.
36 * Both InfluxDbMetrics Reporter and Retriever rely on this configuration service.
37 */
38@Component(immediate = true)
39public class InfluxDbMetricsConfig {
40
41 private final Logger log = getLogger(getClass());
42
43 private static final String DEFAULT_ADDRESS = "localhost";
44 private static final int DEFAULT_PORT = 8086;
Jian Lie1d97c92016-03-22 10:21:44 -070045 private static final String DEFAULT_DATABASE = "onos";
46 private static final String DEFAULT_USERNAME = "onos";
47 private static final String DEFAULT_PASSWORD = "onos.password";
48
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lie1d97c92016-03-22 10:21:44 -070050 protected CoreService coreService;
51
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lie1d97c92016-03-22 10:21:44 -070053 protected InfluxDbMetricsReporter influxDbMetricsReporter;
54
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Libdfd37f2016-03-24 16:36:35 -070056 protected InfluxDbMetricsRetriever influxDbMetricsRetriever;
57
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lie1d97c92016-03-22 10:21:44 -070059 protected ComponentConfigService cfgService;
60
Ray Milkeyd84f89b2018-08-17 14:54:17 -070061 //@Property(name = "address", value = DEFAULT_ADDRESS,
62 // label = "IP address of influxDB server; default is localhost")
Jian Lie1d97c92016-03-22 10:21:44 -070063 protected String address = DEFAULT_ADDRESS;
64
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065 //@Property(name = "port", intValue = DEFAULT_PORT,
66 // label = "Port number of influxDB server; default is 8086")
Jian Lie1d97c92016-03-22 10:21:44 -070067 protected int port = DEFAULT_PORT;
68
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069 //@Property(name = "database", value = DEFAULT_DATABASE,
70 // label = "Database name of influxDB server; default is onos")
Jian Lie1d97c92016-03-22 10:21:44 -070071 protected String database = DEFAULT_DATABASE;
72
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073 //@Property(name = "username", value = DEFAULT_USERNAME,
74 // label = "Username of influxDB server; default is onos")
Jian Lie1d97c92016-03-22 10:21:44 -070075 protected String username = DEFAULT_USERNAME;
76
Ray Milkeyd84f89b2018-08-17 14:54:17 -070077 //@Property(name = "password", value = DEFAULT_PASSWORD,
78 // label = "Password of influxDB server; default is onos.password")
Jian Lie1d97c92016-03-22 10:21:44 -070079 protected String password = DEFAULT_PASSWORD;
80
81 @Activate
82 public void activate() {
83 cfgService.registerProperties(getClass());
84
85 coreService.registerApplication("org.onosproject.influxdbmetrics");
86
87 configReporter(influxDbMetricsReporter);
Jian Libdfd37f2016-03-24 16:36:35 -070088 configRetriever(influxDbMetricsRetriever);
89
Jian Lie1d97c92016-03-22 10:21:44 -070090 log.info("Started");
91 }
92
93 @Deactivate
94 public void deactivate() {
95 cfgService.unregisterProperties(getClass(), false);
96
Jian Lie1d97c92016-03-22 10:21:44 -070097 log.info("Stopped");
98 }
99
100 @Modified
101 public void modified(ComponentContext context) {
102 readComponentConfiguration(context);
103
104 configReporter(influxDbMetricsReporter);
105 influxDbMetricsReporter.restartReport();
Jian Libdfd37f2016-03-24 16:36:35 -0700106
107 configRetriever(influxDbMetricsRetriever);
Jian Lie1d97c92016-03-22 10:21:44 -0700108 }
109
110 private void configReporter(InfluxDbMetricsReporter reporter) {
111 reporter.config(address, port, database, username, password);
112 }
113
Jian Libdfd37f2016-03-24 16:36:35 -0700114 private void configRetriever(InfluxDbMetricsRetriever retriever) {
115 retriever.config(address, port, database, username, password);
116 }
117
Jian Lie1d97c92016-03-22 10:21:44 -0700118 /**
119 * Extracts properties from the component configuration context.
120 *
121 * @param context the component context
122 */
123 private void readComponentConfiguration(ComponentContext context) {
124 Dictionary<?, ?> properties = context.getProperties();
125
126 String addressStr = Tools.get(properties, "address");
127 address = addressStr != null ? addressStr : DEFAULT_ADDRESS;
128 log.info("Configured. InfluxDB server address is {}", address);
129
130 String databaseStr = Tools.get(properties, "database");
131 database = databaseStr != null ? databaseStr : DEFAULT_DATABASE;
132 log.info("Configured. InfluxDB server database is {}", database);
133
134 String usernameStr = Tools.get(properties, "username");
135 username = usernameStr != null ? usernameStr : DEFAULT_USERNAME;
136 log.info("Configured. InfluxDB server username is {}", username);
137
138 String passwordStr = Tools.get(properties, "password");
139 password = passwordStr != null ? passwordStr : DEFAULT_PASSWORD;
140 log.info("Configured. InfluxDB server password is {}", password);
141
142 Integer portConfigured = Tools.getIntegerProperty(properties, "port");
143 if (portConfigured == null) {
144 port = DEFAULT_PORT;
145 log.info("InfluxDB port is not configured, default value is {}", port);
146 } else {
147 port = portConfigured;
148 log.info("Configured. InfluxDB port is configured to {}", port);
149 }
150 }
151}