blob: 069c374881c5b528584a72a4dc2381a785682363 [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;
41import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_PASSWORD;
42import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_SERVER_IP;
43import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_SERVER_PORT;
44import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_USERNAME;
45import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Jian Lib9fe3492018-06-07 17:19:07 +090046
47/**
48 * InfluxDB server configuration manager for publishing openstack telemetry.
49 */
Jian Lid1ce10a2018-06-12 13:47:23 +090050@Component(immediate = true)
51@Service
Jian Lib9fe3492018-06-07 17:19:07 +090052public class InfluxDbTelemetryConfigManager implements InfluxDbTelemetryConfigService {
53
Jian Li4df75b12018-06-07 22:11:04 +090054 private final Logger log = LoggerFactory.getLogger(getClass());
55
Jian Lid1ce10a2018-06-12 13:47:23 +090056 private static final String ENABLE_SERVICE = "enableService";
Jian Li4df75b12018-06-07 22:11:04 +090057 private static final String ADDRESS = "address";
58 private static final String PORT = "port";
59 private static final String USERNAME = "username";
60 private static final String PASSWORD = "password";
61 private static final String DATABASE = "database";
62 private static final String ENABLE_BATCH = "enableBatch";
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected ComponentConfigService componentConfigService;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected InfluxDbTelemetryAdminService influxDbTelemetryAdminService;
69
70 @Property(name = ADDRESS, value = DEFAULT_INFLUXDB_SERVER_IP,
71 label = "Default IP address to establish initial connection to InfluxDB server")
72 protected String address = DEFAULT_INFLUXDB_SERVER_IP;
73
74 @Property(name = PORT, intValue = DEFAULT_INFLUXDB_SERVER_PORT,
75 label = "Default port number to establish initial connection to InfluxDB server")
76 protected Integer port = DEFAULT_INFLUXDB_SERVER_PORT;
77
78 @Property(name = USERNAME, value = DEFAULT_INFLUXDB_USERNAME,
79 label = "Username used for authenticating against InfluxDB server")
80 protected String username = DEFAULT_INFLUXDB_USERNAME;
81
82 @Property(name = PASSWORD, value = DEFAULT_INFLUXDB_PASSWORD,
83 label = "Password used for authenticating against InfluxDB server")
84 protected String password = DEFAULT_INFLUXDB_PASSWORD;
85
86 @Property(name = DATABASE, value = DEFAULT_INFLUXDB_DATABASE,
87 label = "Database of InfluxDB server")
88 protected String database = DEFAULT_INFLUXDB_DATABASE;
89
90 @Property(name = ENABLE_BATCH, boolValue = DEFAULT_INFLUXDB_ENABLE_BATCH,
91 label = "Flag value of enabling batch mode of InfluxDB server")
92 protected Boolean enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH;
93
Jian Lid1ce10a2018-06-12 13:47:23 +090094 @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
95 label = "Specify the default behavior of telemetry service")
96 protected Boolean enableService = DEFAULT_DISABLE;
97
Jian Li4df75b12018-06-07 22:11:04 +090098 @Activate
99 protected void activate(ComponentContext context) {
100 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +0900101
102 if (enableService) {
103 influxDbTelemetryAdminService.start(getConfig());
104 }
Jian Li4df75b12018-06-07 22:11:04 +0900105 log.info("Started");
106 }
107
108 @Deactivate
109 protected void deactivate() {
110 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900111
112 if (enableService) {
113 influxDbTelemetryAdminService.stop();
114 }
Jian Li4df75b12018-06-07 22:11:04 +0900115 log.info("Stopped");
116 }
117
118 @Modified
119 private void modified(ComponentContext context) {
120 readComponentConfiguration(context);
Jian Lid1ce10a2018-06-12 13:47:23 +0900121
122 if (enableService) {
123 if (influxDbTelemetryAdminService.isRunning()) {
124 influxDbTelemetryAdminService.restart(getConfig());
125 } else {
126 influxDbTelemetryAdminService.start(getConfig());
127 }
128 } else {
129 if (influxDbTelemetryAdminService.isRunning()) {
130 influxDbTelemetryAdminService.stop();
131 }
132 }
Jian Li4df75b12018-06-07 22:11:04 +0900133 log.info("Modified");
134 }
135
Jian Lib9fe3492018-06-07 17:19:07 +0900136 @Override
137 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900138 return new DefaultInfluxDbTelemetryConfig.DefaultBuilder()
139 .withAddress(address)
140 .withPort(port)
141 .withUsername(username)
142 .withPassword(password)
143 .withDatabase(database)
144 .withEnableBatch(enableBatch)
145 .build();
146 }
147
148 /**
149 * Extracts properties from the component configuration context.
150 *
151 * @param context the component context
152 */
153 private void readComponentConfiguration(ComponentContext context) {
154 Dictionary<?, ?> properties = context.getProperties();
155
156 String addressStr = Tools.get(properties, ADDRESS);
157 address = addressStr != null ? addressStr : DEFAULT_INFLUXDB_SERVER_IP;
158 log.info("Configured. InfluxDB server address is {}", address);
159
160 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
161 if (portConfigured == null) {
162 port = DEFAULT_INFLUXDB_SERVER_PORT;
163 log.info("InfluxDB server port is NOT configured, default value is {}", port);
164 } else {
165 port = portConfigured;
166 log.info("Configured. InfluxDB server port is {}", port);
167 }
168
169 String usernameStr = Tools.get(properties, USERNAME);
170 username = usernameStr != null ? usernameStr : DEFAULT_INFLUXDB_USERNAME;
171 log.info("Configured. InfluxDB server username is {}", username);
172
173 String passwordStr = Tools.get(properties, PASSWORD);
174 password = passwordStr != null ? passwordStr : DEFAULT_INFLUXDB_PASSWORD;
175 log.info("Configured. InfluxDB server password is {}", password);
176
177 String databaseStr = Tools.get(properties, DATABASE);
178 database = databaseStr != null ? databaseStr : DEFAULT_INFLUXDB_DATABASE;
179 log.info("Configured. InfluxDB server database is {}", database);
180
181 Boolean enableBatchConfigured = getBooleanProperty(properties, ENABLE_BATCH);
182 if (enableBatchConfigured == null) {
183 enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH;
184 log.info("InfluxDB server enable batch flag is " +
185 "NOT configured, default value is {}", enableBatch);
186 } else {
187 enableBatch = enableBatchConfigured;
188 log.info("Configured. InfluxDB server enable batch is {}", enableBatch);
189 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900190
191 Boolean enableServiceConfigured =
192 getBooleanProperty(properties, ENABLE_SERVICE);
193 if (enableServiceConfigured == null) {
194 enableService = DEFAULT_DISABLE;
195 log.info("InfluxDB service enable flag is NOT " +
196 "configured, default value is {}", enableService);
197 } else {
198 enableService = enableServiceConfigured;
199 log.info("Configured. InfluxDB service enable flag is {}", enableService);
200 }
Jian Lib9fe3492018-06-07 17:19:07 +0900201 }
202}