blob: 68b28c10c2d52c65a542e2947ff2d7b9e8d92f65 [file] [log] [blame]
Jian Li2cf1c0b2018-06-07 11:28:56 +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.config;
17
18import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.Maps;
20import org.onosproject.openstacktelemetry.api.config.InfluxDbTelemetryConfig;
Jian Li52c11222018-06-07 11:39:17 +090021import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li2cf1c0b2018-06-07 11:28:56 +090022
23import java.util.Map;
24import java.util.Objects;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * A configuration file contains InfluxDB telemetry parameters.
30 */
31public final class DefaultInfluxDbTelemetryConfig implements InfluxDbTelemetryConfig {
32
33 private final String address;
34 private final int port;
35 private final String username;
36 private final String password;
37 private final String database;
38 private final boolean enableBatch;
39 private final Map<String, Object> configMap;
40
41 private DefaultInfluxDbTelemetryConfig(String address, int port,
42 String username, String password,
43 String database, boolean enableBatch,
44 Map<String, Object> configMap) {
45 this.address = address;
46 this.port = port;
47 this.username = username;
48 this.password = password;
49 this.database = database;
50 this.enableBatch = enableBatch;
51 this.configMap = configMap;
52 }
53
54 @Override
55 public String address() {
56 return address;
57 }
58
59 @Override
60 public int port() {
61 return port;
62 }
63
64 @Override
65 public String username() {
66 return username;
67 }
68
69 @Override
70 public String password() {
71 return password;
72 }
73
74 @Override
75 public String database() {
76 return database;
77 }
78
79 @Override
80 public boolean enableBatch() {
81 return enableBatch;
82 }
83
84 @Override
85 public Map<String, Object> configMap() {
86 if (configMap != null) {
87 return ImmutableMap.copyOf(configMap);
88 } else {
89 return Maps.newConcurrentMap();
90 }
91 }
92
93 @Override
94 public boolean equals(Object obj) {
95 if (this == obj) {
96 return true;
97 }
98
99 if (obj instanceof DefaultInfluxDbTelemetryConfig) {
100 final DefaultInfluxDbTelemetryConfig other = (DefaultInfluxDbTelemetryConfig) obj;
101 return Objects.equals(this.address, other.address) &&
102 Objects.equals(this.port, other.port) &&
103 Objects.equals(this.username, other.username) &&
104 Objects.equals(this.password, other.password) &&
105 Objects.equals(this.database, other.database) &&
106 Objects.equals(this.enableBatch, other.enableBatch) &&
107 Objects.equals(this.configMap, other.configMap);
108 }
109 return false;
110 }
111
112 @Override
113 public int hashCode() {
114 return Objects.hash(address, port, username, password, database, enableBatch, configMap);
115 }
116
Jian Li52c11222018-06-07 11:39:17 +0900117 @Override
118 public TelemetryConfig.Builder createBuilder() {
119 return new DefaultBuilder();
120 }
121
Jian Li2cf1c0b2018-06-07 11:28:56 +0900122 /**
123 * Builder class of DefaultInfluxDbTelemetryConfig.
124 */
Jian Li52c11222018-06-07 11:39:17 +0900125 public static final class DefaultBuilder implements Builder {
Jian Li2cf1c0b2018-06-07 11:28:56 +0900126
127 private String address;
128 private int port;
129 private String username;
130 private String password;
131 private String database;
132 private boolean enableBatch;
133 private Map<String, Object> configMap;
134
135 @Override
136 public Builder withAddress(String address) {
137 this.address = address;
138 return this;
139 }
140
141 @Override
142 public Builder withPort(int port) {
143 this.port = port;
144 return this;
145 }
146
147 @Override
148 public Builder withUsername(String username) {
149 this.username = username;
150 return this;
151 }
152
153 @Override
154 public Builder withPassword(String password) {
155 this.password = password;
156 return this;
157 }
158
159 @Override
160 public Builder withDatabase(String database) {
161 this.database = database;
162 return this;
163 }
164
165 @Override
166 public Builder withEnableBatch(boolean enableBatch) {
167 this.enableBatch = enableBatch;
168 return this;
169 }
170
171 @Override
172 public Builder withConfigMap(Map<String, Object> configMap) {
173 this.configMap = configMap;
174 return this;
175 }
176
177 @Override
178 public InfluxDbTelemetryConfig build() {
179 checkNotNull(address, "InfluxDB server address cannot be null");
180 checkNotNull(username, "InfluxDB server username cannot be null");
181 checkNotNull(password, "InfluxDB server password cannot be null");
182 checkNotNull(database, "InfluxDB server database cannot be null");
183
184 return new DefaultInfluxDbTelemetryConfig(address, port, username,
185 password, database, enableBatch, configMap);
186 }
187 }
188}