Skip to content

Commit

Permalink
feat: add json ld api endpoints for TransferProcess (eclipse-edc#2888)
Browse files Browse the repository at this point in the history
feat: add json ld api endpoints for TransferProcess
  • Loading branch information
ndr-brt authored and majadlymhmd committed May 10, 2023
1 parent 920cc01 commit f901a6a
Show file tree
Hide file tree
Showing 73 changed files with 2,535 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public ServiceResult<Stream<TransferProcess>> query(QuerySpec query) {
}

@Override
public @NotNull ServiceResult<String> initiateTransfer(TransferRequest request) {
public @NotNull ServiceResult<TransferProcess> initiateTransfer(TransferRequest request) {
var validDestination = dataAddressValidator.validate(request.getDataRequest().getDataDestination());
if (validDestination.failed()) {
return ServiceResult.badRequest(validDestination.getFailureMessages().toArray(new String[]{}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,14 @@ void shouldDispatchEventsOnTransferProcessStateChanges(TransferProcessService se
.usingRecursiveComparison().isEqualTo(dataAddress);
});

service.complete(initiateResult.getContent());
var transferProcess = initiateResult.getContent();
service.complete(transferProcess.getId());

await().untilAsserted(() -> {
verify(eventSubscriber).on(argThat(isEnvelopeOf(TransferProcessCompleted.class)));
});

service.deprovision(initiateResult.getContent());
service.deprovision(transferProcess.getId());

await().untilAsserted(() -> {
verify(eventSubscriber).on(argThat(isEnvelopeOf(TransferProcessDeprovisioned.class)));
Expand Down Expand Up @@ -218,7 +219,7 @@ void shouldDispatchEventOnTransferProcessTerminated(TransferProcessService servi

var initiateResult = service.initiateTransfer(transferRequest);

service.terminate(initiateResult.getContent(), "any reason");
service.terminate(initiateResult.getContent().getId(), "any reason");

await().untilAsserted(() -> verify(eventSubscriber).on(argThat(isEnvelopeOf(TransferProcessTerminated.class))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ void getState_whenNotFound() {
@Test
void initiateTransfer() {
var transferRequest = transferRequest();
String processId = "processId";
var transferProcess = transferProcess();
when(dataAddressValidator.validate(any())).thenReturn(Result.success());
when(manager.initiateConsumerRequest(transferRequest)).thenReturn(StatusResult.success(processId));
when(manager.initiateConsumerRequest(transferRequest)).thenReturn(StatusResult.success(transferProcess));

var result = service.initiateTransfer(transferRequest);

assertThat(result.succeeded()).isTrue();
assertThat(result.getContent()).isEqualTo(processId);
assertThat(result.getContent()).isEqualTo(transferProcess);
verify(transactionContext).execute(any(TransactionContext.ResultTransactionBlock.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,30 @@ public void stop() {
*/
@WithSpan
@Override
public StatusResult<String> initiateConsumerRequest(TransferRequest transferRequest) {
return initiateRequest(CONSUMER, transferRequest);
public StatusResult<TransferProcess> initiateConsumerRequest(TransferRequest transferRequest) {
// make the request idempotent: if the process exists, return
var dataRequest = transferRequest.getDataRequest();
var processId = transferProcessStore.processIdForDataRequestId(dataRequest.getId());
if (processId != null) {
return StatusResult.success(transferProcessStore.findById(processId));
}
var id = randomUUID().toString();
var process = TransferProcess.Builder.newInstance()
.id(id)
.dataRequest(dataRequest)
.type(CONSUMER)
.clock(clock)
.properties(dataRequest.getProperties())
.callbackAddresses(transferRequest.getCallbackAddresses())
.traceContext(telemetry.getCurrentTraceContext())
.build();

observable.invokeForEach(l -> l.preCreated(process));
update(process);
observable.invokeForEach(l -> l.initiated(process));
monitor.debug("Process " + process.getId() + " is now " + TransferProcessStates.from(process.getState()));

return StatusResult.success(process);
}

@Override
Expand Down Expand Up @@ -256,32 +278,6 @@ private void handleDeprovisionResult(TransferProcess transferProcess, List<Statu
handleDeprovisionResponses(transferProcess, deprovisionResponses);
}

private StatusResult<String> initiateRequest(TransferProcess.Type type, TransferRequest transferRequest) {
// make the request idempotent: if the process exists, return
var dataRequest = transferRequest.getDataRequest();
var processId = transferProcessStore.processIdForDataRequestId(dataRequest.getId());
if (processId != null) {
return StatusResult.success(processId);
}
var id = randomUUID().toString();
var process = TransferProcess.Builder.newInstance()
.id(id)
.dataRequest(dataRequest)
.type(type)
.clock(clock)
.properties(dataRequest.getProperties())
.callbackAddresses(transferRequest.getCallbackAddresses())
.traceContext(telemetry.getCurrentTraceContext())
.build();

observable.invokeForEach(l -> l.preCreated(process));
update(process);
observable.invokeForEach(l -> l.initiated(process));
monitor.debug("Process " + process.getId() + " is now " + TransferProcessStates.from(process.getState()));

return StatusResult.success(process.getId());
}

/**
* Process INITIAL transfer<p> set it to PROVISIONING
*
Expand Down
1 change: 1 addition & 0 deletions data-protocols/dsp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
api(project(":data-protocols:dsp:dsp-http-spi"))
api(project(":data-protocols:dsp:dsp-negotiation"))
api(project(":data-protocols:dsp:dsp-transfer-process"))
api(project(":data-protocols:dsp:dsp-transfer-process:dsp-transfer-process-transform"))
api(project(":data-protocols:dsp:dsp-transform"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ public JsonObjectFromDataAddressTransformer(JsonBuilderFactory jsonBuilderFactor

return builder.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
import org.eclipse.edc.api.transformer.CallbackAddressDtoToCallbackAddressTransformer;
import org.eclipse.edc.api.transformer.CriterionDtoToCriterionTransformer;
import org.eclipse.edc.api.transformer.CriterionToCriterionDtoTransformer;
import org.eclipse.edc.api.transformer.DataAddressDtoToDataAddressTransformer;
import org.eclipse.edc.api.transformer.DataAddressToDataAddressDtoTransformer;
import org.eclipse.edc.api.transformer.JsonObjectFromCallbackAddressTransformer;
import org.eclipse.edc.api.transformer.JsonObjectFromCriterionDtoTransformer;
import org.eclipse.edc.api.transformer.JsonObjectFromDataAddressDtoTransformer;
import org.eclipse.edc.api.transformer.JsonObjectFromIdResponseDtoTransformer;
import org.eclipse.edc.api.transformer.JsonObjectToCallbackAddressDtoTransformer;
import org.eclipse.edc.api.transformer.JsonObjectToCriterionDtoTransformer;
import org.eclipse.edc.api.transformer.QuerySpecDtoToQuerySpecTransformer;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
Expand Down Expand Up @@ -54,9 +60,18 @@ public void initialize(ServiceExtensionContext context) {
transformerRegistry.register(new CriterionToCriterionDtoTransformer());
transformerRegistry.register(new CriterionDtoToCriterionTransformer());
transformerRegistry.register(new CallbackAddressDtoToCallbackAddressTransformer());
transformerRegistry.register(new DataAddressDtoToDataAddressTransformer());
transformerRegistry.register(new DataAddressToDataAddressDtoTransformer());

var mapper = typeManager.getMapper(JSON_LD);
var jsonFactory = Json.createBuilderFactory(Map.of());

transformerRegistry.register(new JsonObjectFromCallbackAddressTransformer(jsonFactory));
transformerRegistry.register(new JsonObjectFromCriterionDtoTransformer(jsonFactory, mapper));
transformerRegistry.register(new JsonObjectFromDataAddressDtoTransformer(jsonFactory));
transformerRegistry.register(new JsonObjectFromIdResponseDtoTransformer(jsonFactory));

transformerRegistry.register(new JsonObjectToCallbackAddressDtoTransformer());
transformerRegistry.register(new JsonObjectToCriterionDtoTransformer());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 ZF Friedrichshafen AG
* Copyright (c) 2022 - 2023 ZF Friedrichshafen AG
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -8,11 +8,11 @@
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* ZF Friedrichshafen AG - Initial API and Implementation
* ZF Friedrichshafen AG - initial API and implementation
*
*/

package org.eclipse.edc.connector.api.management.asset.model;
package org.eclipse.edc.api.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE;

@JsonDeserialize(builder = IdResponseDto.Builder.class)
public class IdResponseDto extends BaseResponseDto {

public static final String EDC_ID_RESPONSE_DTO_TYPE = EDC_NAMESPACE + "IdResponseDto";
public static final String EDC_ID_RESPONSE_DTO_CREATED_AT = EDC_NAMESPACE + "createdAt";

private String id;

public IdResponseDto() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2022 - 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -12,10 +12,9 @@
*
*/

package org.eclipse.edc.connector.api.management.asset.transform;
package org.eclipse.edc.api.transformer;

import org.eclipse.edc.api.transformer.DtoTransformer;
import org.eclipse.edc.connector.api.management.asset.model.DataAddressDto;
import org.eclipse.edc.api.model.DataAddressDto;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2022 - 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -12,10 +12,9 @@
*
*/

package org.eclipse.edc.connector.api.management.asset.transform;
package org.eclipse.edc.api.transformer;

import org.eclipse.edc.api.transformer.DtoTransformer;
import org.eclipse.edc.connector.api.management.asset.model.DataAddressDto;
import org.eclipse.edc.api.model.DataAddressDto;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*/

package org.eclipse.edc.connector.api.management.contractnegotiation.transform;
package org.eclipse.edc.api.transformer;

import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonBuilderFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering
*
* 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:
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation
*
*/

package org.eclipse.edc.api.transformer;

import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.api.model.DataAddressDto;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class JsonObjectFromDataAddressDtoTransformer extends AbstractJsonLdTransformer<DataAddressDto, JsonObject> {

private final JsonBuilderFactory jsonBuilderFactory;

public JsonObjectFromDataAddressDtoTransformer(JsonBuilderFactory jsonBuilderFactory) {
super(DataAddressDto.class, JsonObject.class);
this.jsonBuilderFactory = jsonBuilderFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull DataAddressDto dataAddress, @NotNull TransformerContext context) {
var builder = jsonBuilderFactory.createObjectBuilder();

dataAddress.getProperties().forEach(builder::add);

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* 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:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.api.transformer;

import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.api.model.IdResponseDto;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.api.model.IdResponseDto.EDC_ID_RESPONSE_DTO_CREATED_AT;
import static org.eclipse.edc.api.model.IdResponseDto.EDC_ID_RESPONSE_DTO_TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;

public class JsonObjectFromIdResponseDtoTransformer extends AbstractJsonLdTransformer<IdResponseDto, JsonObject> {

private final JsonBuilderFactory builderFactory;

public JsonObjectFromIdResponseDtoTransformer(JsonBuilderFactory builderFactory) {
super(IdResponseDto.class, JsonObject.class);
this.builderFactory = builderFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull IdResponseDto input, @NotNull TransformerContext context) {
return builderFactory.createObjectBuilder()
.add(TYPE, EDC_ID_RESPONSE_DTO_TYPE)
.add(ID, input.getId())
.add(EDC_ID_RESPONSE_DTO_CREATED_AT, input.getCreatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*/

package org.eclipse.edc.connector.api.management.contractnegotiation.transform;
package org.eclipse.edc.api.transformer;

import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2022 - 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -12,9 +12,9 @@
*
*/

package org.eclipse.edc.connector.api.management.asset.transform;
package org.eclipse.edc.api.transformer;

import org.eclipse.edc.connector.api.management.asset.model.DataAddressDto;
import org.eclipse.edc.api.model.DataAddressDto;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*/

package org.eclipse.edc.connector.api.management.contractnegotiation.transform;
package org.eclipse.edc.api.transformer;


import jakarta.json.Json;
Expand Down Expand Up @@ -53,4 +53,4 @@ void transform() {
assertThat(json.get(IS_TRANSACTIONAL).toString()).isEqualTo("true");
assertThat(json.getJsonArray(EVENTS)).hasSize(3);
}
}
}
Loading

0 comments on commit f901a6a

Please sign in to comment.