Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

public interface DspClient {

String dspTransferState(String processId);
String dspTransferState(String callbackAddress, String processId);

void sendTransferStartMessage(String processId);
void sendTransferStartMessage(String callbackAddress, String processId);

void sendTransferCompletionMessage(String processId);
void sendTransferCompletionMessage(String callbackAddress, String processId);

void sendTransferTerminationMessage(String processId);
void sendTransferTerminationMessage(String callbackAddress, String processId);

String sendTransferRequestMessage(String address, String agreementId, String transferType);
TransferRequestResult sendTransferRequestMessage(String address, String agreementId, String transferType);

void sendTransferSuspensionMessage(String processId);
void sendTransferSuspensionMessage(String callbackAddress, String processId);

record TransferRequestResult(String processId, String address) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@
public class HttpDspClient implements DspClient {

private static final MediaType JSON = MediaType.get("application/json");
private final String protocolUrl;
private final String initialDspUrl;
private final Monitor monitor;
private final OkHttpClient httpClient;
private final ObjectMapper mapper;

public HttpDspClient(String protocolUrl, Monitor monitor, ObjectMapper mapper) {
this.protocolUrl = protocolUrl;
this.initialDspUrl = protocolUrl;
this.monitor = monitor;
this.mapper = mapper;
this.httpClient = new OkHttpClient();
}

@Override
public String dspTransferState(String processId) {
public String dspTransferState(String callbackAddress, String processId) {
try {
var url = protocolUrl + "/transfers/" + processId;
var url = callbackAddress + "/transfers/" + processId;

var authentication = Map.of("clientId", "providerId");
var request = new Request.Builder()
Expand All @@ -62,31 +62,31 @@ public String dspTransferState(String processId) {
}

@Override
public void sendTransferStartMessage(String processId) {
public void sendTransferStartMessage(String callbackAddress, String processId) {
var requestBody = message(processId, "TransferStartMessage");
send(protocolUrl + "/transfers/" + processId + "/start", requestBody, "providerId");
send(callbackAddress + "/transfers/" + processId + "/start", requestBody, "providerId");
}

@Override
public void sendTransferCompletionMessage(String processId) {
public void sendTransferCompletionMessage(String callbackAddress, String processId) {
var requestBody = message(processId, "TransferCompletionMessage");
send(protocolUrl + "/transfers/" + processId + "/completion", requestBody, "providerId");
send(callbackAddress + "/transfers/" + processId + "/completion", requestBody, "providerId");
}

@Override
public void sendTransferTerminationMessage(String processId) {
public void sendTransferTerminationMessage(String callbackAddress, String processId) {
var requestBody = message(processId, "TransferTerminationMessage");
send(protocolUrl + "/transfers/" + processId + "/termination", requestBody, "providerId");
send(callbackAddress + "/transfers/" + processId + "/termination", requestBody, "providerId");
}

@Override
public void sendTransferSuspensionMessage(String processId) {
public void sendTransferSuspensionMessage(String callbackAddress, String processId) {
var requestBody = message(processId, "TransferSuspensionMessage");
send(protocolUrl + "/transfers/" + processId + "/suspension", requestBody, "providerId");
send(callbackAddress + "/transfers/" + processId + "/suspension", requestBody, "providerId");
}

@Override
public String sendTransferRequestMessage(String address, String agreementId, String transferType) {
public TransferRequestResult sendTransferRequestMessage(String address, String agreementId, String transferType) {
var requestBody = Map.of(
"@context", "https://w3id.org/dspace/2025/1/context.jsonld",
"@type", "TransferRequestMessage",
Expand All @@ -95,8 +95,9 @@ public String sendTransferRequestMessage(String address, String agreementId, Str
"agreementId", agreementId,
"format", transferType
);
var response = send(protocolUrl + "/transfers/request", requestBody, "consumerId");
return response.get("providerPid").toString();
var response = send(initialDspUrl + "/transfers/request", requestBody, "consumerId");
var providerPid = response.get("providerPid").toString();
return new TransferRequestResult(providerPid, initialDspUrl);
}

private Map<String, Object> send(String url, Map<String, String> requestBody, String senderId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,33 @@ public LocalDspClient(LocalControlPlaneConnector connector) {
}

@Override
public String dspTransferState(String processId) {
public String dspTransferState(String callbackAddress, String processId) {
return connector.getTransferState(processId);
}

@Override
public void sendTransferStartMessage(String processId) {
public void sendTransferStartMessage(String callbackAddress, String processId) {
connector.receiveTransferStart(processId);
}

@Override
public void sendTransferCompletionMessage(String processId) {
public void sendTransferCompletionMessage(String callbackAddress, String processId) {
connector.receiveTransferCompletion(processId);
}

@Override
public void sendTransferTerminationMessage(String processId) {
public void sendTransferTerminationMessage(String callbackAddress, String processId) {
connector.receiveTransferTermination(processId);
}

@Override
public String sendTransferRequestMessage(String address, String agreementId, String transferType) {
return connector.receiveTransferRequestMessage(address, agreementId);
public TransferRequestResult sendTransferRequestMessage(String address, String agreementId, String transferType) {
var providerPid = connector.receiveTransferRequestMessage(address, agreementId);
return new TransferRequestResult(providerPid, null);
}

@Override
public void sendTransferSuspensionMessage(String processId) {
public void sendTransferSuspensionMessage(String callbackAddress, String processId) {
connector.receiveTransferSuspension(processId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class ControlPlaneSignalingPipeline extends AbstractAsyncPipeline<Control
private final DspClient dspClient;
private final AtomicReference<ReceivedDpsMessage> lastDpsReceivedMessage = new AtomicReference<>();
private final AtomicReference<Map<String, Object>> lastDspReceivedMessage = new AtomicReference<>();
private final AtomicReference<String> counterPartyProcessId = new AtomicReference<>();
private final AtomicReference<CounterParty> lastCounterParty = new AtomicReference<>();
private final AtomicReference<String> capturedProcessId = new AtomicReference<>();
private final AtomicReference<String> lastReceivedDataFlowId = new AtomicReference<>();
private final AtomicReference<String> lastReceivedCallbackAddress = new AtomicReference<>();
Expand Down Expand Up @@ -215,69 +215,69 @@ public ControlPlaneSignalingPipeline thenWaitForTransferRequestMessage() {

public ControlPlaneSignalingPipeline thenWaitForTransferToBeInState(String state) {
return thenWait("transfer to be in state " + state, () -> {
var id = counterPartyProcessId.get();
if (id == null) {
throw new RuntimeException("Cannot signal completion: no actual process ID received from prepare message");
var counterParty = lastCounterParty.get();
if (counterParty == null) {
throw new RuntimeException("Cannot wait for transfer state: no counter party data has been set");
}
var actualState = dspClient.dspTransferState(id);
monitor.debug("TCK. DSP: expecting processId %s state to be %s. Actual state: %s".formatted(id, state, actualState));
var actualState = dspClient.dspTransferState(counterParty.address(), counterParty.processId());
monitor.debug("TCK. DSP: expecting processId %s state to be %s. Actual state: %s".formatted(counterParty.processId(), state, actualState));
return Objects.equals(actualState, state);
});
}

public ControlPlaneSignalingPipeline sendTransferRequestMessage(String agreementId, String transferType) {
stages.add(() -> {
monitor.debug("Send DSP TransferRequestMessage");
var id = dspClient.sendTransferRequestMessage(endpoint.getAddress(), agreementId, transferType);
counterPartyProcessId.set(id);
var requestResult = dspClient.sendTransferRequestMessage(endpoint.getAddress(), agreementId, transferType);
lastCounterParty.set(new CounterParty(requestResult.processId(), requestResult.address()));
});
return this;
}

public ControlPlaneSignalingPipeline sendTransferStartMessage() {
stages.add(() -> {
var id = counterPartyProcessId.get();
if (id == null) {
throw new RuntimeException("Cannot signal start: no actual process ID received from prepare message");
var counterParty = lastCounterParty.get();
if (counterParty == null) {
throw new RuntimeException("Cannot signal start: no counter party data has been set");
}
monitor.debug("TCK. DSP: send TransferStartMessage for processId=" + id);
dspClient.sendTransferStartMessage(id);
monitor.debug("TCK. DSP: send TransferStartMessage for processId=" + counterParty.processId());
dspClient.sendTransferStartMessage(counterParty.address(), counterParty.processId());
});
return this;
}

public ControlPlaneSignalingPipeline sendTransferCompletionMessage() {
stages.add(() -> {
var id = counterPartyProcessId.get();
if (id == null) {
throw new RuntimeException("Cannot signal completion: no actual process ID received from prepare message");
var counterParty = lastCounterParty.get();
if (counterParty == null) {
throw new RuntimeException("Cannot signal completion: no counter party data has been set");
}
monitor.debug("TCK. DSP: send TransferCompletionMessage for processId=" + id);
dspClient.sendTransferCompletionMessage(id);
monitor.debug("TCK. DSP: send TransferCompletionMessage for processId=" + counterParty.processId());
dspClient.sendTransferCompletionMessage(counterParty.address(), counterParty.processId());
});
return this;
}

public ControlPlaneSignalingPipeline sendTransferTerminationMessage() {
stages.add(() -> {
var id = counterPartyProcessId.get();
if (id == null) {
throw new RuntimeException("Cannot signal termination: no actual process ID received from prepare message");
var counterParty = lastCounterParty.get();
if (counterParty == null) {
throw new RuntimeException("Cannot signal termination: no counter party data has been set");
}
monitor.debug("TCK. DSP: send TransferTerminationMessage for processId=" + id);
dspClient.sendTransferTerminationMessage(id);
monitor.debug("TCK. DSP: send TransferTerminationMessage for processId=" + counterParty.processId());
dspClient.sendTransferTerminationMessage(counterParty.address(), counterParty.processId());
});
return this;
}

public ControlPlaneSignalingPipeline sendTransferSuspensionMessage() {
stages.add(() -> {
var id = counterPartyProcessId.get();
if (id == null) {
throw new RuntimeException("Cannot signal suspension: no actual process ID received");
var counterParty = lastCounterParty.get();
if (counterParty == null) {
throw new RuntimeException("Cannot signal suspension: no counter party data has been set");
}
monitor.debug("TCK. DSP: send TransferSuspensionMessage for processId=" + id);
dspClient.sendTransferSuspensionMessage(id);
monitor.debug("TCK. DSP: send TransferSuspensionMessage for processId=" + counterParty.processId());
dspClient.sendTransferSuspensionMessage(counterParty.address(), counterParty.processId());
});
return this;
}
Expand Down Expand Up @@ -343,13 +343,15 @@ private void registerDspTransferRequestHandler() {
try {
var message = mapper.readValue(body, Map.class);
lastDspReceivedMessage.set(message);
counterPartyProcessId.set((String) message.get("consumerPid"));
monitor.debug("Received TransferRequestMessage from control plane: %s, processId=%s".formatted(message, counterPartyProcessId.get()));
var consumerProcessId = (String) message.get("consumerPid");
var callbackAddress = (String) message.get("callbackAddress");
lastCounterParty.set(new CounterParty(consumerProcessId, callbackAddress));
monitor.debug("Received TransferRequestMessage from control plane: %s, processId=%s".formatted(message, consumerProcessId));
return new HandlerResponse(200, mapper.writeValueAsString(Map.of(
"@context", "https://w3id.org/dspace/2025/1/context.jsonld",
"@type", "TransferProcess",
"providerPid", UUID.randomUUID().toString(),
"consumerPid", counterPartyProcessId.get()
"consumerPid", consumerProcessId
)));
} catch (IOException e) {
return new HandlerResponse(400, "Failed to parse TransferRequestMessage: " + e.getMessage());
Expand All @@ -363,12 +365,12 @@ private void registerDspTransferStartHandler() {
try {
var message = mapper.readValue(body, Map.class);
lastDspReceivedMessage.set(message);
counterPartyProcessId.set((String) message.get("providerPid"));
monitor.debug("Received TransferStartMessage from control plane: %s. processId=%s".formatted(message, counterPartyProcessId.get()));
var providerProcessId = (String) message.get("providerPid");
monitor.debug("Received TransferStartMessage from control plane: %s. processId=%s".formatted(message, providerProcessId));
return new HandlerResponse(200, mapper.writeValueAsString(Map.of(
"@context", "https://w3id.org/dspace/2025/1/context.jsonld",
"@type", "TransferProcess",
"providerPid", counterPartyProcessId.get(),
"providerPid", providerProcessId,
"consumerPid", UUID.randomUUID()
)));
} catch (IOException e) {
Expand Down Expand Up @@ -409,4 +411,6 @@ private HandlerResponse badRequest(List<Error> validationErrors) {
}

record ReceivedDpsMessage(String path, DpsMessage type, Map<String, Object> content) {}

record CounterParty(String processId, String address) {}
}
Loading