blob: e3db9473cc84bc157559df045479c32322775844 [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
Jian Lid1ce10a2018-06-12 13:47:23 +090036import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE;
Jian Li4df75b12018-06-07 22:11:04 +090037import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_DATABASE;
38import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_ENABLE_BATCH;
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090039import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_MEASUREMENT;
Jian Li4df75b12018-06-07 22:11:04 +090040import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_PASSWORD;
41import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_SERVER_IP;
42import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_SERVER_PORT;
43import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_USERNAME;
44import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Jian Li3ed7f302018-08-27 17:16:27 +090045import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService;
Jian Lib9fe3492018-06-07 17:19:07 +090046
47/**
48 * InfluxDB server configuration manager for publishing openstack telemetry.
49 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050@Component(immediate = true, service = InfluxDbTelemetryConfigService.class)
Jian Lib9fe3492018-06-07 17:19:07 +090051public class InfluxDbTelemetryConfigManager implements InfluxDbTelemetryConfigService {
52
Jian Li4df75b12018-06-07 22:11:04 +090053 private final Logger log = LoggerFactory.getLogger(getClass());
54
Jian Lid1ce10a2018-06-12 13:47:23 +090055 private static final String ENABLE_SERVICE = "enableService";
Jian Li4df75b12018-06-07 22:11:04 +090056 private static final String ADDRESS = "address";
57 private static final String PORT = "port";
58 private static final String USERNAME = "username";
59 private static final String PASSWORD = "password";
60 private static final String DATABASE = "database";
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090061 private static final String MEASUREMENT = "measurement";
Jian Li4df75b12018-06-07 22:11:04 +090062 private static final String ENABLE_BATCH = "enableBatch";
63
Ray Milkeyd84f89b2018-08-17 14:54:17 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090065 protected ComponentConfigService componentConfigService;
66
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090068 protected InfluxDbTelemetryAdminService influxDbTelemetryAdminService;
69
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070 //@Property(name = ADDRESS, value = DEFAULT_INFLUXDB_SERVER_IP,
71 // label = "Default IP address to establish initial connection to InfluxDB server")
Jian Li4df75b12018-06-07 22:11:04 +090072 protected String address = DEFAULT_INFLUXDB_SERVER_IP;
73
Ray Milkeyd84f89b2018-08-17 14:54:17 -070074 //@Property(name = PORT, intValue = DEFAULT_INFLUXDB_SERVER_PORT,
75 // label = "Default port number to establish initial connection to InfluxDB server")
Jian Li4df75b12018-06-07 22:11:04 +090076 protected Integer port = DEFAULT_INFLUXDB_SERVER_PORT;
77
Ray Milkeyd84f89b2018-08-17 14:54:17 -070078 //@Property(name = USERNAME, value = DEFAULT_INFLUXDB_USERNAME,
79 // label = "Username used for authenticating against InfluxDB server")
Jian Li4df75b12018-06-07 22:11:04 +090080 protected String username = DEFAULT_INFLUXDB_USERNAME;
81
Ray Milkeyd84f89b2018-08-17 14:54:17 -070082 //@Property(name = PASSWORD, value = DEFAULT_INFLUXDB_PASSWORD,
83 // label = "Password used for authenticating against InfluxDB server")
Jian Li4df75b12018-06-07 22:11:04 +090084 protected String password = DEFAULT_INFLUXDB_PASSWORD;
85
Ray Milkeyd84f89b2018-08-17 14:54:17 -070086 //@Property(name = DATABASE, value = DEFAULT_INFLUXDB_DATABASE,
87 // label = "Database of InfluxDB server")
Jian Li4df75b12018-06-07 22:11:04 +090088 protected String database = DEFAULT_INFLUXDB_DATABASE;
89
Ray Milkeyd84f89b2018-08-17 14:54:17 -070090 //@Property(name = MEASUREMENT, value = DEFAULT_INFLUXDB_MEASUREMENT,
91 // label = "Measurement of InfluxDB server")
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090092 protected String measurement = DEFAULT_INFLUXDB_MEASUREMENT;
93
Ray Milkeyd84f89b2018-08-17 14:54:17 -070094 //@Property(name = ENABLE_BATCH, boolValue = DEFAULT_INFLUXDB_ENABLE_BATCH,
95 // label = "Flag value of enabling batch mode of InfluxDB server")
Jian Li4df75b12018-06-07 22:11:04 +090096 protected Boolean enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH;
97
Ray Milkeyd84f89b2018-08-17 14:54:17 -070098 //@Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
99 // label = "Specify the default behavior of telemetry service")
Jian Lid1ce10a2018-06-12 13:47:23 +0900100 protected Boolean enableService = DEFAULT_DISABLE;
101
Jian Li4df75b12018-06-07 22:11:04 +0900102 @Activate
103 protected void activate(ComponentContext context) {
104 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +0900105
106 if (enableService) {
107 influxDbTelemetryAdminService.start(getConfig());
108 }
Jian Li4df75b12018-06-07 22:11:04 +0900109 log.info("Started");
110 }
111
112 @Deactivate
113 protected void deactivate() {
114 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900115
116 if (enableService) {
117 influxDbTelemetryAdminService.stop();
118 }
Jian Li4df75b12018-06-07 22:11:04 +0900119 log.info("Stopped");
120 }
121
122 @Modified
123 private void modified(ComponentContext context) {
124 readComponentConfiguration(context);
Jian Li3ed7f302018-08-27 17:16:27 +0900125 initTelemetryService(influxDbTelemetryAdminService, getConfig(), enableService);
Jian Li4df75b12018-06-07 22:11:04 +0900126 log.info("Modified");
127 }
128
Jian Lib9fe3492018-06-07 17:19:07 +0900129 @Override
130 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900131 return new DefaultInfluxDbTelemetryConfig.DefaultBuilder()
132 .withAddress(address)
133 .withPort(port)
134 .withUsername(username)
135 .withPassword(password)
136 .withDatabase(database)
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900137 .withMeasurement(measurement)
Jian Li4df75b12018-06-07 22:11:04 +0900138 .withEnableBatch(enableBatch)
139 .build();
140 }
141
142 /**
143 * Extracts properties from the component configuration context.
144 *
145 * @param context the component context
146 */
147 private void readComponentConfiguration(ComponentContext context) {
148 Dictionary<?, ?> properties = context.getProperties();
149
150 String addressStr = Tools.get(properties, ADDRESS);
151 address = addressStr != null ? addressStr : DEFAULT_INFLUXDB_SERVER_IP;
152 log.info("Configured. InfluxDB server address is {}", address);
153
154 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
155 if (portConfigured == null) {
156 port = DEFAULT_INFLUXDB_SERVER_PORT;
157 log.info("InfluxDB server port is NOT configured, default value is {}", port);
158 } else {
159 port = portConfigured;
160 log.info("Configured. InfluxDB server port is {}", port);
161 }
162
163 String usernameStr = Tools.get(properties, USERNAME);
164 username = usernameStr != null ? usernameStr : DEFAULT_INFLUXDB_USERNAME;
165 log.info("Configured. InfluxDB server username is {}", username);
166
167 String passwordStr = Tools.get(properties, PASSWORD);
168 password = passwordStr != null ? passwordStr : DEFAULT_INFLUXDB_PASSWORD;
169 log.info("Configured. InfluxDB server password is {}", password);
170
171 String databaseStr = Tools.get(properties, DATABASE);
172 database = databaseStr != null ? databaseStr : DEFAULT_INFLUXDB_DATABASE;
173 log.info("Configured. InfluxDB server database is {}", database);
174
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900175 String measurementStr = Tools.get(properties, MEASUREMENT);
176 measurement = measurementStr != null ? measurementStr : DEFAULT_INFLUXDB_MEASUREMENT;
177 log.info("Configured. InfluxDB server measurement is {}", measurement);
178
Jian Li4df75b12018-06-07 22:11:04 +0900179 Boolean enableBatchConfigured = getBooleanProperty(properties, ENABLE_BATCH);
180 if (enableBatchConfigured == null) {
181 enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH;
182 log.info("InfluxDB server enable batch flag is " +
183 "NOT configured, default value is {}", enableBatch);
184 } else {
185 enableBatch = enableBatchConfigured;
186 log.info("Configured. InfluxDB server enable batch is {}", enableBatch);
187 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900188
189 Boolean enableServiceConfigured =
190 getBooleanProperty(properties, ENABLE_SERVICE);
191 if (enableServiceConfigured == null) {
192 enableService = DEFAULT_DISABLE;
193 log.info("InfluxDB service enable flag is NOT " +
194 "configured, default value is {}", enableService);
195 } else {
196 enableService = enableServiceConfigured;
197 log.info("Configured. InfluxDB service enable flag is {}", enableService);
198 }
Jian Lib9fe3492018-06-07 17:19:07 +0900199 }
200}