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 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 36 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_DATABASE; |
| 37 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_DATABASE_DEFAULT; |
| 38 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_ENABLE_BATCH; |
| 39 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_ENABLE_BATCH_DEFAULT; |
| 40 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_ENABLE_SERVICE; |
| 41 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_ENABLE_SERVICE_DEFAULT; |
| 42 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_MEASUREMENT; |
| 43 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_MEASUREMENT_DEFAULT; |
| 44 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_PASSWORD; |
| 45 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_PASSWORD_DEFAULT; |
| 46 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_SERVER_ADDRESS; |
| 47 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_SERVER_ADDRESS_DEFAULT; |
| 48 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_SERVER_PORT; |
| 49 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_SERVER_PORT_DEFAULT; |
| 50 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_USERNAME; |
| 51 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_INFLUXDB_USERNAME_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 52 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 53 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * InfluxDB server configuration manager for publishing openstack telemetry. |
| 57 | */ |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 58 | @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 Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 72 | public class InfluxDbTelemetryConfigManager implements InfluxDbTelemetryConfigService { |
| 73 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 74 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 75 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 77 | protected ComponentConfigService componentConfigService; |
| 78 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 80 | protected InfluxDbTelemetryAdminService influxDbTelemetryAdminService; |
| 81 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 82 | /** Default IP address to establish initial connection to InfluxDB server. */ |
| 83 | protected String address = PROP_INFLUXDB_SERVER_ADDRESS_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 84 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 85 | /** Default port number to establish initial connection to InfluxDB server. */ |
| 86 | protected Integer port = PROP_INFLUXDB_SERVER_PORT_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 87 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 88 | /** Username used for authenticating against InfluxDB server. */ |
| 89 | protected String username = PROP_INFLUXDB_USERNAME_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 90 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 91 | /** Password used for authenticating against InfluxDB server. */ |
| 92 | protected String password = PROP_INFLUXDB_PASSWORD_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 93 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 94 | /** Database of InfluxDB server. */ |
| 95 | protected String database = PROP_INFLUXDB_DATABASE_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 96 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 97 | /** Measurement of InfluxDB server. */ |
| 98 | protected String measurement = PROP_INFLUXDB_MEASUREMENT_DEFAULT; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 99 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 100 | /** Flag value of enabling batch mode of InfluxDB server. */ |
| 101 | protected Boolean enableBatch = PROP_INFLUXDB_ENABLE_SERVICE_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 102 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 103 | /** Specify the default behavior of telemetry service. */ |
| 104 | protected Boolean enableService = PROP_INFLUXDB_ENABLE_SERVICE_DEFAULT; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 105 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 106 | @Activate |
| 107 | protected void activate(ComponentContext context) { |
| 108 | componentConfigService.registerProperties(getClass()); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 109 | |
| 110 | if (enableService) { |
| 111 | influxDbTelemetryAdminService.start(getConfig()); |
| 112 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 113 | log.info("Started"); |
| 114 | } |
| 115 | |
| 116 | @Deactivate |
| 117 | protected void deactivate() { |
| 118 | componentConfigService.unregisterProperties(getClass(), false); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 119 | |
| 120 | if (enableService) { |
| 121 | influxDbTelemetryAdminService.stop(); |
| 122 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 123 | log.info("Stopped"); |
| 124 | } |
| 125 | |
| 126 | @Modified |
| 127 | private void modified(ComponentContext context) { |
| 128 | readComponentConfiguration(context); |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 129 | initTelemetryService(influxDbTelemetryAdminService, getConfig(), enableService); |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 130 | log.info("Modified"); |
| 131 | } |
| 132 | |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 133 | @Override |
| 134 | public TelemetryConfig getConfig() { |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 135 | return new DefaultInfluxDbTelemetryConfig.DefaultBuilder() |
| 136 | .withAddress(address) |
| 137 | .withPort(port) |
| 138 | .withUsername(username) |
| 139 | .withPassword(password) |
| 140 | .withDatabase(database) |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 141 | .withMeasurement(measurement) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 142 | .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 Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 154 | String addressStr = Tools.get(properties, PROP_INFLUXDB_SERVER_ADDRESS); |
| 155 | address = addressStr != null ? addressStr : PROP_INFLUXDB_SERVER_ADDRESS_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 156 | log.info("Configured. InfluxDB server address is {}", address); |
| 157 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 158 | Integer portConfigured = Tools.getIntegerProperty(properties, PROP_INFLUXDB_SERVER_PORT); |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 159 | if (portConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 160 | port = PROP_INFLUXDB_SERVER_PORT_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 161 | 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 Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 167 | String usernameStr = Tools.get(properties, PROP_INFLUXDB_USERNAME); |
| 168 | username = usernameStr != null ? usernameStr : PROP_INFLUXDB_USERNAME_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 169 | log.info("Configured. InfluxDB server username is {}", username); |
| 170 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 171 | String passwordStr = Tools.get(properties, PROP_INFLUXDB_PASSWORD); |
| 172 | password = passwordStr != null ? passwordStr : PROP_INFLUXDB_PASSWORD_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 173 | log.info("Configured. InfluxDB server password is {}", password); |
| 174 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 175 | String databaseStr = Tools.get(properties, PROP_INFLUXDB_DATABASE); |
| 176 | database = databaseStr != null ? databaseStr : PROP_INFLUXDB_DATABASE_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 177 | log.info("Configured. InfluxDB server database is {}", database); |
| 178 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 179 | String measurementStr = Tools.get(properties, PROP_INFLUXDB_MEASUREMENT); |
| 180 | measurement = measurementStr != null ? measurementStr : PROP_INFLUXDB_MEASUREMENT_DEFAULT; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 181 | log.info("Configured. InfluxDB server measurement is {}", measurement); |
| 182 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 183 | Boolean enableBatchConfigured = getBooleanProperty(properties, PROP_INFLUXDB_ENABLE_BATCH); |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 184 | if (enableBatchConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 185 | enableBatch = PROP_INFLUXDB_ENABLE_BATCH_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 186 | 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 Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 192 | |
| 193 | Boolean enableServiceConfigured = |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 194 | getBooleanProperty(properties, PROP_INFLUXDB_ENABLE_SERVICE); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 195 | if (enableServiceConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 196 | enableService = PROP_INFLUXDB_ENABLE_SERVICE_DEFAULT; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 197 | 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 Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 203 | } |
| 204 | } |