-
Notifications
You must be signed in to change notification settings - Fork 215
/
Connection.java
383 lines (329 loc) · 12.5 KB
/
Connection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.model;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.json.Jsonifiable;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
/**
* Represents a connection within the Connectivity service.
*/
@Immutable
public interface Connection extends Jsonifiable.WithFieldSelectorAndPredicate<JsonField> {
/**
* Returns the identifier of this {@code Connection}.
*
* @return the identifier.
*/
ConnectionId getId();
/**
* Returns the name of this {@code Connection}.
*
* @return the name.
*/
Optional<String> getName();
/**
* Returns the connection type of this {@code Connection}.
*
* @return the connection type
*/
ConnectionType getConnectionType();
/**
* Returns the persisted/desired ConnectionStatus of this {@code Connection}.
*
* @return the persisted ConnectionStatus
*/
ConnectivityStatus getConnectionStatus();
/**
* Returns a list of the sources of this {@code Connection}.
*
* @return the sources
*/
List<Source> getSources();
/**
* Returns a list of targets of this {@code Connection}.
*
* @return the targets
*/
List<Target> getTargets();
/**
* Return the ssh tunnel if any exist.
*
* @return the ssh tunnel or an empty optional.
* @since 2.0.0
*/
Optional<SshTunnel> getSshTunnel();
/**
* Returns how many clients on different cluster nodes should establish the {@code Connection}.
* <p>
* If greater than 1, the connection is created in a HA mode, running on at least 2 cluster nodes.
* </p>
*
* @return the client count.
*/
int getClientCount();
/**
* Returns whether or not failover is enabled for this {@code Connection}.
*
* @return {@code true} if failover is enabled, else {@code false}.
*/
boolean isFailoverEnabled();
/**
* Return the persisted credentials if any exist.
*
* @return the credentials or an empty optional.
*/
Optional<Credentials> getCredentials();
/**
* Return trusted certificates in PEM format if configured.
*
* @return the trusted certificates or an empty optional.
*/
Optional<String> getTrustedCertificates();
/**
* Returns the URI of this {@code Connection}.
*
* @return the URI.
*/
String getUri();
/**
* Returns the protocol part of the URI of this {@code Connection}.
*
* @return the protocol.
*/
String getProtocol();
/**
* Returns the username part of the URI of this {@code Connection}.
*
* @param shouldUriDecode whether the username should be URI-decoded.
* @return the username.
*/
Optional<String> getUsername(boolean shouldUriDecode);
/**
* Returns the password part of the URI of this {@code Connection}.
*
* @param shouldUriDecode whether the password should be URI-decoded.
* @return the password.
*/
Optional<String> getPassword(boolean shouldUriDecode);
/**
* Returns the hostname part of the URI of this {@code Connection}.
*
* @return the hostname.
*/
String getHostname();
/**
* Returns the port part of the URI of this {@code Connection}.
*
* @return the port.
*/
int getPort();
/**
* Returns the path part of the URI of this {@code Connection}.
*
* @return the path.
*/
Optional<String> getPath();
/**
* Whether to validate server certificates on connection establishment,
*
* @return {@code true} (default) if server certificates must be valid
*/
boolean isValidateCertificates();
/**
* The size of the command processor pool i.e. how many processor actors.
*
* @return size of the command processor actor pool
*/
int getProcessorPoolSize();
/**
* Returns configuration which is only applicable for a specific {@link ConnectionType}.
*
* @return an arbitrary map of config keys to config values
*/
Map<String, String> getSpecificConfig();
/**
* Returns the payload mapping definitions for this connection.
*
* @return the payload mapping definitions for this connection
*/
PayloadMappingDefinition getPayloadMappingDefinition();
/**
* Returns the tags of this {@code Connection}.
*
* @return the tags.
*/
Set<String> getTags();
/**
* Returns the current lifecycle of this Connection.
*
* @return the current lifecycle of this Connection.
*/
Optional<ConnectionLifecycle> getLifecycle();
/**
* Indicates whether this Connection has the given lifecycle.
*
* @param lifecycle the lifecycle to be checked for.
* @return {@code true} if this Connection has {@code lifecycle} as its lifecycle, {@code false} else.
*/
default boolean hasLifecycle(final ConnectionLifecycle lifecycle) {
return getLifecycle()
.filter(actualLifecycle -> Objects.equals(actualLifecycle, lifecycle))
.isPresent();
}
/**
* Returns a mutable builder with a fluent API for immutable {@code Connection}. The builder is initialised with the
* entries of this instance.
*
* @return the new builder.
*/
default ConnectionBuilder toBuilder() {
return ConnectivityModelFactory.newConnectionBuilder(this);
}
/**
* Returns all non-hidden marked fields of this {@code Connection}.
*
* @return a JSON object representation of this Connection including only non-hidden marked fields.
*/
@Override
default JsonObject toJson() {
return toJson(FieldType.notHidden());
}
@Override
default JsonObject toJson(final JsonSchemaVersion schemaVersion, final JsonFieldSelector fieldSelector) {
return toJson(schemaVersion, FieldType.notHidden()).get(fieldSelector);
}
/**
* An enumeration of the known {@code JsonField}s of a {@code Connection}.
*/
@Immutable
final class JsonFields {
/**
* JSON field containing the Connection's lifecycle.
*/
public static final JsonFieldDefinition<String> LIFECYCLE = JsonFactory.newStringFieldDefinition("__lifecycle",
FieldType.SPECIAL,
FieldType.HIDDEN,
JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} identifier.
*/
public static final JsonFieldDefinition<String> ID =
JsonFactory.newStringFieldDefinition("id", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} name.
*/
public static final JsonFieldDefinition<String> NAME =
JsonFactory.newStringFieldDefinition("name", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code ConnectionType}.
*/
public static final JsonFieldDefinition<String> CONNECTION_TYPE =
JsonFactory.newStringFieldDefinition("connectionType", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code ConnectionStatus}.
*/
public static final JsonFieldDefinition<String> CONNECTION_STATUS =
JsonFactory.newStringFieldDefinition("connectionStatus", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing credentials.
*/
public static final JsonFieldDefinition<JsonObject> CREDENTIALS =
JsonFactory.newJsonObjectFieldDefinition("credentials", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} uri.
*/
public static final JsonFieldDefinition<String> URI =
JsonFactory.newStringFieldDefinition("uri", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} sources configuration.
*/
public static final JsonFieldDefinition<JsonArray> SOURCES =
JsonFactory.newJsonArrayFieldDefinition("sources", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} targets configuration.
*/
public static final JsonFieldDefinition<JsonArray> TARGETS =
JsonFactory.newJsonArrayFieldDefinition("targets", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code sshTunnel} configuration.
*/
public static final JsonFieldDefinition<JsonObject> SSH_TUNNEL =
JsonFactory.newJsonObjectFieldDefinition("sshTunnel", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} client count.
*/
public static final JsonFieldDefinition<Integer> CLIENT_COUNT =
JsonFactory.newIntFieldDefinition("clientCount", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} failover enabled.
*/
public static final JsonFieldDefinition<Boolean> FAILOVER_ENABLED =
JsonFactory.newBooleanFieldDefinition("failoverEnabled", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} trust all certificates.
*/
public static final JsonFieldDefinition<Boolean> VALIDATE_CERTIFICATES =
JsonFactory.newBooleanFieldDefinition("validateCertificates", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} processor pool size.
*/
public static final JsonFieldDefinition<Integer> PROCESSOR_POOL_SIZE =
JsonFactory.newIntFieldDefinition("processorPoolSize", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} {@link ConnectionType} specific config.
*/
public static final JsonFieldDefinition<JsonObject> SPECIFIC_CONFIG =
JsonFactory.newJsonObjectFieldDefinition("specificConfig", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} {@link MappingContext} to apply.
*
* @deprecated MAPPING_CONTEXT is deprecated, use MAPPING_DEFINITIONS instead
*/
@Deprecated(/*forRemoval = false*/) // This MUST NOT be deleted from the model as there are still connections
// with that field which have to be deserialized.
public static final JsonFieldDefinition<JsonObject> MAPPING_CONTEXT =
JsonFactory.newJsonObjectFieldDefinition("mappingContext", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field containing the definitions of {@code Connection} mappings.
*/
public static final JsonFieldDefinition<JsonObject> MAPPING_DEFINITIONS =
JsonFactory.newJsonObjectFieldDefinition("mappingDefinitions",
FieldType.REGULAR,
JsonSchemaVersion.V_2);
/**
* JSON field containing the {@code Connection} tags configuration.
*/
public static final JsonFieldDefinition<JsonArray> TAGS =
JsonFactory.newJsonArrayFieldDefinition("tags", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* JSON field definition of trusted certificates.
*/
public static final JsonFieldDefinition<String> TRUSTED_CERTIFICATES =
JsonFieldDefinition.ofString("ca", FieldType.REGULAR, JsonSchemaVersion.V_2);
private JsonFields() {
throw new AssertionError();
}
}
}