blob: d9107ca2650ee3803b47725eb6b67bb85ad00955 [file] [log] [blame]
Jian Lib9fe3492018-06-07 17:19:07 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
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.openstacktelemetry.impl;
17
Jian Li4df75b12018-06-07 22:11:04 +090018import org.onlab.util.Tools;
19import org.onosproject.cfg.ComponentConfigService;
20import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryAdminService;
Jian Lib9fe3492018-06-07 17:19:07 +090021import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryConfigService;
22import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li4df75b12018-06-07 22:11:04 +090023import org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig;
24import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.osgi.service.component.annotations.Activate;
26import org.osgi.service.component.annotations.Component;
27import org.osgi.service.component.annotations.Deactivate;
28import org.osgi.service.component.annotations.Modified;
29import org.osgi.service.component.annotations.Reference;
30import org.osgi.service.component.annotations.ReferenceCardinality;
Jian Li4df75b12018-06-07 22:11:04 +090031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import java.util.Dictionary;
35
Ray Milkey8e406512018-10-24 15:56:50 -070036import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_DATABASE;
37import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_DATABASE_DEFAULT;
38import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_ENABLE_BATCH;
39import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_ENABLE_BATCH_DEFAULT;
40import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_ENABLE_SERVICE;
41import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_ENABLE_SERVICE_DEFAULT;
42import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_MEASUREMENT;
43import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_MEASUREMENT_DEFAULT;
44import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_PASSWORD;
45import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_PASSWORD_DEFAULT;
46import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_SERVER_ADDRESS;
47import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_SERVER_ADDRESS_DEFAULT;
48import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_SERVER_PORT;
49import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_SERVER_PORT_DEFAULT;
50import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_USERNAME;
51import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_USERNAME_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090052import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Jian Li3ed7f302018-08-27 17:16:27 +090053import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService;
Jian Lib9fe3492018-06-07 17:19:07 +090054
55/**
56 * InfluxDB server configuration manager for publishing openstack telemetry.
57 */
Ray Milkey8e406512018-10-24 15:56:50 -070058@Component(
59 immediate = true,
60 service = InfluxDbTelemetryConfigService.class,
61 property = {
62 PROP_INFLUXDB_ENABLE_SERVICE + ":Boolean=" + PROP_INFLUXDB_ENABLE_SERVICE_DEFAULT,
63 PROP_INFLUXDB_SERVER_ADDRESS + "=" + PROP_INFLUXDB_SERVER_ADDRESS_DEFAULT,
64 PROP_INFLUXDB_SERVER_PORT + ":Integer=" + PROP_INFLUXDB_SERVER_PORT_DEFAULT,
65 PROP_INFLUXDB_USERNAME + "=" + PROP_INFLUXDB_USERNAME_DEFAULT,
66 PROP_INFLUXDB_PASSWORD + "=" + PROP_INFLUXDB_PASSWORD_DEFAULT,
67 PROP_INFLUXDB_DATABASE + "=" + PROP_INFLUXDB_DATABASE_DEFAULT,
68 PROP_INFLUXDB_MEASUREMENT + "=" + PROP_INFLUXDB_MEASUREMENT_DEFAULT,
69 PROP_INFLUXDB_ENABLE_BATCH + ":Boolean=" + PROP_INFLUXDB_ENABLE_BATCH_DEFAULT
70 }
71)
Jian Lib9fe3492018-06-07 17:19:07 +090072public class InfluxDbTelemetryConfigManager implements InfluxDbTelemetryConfigService {
73
Jian Li4df75b12018-06-07 22:11:04 +090074 private final Logger log = LoggerFactory.getLogger(getClass());
75
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090077 protected ComponentConfigService componentConfigService;
78
Ray Milkeyd84f89b2018-08-17 14:54:17 -070079 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090080 protected InfluxDbTelemetryAdminService influxDbTelemetryAdminService;
81
Ray Milkey8e406512018-10-24 15:56:50 -070082 /** Default IP address to establish initial connection to InfluxDB server. */
83 protected String address = PROP_INFLUXDB_SERVER_ADDRESS_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090084
Ray Milkey8e406512018-10-24 15:56:50 -070085 /** Default port number to establish initial connection to InfluxDB server. */
86 protected Integer port = PROP_INFLUXDB_SERVER_PORT_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090087
Ray Milkey8e406512018-10-24 15:56:50 -070088 /** Username used for authenticating against InfluxDB server. */
89 protected String username = PROP_INFLUXDB_USERNAME_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090090
Ray Milkey8e406512018-10-24 15:56:50 -070091 /** Password used for authenticating against InfluxDB server. */
92 protected String password = PROP_INFLUXDB_PASSWORD_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090093
Ray Milkey8e406512018-10-24 15:56:50 -070094 /** Database of InfluxDB server. */
95 protected String database = PROP_INFLUXDB_DATABASE_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090096
Ray Milkey8e406512018-10-24 15:56:50 -070097 /** Measurement of InfluxDB server. */
98 protected String measurement = PROP_INFLUXDB_MEASUREMENT_DEFAULT;
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090099
Ray Milkey8e406512018-10-24 15:56:50 -0700100 /** Flag value of enabling batch mode of InfluxDB server. */
101 protected Boolean enableBatch = PROP_INFLUXDB_ENABLE_SERVICE_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900102
Ray Milkey8e406512018-10-24 15:56:50 -0700103 /** Specify the default behavior of telemetry service. */
104 protected Boolean enableService = PROP_INFLUXDB_ENABLE_SERVICE_DEFAULT;
Jian Lid1ce10a2018-06-12 13:47:23 +0900105
Jian Li4df75b12018-06-07 22:11:04 +0900106 @Activate
107 protected void activate(ComponentContext context) {
108 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +0900109
110 if (enableService) {
111 influxDbTelemetryAdminService.start(getConfig());
112 }
Jian Li4df75b12018-06-07 22:11:04 +0900113 log.info("Started");
114 }
115
116 @Deactivate
117 protected void deactivate() {
118 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900119
120 if (enableService) {
121 influxDbTelemetryAdminService.stop();
122 }
Jian Li4df75b12018-06-07 22:11:04 +0900123 log.info("Stopped");
124 }
125
126 @Modified
127 private void modified(ComponentContext context) {
128 readComponentConfiguration(context);
Jian Li3ed7f302018-08-27 17:16:27 +0900129 initTelemetryService(influxDbTelemetryAdminService, getConfig(), enableService);
Jian Li4df75b12018-06-07 22:11:04 +0900130 log.info("Modified");
131 }
132
Jian Lib9fe3492018-06-07 17:19:07 +0900133 @Override
134 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900135 return new DefaultInfluxDbTelemetryConfig.DefaultBuilder()
136 .withAddress(address)
137 .withPort(port)
138 .withUsername(username)
139 .withPassword(password)
140 .withDatabase(database)
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900141 .withMeasurement(measurement)
Jian Li4df75b12018-06-07 22:11:04 +0900142 .withEnableBatch(enableBatch)
143 .build();
144 }
145
146 /**
147 * Extracts properties from the component configuration context.
148 *
149 * @param context the component context
150 */
151 private void readComponentConfiguration(ComponentContext context) {
152 Dictionary<?, ?> properties = context.getProperties();
153
Ray Milkey8e406512018-10-24 15:56:50 -0700154 String addressStr = Tools.get(properties, PROP_INFLUXDB_SERVER_ADDRESS);
155 address = addressStr != null ? addressStr : PROP_INFLUXDB_SERVER_ADDRESS_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900156 log.info("Configured. InfluxDB server address is {}", address);
157
Ray Milkey8e406512018-10-24 15:56:50 -0700158 Integer portConfigured = Tools.getIntegerProperty(properties, PROP_INFLUXDB_SERVER_PORT);
Jian Li4df75b12018-06-07 22:11:04 +0900159 if (portConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -0700160 port = PROP_INFLUXDB_SERVER_PORT_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900161 log.info("InfluxDB server port is NOT configured, default value is {}", port);
162 } else {
163 port = portConfigured;
164 log.info("Configured. InfluxDB server port is {}", port);
165 }
166
Ray Milkey8e406512018-10-24 15:56:50 -0700167 String usernameStr = Tools.get(properties, PROP_INFLUXDB_USERNAME);
168 username = usernameStr != null ? usernameStr : PROP_INFLUXDB_USERNAME_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900169 log.info("Configured. InfluxDB server username is {}", username);
170
Ray Milkey8e406512018-10-24 15:56:50 -0700171 String passwordStr = Tools.get(properties, PROP_INFLUXDB_PASSWORD);
172 password = passwordStr != null ? passwordStr : PROP_INFLUXDB_PASSWORD_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900173 log.info("Configured. InfluxDB server password is {}", password);
174
Ray Milkey8e406512018-10-24 15:56:50 -0700175 String databaseStr = Tools.get(properties, PROP_INFLUXDB_DATABASE);
176 database = databaseStr != null ? databaseStr : PROP_INFLUXDB_DATABASE_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900177 log.info("Configured. InfluxDB server database is {}", database);
178
Ray Milkey8e406512018-10-24 15:56:50 -0700179 String measurementStr = Tools.get(properties, PROP_INFLUXDB_MEASUREMENT);
180 measurement = measurementStr != null ? measurementStr : PROP_INFLUXDB_MEASUREMENT_DEFAULT;
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900181 log.info("Configured. InfluxDB server measurement is {}", measurement);
182
Ray Milkey8e406512018-10-24 15:56:50 -0700183 Boolean enableBatchConfigured = getBooleanProperty(properties, PROP_INFLUXDB_ENABLE_BATCH);
Jian Li4df75b12018-06-07 22:11:04 +0900184 if (enableBatchConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -0700185 enableBatch = PROP_INFLUXDB_ENABLE_BATCH_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900186 log.info("InfluxDB server enable batch flag is " +
187 "NOT configured, default value is {}", enableBatch);
188 } else {
189 enableBatch = enableBatchConfigured;
190 log.info("Configured. InfluxDB server enable batch is {}", enableBatch);
191 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900192
193 Boolean enableServiceConfigured =
Ray Milkey8e406512018-10-24 15:56:50 -0700194 getBooleanProperty(properties, PROP_INFLUXDB_ENABLE_SERVICE);
Jian Lid1ce10a2018-06-12 13:47:23 +0900195 if (enableServiceConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -0700196 enableService = PROP_INFLUXDB_ENABLE_SERVICE_DEFAULT;
Jian Lid1ce10a2018-06-12 13:47:23 +0900197 log.info("InfluxDB service enable flag is NOT " +
198 "configured, default value is {}", enableService);
199 } else {
200 enableService = enableServiceConfigured;
201 log.info("Configured. InfluxDB service enable flag is {}", enableService);
202 }
Jian Lib9fe3492018-06-07 17:19:07 +0900203 }
204}