Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 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.influxdbmetrics; |
| 17 | |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 18 | import org.onlab.util.Tools; |
| 19 | import org.onosproject.cfg.ComponentConfigService; |
| 20 | import org.onosproject.core.CoreService; |
| 21 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.osgi.service.component.annotations.Activate; |
| 23 | import org.osgi.service.component.annotations.Component; |
| 24 | import org.osgi.service.component.annotations.Deactivate; |
| 25 | import org.osgi.service.component.annotations.Modified; |
| 26 | import org.osgi.service.component.annotations.Reference; |
| 27 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import java.util.Dictionary; |
| 31 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 32 | import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.ADDRESS; |
| 33 | import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.ADDRESS_DEFAULT; |
| 34 | import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.DATABASE; |
| 35 | import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.DATABASE_DEFAULT; |
| 36 | import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.PASSWORD; |
| 37 | import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.PASSWORD_DEFAULT; |
| 38 | import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.PORT; |
| 39 | import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.PORT_DEFAULT; |
| 40 | import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.USERNAME; |
| 41 | import static org.onosproject.influxdbmetrics.OsgiPropertyConstants.USERNAME_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * A configuration service for InfluxDB metrics. |
| 46 | * Both InfluxDbMetrics Reporter and Retriever rely on this configuration service. |
| 47 | */ |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 48 | @Component( |
| 49 | immediate = true, |
| 50 | property = { |
| 51 | ADDRESS + "=" + ADDRESS_DEFAULT, |
| 52 | PORT + ":Integer=" + PORT_DEFAULT, |
| 53 | DATABASE + "=" + DATABASE_DEFAULT, |
| 54 | USERNAME + "=" + USERNAME_DEFAULT, |
| 55 | PASSWORD + "=" + PASSWORD_DEFAULT |
| 56 | } |
| 57 | ) |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 58 | public class InfluxDbMetricsConfig { |
| 59 | |
| 60 | private final Logger log = getLogger(getClass()); |
| 61 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 63 | protected CoreService coreService; |
| 64 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 66 | protected InfluxDbMetricsReporter influxDbMetricsReporter; |
| 67 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | bdfd37f | 2016-03-24 16:36:35 -0700 | [diff] [blame] | 69 | protected InfluxDbMetricsRetriever influxDbMetricsRetriever; |
| 70 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 72 | protected ComponentConfigService cfgService; |
| 73 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 74 | /** IP address of influxDB server; default is localhost. */ |
| 75 | protected String address = ADDRESS_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 76 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 77 | /** Port number of influxDB server; default is 8086. */ |
| 78 | protected int port = PORT_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 79 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 80 | /** Database name of influxDB server; default is onos. */ |
| 81 | protected String database = DATABASE_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 82 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 83 | /** Username of influxDB server; default is onos. */ |
| 84 | protected String username = USERNAME_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 85 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 86 | /** Password of influxDB server; default is onos.password. */ |
| 87 | protected String password = PASSWORD_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 88 | |
| 89 | @Activate |
| 90 | public void activate() { |
| 91 | cfgService.registerProperties(getClass()); |
| 92 | |
| 93 | coreService.registerApplication("org.onosproject.influxdbmetrics"); |
| 94 | |
| 95 | configReporter(influxDbMetricsReporter); |
Jian Li | bdfd37f | 2016-03-24 16:36:35 -0700 | [diff] [blame] | 96 | configRetriever(influxDbMetricsRetriever); |
| 97 | |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 98 | log.info("Started"); |
| 99 | } |
| 100 | |
| 101 | @Deactivate |
| 102 | public void deactivate() { |
| 103 | cfgService.unregisterProperties(getClass(), false); |
| 104 | |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 105 | log.info("Stopped"); |
| 106 | } |
| 107 | |
| 108 | @Modified |
| 109 | public void modified(ComponentContext context) { |
| 110 | readComponentConfiguration(context); |
| 111 | |
| 112 | configReporter(influxDbMetricsReporter); |
| 113 | influxDbMetricsReporter.restartReport(); |
Jian Li | bdfd37f | 2016-03-24 16:36:35 -0700 | [diff] [blame] | 114 | |
| 115 | configRetriever(influxDbMetricsRetriever); |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private void configReporter(InfluxDbMetricsReporter reporter) { |
| 119 | reporter.config(address, port, database, username, password); |
| 120 | } |
| 121 | |
Jian Li | bdfd37f | 2016-03-24 16:36:35 -0700 | [diff] [blame] | 122 | private void configRetriever(InfluxDbMetricsRetriever retriever) { |
| 123 | retriever.config(address, port, database, username, password); |
| 124 | } |
| 125 | |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Extracts properties from the component configuration context. |
| 128 | * |
| 129 | * @param context the component context |
| 130 | */ |
| 131 | private void readComponentConfiguration(ComponentContext context) { |
| 132 | Dictionary<?, ?> properties = context.getProperties(); |
| 133 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 134 | String addressStr = Tools.get(properties, ADDRESS); |
| 135 | address = addressStr != null ? addressStr : ADDRESS_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 136 | log.info("Configured. InfluxDB server address is {}", address); |
| 137 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 138 | String databaseStr = Tools.get(properties, DATABASE); |
| 139 | database = databaseStr != null ? databaseStr : DATABASE_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 140 | log.info("Configured. InfluxDB server database is {}", database); |
| 141 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 142 | String usernameStr = Tools.get(properties, USERNAME); |
| 143 | username = usernameStr != null ? usernameStr : USERNAME_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 144 | log.info("Configured. InfluxDB server username is {}", username); |
| 145 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 146 | String passwordStr = Tools.get(properties, PASSWORD); |
| 147 | password = passwordStr != null ? passwordStr : PASSWORD_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 148 | log.info("Configured. InfluxDB server password is {}", password); |
| 149 | |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 150 | Integer portConfigured = Tools.getIntegerProperty(properties, PORT); |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 151 | if (portConfigured == null) { |
Ray Milkey | ff74f06 | 2018-10-30 16:38:18 -0700 | [diff] [blame] | 152 | port = PORT_DEFAULT; |
Jian Li | e1d97c9 | 2016-03-22 10:21:44 -0700 | [diff] [blame] | 153 | log.info("InfluxDB port is not configured, default value is {}", port); |
| 154 | } else { |
| 155 | port = portConfigured; |
| 156 | log.info("Configured. InfluxDB port is configured to {}", port); |
| 157 | } |
| 158 | } |
| 159 | } |