blob: 12038f2abb99a75531f913afdec88af600d3a035 [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
Ray Milkeyff74f062018-10-30 16:38:18 -070032import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.ADDRESS;
33import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.ADDRESS_DEFAULT;
34import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.DATABASE;
35import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.DATABASE_DEFAULT;
36import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.PASSWORD;
37import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.PASSWORD_DEFAULT;
38import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.PORT;
39import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.PORT_DEFAULT;
40import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.USERNAME;
41import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.USERNAME_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -070042import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * A configuration service for InfluxDB metrics.
46 * Both InfluxDbMetrics Reporter and Retriever rely on this configuration service.
47 */
Ray Milkeyff74f062018-10-30 16:38:18 -070048@Component(
49 immediate = true,
50 property = {
51 ADDRESS + "=" + ADDRESS_DEFAULT,
52 PORT + ":Integer=" + PORT_DEFAULT,
53 DATABASE + "=" + DATABASE_DEFAULT,
54 USERNAME + "=" + USERNAME_DEFAULT,
55 PASSWORD + "=" + PASSWORD_DEFAULT
56 }
57)
Jian Lie1d97c92016-03-22 10:21:44 -070058public class InfluxDbMetricsConfig {
59
60 private final Logger log = getLogger(getClass());
61
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lie1d97c92016-03-22 10:21:44 -070063 protected CoreService coreService;
64
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lie1d97c92016-03-22 10:21:44 -070066 protected InfluxDbMetricsReporter influxDbMetricsReporter;
67
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Libdfd37f2016-03-24 16:36:35 -070069 protected InfluxDbMetricsRetriever influxDbMetricsRetriever;
70
Ray Milkeyd84f89b2018-08-17 14:54:17 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lie1d97c92016-03-22 10:21:44 -070072 protected ComponentConfigService cfgService;
73
Ray Milkeyff74f062018-10-30 16:38:18 -070074 /** IP address of influxDB server; default is localhost. */
75 protected String address = ADDRESS_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -070076
Ray Milkeyff74f062018-10-30 16:38:18 -070077 /** Port number of influxDB server; default is 8086. */
78 protected int port = PORT_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -070079
Ray Milkeyff74f062018-10-30 16:38:18 -070080 /** Database name of influxDB server; default is onos. */
81 protected String database = DATABASE_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -070082
Ray Milkeyff74f062018-10-30 16:38:18 -070083 /** Username of influxDB server; default is onos. */
84 protected String username = USERNAME_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -070085
Ray Milkeyff74f062018-10-30 16:38:18 -070086 /** Password of influxDB server; default is onos.password. */
87 protected String password = PASSWORD_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -070088
89 @Activate
90 public void activate() {
91 cfgService.registerProperties(getClass());
92
93 coreService.registerApplication("org.onosproject.influxdbmetrics");
94
95 configReporter(influxDbMetricsReporter);
Jian Libdfd37f2016-03-24 16:36:35 -070096 configRetriever(influxDbMetricsRetriever);
97
Jian Lie1d97c92016-03-22 10:21:44 -070098 log.info("Started");
99 }
100
101 @Deactivate
102 public void deactivate() {
103 cfgService.unregisterProperties(getClass(), false);
104
Jian Lie1d97c92016-03-22 10:21:44 -0700105 log.info("Stopped");
106 }
107
108 @Modified
109 public void modified(ComponentContext context) {
110 readComponentConfiguration(context);
111
112 configReporter(influxDbMetricsReporter);
113 influxDbMetricsReporter.restartReport();
Jian Libdfd37f2016-03-24 16:36:35 -0700114
115 configRetriever(influxDbMetricsRetriever);
Jian Lie1d97c92016-03-22 10:21:44 -0700116 }
117
118 private void configReporter(InfluxDbMetricsReporter reporter) {
119 reporter.config(address, port, database, username, password);
120 }
121
Jian Libdfd37f2016-03-24 16:36:35 -0700122 private void configRetriever(InfluxDbMetricsRetriever retriever) {
123 retriever.config(address, port, database, username, password);
124 }
125
Jian Lie1d97c92016-03-22 10:21:44 -0700126 /**
127 * Extracts properties from the component configuration context.
128 *
129 * @param context the component context
130 */
131 private void readComponentConfiguration(ComponentContext context) {
132 Dictionary<?, ?> properties = context.getProperties();
133
Ray Milkeyff74f062018-10-30 16:38:18 -0700134 String addressStr = Tools.get(properties, ADDRESS);
135 address = addressStr != null ? addressStr : ADDRESS_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -0700136 log.info("Configured. InfluxDB server address is {}", address);
137
Ray Milkeyff74f062018-10-30 16:38:18 -0700138 String databaseStr = Tools.get(properties, DATABASE);
139 database = databaseStr != null ? databaseStr : DATABASE_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -0700140 log.info("Configured. InfluxDB server database is {}", database);
141
Ray Milkeyff74f062018-10-30 16:38:18 -0700142 String usernameStr = Tools.get(properties, USERNAME);
143 username = usernameStr != null ? usernameStr : USERNAME_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -0700144 log.info("Configured. InfluxDB server username is {}", username);
145
Ray Milkeyff74f062018-10-30 16:38:18 -0700146 String passwordStr = Tools.get(properties, PASSWORD);
147 password = passwordStr != null ? passwordStr : PASSWORD_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -0700148 log.info("Configured. InfluxDB server password is {}", password);
149
Ray Milkeyff74f062018-10-30 16:38:18 -0700150 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
Jian Lie1d97c92016-03-22 10:21:44 -0700151 if (portConfigured == null) {
Ray Milkeyff74f062018-10-30 16:38:18 -0700152 port = PORT_DEFAULT;
Jian Lie1d97c92016-03-22 10:21:44 -0700153 log.info("InfluxDB port is not configured, default value is {}", port);
154 } else {
155 port = portConfigured;
156 log.info("Configured. InfluxDB port is configured to {}", port);
157 }
158 }
159}