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.apache.felix.scr.annotations.Activate; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Component; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Service; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 26 | import org.onlab.util.Tools; |
| 27 | import org.onosproject.cfg.ComponentConfigService; |
| 28 | import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryAdminService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 29 | import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryConfigService; |
| 30 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 31 | import org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig; |
| 32 | import org.osgi.service.component.ComponentContext; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.util.Dictionary; |
| 37 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 38 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 39 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_DATABASE; |
| 40 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_ENABLE_BATCH; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame^] | 41 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_MEASUREMENT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 42 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_PASSWORD; |
| 43 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_SERVER_IP; |
| 44 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_SERVER_PORT; |
| 45 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_USERNAME; |
| 46 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * InfluxDB server configuration manager for publishing openstack telemetry. |
| 50 | */ |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 51 | @Component(immediate = true) |
| 52 | @Service |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 53 | public class InfluxDbTelemetryConfigManager implements InfluxDbTelemetryConfigService { |
| 54 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 55 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 56 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 57 | private static final String ENABLE_SERVICE = "enableService"; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 58 | private static final String ADDRESS = "address"; |
| 59 | private static final String PORT = "port"; |
| 60 | private static final String USERNAME = "username"; |
| 61 | private static final String PASSWORD = "password"; |
| 62 | private static final String DATABASE = "database"; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame^] | 63 | private static final String MEASUREMENT = "measurement"; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 64 | private static final String ENABLE_BATCH = "enableBatch"; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected ComponentConfigService componentConfigService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected InfluxDbTelemetryAdminService influxDbTelemetryAdminService; |
| 71 | |
| 72 | @Property(name = ADDRESS, value = DEFAULT_INFLUXDB_SERVER_IP, |
| 73 | label = "Default IP address to establish initial connection to InfluxDB server") |
| 74 | protected String address = DEFAULT_INFLUXDB_SERVER_IP; |
| 75 | |
| 76 | @Property(name = PORT, intValue = DEFAULT_INFLUXDB_SERVER_PORT, |
| 77 | label = "Default port number to establish initial connection to InfluxDB server") |
| 78 | protected Integer port = DEFAULT_INFLUXDB_SERVER_PORT; |
| 79 | |
| 80 | @Property(name = USERNAME, value = DEFAULT_INFLUXDB_USERNAME, |
| 81 | label = "Username used for authenticating against InfluxDB server") |
| 82 | protected String username = DEFAULT_INFLUXDB_USERNAME; |
| 83 | |
| 84 | @Property(name = PASSWORD, value = DEFAULT_INFLUXDB_PASSWORD, |
| 85 | label = "Password used for authenticating against InfluxDB server") |
| 86 | protected String password = DEFAULT_INFLUXDB_PASSWORD; |
| 87 | |
| 88 | @Property(name = DATABASE, value = DEFAULT_INFLUXDB_DATABASE, |
| 89 | label = "Database of InfluxDB server") |
| 90 | protected String database = DEFAULT_INFLUXDB_DATABASE; |
| 91 | |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame^] | 92 | @Property(name = MEASUREMENT, value = DEFAULT_INFLUXDB_MEASUREMENT, |
| 93 | label = "Measurement of InfluxDB server") |
| 94 | protected String measurement = DEFAULT_INFLUXDB_MEASUREMENT; |
| 95 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 96 | @Property(name = ENABLE_BATCH, boolValue = DEFAULT_INFLUXDB_ENABLE_BATCH, |
| 97 | label = "Flag value of enabling batch mode of InfluxDB server") |
| 98 | protected Boolean enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH; |
| 99 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 100 | @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE, |
| 101 | label = "Specify the default behavior of telemetry service") |
| 102 | protected Boolean enableService = DEFAULT_DISABLE; |
| 103 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 104 | @Activate |
| 105 | protected void activate(ComponentContext context) { |
| 106 | componentConfigService.registerProperties(getClass()); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 107 | |
| 108 | if (enableService) { |
| 109 | influxDbTelemetryAdminService.start(getConfig()); |
| 110 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 111 | log.info("Started"); |
| 112 | } |
| 113 | |
| 114 | @Deactivate |
| 115 | protected void deactivate() { |
| 116 | componentConfigService.unregisterProperties(getClass(), false); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 117 | |
| 118 | if (enableService) { |
| 119 | influxDbTelemetryAdminService.stop(); |
| 120 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 121 | log.info("Stopped"); |
| 122 | } |
| 123 | |
| 124 | @Modified |
| 125 | private void modified(ComponentContext context) { |
| 126 | readComponentConfiguration(context); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 127 | |
| 128 | if (enableService) { |
| 129 | if (influxDbTelemetryAdminService.isRunning()) { |
| 130 | influxDbTelemetryAdminService.restart(getConfig()); |
| 131 | } else { |
| 132 | influxDbTelemetryAdminService.start(getConfig()); |
| 133 | } |
| 134 | } else { |
| 135 | if (influxDbTelemetryAdminService.isRunning()) { |
| 136 | influxDbTelemetryAdminService.stop(); |
| 137 | } |
| 138 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 139 | log.info("Modified"); |
| 140 | } |
| 141 | |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 142 | @Override |
| 143 | public TelemetryConfig getConfig() { |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 144 | return new DefaultInfluxDbTelemetryConfig.DefaultBuilder() |
| 145 | .withAddress(address) |
| 146 | .withPort(port) |
| 147 | .withUsername(username) |
| 148 | .withPassword(password) |
| 149 | .withDatabase(database) |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame^] | 150 | .withMeasurement(measurement) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 151 | .withEnableBatch(enableBatch) |
| 152 | .build(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Extracts properties from the component configuration context. |
| 157 | * |
| 158 | * @param context the component context |
| 159 | */ |
| 160 | private void readComponentConfiguration(ComponentContext context) { |
| 161 | Dictionary<?, ?> properties = context.getProperties(); |
| 162 | |
| 163 | String addressStr = Tools.get(properties, ADDRESS); |
| 164 | address = addressStr != null ? addressStr : DEFAULT_INFLUXDB_SERVER_IP; |
| 165 | log.info("Configured. InfluxDB server address is {}", address); |
| 166 | |
| 167 | Integer portConfigured = Tools.getIntegerProperty(properties, PORT); |
| 168 | if (portConfigured == null) { |
| 169 | port = DEFAULT_INFLUXDB_SERVER_PORT; |
| 170 | log.info("InfluxDB server port is NOT configured, default value is {}", port); |
| 171 | } else { |
| 172 | port = portConfigured; |
| 173 | log.info("Configured. InfluxDB server port is {}", port); |
| 174 | } |
| 175 | |
| 176 | String usernameStr = Tools.get(properties, USERNAME); |
| 177 | username = usernameStr != null ? usernameStr : DEFAULT_INFLUXDB_USERNAME; |
| 178 | log.info("Configured. InfluxDB server username is {}", username); |
| 179 | |
| 180 | String passwordStr = Tools.get(properties, PASSWORD); |
| 181 | password = passwordStr != null ? passwordStr : DEFAULT_INFLUXDB_PASSWORD; |
| 182 | log.info("Configured. InfluxDB server password is {}", password); |
| 183 | |
| 184 | String databaseStr = Tools.get(properties, DATABASE); |
| 185 | database = databaseStr != null ? databaseStr : DEFAULT_INFLUXDB_DATABASE; |
| 186 | log.info("Configured. InfluxDB server database is {}", database); |
| 187 | |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame^] | 188 | String measurementStr = Tools.get(properties, MEASUREMENT); |
| 189 | measurement = measurementStr != null ? measurementStr : DEFAULT_INFLUXDB_MEASUREMENT; |
| 190 | log.info("Configured. InfluxDB server measurement is {}", measurement); |
| 191 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 192 | Boolean enableBatchConfigured = getBooleanProperty(properties, ENABLE_BATCH); |
| 193 | if (enableBatchConfigured == null) { |
| 194 | enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH; |
| 195 | log.info("InfluxDB server enable batch flag is " + |
| 196 | "NOT configured, default value is {}", enableBatch); |
| 197 | } else { |
| 198 | enableBatch = enableBatchConfigured; |
| 199 | log.info("Configured. InfluxDB server enable batch is {}", enableBatch); |
| 200 | } |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 201 | |
| 202 | Boolean enableServiceConfigured = |
| 203 | getBooleanProperty(properties, ENABLE_SERVICE); |
| 204 | if (enableServiceConfigured == null) { |
| 205 | enableService = DEFAULT_DISABLE; |
| 206 | log.info("InfluxDB service enable flag is NOT " + |
| 207 | "configured, default value is {}", enableService); |
| 208 | } else { |
| 209 | enableService = enableServiceConfigured; |
| 210 | log.info("Configured. InfluxDB service enable flag is {}", enableService); |
| 211 | } |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 212 | } |
| 213 | } |