Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacktelemetry.config; |
| 17 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 18 | import com.google.common.base.Strings; |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.onosproject.openstacktelemetry.api.config.InfluxDbTelemetryConfig; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 22 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 23 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfigProperties; |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 24 | |
| 25 | import java.util.Map; |
| 26 | import java.util.Objects; |
| 27 | |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame] | 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * A configuration file contains InfluxDB telemetry parameters. |
| 33 | */ |
| 34 | public final class DefaultInfluxDbTelemetryConfig implements InfluxDbTelemetryConfig { |
| 35 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 36 | protected static final String ADDRESS = "address"; |
| 37 | protected static final String PORT = "port"; |
| 38 | protected static final String USERNAME = "username"; |
| 39 | protected static final String PASSWORD = "password"; |
| 40 | protected static final String DATABASE = "database"; |
| 41 | protected static final String MEASUREMENT = "measurement"; |
| 42 | protected static final String ENABLE_BATCH = "enableBatch"; |
| 43 | protected static final String CONFIG_MAP = "configMap"; |
| 44 | |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 45 | private final String address; |
| 46 | private final int port; |
| 47 | private final String username; |
| 48 | private final String password; |
| 49 | private final String database; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 50 | private final String measurement; |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 51 | private final boolean enableBatch; |
| 52 | private final Map<String, Object> configMap; |
| 53 | |
| 54 | private DefaultInfluxDbTelemetryConfig(String address, int port, |
| 55 | String username, String password, |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 56 | String database, String measurement, |
| 57 | boolean enableBatch, |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 58 | Map<String, Object> configMap) { |
| 59 | this.address = address; |
| 60 | this.port = port; |
| 61 | this.username = username; |
| 62 | this.password = password; |
| 63 | this.database = database; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 64 | this.measurement = measurement; |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 65 | this.enableBatch = enableBatch; |
| 66 | this.configMap = configMap; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String address() { |
| 71 | return address; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public int port() { |
| 76 | return port; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String username() { |
| 81 | return username; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public String password() { |
| 86 | return password; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public String database() { |
| 91 | return database; |
| 92 | } |
| 93 | |
| 94 | @Override |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 95 | public String measurement() { |
| 96 | return measurement; |
| 97 | } |
| 98 | |
| 99 | @Override |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 100 | public boolean enableBatch() { |
| 101 | return enableBatch; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public Map<String, Object> configMap() { |
| 106 | if (configMap != null) { |
| 107 | return ImmutableMap.copyOf(configMap); |
| 108 | } else { |
| 109 | return Maps.newConcurrentMap(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean equals(Object obj) { |
| 115 | if (this == obj) { |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | if (obj instanceof DefaultInfluxDbTelemetryConfig) { |
| 120 | final DefaultInfluxDbTelemetryConfig other = (DefaultInfluxDbTelemetryConfig) obj; |
| 121 | return Objects.equals(this.address, other.address) && |
| 122 | Objects.equals(this.port, other.port) && |
| 123 | Objects.equals(this.username, other.username) && |
| 124 | Objects.equals(this.password, other.password) && |
| 125 | Objects.equals(this.database, other.database) && |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 126 | Objects.equals(this.measurement, other.measurement) && |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 127 | Objects.equals(this.enableBatch, other.enableBatch) && |
| 128 | Objects.equals(this.configMap, other.configMap); |
| 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public int hashCode() { |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 135 | return Objects.hash(address, port, username, password, database, |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 136 | measurement, enableBatch, configMap); |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 137 | } |
| 138 | |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 139 | @Override |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame] | 140 | public String toString() { |
| 141 | return toStringHelper(this) |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 142 | .add(ADDRESS, address) |
| 143 | .add(PORT, port) |
| 144 | .add(USERNAME, username) |
| 145 | .add(PASSWORD, password) |
| 146 | .add(DATABASE, database) |
| 147 | .add(MEASUREMENT, measurement) |
| 148 | .add(ENABLE_BATCH, enableBatch) |
| 149 | .add(CONFIG_MAP, configMap) |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame] | 150 | .toString(); |
| 151 | } |
| 152 | |
| 153 | @Override |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 154 | public TelemetryConfigProperties.Builder createBuilder() { |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 155 | return new DefaultBuilder(); |
| 156 | } |
| 157 | |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 158 | /** |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 159 | * Builds an influxDB telemetry config from telemetry config instance. |
| 160 | * |
| 161 | * @param config telemetry config |
| 162 | * @return influxDB telemetry config |
| 163 | */ |
| 164 | public static InfluxDbTelemetryConfig fromTelemetryConfig(TelemetryConfig config) { |
| 165 | if (config.type() != TelemetryConfig.ConfigType.INFLUXDB) { |
| 166 | return null; |
| 167 | } |
| 168 | |
| 169 | boolean enableBatch = Strings.isNullOrEmpty(config.getProperty(ENABLE_BATCH)) ? false : |
| 170 | Boolean.valueOf(config.getProperty(ENABLE_BATCH)); |
| 171 | |
| 172 | return new DefaultBuilder() |
| 173 | .withAddress(config.getProperty(ADDRESS)) |
| 174 | .withPort(Integer.valueOf(config.getProperty(PORT))) |
| 175 | .withUsername(config.getProperty(USERNAME)) |
| 176 | .withPassword(config.getProperty(PASSWORD)) |
| 177 | .withDatabase(config.getProperty(DATABASE)) |
| 178 | .withMeasurement(config.getProperty(MEASUREMENT)) |
| 179 | .withEnableBatch(enableBatch) |
| 180 | .build(); |
| 181 | } |
| 182 | |
| 183 | /** |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 184 | * Builder class of DefaultInfluxDbTelemetryConfig. |
| 185 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 186 | public static final class DefaultBuilder implements Builder { |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 187 | |
| 188 | private String address; |
| 189 | private int port; |
| 190 | private String username; |
| 191 | private String password; |
| 192 | private String database; |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 193 | private String measurement; |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 194 | private boolean enableBatch; |
| 195 | private Map<String, Object> configMap; |
| 196 | |
| 197 | @Override |
| 198 | public Builder withAddress(String address) { |
| 199 | this.address = address; |
| 200 | return this; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Builder withPort(int port) { |
| 205 | this.port = port; |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public Builder withUsername(String username) { |
| 211 | this.username = username; |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public Builder withPassword(String password) { |
| 217 | this.password = password; |
| 218 | return this; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public Builder withDatabase(String database) { |
| 223 | this.database = database; |
| 224 | return this; |
| 225 | } |
| 226 | |
| 227 | @Override |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 228 | public Builder withMeasurement(String measurement) { |
| 229 | this.measurement = measurement; |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | @Override |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 234 | public Builder withEnableBatch(boolean enableBatch) { |
| 235 | this.enableBatch = enableBatch; |
| 236 | return this; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public Builder withConfigMap(Map<String, Object> configMap) { |
| 241 | this.configMap = configMap; |
| 242 | return this; |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public InfluxDbTelemetryConfig build() { |
| 247 | checkNotNull(address, "InfluxDB server address cannot be null"); |
| 248 | checkNotNull(username, "InfluxDB server username cannot be null"); |
| 249 | checkNotNull(password, "InfluxDB server password cannot be null"); |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 250 | |
| 251 | return new DefaultInfluxDbTelemetryConfig(address, port, username, |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 252 | password, database, measurement, enableBatch, configMap); |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 253 | } |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 254 | |
Jian Li | 2cf1c0b | 2018-06-07 11:28:56 +0900 | [diff] [blame] | 255 | } |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 256 | } |