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