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