forked from eclipse-dataplane-core/dataplane-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataplane.java
More file actions
437 lines (370 loc) · 17 KB
/
Dataplane.java
File metadata and controls
437 lines (370 loc) · 17 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Copyright (c) 2025 Think-it GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Think-it GmbH - initial API and implementation
* Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - data flow properties
*
*/
package org.eclipse.dataplane;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.dataplane.domain.Result;
import org.eclipse.dataplane.domain.controlplane.ControlPlane;
import org.eclipse.dataplane.domain.dataflow.DataFlow;
import org.eclipse.dataplane.domain.dataflow.DataFlowPrepareMessage;
import org.eclipse.dataplane.domain.dataflow.DataFlowResponseMessage;
import org.eclipse.dataplane.domain.dataflow.DataFlowStartMessage;
import org.eclipse.dataplane.domain.dataflow.DataFlowStartedNotificationMessage;
import org.eclipse.dataplane.domain.dataflow.DataFlowStatusResponseMessage;
import org.eclipse.dataplane.domain.dataflow.DataFlowSuspendMessage;
import org.eclipse.dataplane.domain.dataflow.DataFlowTerminateMessage;
import org.eclipse.dataplane.domain.registration.Authorization;
import org.eclipse.dataplane.domain.registration.ControlPlaneRegistrationMessage;
import org.eclipse.dataplane.domain.registration.DataPlaneRegistrationMessage;
import org.eclipse.dataplane.logic.OnCompleted;
import org.eclipse.dataplane.logic.OnPrepare;
import org.eclipse.dataplane.logic.OnStart;
import org.eclipse.dataplane.logic.OnStarted;
import org.eclipse.dataplane.logic.OnSuspend;
import org.eclipse.dataplane.logic.OnTerminate;
import org.eclipse.dataplane.port.DataPlaneRegistrationApiController;
import org.eclipse.dataplane.port.DataPlaneSignalingApiController;
import org.eclipse.dataplane.port.exception.AuthorizationNotSupported;
import org.eclipse.dataplane.port.exception.DataFlowNotifyControlPlaneFailed;
import org.eclipse.dataplane.port.exception.DataplaneNotRegistered;
import org.eclipse.dataplane.port.store.ControlPlaneStore;
import org.eclipse.dataplane.port.store.DataFlowStore;
import org.eclipse.dataplane.port.store.InMemoryControlPlaneStore;
import org.eclipse.dataplane.port.store.InMemoryDataFlowStore;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static java.util.Collections.emptyMap;
public class Dataplane {
private final ObjectMapper objectMapper = new ObjectMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
private final DataFlowStore dataFlowStore = new InMemoryDataFlowStore(objectMapper);
private final ControlPlaneStore controlPlaneStore = new InMemoryControlPlaneStore(objectMapper);
private String id;
private URI endpoint;
private final Set<String> transferTypes = new HashSet<>();
private final Set<String> labels = new HashSet<>();
private OnPrepare onPrepare = dataFlow -> Result.failure(new UnsupportedOperationException("onPrepare is not implemented"));
private OnStart onStart = dataFlow -> Result.failure(new UnsupportedOperationException("onStart is not implemented"));
private OnTerminate onTerminate = dataFlow -> Result.failure(new UnsupportedOperationException("onTerminate is not implemented"));
private OnSuspend onSuspend = dataFlow -> Result.failure(new UnsupportedOperationException("onSuspend is not implemented"));
private OnStarted onStarted = dataFlow -> Result.failure(new UnsupportedOperationException("onStarted is not implemented"));
private OnCompleted onCompleted = dataFlow -> Result.failure(new UnsupportedOperationException("onCompleted is not implemented"));
private final HttpClient httpClient = HttpClient.newHttpClient();
private final Map<String, Authorization> authorizations = new HashMap<>();
public static Builder newInstance() {
return new Builder();
}
public DataPlaneSignalingApiController controller() {
return new DataPlaneSignalingApiController(this);
}
public DataPlaneRegistrationApiController registrationController() {
return new DataPlaneRegistrationApiController(this);
}
public Result<DataFlow> getById(String dataFlowId) {
return dataFlowStore.findById(dataFlowId);
}
public Result<Void> save(DataFlow dataFlow) {
return dataFlowStore.save(dataFlow);
}
public Result<DataFlowStatusResponseMessage> status(String dataFlowId) {
return dataFlowStore.findById(dataFlowId)
.map(f -> new DataFlowStatusResponseMessage(f.getId(), f.getState().name()));
}
public Result<DataFlowResponseMessage> prepare(DataFlowPrepareMessage message) {
var initialDataFlow = DataFlow.newInstance()
.id(message.processId())
.state(DataFlow.State.INITIATING)
.labels(message.labels())
.metadata(message.metadata())
.callbackAddress(message.callbackAddress())
.transferType(message.transferType())
.datasetId(message.datasetId())
.agreementId(message.agreementId())
.participantId(message.participantId())
.counterPartyId(message.counterPartyId())
.dataspaceContext(message.dataspaceContext())
.build();
return onPrepare.action(initialDataFlow)
.compose(dataFlow -> {
if (dataFlow.isInitiating()) {
dataFlow.transitionToPrepared();
}
DataFlowResponseMessage response;
if (dataFlow.isPrepared() && dataFlow.isPush()) {
response = new DataFlowResponseMessage(id, dataFlow.getDataAddress(), initialDataFlow.getState().name(), null);
} else {
response = new DataFlowResponseMessage(id, null, initialDataFlow.getState().name(), null);
}
return save(dataFlow).map(it -> response);
});
}
public Result<DataFlowResponseMessage> start(DataFlowStartMessage message) {
var initialDataFlow = DataFlow.newInstance()
.id(message.processId())
.state(DataFlow.State.INITIATING)
.dataAddress(message.dataAddress())
.callbackAddress(message.callbackAddress())
.transferType(message.transferType())
.datasetId(message.datasetId())
.agreementId(message.agreementId())
.participantId(message.participantId())
.counterPartyId(message.counterPartyId())
.dataspaceContext(message.dataspaceContext())
.build();
return onStart.action(initialDataFlow)
.compose(dataFlow -> {
if (dataFlow.isInitiating()) {
dataFlow.transitionToStarted();
}
DataFlowResponseMessage response;
if (dataFlow.isStarted() && dataFlow.isPull()) {
response = new DataFlowResponseMessage(id, dataFlow.getDataAddress(), dataFlow.getState().name(), null);
} else {
response = new DataFlowResponseMessage(id, null, dataFlow.getState().name(), null);
}
return save(dataFlow).map(it -> response);
});
}
public Result<Void> suspend(String flowId, DataFlowSuspendMessage message) {
return dataFlowStore.findById(flowId)
.map(dataFlow -> {
dataFlow.transitionToSuspended(message.reason());
return dataFlow;
})
.compose(onSuspend::action)
.compose(dataFlowStore::save)
.map(it -> null);
}
public Result<Void> terminate(String dataFlowId, DataFlowTerminateMessage message) {
return dataFlowStore.findById(dataFlowId)
.map(dataFlow -> {
dataFlow.transitionToTerminated(message.reason());
return dataFlow;
})
.compose(onTerminate::action)
.compose(dataFlowStore::save)
.map(it -> null);
}
/**
* Notify the control plane that the data flow has been prepared.
*
* @param dataFlowId the data flow id.
*/
public Result<Void> notifyPrepared(String dataFlowId, OnPrepare onPrepare) {
return dataFlowStore.findById(dataFlowId)
.compose(onPrepare::action)
.compose(dataFlow -> {
dataFlow.transitionToPrepared();
var message = new DataFlowResponseMessage(id, dataFlow.getDataAddress(), dataFlow.getState().name(), null);
return notifyControlPlane("prepared", dataFlow, message);
});
}
/**
* Notify the control plane that the data flow has been started.
*
* @param dataFlowId the data flow id.
*/
public Result<Void> notifyStarted(String dataFlowId, OnStart onStart) {
return dataFlowStore.findById(dataFlowId)
.compose(onStart::action)
.compose(dataFlow -> {
dataFlow.transitionToStarted();
var message = new DataFlowResponseMessage(id, dataFlow.getDataAddress(), dataFlow.getState().name(), null);
return notifyControlPlane("started", dataFlow, message);
});
}
/**
* Notify the control plane that the data flow has been completed.
*
* @param dataFlowId id of the data flow
*/
public Result<Void> notifyCompleted(String dataFlowId) {
return dataFlowStore.findById(dataFlowId)
.compose(dataFlow -> {
dataFlow.transitionToCompleted();
return notifyControlPlane("completed", dataFlow, emptyMap());
});
}
/**
* Notify the control plane that the data flow failed for some reason
*
* @param dataFlowId id of the data flow
* @param throwable the error
*/
public Result<Void> notifyErrored(String dataFlowId, Throwable throwable) {
return dataFlowStore.findById(dataFlowId)
.compose(dataFlow -> {
dataFlow.transitionToTerminated(throwable.getMessage());
return notifyControlPlane("errored", dataFlow, emptyMap()); // TODO DataFlowErroredMessage not defined
});
}
public Result<Void> started(String flowId, DataFlowStartedNotificationMessage startedNotificationMessage) {
return dataFlowStore.findById(flowId)
.map(dataFlow -> {
dataFlow.setDataAddress(startedNotificationMessage.dataAddress());
return dataFlow;
})
.compose(onStarted::action)
.compose(dataFlow -> {
dataFlow.transitionToStarted();
return save(dataFlow);
});
}
/**
* Received notification that the flow has been completed
*
* @param flowId id of the data flow
* @return result indicating whether data flow was completed successfully
*/
public Result<Void> completed(String flowId) {
return dataFlowStore.findById(flowId).compose(onCompleted::action)
.compose(dataFlow -> {
dataFlow.transitionToCompleted();
return save(dataFlow);
});
}
public Result<Void> registerOn(String controlPlaneEndpoint) {
var message = new DataPlaneRegistrationMessage(id, endpoint, transferTypes, labels);
return toJson(message)
.map(body -> HttpRequest.newBuilder()
.uri(URI.create(controlPlaneEndpoint + "/dataplanes/register"))
.header("content-type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build()
)
.compose(request -> {
var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
return Result.success();
} else {
return Result.failure(new DataplaneNotRegistered(response.body()));
}
});
}
private Result<Void> notifyControlPlane(String action, DataFlow dataFlow, Object message) {
return toJson(message)
.map(body -> {
var endpoint = dataFlow.callbackEndpointFor(action);
var requestBuilder = HttpRequest.newBuilder()
.uri(endpoint)
.header("content-type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body));
var controlPlane = controlPlaneStore.findByEndpoint(dataFlow.getCallbackAddress());
if (controlPlane.succeeded()) {
var authorizationProfile = controlPlane.getContent().authorization();
if (authorizationProfile != null) {
var authorization = authorizations.get(authorizationProfile.getType());
authorization.apply(requestBuilder, authorizationProfile);
}
}
return httpClient.send(requestBuilder.build(), HttpResponse.BodyHandlers.discarding());
})
.compose(response -> {
var successful = response.statusCode() >= 200 && response.statusCode() < 300;
if (successful) {
return save(dataFlow);
}
return Result.failure(new DataFlowNotifyControlPlaneFailed(action, response));
});
}
private Result<String> toJson(Object message) {
try {
return Result.success(objectMapper.writeValueAsString(message));
} catch (JsonProcessingException e) {
return Result.failure(e);
}
}
public ControlPlaneStore controlPlaneStore() {
return controlPlaneStore;
}
public Result<Void> registerControlPlane(ControlPlaneRegistrationMessage message) {
for (var auth : message.authorization()) {
if (!authorizations.containsKey(auth.getType())) {
return Result.failure(new AuthorizationNotSupported(auth));
}
}
var controlPlane = ControlPlane.newInstance()
.id(message.controlplaneId())
.endpoint(message.endpoint())
.authorization(message.authorization())
.build();
return controlPlaneStore.save(controlPlane);
}
public Result<Void> deleteControlPlane(String id) {
return controlPlaneStore.delete(id);
}
public static class Builder {
private final Dataplane dataplane = new Dataplane();
private Builder() {
}
public Dataplane build() {
if (dataplane.id == null) {
dataplane.id = UUID.randomUUID().toString();
}
return dataplane;
}
public Builder id(String id) {
dataplane.id = id;
return this;
}
public Builder endpoint(URI endpoint) {
dataplane.endpoint = endpoint;
return this;
}
public Builder transferType(String transferType) {
dataplane.transferTypes.add(transferType);
return this;
}
public Builder label(String label) {
dataplane.labels.add(label);
return this;
}
public Builder onPrepare(OnPrepare onPrepare) {
dataplane.onPrepare = onPrepare;
return this;
}
public Builder onStart(OnStart onStart) {
dataplane.onStart = onStart;
return this;
}
public Builder onStarted(OnStarted onStarted) {
dataplane.onStarted = onStarted;
return this;
}
public Builder onCompleted(OnCompleted onCompleted) {
dataplane.onCompleted = onCompleted;
return this;
}
public Builder onSuspend(OnSuspend onSuspend) {
dataplane.onSuspend = onSuspend;
return this;
}
public Builder onTerminate(OnTerminate onTerminate) {
dataplane.onTerminate = onTerminate;
return this;
}
public Builder registerAuthorization(Authorization authorization) {
dataplane.authorizations.put(authorization.type(), authorization);
return this;
}
}
}