blob: 52eac35db3b39f1cd17279e481738540c271b18a [file] [log] [blame]
Jian Lib9fe3492018-06-07 17:19:07 +09001/*
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 */
16package org.onosproject.openstacktelemetry.impl;
17
Jian Li4df75b12018-06-07 22:11:04 +090018import org.apache.felix.scr.annotations.Activate;
Jian Lid1ce10a2018-06-12 13:47:23 +090019import org.apache.felix.scr.annotations.Component;
Jian Li4df75b12018-06-07 22:11:04 +090020import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Lid1ce10a2018-06-12 13:47:23 +090025import org.apache.felix.scr.annotations.Service;
Jian Li4df75b12018-06-07 22:11:04 +090026import org.onlab.util.Tools;
27import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryAdminService;
Jian Lib9fe3492018-06-07 17:19:07 +090029import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryConfigService;
30import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li4df75b12018-06-07 22:11:04 +090031import org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig;
32import org.osgi.service.component.ComponentContext;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.util.Dictionary;
37
Jian Lid1ce10a2018-06-12 13:47:23 +090038import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE;
Jian Li4df75b12018-06-07 22:11:04 +090039import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_DATABASE;
40import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_ENABLE_BATCH;
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090041import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_MEASUREMENT;
Jian Li4df75b12018-06-07 22:11:04 +090042import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_PASSWORD;
43import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_SERVER_IP;
44import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_SERVER_PORT;
45import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_USERNAME;
46import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Jian Li3ed7f302018-08-27 17:16:27 +090047import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService;
Jian Lib9fe3492018-06-07 17:19:07 +090048
49/**
50 * InfluxDB server configuration manager for publishing openstack telemetry.
51 */
Jian Lid1ce10a2018-06-12 13:47:23 +090052@Component(immediate = true)
53@Service
Jian Lib9fe3492018-06-07 17:19:07 +090054public class InfluxDbTelemetryConfigManager implements InfluxDbTelemetryConfigService {
55
Jian Li4df75b12018-06-07 22:11:04 +090056 private final Logger log = LoggerFactory.getLogger(getClass());
57
Jian Lid1ce10a2018-06-12 13:47:23 +090058 private static final String ENABLE_SERVICE = "enableService";
Jian Li4df75b12018-06-07 22:11:04 +090059 private static final String ADDRESS = "address";
60 private static final String PORT = "port";
61 private static final String USERNAME = "username";
62 private static final String PASSWORD = "password";
63 private static final String DATABASE = "database";
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090064 private static final String MEASUREMENT = "measurement";
Jian Li4df75b12018-06-07 22:11:04 +090065 private static final String ENABLE_BATCH = "enableBatch";
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected ComponentConfigService componentConfigService;
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected InfluxDbTelemetryAdminService influxDbTelemetryAdminService;
72
73 @Property(name = ADDRESS, value = DEFAULT_INFLUXDB_SERVER_IP,
74 label = "Default IP address to establish initial connection to InfluxDB server")
75 protected String address = DEFAULT_INFLUXDB_SERVER_IP;
76
77 @Property(name = PORT, intValue = DEFAULT_INFLUXDB_SERVER_PORT,
78 label = "Default port number to establish initial connection to InfluxDB server")
79 protected Integer port = DEFAULT_INFLUXDB_SERVER_PORT;
80
81 @Property(name = USERNAME, value = DEFAULT_INFLUXDB_USERNAME,
82 label = "Username used for authenticating against InfluxDB server")
83 protected String username = DEFAULT_INFLUXDB_USERNAME;
84
85 @Property(name = PASSWORD, value = DEFAULT_INFLUXDB_PASSWORD,
86 label = "Password used for authenticating against InfluxDB server")
87 protected String password = DEFAULT_INFLUXDB_PASSWORD;
88
89 @Property(name = DATABASE, value = DEFAULT_INFLUXDB_DATABASE,
90 label = "Database of InfluxDB server")
91 protected String database = DEFAULT_INFLUXDB_DATABASE;
92
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090093 @Property(name = MEASUREMENT, value = DEFAULT_INFLUXDB_MEASUREMENT,
94 label = "Measurement of InfluxDB server")
95 protected String measurement = DEFAULT_INFLUXDB_MEASUREMENT;
96
Jian Li4df75b12018-06-07 22:11:04 +090097 @Property(name = ENABLE_BATCH, boolValue = DEFAULT_INFLUXDB_ENABLE_BATCH,
98 label = "Flag value of enabling batch mode of InfluxDB server")
99 protected Boolean enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH;
100
Jian Lid1ce10a2018-06-12 13:47:23 +0900101 @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
102 label = "Specify the default behavior of telemetry service")
103 protected Boolean enableService = DEFAULT_DISABLE;
104
Jian Li4df75b12018-06-07 22:11:04 +0900105 @Activate
106 protected void activate(ComponentContext context) {
107 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +0900108
109 if (enableService) {
110 influxDbTelemetryAdminService.start(getConfig());
111 }
Jian Li4df75b12018-06-07 22:11:04 +0900112 log.info("Started");
113 }
114
115 @Deactivate
116 protected void deactivate() {
117 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900118
119 if (enableService) {
120 influxDbTelemetryAdminService.stop();
121 }
Jian Li4df75b12018-06-07 22:11:04 +0900122 log.info("Stopped");
123 }
124
125 @Modified
126 private void modified(ComponentContext context) {
127 readComponentConfiguration(context);
Jian Li3ed7f302018-08-27 17:16:27 +0900128 initTelemetryService(influxDbTelemetryAdminService, getConfig(), enableService);
Jian Li4df75b12018-06-07 22:11:04 +0900129 log.info("Modified");
130 }
131
Jian Lib9fe3492018-06-07 17:19:07 +0900132 @Override
133 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900134 return new DefaultInfluxDbTelemetryConfig.DefaultBuilder()
135 .withAddress(address)
136 .withPort(port)
137 .withUsername(username)
138 .withPassword(password)
139 .withDatabase(database)
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900140 .withMeasurement(measurement)
Jian Li4df75b12018-06-07 22:11:04 +0900141 .withEnableBatch(enableBatch)
142 .build();
143 }
144
145 /**
146 * Extracts properties from the component configuration context.
147 *
148 * @param context the component context
149 */
150 private void readComponentConfiguration(ComponentContext context) {
151 Dictionary<?, ?> properties = context.getProperties();
152
153 String addressStr = Tools.get(properties, ADDRESS);
154 address = addressStr != null ? addressStr : DEFAULT_INFLUXDB_SERVER_IP;
155 log.info("Configured. InfluxDB server address is {}", address);
156
157 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
158 if (portConfigured == null) {
159 port = DEFAULT_INFLUXDB_SERVER_PORT;
160 log.info("InfluxDB server port is NOT configured, default value is {}", port);
161 } else {
162 port = portConfigured;
163 log.info("Configured. InfluxDB server port is {}", port);
164 }
165
166 String usernameStr = Tools.get(properties, USERNAME);
167 username = usernameStr != null ? usernameStr : DEFAULT_INFLUXDB_USERNAME;
168 log.info("Configured. InfluxDB server username is {}", username);
169
170 String passwordStr = Tools.get(properties, PASSWORD);
171 password = passwordStr != null ? passwordStr : DEFAULT_INFLUXDB_PASSWORD;
172 log.info("Configured. InfluxDB server password is {}", password);
173
174 String databaseStr = Tools.get(properties, DATABASE);
175 database = databaseStr != null ? databaseStr : DEFAULT_INFLUXDB_DATABASE;
176 log.info("Configured. InfluxDB server database is {}", database);
177
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900178 String measurementStr = Tools.get(properties, MEASUREMENT);
179 measurement = measurementStr != null ? measurementStr : DEFAULT_INFLUXDB_MEASUREMENT;
180 log.info("Configured. InfluxDB server measurement is {}", measurement);
181
Jian Li4df75b12018-06-07 22:11:04 +0900182 Boolean enableBatchConfigured = getBooleanProperty(properties, ENABLE_BATCH);
183 if (enableBatchConfigured == null) {
184 enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH;
185 log.info("InfluxDB server enable batch flag is " +
186 "NOT configured, default value is {}", enableBatch);
187 } else {
188 enableBatch = enableBatchConfigured;
189 log.info("Configured. InfluxDB server enable batch is {}", enableBatch);
190 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900191
192 Boolean enableServiceConfigured =
193 getBooleanProperty(properties, ENABLE_SERVICE);
194 if (enableServiceConfigured == null) {
195 enableService = DEFAULT_DISABLE;
196 log.info("InfluxDB service enable flag is NOT " +
197 "configured, default value is {}", enableService);
198 } else {
199 enableService = enableServiceConfigured;
200 log.info("Configured. InfluxDB service enable flag is {}", enableService);
201 }
Jian Lib9fe3492018-06-07 17:19:07 +0900202 }
203}