Skip to content

Commit

Permalink
feature(api): Update NegotiateEdrRequestDto (#1135)
Browse files Browse the repository at this point in the history
* feature(api): Update NegotiateEdrRequestDto

Signed-off-by: Sascha Isele <[email protected]>

* feature(api): adds `LogDeprecatedValue` to `NegotiateEdrRequestDtoValidator`

Signed-off-by: Sascha Isele <[email protected]>

---------

Signed-off-by: Sascha Isele <[email protected]>
  • Loading branch information
saschaisele-zf authored Mar 18, 2024
1 parent 1d5de46 commit 7ddba8e
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.eclipse.tractusx.edc.api.edr;

import org.eclipse.edc.connector.api.management.configuration.ManagementApiConfiguration;
import org.eclipse.edc.connector.api.management.contractnegotiation.transform.JsonObjectToContractOfferTransformer;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.monitor.Monitor;
Expand Down Expand Up @@ -67,9 +68,10 @@ public void initialize(ServiceExtensionContext context) {
var mgmtApiTransformerRegistry = transformerRegistry.forContext("management-api");
mgmtApiTransformerRegistry.register(new NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer());
mgmtApiTransformerRegistry.register(new JsonObjectToNegotiateEdrRequestDtoTransformer());
mgmtApiTransformerRegistry.register(new JsonObjectToContractOfferTransformer());
mgmtApiTransformerRegistry.register(new JsonObjectFromEndpointDataReferenceEntryTransformer());
mgmtApiTransformerRegistry.register(new EndpointDataReferenceToDataAddressTransformer());
validatorRegistry.register(EDR_REQUEST_DTO_TYPE, NegotiateEdrRequestDtoValidator.instance());
validatorRegistry.register(EDR_REQUEST_DTO_TYPE, NegotiateEdrRequestDtoValidator.instance(context.getMonitor()));
webService.registerResource(apiConfig.getContextAlias(), new EdrController(edrService, mgmtApiTransformerRegistry, validatorRegistry, monitor));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription;
import org.eclipse.edc.spi.types.domain.callback.CallbackAddress;
import org.eclipse.edc.spi.types.domain.offer.ContractOffer;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -37,6 +38,7 @@ public class NegotiateEdrRequestDto {
public static final String EDR_REQUEST_DTO_COUNTERPARTY_ID = EDC_NAMESPACE + "counterPartyId";
public static final String EDR_REQUEST_DTO_PROVIDER_ID = EDC_NAMESPACE + "providerId";
public static final String EDR_REQUEST_DTO_OFFER = EDC_NAMESPACE + "offer";
public static final String EDR_REQUEST_DTO_POLICY = EDC_NAMESPACE + "policy";
public static final String EDR_REQUEST_DTO_CALLBACK_ADDRESSES = EDC_NAMESPACE + "callbackAddresses";

private String counterPartyAddress;
Expand All @@ -45,7 +47,9 @@ public class NegotiateEdrRequestDto {

private String providerId;

@Deprecated(since = "0.6.1")
private ContractOfferDescription offer;
private ContractOffer contractOffer;
private List<CallbackAddress> callbackAddresses = new ArrayList<>();

private NegotiateEdrRequestDto() {
Expand All @@ -72,10 +76,15 @@ public List<CallbackAddress> getCallbackAddresses() {
return callbackAddresses;
}

@Deprecated(since = "0.6.1")
public ContractOfferDescription getOffer() {
return offer;
}

public ContractOffer getContractOffer() {
return contractOffer;
}

public static final class Builder {
private final NegotiateEdrRequestDto dto;

Expand All @@ -98,11 +107,17 @@ public Builder counterPartyId(String connectorId) {
return this;
}

@Deprecated(since = "0.6.1")
public Builder offer(ContractOfferDescription offer) {
dto.offer = offer;
return this;
}

public Builder contractOffer(ContractOffer contractOffer) {
dto.contractOffer = contractOffer;
return this;
}

public Builder providerId(String providerId) {
dto.providerId = providerId;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,38 @@ public record NegotiateEdrRequestSchema(
@Schema(deprecated = true, description = "please use providerId instead")
String connectorId,
String providerId,
@Deprecated(since = "0.6.1")
@Schema(deprecated = true, description = "please use policy instead")
ContractNegotiationApi.ContractOfferDescriptionSchema offer,
ManagementApiSchema.PolicySchema policy,
List<ManagementApiSchema.CallbackAddressSchema> callbackAddresses) {

public static final String NEGOTIATE_EDR_REQUEST_EXAMPLE = """
{
"@context": { "edc": "https://w3id.org/edc/v0.0.1/ns/" },
"@type": "NegotiateEdrRequestDto",
"counterPartyAddress": "http://provider-address",
"protocol": "dataspace-protocol-http",
"providerId": "provider-id",
"policy": {
"@context": "http://www.w3.org/ns/odrl.jsonld",
"@type": "Set",
"@id": "policy-id",
"target": "asset-id",
"permission": [{
"action": "display"
}]
},
"callbackAddresses": [{
"transactional": false,
"uri": "http://callback/url",
"events": ["contract.negotiation", "transfer.process"]
}]
}
""";

@Deprecated(since = "0.6.1")
public static final String NEGOTIATE_EDR_REQUEST_DEPRECATED_EXAMPLE = """
{
"@context": { "edc": "https://w3id.org/edc/v0.0.1/ns/" },
"@type": "NegotiateEdrRequestDto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.spi.types.domain.callback.CallbackAddress;
import org.eclipse.edc.spi.types.domain.offer.ContractOffer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto;
import org.jetbrains.annotations.NotNull;
Expand All @@ -35,6 +36,7 @@
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ID;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_OFFER;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_POLICY;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROTOCOL;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROVIDER_ID;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE;
Expand Down Expand Up @@ -76,6 +78,9 @@ private void setProperties(String key, JsonValue value, NegotiateEdrRequestDto.B
case EDR_REQUEST_DTO_OFFER:
transformArrayOrObject(value, ContractOfferDescription.class, builder::offer, context);
break;
case EDR_REQUEST_DTO_POLICY:
transformArrayOrObject(value, ContractOffer.class, builder::contractOffer, context);
break;
default:
context.problem()
.unexpectedType()
Expand All @@ -88,6 +93,7 @@ private void setProperties(String key, JsonValue value, NegotiateEdrRequestDto.B
.expected(EDR_REQUEST_DTO_PROVIDER_ID)
.expected(EDR_REQUEST_DTO_CALLBACK_ADDRESSES)
.expected(EDR_REQUEST_DTO_OFFER)
.expected(EDR_REQUEST_DTO_POLICY)
.report();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ public Class<NegotiateEdrRequest> getOutputType() {

@Override
public @Nullable NegotiateEdrRequest transform(@NotNull NegotiateEdrRequestDto object, @NotNull TransformerContext context) {
var contractOffer = ContractOffer.Builder.newInstance()
.id(object.getOffer().getOfferId())
.assetId(object.getOffer().getAssetId())
.policy(object.getOffer().getPolicy())
.build();
ContractOffer contractOffer = null;

if (object.getContractOffer() != null) {
contractOffer = object.getContractOffer();
} else if (object.getOffer() != null) {
contractOffer = ContractOffer.Builder.newInstance()
.id(object.getOffer().getOfferId())
.assetId(object.getOffer().getAssetId())
.policy(object.getOffer().getPolicy())
.build();
}

return NegotiateEdrRequest.Builder.newInstance()
.connectorId(object.getCounterPartyId())
Expand All @@ -56,8 +62,4 @@ public Class<NegotiateEdrRequest> getOutputType() {
.build();
}

private String getId(String value, String defaultValue) {
return value != null ? value : defaultValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,65 @@
package org.eclipse.tractusx.edc.api.edr.validation;

import jakarta.json.JsonObject;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.validator.jsonobject.JsonLdPath;
import org.eclipse.edc.validator.jsonobject.JsonObjectValidator;
import org.eclipse.edc.validator.jsonobject.validators.LogDeprecatedValue;
import org.eclipse.edc.validator.jsonobject.validators.MandatoryObject;
import org.eclipse.edc.validator.jsonobject.validators.MandatoryValue;
import org.eclipse.edc.validator.spi.ValidationResult;
import org.eclipse.edc.validator.spi.Validator;

import static org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription.ASSET_ID;
import static org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription.OFFER_ID;
import static org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription.POLICY;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_OFFER;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_POLICY;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROTOCOL;
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE;


public class NegotiateEdrRequestDtoValidator {

private NegotiateEdrRequestDtoValidator() {
}

public static Validator<JsonObject> instance() {
public static Validator<JsonObject> instance(Monitor monitor) {
return JsonObjectValidator.newValidator()
.verify(EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS, MandatoryValue::new)
.verify(EDR_REQUEST_DTO_PROTOCOL, MandatoryValue::new)
.verify(EDR_REQUEST_DTO_OFFER, MandatoryObject::new)
.verifyObject(EDR_REQUEST_DTO_OFFER, v -> v
.verify(OFFER_ID, MandatoryValue::new)
.verify(ASSET_ID, MandatoryValue::new)
.verify(POLICY, MandatoryObject::new)
)
.verify(EDR_REQUEST_DTO_OFFER, path -> new LogDeprecatedValue(path, EDR_REQUEST_DTO_TYPE, EDR_REQUEST_DTO_POLICY, monitor))
.verify(MandatoryOfferOrPolicy::new)
.build();
}

private record MandatoryOfferOrPolicy(JsonLdPath path) implements Validator<JsonObject> {
@Override
public ValidationResult validate(JsonObject input) {
if (input.containsKey(EDR_REQUEST_DTO_OFFER)) {
return new OfferValidator(path.append(EDR_REQUEST_DTO_OFFER)).validate(input);
}
return new EdrPolicyValidator(path).validate(input);
}
}

private record OfferValidator(JsonLdPath path) implements Validator<JsonObject> {
@Override
public ValidationResult validate(JsonObject input) {
return JsonObjectValidator.newValidator()
.verifyObject(EDR_REQUEST_DTO_OFFER, v -> v
.verify(OFFER_ID, MandatoryValue::new)
.verify(ASSET_ID, MandatoryValue::new)
.verify(EDR_REQUEST_DTO_POLICY, MandatoryObject::new)
).build().validate(input);
}
}

private record EdrPolicyValidator(JsonLdPath path) implements Validator<JsonObject> {
@Override
public ValidationResult validate(JsonObject input) {
return JsonObjectValidator.newValidator()
.verify(EDR_REQUEST_DTO_POLICY, MandatoryObject::new).build().validate(input);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jakarta.json.JsonObject;
import org.eclipse.edc.api.transformer.JsonObjectToCallbackAddressTransformer;
import org.eclipse.edc.connector.api.management.contractnegotiation.transform.JsonObjectToContractOfferDescriptionTransformer;
import org.eclipse.edc.connector.api.management.contractnegotiation.transform.JsonObjectToContractOfferTransformer;
import org.eclipse.edc.connector.api.management.contractnegotiation.transform.JsonObjectToContractRequestTransformer;
import org.eclipse.edc.core.transform.TypeTransformerRegistryImpl;
import org.eclipse.edc.core.transform.transformer.odrl.OdrlTransformersFactory;
Expand All @@ -43,6 +44,7 @@
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE;
import static org.eclipse.edc.junit.extensions.TestServiceExtensionContext.testServiceExtensionContext;
import static org.eclipse.tractusx.edc.api.edr.schema.EdrSchema.EndpointDataReferenceEntrySchema.ENDPOINT_DATA_REFERENCE_ENTRY_EXAMPLE;
import static org.eclipse.tractusx.edc.api.edr.schema.EdrSchema.NegotiateEdrRequestSchema.NEGOTIATE_EDR_REQUEST_DEPRECATED_EXAMPLE;
import static org.eclipse.tractusx.edc.api.edr.schema.EdrSchema.NegotiateEdrRequestSchema.NEGOTIATE_EDR_REQUEST_EXAMPLE;
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;
Expand All @@ -66,6 +68,7 @@ public class EdrApiTest {
void setUp() {
transformer.register(new JsonObjectToContractRequestTransformer());
transformer.register(new JsonObjectToContractOfferDescriptionTransformer());
transformer.register(new JsonObjectToContractOfferTransformer());
transformer.register(new JsonObjectToCallbackAddressTransformer());
transformer.register(new JsonObjectToNegotiateEdrRequestDtoTransformer());
var mapper = mock(ParticipantIdMapper.class);
Expand All @@ -77,11 +80,30 @@ void setUp() {

@Test
void edrRequestExample() throws JsonProcessingException {
var validator = NegotiateEdrRequestDtoValidator.instance();
var validator = NegotiateEdrRequestDtoValidator.instance(testServiceExtensionContext().getMonitor());

var jsonObject = objectMapper.readValue(NEGOTIATE_EDR_REQUEST_EXAMPLE, JsonObject.class);
assertThat(jsonObject).isNotNull();

var expanded = jsonLd.expand(jsonObject);
AbstractResultAssert.assertThat(expanded).isSucceeded()
.satisfies(exp -> AbstractResultAssert.assertThat(validator.validate(exp)).isSucceeded())
.extracting(e -> transformer.transform(e, NegotiateEdrRequestDto.class))
.satisfies(transformResult -> AbstractResultAssert.assertThat(transformResult).isSucceeded()
.satisfies(transformed -> {
assertThat(transformed.getContractOffer()).isNotNull();
assertThat(transformed.getCallbackAddresses()).asList().hasSize(1);
assertThat(transformed.getProviderId()).isNotBlank();
}));
}

@Test
void edrDeprecatedRequestExample() throws JsonProcessingException {
var validator = NegotiateEdrRequestDtoValidator.instance(testServiceExtensionContext().getMonitor());

var jsonObject = objectMapper.readValue(NEGOTIATE_EDR_REQUEST_DEPRECATED_EXAMPLE, JsonObject.class);
assertThat(jsonObject).isNotNull();

var expanded = jsonLd.expand(jsonObject);
AbstractResultAssert.assertThat(expanded).isSucceeded()
.satisfies(exp -> AbstractResultAssert.assertThat(validator.validate(exp)).isSucceeded())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public static ContractOfferDescription createOffer() {
return createOffer(UUID.randomUUID().toString(), UUID.randomUUID().toString());
}

public static ContractOffer createContractOffer() {
return ContractOffer.Builder.newInstance()
.id("offerId")
.assetId("assetId")
.policy(Policy.Builder.newInstance().build()).build();
}

public static JsonObject negotiationRequest() {
return Json.createObjectBuilder()
.add(TYPE, NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE)
Expand Down
Loading

0 comments on commit 7ddba8e

Please sign in to comment.