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