Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacktelemetry.impl; |
| 17 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 18 | import org.onlab.util.Tools; |
| 19 | import org.onosproject.cfg.ComponentConfigService; |
| 20 | import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryAdminService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 21 | import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryConfigService; |
| 22 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 23 | import org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig; |
| 24 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 25 | import org.osgi.service.component.annotations.Activate; |
| 26 | import org.osgi.service.component.annotations.Component; |
| 27 | import org.osgi.service.component.annotations.Deactivate; |
| 28 | import org.osgi.service.component.annotations.Modified; |
| 29 | import org.osgi.service.component.annotations.Reference; |
| 30 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import java.util.Dictionary; |
| 35 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 36 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 37 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_DATABASE; |
| 38 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_ENABLE_BATCH; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 39 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_MEASUREMENT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 40 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_PASSWORD; |
| 41 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_SERVER_IP; |
| 42 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_SERVER_PORT; |
| 43 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_USERNAME; |
| 44 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 45 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * InfluxDB server configuration manager for publishing openstack telemetry. |
| 49 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 50 | @Component(immediate = true, service = InfluxDbTelemetryConfigService.class) |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 51 | public class InfluxDbTelemetryConfigManager implements InfluxDbTelemetryConfigService { |
| 52 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 53 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 54 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 55 | private static final String ENABLE_SERVICE = "enableService"; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 56 | 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 Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 61 | private static final String MEASUREMENT = "measurement"; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 62 | private static final String ENABLE_BATCH = "enableBatch"; |
| 63 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 65 | protected ComponentConfigService componentConfigService; |
| 66 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 68 | protected InfluxDbTelemetryAdminService influxDbTelemetryAdminService; |
| 69 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 70 | //@Property(name = ADDRESS, value = DEFAULT_INFLUXDB_SERVER_IP, |
| 71 | // label = "Default IP address to establish initial connection to InfluxDB server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 72 | protected String address = DEFAULT_INFLUXDB_SERVER_IP; |
| 73 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 74 | //@Property(name = PORT, intValue = DEFAULT_INFLUXDB_SERVER_PORT, |
| 75 | // label = "Default port number to establish initial connection to InfluxDB server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 76 | protected Integer port = DEFAULT_INFLUXDB_SERVER_PORT; |
| 77 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 78 | //@Property(name = USERNAME, value = DEFAULT_INFLUXDB_USERNAME, |
| 79 | // label = "Username used for authenticating against InfluxDB server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 80 | protected String username = DEFAULT_INFLUXDB_USERNAME; |
| 81 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 82 | //@Property(name = PASSWORD, value = DEFAULT_INFLUXDB_PASSWORD, |
| 83 | // label = "Password used for authenticating against InfluxDB server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 84 | protected String password = DEFAULT_INFLUXDB_PASSWORD; |
| 85 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 86 | //@Property(name = DATABASE, value = DEFAULT_INFLUXDB_DATABASE, |
| 87 | // label = "Database of InfluxDB server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 88 | protected String database = DEFAULT_INFLUXDB_DATABASE; |
| 89 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 90 | //@Property(name = MEASUREMENT, value = DEFAULT_INFLUXDB_MEASUREMENT, |
| 91 | // label = "Measurement of InfluxDB server") |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 92 | protected String measurement = DEFAULT_INFLUXDB_MEASUREMENT; |
| 93 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 94 | //@Property(name = ENABLE_BATCH, boolValue = DEFAULT_INFLUXDB_ENABLE_BATCH, |
| 95 | // label = "Flag value of enabling batch mode of InfluxDB server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 96 | protected Boolean enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH; |
| 97 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 98 | //@Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE, |
| 99 | // label = "Specify the default behavior of telemetry service") |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 100 | protected Boolean enableService = DEFAULT_DISABLE; |
| 101 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 102 | @Activate |
| 103 | protected void activate(ComponentContext context) { |
| 104 | componentConfigService.registerProperties(getClass()); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 105 | |
| 106 | if (enableService) { |
| 107 | influxDbTelemetryAdminService.start(getConfig()); |
| 108 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 109 | log.info("Started"); |
| 110 | } |
| 111 | |
| 112 | @Deactivate |
| 113 | protected void deactivate() { |
| 114 | componentConfigService.unregisterProperties(getClass(), false); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 115 | |
| 116 | if (enableService) { |
| 117 | influxDbTelemetryAdminService.stop(); |
| 118 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 119 | log.info("Stopped"); |
| 120 | } |
| 121 | |
| 122 | @Modified |
| 123 | private void modified(ComponentContext context) { |
| 124 | readComponentConfiguration(context); |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 125 | initTelemetryService(influxDbTelemetryAdminService, getConfig(), enableService); |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 126 | log.info("Modified"); |
| 127 | } |
| 128 | |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 129 | @Override |
| 130 | public TelemetryConfig getConfig() { |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 131 | return new DefaultInfluxDbTelemetryConfig.DefaultBuilder() |
| 132 | .withAddress(address) |
| 133 | .withPort(port) |
| 134 | .withUsername(username) |
| 135 | .withPassword(password) |
| 136 | .withDatabase(database) |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 137 | .withMeasurement(measurement) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 138 | .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 Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 175 | 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 Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 179 | 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 Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 188 | |
| 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 Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 199 | } |
| 200 | } |