blob: 4ce3e528b7823d125c12abb81dae25914302df1b [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 Lib9fe3492018-06-07 17:19:07 +090047
48/**
49 * InfluxDB server configuration manager for publishing openstack telemetry.
50 */
Jian Lid1ce10a2018-06-12 13:47:23 +090051@Component(immediate = true)
52@Service
Jian Lib9fe3492018-06-07 17:19:07 +090053public class InfluxDbTelemetryConfigManager implements InfluxDbTelemetryConfigService {
54
Jian Li4df75b12018-06-07 22:11:04 +090055 private final Logger log = LoggerFactory.getLogger(getClass());
56
Jian Lid1ce10a2018-06-12 13:47:23 +090057 private static final String ENABLE_SERVICE = "enableService";
Jian Li4df75b12018-06-07 22:11:04 +090058 private static final String ADDRESS = "address";
59 private static final String PORT = "port";
60 private static final String USERNAME = "username";
61 private static final String PASSWORD = "password";
62 private static final String DATABASE = "database";
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090063 private static final String MEASUREMENT = "measurement";
Jian Li4df75b12018-06-07 22:11:04 +090064 private static final String ENABLE_BATCH = "enableBatch";
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected ComponentConfigService componentConfigService;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected InfluxDbTelemetryAdminService influxDbTelemetryAdminService;
71
72 @Property(name = ADDRESS, value = DEFAULT_INFLUXDB_SERVER_IP,
73 label = "Default IP address to establish initial connection to InfluxDB server")
74 protected String address = DEFAULT_INFLUXDB_SERVER_IP;
75
76 @Property(name = PORT, intValue = DEFAULT_INFLUXDB_SERVER_PORT,
77 label = "Default port number to establish initial connection to InfluxDB server")
78 protected Integer port = DEFAULT_INFLUXDB_SERVER_PORT;
79
80 @Property(name = USERNAME, value = DEFAULT_INFLUXDB_USERNAME,
81 label = "Username used for authenticating against InfluxDB server")
82 protected String username = DEFAULT_INFLUXDB_USERNAME;
83
84 @Property(name = PASSWORD, value = DEFAULT_INFLUXDB_PASSWORD,
85 label = "Password used for authenticating against InfluxDB server")
86 protected String password = DEFAULT_INFLUXDB_PASSWORD;
87
88 @Property(name = DATABASE, value = DEFAULT_INFLUXDB_DATABASE,
89 label = "Database of InfluxDB server")
90 protected String database = DEFAULT_INFLUXDB_DATABASE;
91
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090092 @Property(name = MEASUREMENT, value = DEFAULT_INFLUXDB_MEASUREMENT,
93 label = "Measurement of InfluxDB server")
94 protected String measurement = DEFAULT_INFLUXDB_MEASUREMENT;
95
Jian Li4df75b12018-06-07 22:11:04 +090096 @Property(name = ENABLE_BATCH, boolValue = DEFAULT_INFLUXDB_ENABLE_BATCH,
97 label = "Flag value of enabling batch mode of InfluxDB server")
98 protected Boolean enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH;
99
Jian Lid1ce10a2018-06-12 13:47:23 +0900100 @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
101 label = "Specify the default behavior of telemetry service")
102 protected Boolean enableService = DEFAULT_DISABLE;
103
Jian Li4df75b12018-06-07 22:11:04 +0900104 @Activate
105 protected void activate(ComponentContext context) {
106 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +0900107
108 if (enableService) {
109 influxDbTelemetryAdminService.start(getConfig());
110 }
Jian Li4df75b12018-06-07 22:11:04 +0900111 log.info("Started");
112 }
113
114 @Deactivate
115 protected void deactivate() {
116 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900117
118 if (enableService) {
119 influxDbTelemetryAdminService.stop();
120 }
Jian Li4df75b12018-06-07 22:11:04 +0900121 log.info("Stopped");
122 }
123
124 @Modified
125 private void modified(ComponentContext context) {
126 readComponentConfiguration(context);
Jian Lid1ce10a2018-06-12 13:47:23 +0900127
128 if (enableService) {
129 if (influxDbTelemetryAdminService.isRunning()) {
130 influxDbTelemetryAdminService.restart(getConfig());
131 } else {
132 influxDbTelemetryAdminService.start(getConfig());
133 }
134 } else {
135 if (influxDbTelemetryAdminService.isRunning()) {
136 influxDbTelemetryAdminService.stop();
137 }
138 }
Jian Li4df75b12018-06-07 22:11:04 +0900139 log.info("Modified");
140 }
141
Jian Lib9fe3492018-06-07 17:19:07 +0900142 @Override
143 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900144 return new DefaultInfluxDbTelemetryConfig.DefaultBuilder()
145 .withAddress(address)
146 .withPort(port)
147 .withUsername(username)
148 .withPassword(password)
149 .withDatabase(database)
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900150 .withMeasurement(measurement)
Jian Li4df75b12018-06-07 22:11:04 +0900151 .withEnableBatch(enableBatch)
152 .build();
153 }
154
155 /**
156 * Extracts properties from the component configuration context.
157 *
158 * @param context the component context
159 */
160 private void readComponentConfiguration(ComponentContext context) {
161 Dictionary<?, ?> properties = context.getProperties();
162
163 String addressStr = Tools.get(properties, ADDRESS);
164 address = addressStr != null ? addressStr : DEFAULT_INFLUXDB_SERVER_IP;
165 log.info("Configured. InfluxDB server address is {}", address);
166
167 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
168 if (portConfigured == null) {
169 port = DEFAULT_INFLUXDB_SERVER_PORT;
170 log.info("InfluxDB server port is NOT configured, default value is {}", port);
171 } else {
172 port = portConfigured;
173 log.info("Configured. InfluxDB server port is {}", port);
174 }
175
176 String usernameStr = Tools.get(properties, USERNAME);
177 username = usernameStr != null ? usernameStr : DEFAULT_INFLUXDB_USERNAME;
178 log.info("Configured. InfluxDB server username is {}", username);
179
180 String passwordStr = Tools.get(properties, PASSWORD);
181 password = passwordStr != null ? passwordStr : DEFAULT_INFLUXDB_PASSWORD;
182 log.info("Configured. InfluxDB server password is {}", password);
183
184 String databaseStr = Tools.get(properties, DATABASE);
185 database = databaseStr != null ? databaseStr : DEFAULT_INFLUXDB_DATABASE;
186 log.info("Configured. InfluxDB server database is {}", database);
187
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900188 String measurementStr = Tools.get(properties, MEASUREMENT);
189 measurement = measurementStr != null ? measurementStr : DEFAULT_INFLUXDB_MEASUREMENT;
190 log.info("Configured. InfluxDB server measurement is {}", measurement);
191
Jian Li4df75b12018-06-07 22:11:04 +0900192 Boolean enableBatchConfigured = getBooleanProperty(properties, ENABLE_BATCH);
193 if (enableBatchConfigured == null) {
194 enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH;
195 log.info("InfluxDB server enable batch flag is " +
196 "NOT configured, default value is {}", enableBatch);
197 } else {
198 enableBatch = enableBatchConfigured;
199 log.info("Configured. InfluxDB server enable batch is {}", enableBatch);
200 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900201
202 Boolean enableServiceConfigured =
203 getBooleanProperty(properties, ENABLE_SERVICE);
204 if (enableServiceConfigured == null) {
205 enableService = DEFAULT_DISABLE;
206 log.info("InfluxDB service enable flag is NOT " +
207 "configured, default value is {}", enableService);
208 } else {
209 enableService = enableServiceConfigured;
210 log.info("Configured. InfluxDB service enable flag is {}", enableService);
211 }
Jian Lib9fe3492018-06-07 17:19:07 +0900212 }
213}