Jian Li
Committed by Gerrit Code Review

[ONOS-4176] Extract Retriver config into a separate config service

Change-Id: I06a2fa69814bda061b7e481f765d53fd84f8871c
...@@ -24,8 +24,6 @@ import org.apache.commons.lang.StringUtils; ...@@ -24,8 +24,6 @@ import org.apache.commons.lang.StringUtils;
24 import org.apache.felix.scr.annotations.Activate; 24 import org.apache.felix.scr.annotations.Activate;
25 import org.apache.felix.scr.annotations.Component; 25 import org.apache.felix.scr.annotations.Component;
26 import org.apache.felix.scr.annotations.Deactivate; 26 import org.apache.felix.scr.annotations.Deactivate;
27 -import org.apache.felix.scr.annotations.Modified;
28 -import org.apache.felix.scr.annotations.Property;
29 import org.apache.felix.scr.annotations.Reference; 27 import org.apache.felix.scr.annotations.Reference;
30 import org.apache.felix.scr.annotations.ReferenceCardinality; 28 import org.apache.felix.scr.annotations.ReferenceCardinality;
31 import org.apache.felix.scr.annotations.Service; 29 import org.apache.felix.scr.annotations.Service;
...@@ -33,14 +31,10 @@ import org.influxdb.InfluxDB; ...@@ -33,14 +31,10 @@ import org.influxdb.InfluxDB;
33 import org.influxdb.InfluxDBFactory; 31 import org.influxdb.InfluxDBFactory;
34 import org.influxdb.dto.Query; 32 import org.influxdb.dto.Query;
35 import org.influxdb.dto.QueryResult; 33 import org.influxdb.dto.QueryResult;
36 -import org.onlab.util.Tools;
37 -import org.onosproject.cfg.ComponentConfigService;
38 import org.onosproject.cluster.NodeId; 34 import org.onosproject.cluster.NodeId;
39 import org.onosproject.core.CoreService; 35 import org.onosproject.core.CoreService;
40 -import org.osgi.service.component.ComponentContext;
41 import org.slf4j.Logger; 36 import org.slf4j.Logger;
42 37
43 -import java.util.Dictionary;
44 import java.util.List; 38 import java.util.List;
45 import java.util.Map; 39 import java.util.Map;
46 import java.util.Set; 40 import java.util.Set;
...@@ -58,11 +52,6 @@ public class DefaultInfluxDbMetricsRetriever implements InfluxDbMetricsRetriever ...@@ -58,11 +52,6 @@ public class DefaultInfluxDbMetricsRetriever implements InfluxDbMetricsRetriever
58 private final Logger log = getLogger(getClass()); 52 private final Logger log = getLogger(getClass());
59 53
60 private static final String DEFAULT_PROTOCOL = "http"; 54 private static final String DEFAULT_PROTOCOL = "http";
61 - private static final String DEFAULT_ADDRESS = "localhost";
62 - private static final int DEFAULT_PORT = 8086;
63 - private static final String DEFAULT_DATABASE = "onos";
64 - private static final String DEFAULT_USERNAME = "onos";
65 - private static final String DEFAULT_PASSWORD = "onos.password";
66 private static final String DEFAULT_POLICY = "default"; 55 private static final String DEFAULT_POLICY = "default";
67 private static final String COLON_SEPARATOR = ":"; 56 private static final String COLON_SEPARATOR = ":";
68 private static final String SLASH_SEPARATOR = "//"; 57 private static final String SLASH_SEPARATOR = "//";
...@@ -86,58 +75,24 @@ public class DefaultInfluxDbMetricsRetriever implements InfluxDbMetricsRetriever ...@@ -86,58 +75,24 @@ public class DefaultInfluxDbMetricsRetriever implements InfluxDbMetricsRetriever
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected CoreService coreService; 76 protected CoreService coreService;
88 77
89 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 78 + protected String database;
90 - protected ComponentConfigService cfgService;
91 -
92 - @Property(name = "address", value = DEFAULT_ADDRESS,
93 - label = "IP address of influxDB server; " +
94 - "default is localhost")
95 - protected String address = DEFAULT_ADDRESS;
96 -
97 - @Property(name = "port", intValue = DEFAULT_PORT,
98 - label = "Port number of influxDB server; " +
99 - "default is 8086")
100 - protected int port = DEFAULT_PORT;
101 -
102 - @Property(name = "database", value = DEFAULT_DATABASE,
103 - label = "Database name of influxDB server; " +
104 - "default is onos")
105 - protected String database = DEFAULT_DATABASE;
106 -
107 - @Property(name = "username", value = DEFAULT_USERNAME,
108 - label = "Username of influxDB server; default is onos")
109 - protected String username = DEFAULT_USERNAME;
110 -
111 - @Property(name = "password", value = DEFAULT_PASSWORD,
112 - label = "Password of influxDB server; default is onos.password")
113 - protected String password = DEFAULT_PASSWORD;
114 79
115 InfluxDB influxDB; 80 InfluxDB influxDB;
116 81
117 @Activate 82 @Activate
118 public void activate() { 83 public void activate() {
119 - cfgService.registerProperties(getClass());
120 coreService.registerApplication("org.onosproject.influxdbmetrics"); 84 coreService.registerApplication("org.onosproject.influxdbmetrics");
121 85
122 - config();
123 -
124 log.info("Started"); 86 log.info("Started");
125 } 87 }
126 88
127 @Deactivate 89 @Deactivate
128 public void deactivate() { 90 public void deactivate() {
129 - cfgService.unregisterProperties(getClass(), false);
130 -
131 log.info("Stopped"); 91 log.info("Stopped");
132 } 92 }
133 93
134 - @Modified 94 + @Override
135 - public void modified(ComponentContext context) { 95 + public void config(String address, int port, String database, String username, String password) {
136 - readComponentConfiguration(context);
137 - config();
138 - }
139 -
140 - private void config() {
141 StringBuilder url = new StringBuilder(); 96 StringBuilder url = new StringBuilder();
142 url.append(DEFAULT_PROTOCOL); 97 url.append(DEFAULT_PROTOCOL);
143 url.append(COLON_SEPARATOR + SLASH_SEPARATOR); 98 url.append(COLON_SEPARATOR + SLASH_SEPARATOR);
...@@ -145,7 +100,8 @@ public class DefaultInfluxDbMetricsRetriever implements InfluxDbMetricsRetriever ...@@ -145,7 +100,8 @@ public class DefaultInfluxDbMetricsRetriever implements InfluxDbMetricsRetriever
145 url.append(COLON_SEPARATOR); 100 url.append(COLON_SEPARATOR);
146 url.append(port); 101 url.append(port);
147 102
148 - influxDB = InfluxDBFactory.connect(url.toString(), username, password); 103 + this.influxDB = InfluxDBFactory.connect(url.toString(), username, password);
104 + this.database = database;
149 } 105 }
150 106
151 @Override 107 @Override
...@@ -427,38 +383,4 @@ public class DefaultInfluxDbMetricsRetriever implements InfluxDbMetricsRetriever ...@@ -427,38 +383,4 @@ public class DefaultInfluxDbMetricsRetriever implements InfluxDbMetricsRetriever
427 return null; 383 return null;
428 } 384 }
429 } 385 }
430 -
431 - /**
432 - * Extracts properties from the component configuration context.
433 - *
434 - * @param context the component context
435 - */
436 - private void readComponentConfiguration(ComponentContext context) {
437 - Dictionary<?, ?> properties = context.getProperties();
438 -
439 - String addressStr = Tools.get(properties, "address");
440 - address = addressStr != null ? addressStr : DEFAULT_ADDRESS;
441 - log.info("Configured. InfluxDB server address is {}", address);
442 -
443 - String databaseStr = Tools.get(properties, "database");
444 - database = databaseStr != null ? databaseStr : DEFAULT_DATABASE;
445 - log.info("Configured. InfluxDB server database is {}", database);
446 -
447 - String usernameStr = Tools.get(properties, "username");
448 - username = usernameStr != null ? usernameStr : DEFAULT_USERNAME;
449 - log.info("Configured. InfluxDB server username is {}", username);
450 -
451 - String passwordStr = Tools.get(properties, "password");
452 - password = passwordStr != null ? passwordStr : DEFAULT_PASSWORD;
453 - log.info("Configured. InfluxDB server password is {}", password);
454 -
455 - Integer portConfigured = Tools.getIntegerProperty(properties, "port");
456 - if (portConfigured == null) {
457 - port = DEFAULT_PORT;
458 - log.info("InfluxDB port is not configured, default value is {}", port);
459 - } else {
460 - port = portConfigured;
461 - log.info("Configured. InfluxDB port is configured to {}", port);
462 - }
463 - }
464 } 386 }
......
...@@ -43,7 +43,6 @@ public class InfluxDbMetricsConfig { ...@@ -43,7 +43,6 @@ public class InfluxDbMetricsConfig {
43 43
44 private static final String DEFAULT_ADDRESS = "localhost"; 44 private static final String DEFAULT_ADDRESS = "localhost";
45 private static final int DEFAULT_PORT = 8086; 45 private static final int DEFAULT_PORT = 8086;
46 -
47 private static final String DEFAULT_DATABASE = "onos"; 46 private static final String DEFAULT_DATABASE = "onos";
48 private static final String DEFAULT_USERNAME = "onos"; 47 private static final String DEFAULT_USERNAME = "onos";
49 private static final String DEFAULT_PASSWORD = "onos.password"; 48 private static final String DEFAULT_PASSWORD = "onos.password";
...@@ -55,6 +54,9 @@ public class InfluxDbMetricsConfig { ...@@ -55,6 +54,9 @@ public class InfluxDbMetricsConfig {
55 protected InfluxDbMetricsReporter influxDbMetricsReporter; 54 protected InfluxDbMetricsReporter influxDbMetricsReporter;
56 55
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 + protected InfluxDbMetricsRetriever influxDbMetricsRetriever;
58 +
59 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected ComponentConfigService cfgService; 60 protected ComponentConfigService cfgService;
59 61
60 @Property(name = "address", value = DEFAULT_ADDRESS, 62 @Property(name = "address", value = DEFAULT_ADDRESS,
...@@ -86,6 +88,8 @@ public class InfluxDbMetricsConfig { ...@@ -86,6 +88,8 @@ public class InfluxDbMetricsConfig {
86 configReporter(influxDbMetricsReporter); 88 configReporter(influxDbMetricsReporter);
87 influxDbMetricsReporter.startReport(); 89 influxDbMetricsReporter.startReport();
88 90
91 + configRetriever(influxDbMetricsRetriever);
92 +
89 log.info("Started"); 93 log.info("Started");
90 } 94 }
91 95
...@@ -103,12 +107,18 @@ public class InfluxDbMetricsConfig { ...@@ -103,12 +107,18 @@ public class InfluxDbMetricsConfig {
103 107
104 configReporter(influxDbMetricsReporter); 108 configReporter(influxDbMetricsReporter);
105 influxDbMetricsReporter.restartReport(); 109 influxDbMetricsReporter.restartReport();
110 +
111 + configRetriever(influxDbMetricsRetriever);
106 } 112 }
107 113
108 private void configReporter(InfluxDbMetricsReporter reporter) { 114 private void configReporter(InfluxDbMetricsReporter reporter) {
109 reporter.config(address, port, database, username, password); 115 reporter.config(address, port, database, username, password);
110 } 116 }
111 117
118 + private void configRetriever(InfluxDbMetricsRetriever retriever) {
119 + retriever.config(address, port, database, username, password);
120 + }
121 +
112 /** 122 /**
113 * Extracts properties from the component configuration context. 123 * Extracts properties from the component configuration context.
114 * 124 *
......
...@@ -97,4 +97,15 @@ public interface InfluxDbMetricsRetriever { ...@@ -97,4 +97,15 @@ public interface InfluxDbMetricsRetriever {
97 * @return metric value of a node 97 * @return metric value of a node
98 */ 98 */
99 List<InfluxMetric> metric(NodeId nodeId, String metricName, int period, TimeUnit unit); 99 List<InfluxMetric> metric(NodeId nodeId, String metricName, int period, TimeUnit unit);
100 +
101 + /**
102 + * Configures default parameters for influx database metrics retriever.
103 + *
104 + * @param address IP address of influxDB server
105 + * @param port Port number of influxDB server
106 + * @param database Database name of influxDB server
107 + * @param username Username of influxDB server
108 + * @param password Password of influxDB server
109 + */
110 + void config(String address, int port, String database, String username, String password);
100 } 111 }
......