Skip to content

Commit

Permalink
feat(Edr API): associates contractNegotiationId if present to the End…
Browse files Browse the repository at this point in the history
…pointDataReferenceEntry and make it filtrable
  • Loading branch information
wolf4ood committed Oct 24, 2023
1 parent 6ffe4eb commit 26a151d
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,25 @@ public <T> Predicate<T> convert(Criterion criterion) {
case "=" -> equalPredicate(criterion);
case "in" -> inPredicate(criterion);
case "like" -> likePredicate(criterion);
default -> throw new IllegalArgumentException(format("Operator [%s] is not supported by this converter!", criterion.getOperator()));
default ->
throw new IllegalArgumentException(format("Operator [%s] is not supported by this converter!", criterion.getOperator()));
};
}

protected Object property(String key, Object object) {
if (object instanceof EndpointDataReferenceEntry entry) {
return switch (key) {
case "assetId" -> entry.getAssetId();
case "agreementId" -> entry.getAgreementId();
case "providerId" -> entry.getProviderId();
case "contractNegotiationId" -> entry.getContractNegotiationId();
case "state" -> entry.getState();
default -> null;
};
}
throw new IllegalArgumentException("Can only handle objects of type " + EndpointDataReferenceEntry.class.getSimpleName() + " but received an " + object.getClass().getSimpleName());
}

@NotNull
private <T> Predicate<T> equalPredicate(Criterion criterion) {
return t -> {
Expand Down Expand Up @@ -117,17 +132,4 @@ private <T> Predicate<T> likePredicate(Criterion criterion) {
return false;
};
}

protected Object property(String key, Object object) {
if (object instanceof EndpointDataReferenceEntry entry) {
return switch (key) {
case "assetId" -> entry.getAssetId();
case "agreementId" -> entry.getAgreementId();
case "providerId" -> entry.getProviderId();
case "state" -> entry.getState();
default -> null;
};
}
throw new IllegalArgumentException("Can only handle objects of type " + EndpointDataReferenceEntry.class.getSimpleName() + " but received an " + object.getClass().getSimpleName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface EdrApi {
@ApiResponse(responseCode = "400", description = "Request was malformed",
content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiErrorDetail.class)))) }
)
JsonArray queryEdrs(String assetId, String agreementId, String providerId);
JsonArray queryEdrs(String assetId, String agreementId, String contractNegotiationId, String providerId);

@Operation(description = "Gets an EDR with the given transfer process ID",
responses = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.AGREEMENT_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.ASSET_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.CONTRACT_NEGOTIATION_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.PROVIDER_ID;

@Consumes({ MediaType.APPLICATION_JSON })
Expand Down Expand Up @@ -89,11 +90,14 @@ public JsonObject initiateEdrNegotiation(JsonObject requestObject) {

@GET
@Override
public JsonArray queryEdrs(@QueryParam("assetId") String assetId, @QueryParam("agreementId") String agreementId, @QueryParam("providerId") String providerId) {
if (assetId == null && agreementId == null) {
throw new InvalidRequestException("At least one of this query parameter is required [assetId,agreementId]");
public JsonArray queryEdrs(@QueryParam("assetId") String assetId,
@QueryParam("agreementId") String agreementId,
@QueryParam("contractNegotiationId") String contractNegotiationId,
@QueryParam("providerId") String providerId) {
if (assetId == null && agreementId == null && contractNegotiationId == null) {
throw new InvalidRequestException("At least one of this query parameter is required [assetId, agreementId, contractNegotiationId]");
}
return edrService.findBy(querySpec(assetId, agreementId, providerId))
return edrService.findBy(querySpec(assetId, agreementId, contractNegotiationId, providerId))
.orElseThrow(exceptionMapper(EndpointDataReferenceEntry.class))
.stream()
.map(edrCached -> transformerRegistry.transform(edrCached, JsonObject.class))
Expand Down Expand Up @@ -124,14 +128,17 @@ private void logIfError(Result<?> result) {
result.onFailure(f -> monitor.warning(f.getFailureDetail()));
}

private QuerySpec querySpec(String assetId, String agreementId, String providerId) {
private QuerySpec querySpec(String assetId, String agreementId, String contractNegotiationId, String providerId) {
var queryBuilder = QuerySpec.Builder.newInstance();
if (assetId != null) {
queryBuilder.filter(fieldFilter(ASSET_ID, assetId));
}
if (agreementId != null) {
queryBuilder.filter(fieldFilter(AGREEMENT_ID, agreementId));
}
if (contractNegotiationId != null) {
queryBuilder.filter(fieldFilter(CONTRACT_NEGOTIATION_ID, contractNegotiationId));
}
if (providerId != null) {
queryBuilder.filter(fieldFilter(PROVIDER_ID, providerId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_AGREEMENT_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_ASSET_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_CONTRACT_NEGOTIATION_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_EXPIRATION_DATE;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_PROVIDER_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_STATE;
Expand All @@ -40,15 +41,24 @@ public JsonObjectFromEndpointDataReferenceEntryTransformer() {

@Override
public @Nullable JsonObject transform(@NotNull EndpointDataReferenceEntry dto, @NotNull TransformerContext context) {
return Json.createObjectBuilder()

var builder = Json.createObjectBuilder()
.add(TYPE, EDR_ENTRY_TYPE)
.add(EDR_ENTRY_AGREEMENT_ID, dto.getAgreementId())
.add(EDR_ENTRY_TRANSFER_PROCESS_ID, dto.getTransferProcessId())
.add(EDR_ENTRY_ASSET_ID, dto.getAssetId())
.add(EDR_ENTRY_PROVIDER_ID, dto.getProviderId())
.add(EDR_ENTRY_STATE, dto.getEdrState())
.add(EDR_ENTRY_EXPIRATION_DATE, dto.getExpirationTimestamp())
.build();
.add(EDR_ENTRY_EXPIRATION_DATE, dto.getExpirationTimestamp());

if (dto.getProviderId() != null) {
builder.add(EDR_ENTRY_PROVIDER_ID, dto.getProviderId());
}

if (dto.getContractNegotiationId() != null) {
builder.add(EDR_ENTRY_CONTRACT_NEGOTIATION_ID, dto.getContractNegotiationId());
}

return builder.build();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@
import static org.eclipse.tractusx.edc.edr.spi.CoreConstants.TX_NAMESPACE;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.AGREEMENT_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.ASSET_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.CONTRACT_NEGOTIATION_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_AGREEMENT_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_ASSET_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_CONTRACT_NEGOTIATION_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_PROVIDER_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_STATE;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_TRANSFER_PROCESS_ID;
Expand Down Expand Up @@ -269,6 +271,53 @@ void queryEdrs_shouldReturnCachedEntries_whenAgreementIdIsProvided() {
.body("[0].'edc:providerId'", is(entry.getProviderId()));
}

@Test
void queryEdrs_shouldReturnCachedEntries_whenContractNegotiationIdIsProvided() {
var assetId = "assetId";
var transferProcessId = "transferProcessId";
var agreementId = "agreementId";
var providerId = "providerId";
var contractNegotiationId = "contractNegotiationId";

var entry = EndpointDataReferenceEntry.Builder.newInstance()
.transferProcessId(transferProcessId)
.agreementId(agreementId)
.assetId(assetId)
.providerId(providerId)
.contractNegotiationId(contractNegotiationId)
.build();


var response = Json.createObjectBuilder()
.add(TYPE, EDR_ENTRY_TYPE)
.add(EDR_ENTRY_ASSET_ID, entry.getAssetId())
.add(EDR_ENTRY_TRANSFER_PROCESS_ID, entry.getTransferProcessId())
.add(EDR_ENTRY_AGREEMENT_ID, entry.getAgreementId())
.add(EDR_ENTRY_CONTRACT_NEGOTIATION_ID, entry.getContractNegotiationId())
.add(EDR_ENTRY_PROVIDER_ID, entry.getProviderId())
.build();

var filter = QuerySpec.Builder.newInstance()
.filter(fieldFilter(CONTRACT_NEGOTIATION_ID, contractNegotiationId))
.filter(fieldFilter(PROVIDER_ID, entry.getProviderId()))
.build();

when(edrService.findBy(eq(filter))).thenReturn(ServiceResult.success(List.of(entry)));
when(transformerRegistry.transform(any(EndpointDataReferenceEntry.class), eq(JsonObject.class))).thenReturn(Result.success(response));

baseRequest()
.contentType(MediaType.APPLICATION_JSON)
.get(EDR_PATH + format("?=contractNegotiationId=%s&providerId=%s", entry.getContractNegotiationId(), entry.getProviderId()))
.then()
.log().all(true)
.statusCode(200)
.body("[0].'edc:transferProcessId'", is(entry.getTransferProcessId()))
.body("[0].'edc:agreementId'", is(entry.getAgreementId()))
.body("[0].'edc:contractNegotiationId'", is(entry.getContractNegotiationId()))
.body("[0].'edc:assetId'", is(entry.getAssetId()))
.body("[0].'edc:providerId'", is(entry.getProviderId()));
}

@Test
void deleteEdr() {
var transferProcessId = "id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_AGREEMENT_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_ASSET_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_CONTRACT_NEGOTIATION_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_EXPIRATION_DATE;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_PROVIDER_ID;
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_STATE;
Expand All @@ -49,6 +50,7 @@ void transform() {
.transferProcessId("tpId")
.agreementId("aId")
.providerId("providerId")
.contractNegotiationId("contractNegotiationId")
.state(EndpointDataReferenceEntryStates.NEGOTIATED.code())
.expirationTimestamp(Instant.now().toEpochMilli())
.build();
Expand All @@ -57,6 +59,7 @@ void transform() {

assertThat(jsonObject).isNotNull();
assertThat(jsonObject.getJsonString(EDR_ENTRY_AGREEMENT_ID).getString()).isNotNull().isEqualTo(dto.getAgreementId());
assertThat(jsonObject.getJsonString(EDR_ENTRY_CONTRACT_NEGOTIATION_ID).getString()).isNotNull().isEqualTo(dto.getContractNegotiationId());
assertThat(jsonObject.getJsonString(EDR_ENTRY_ASSET_ID).getString()).isNotNull().isEqualTo(dto.getAssetId());
assertThat(jsonObject.getJsonString(EDR_ENTRY_TRANSFER_PROCESS_ID).getString()).isNotNull().isEqualTo(dto.getTransferProcessId());
assertThat(jsonObject.getJsonString(EDR_ENTRY_PROVIDER_ID).getString()).isNotNull().isEqualTo(dto.getProviderId());
Expand Down
1 change: 1 addition & 0 deletions edc-extensions/edr/edr-cache-sql/docs/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ CREATE TABLE IF NOT EXISTS edc_edr_cache
agreement_id VARCHAR NOT NULL,
asset_id VARCHAR NOT NULL,
edr_id VARCHAR NOT NULL,
contract_negotiation_id VARCHAR,
provider_id VARCHAR,
expiration_timestamp BIGINT,
state INTEGER DEFAULT 0 NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public void save(EndpointDataReferenceEntry entry, EndpointDataReference edr) {
entry.getAgreementId(),
edr.getId(),
entry.getProviderId(),
entry.getContractNegotiationId(),
entry.getExpirationTimestamp(),
entry.getState(),
entry.getStateCount(),
Expand Down Expand Up @@ -253,6 +254,7 @@ private EndpointDataReferenceEntry mapResultSet(ResultSet resultSet) throws SQLE
.transferProcessId(resultSet.getString(statements.getTransferProcessIdColumn()))
.assetId(resultSet.getString(statements.getAssetIdColumn()))
.agreementId(resultSet.getString(statements.getAgreementIdColumn()))
.contractNegotiationId(resultSet.getString(statements.getContractNegotiationIdColumn()))
.providerId(resultSet.getString(statements.getProviderIdColumn()))
.state(resultSet.getInt(statements.getStateColumn()))
.stateTimestamp(resultSet.getLong(statements.getStateTimestampColumn()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public SqlQueryStatement createQuery(QuerySpec querySpec) {

@Override
public String getInsertTemplate() {
return format("INSERT INTO %s (%s, %s, %s, %s,%s, %s, %s, %s, %s, %s, %s, %s) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
return format("INSERT INTO %s (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
getEdrTable(),
getTransferProcessIdColumn(),
getAssetIdColumn(),
getAgreementIdColumn(),
getEdrId(),
getProviderIdColumn(),
getContractNegotiationIdColumn(),
getExpirationTimestampColumn(),
getStateColumn(),
getStateCountColumn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public EdrMapping(EdrStatements statements) {
add("assetId", statements.getAssetIdColumn());
add("agreementId", statements.getAgreementIdColumn());
add("providerId", statements.getProviderIdColumn());
add("contractNegotiationId", statements.getContractNegotiationIdColumn());
add("state", statements.getStateColumn());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ default String getProviderIdColumn() {
return "provider_id";
}

default String getContractNegotiationIdColumn() {
return "contract_negotiation_id";
}

default String getAssetIdColumn() {
return "asset_id";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.tractusx.edc.callback;

import org.eclipse.edc.connector.spi.callback.CallbackProtocolResolverRegistry;
import org.eclipse.edc.connector.spi.contractagreement.ContractAgreementService;
import org.eclipse.edc.connector.spi.transferprocess.TransferProcessService;
import org.eclipse.edc.connector.transfer.spi.store.TransferProcessStore;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
Expand Down Expand Up @@ -62,6 +63,9 @@ public class LocalCallbackExtension implements ServiceExtension {
@Inject
private EndpointDataReferenceCache endpointDataReferenceCache;

@Inject
private ContractAgreementService agreementService;

@Inject
private TypeTransformerRegistry transformerRegistry;

Expand All @@ -74,7 +78,7 @@ public String name() {
public void initialize(ServiceExtensionContext context) {

callbackRegistry.registerHandler(new ContractNegotiationCallback(transferProcessService, monitor));
callbackRegistry.registerHandler(new TransferProcessLocalCallback(edrCache, transferProcessStore, transformerRegistry, transactionContext, monitor));
callbackRegistry.registerHandler(new TransferProcessLocalCallback(edrCache, transferProcessStore, agreementService, transformerRegistry, transactionContext, monitor));

resolverRegistry.registerResolver(this::resolveProtocol);
registry.register(new InProcessCallbackMessageDispatcher(callbackRegistry));
Expand Down
Loading

0 comments on commit 26a151d

Please sign in to comment.