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