Skip to content

Commit

Permalink
refactor(management-api): counterPartyAddress (eclipse-edc#3343)
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Isele <[email protected]>
  • Loading branch information
saschaisele-zf committed Nov 10, 2023
1 parent 0d6086d commit 26ed509
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private TransferRequest createTransferRequest() {
.assetId("assetId")
.dataDestination(DataAddress.Builder.newInstance().type("any").build())
.protocol("test")
.connectorAddress("http://an/address")
.counterPartyAddress("http://an/address")
.contractId("contractId")
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public StatusResult<TransferProcess> initiateConsumerRequest(TransferRequest tra
.assetId(transferRequest.getAssetId())
.connectorId(transferRequest.getConnectorId())
.dataDestination(transferRequest.getDataDestination())
.connectorAddress(transferRequest.getConnectorAddress())
.connectorAddress(transferRequest.getCounterPartyAddress())
.contractId(transferRequest.getContractId())
.destinationType(transferRequest.getDataDestination().getType())
.protocol(transferRequest.getProtocol())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ record ContractRequestSchema(
String type,
@Schema(requiredMode = REQUIRED)
String protocol,
@Schema(requiredMode = REQUIRED)
@Deprecated(since = "0.3.2")
@Schema(deprecated = true, description = "please use counterPartyAddress instead")
String connectorAddress,
@Schema(requiredMode = REQUIRED)
String counterPartyAddress,
@Schema(requiredMode = REQUIRED)
String providerId,
ContractOfferDescriptionSchema offer,
List<ManagementApiSchema.CallbackAddressSchema> callbackAddresses) {
Expand All @@ -139,7 +142,7 @@ record ContractRequestSchema(
{
"@context": { "@vocab": "https://w3id.org/edc/v0.0.1/ns/" },
"@type": "https://w3id.org/edc/v0.0.1/ns/ContractRequest",
"connectorAddress": "http://provider-address",
"counterPartyAddress": "http://provider-address",
"protocol": "dataspace-protocol-http",
"providerId": "provider-id",
"offer": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.CALLBACK_ADDRESSES;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.CONNECTOR_ADDRESS;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.OFFER;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.PROTOCOL;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.PROVIDER_ID;
Expand All @@ -42,7 +43,7 @@ public JsonObjectToContractRequestTransformer() {
public @Nullable ContractRequest transform(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) {
var contractRequestBuilder = ContractRequest.Builder.newInstance()
.providerId(getProviderId(jsonObject, context))
.counterPartyAddress(transformString(jsonObject.get(CONNECTOR_ADDRESS), context))
.counterPartyAddress(counterPartyAddressOrConnectorAddress(jsonObject, context))
.protocol(transformString(jsonObject.get(PROTOCOL), context));

var contractOfferDescription = transformObject(jsonObject.get(OFFER), ContractOfferDescription.class, context);
Expand All @@ -69,8 +70,13 @@ private String getProviderId(@NotNull JsonObject jsonObject, @NotNull Transforme
return transformString(providerId, context);
}

return transformString(jsonObject.get(CONNECTOR_ADDRESS), context);
return counterPartyAddressOrConnectorAddress(jsonObject, context);

}

private String counterPartyAddressOrConnectorAddress(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) {
var counterPartyAddress = transformString(jsonObject.get(CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS), context);
var connectorAddress = transformString(jsonObject.get(CONNECTOR_ADDRESS), context);
return connectorAddress != null ? connectorAddress : counterPartyAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@
package org.eclipse.edc.connector.api.management.contractnegotiation.validation;

import jakarta.json.JsonObject;
import org.eclipse.edc.validator.jsonobject.JsonLdPath;
import org.eclipse.edc.validator.jsonobject.JsonObjectValidator;
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.edc.connector.contract.spi.types.negotiation.ContractRequest.CONNECTOR_ADDRESS;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.OFFER;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.PROTOCOL;

public class ContractRequestValidator {
public static Validator<JsonObject> instance() {
return JsonObjectValidator.newValidator()
.verify(CONNECTOR_ADDRESS, MandatoryValue::new)
.verify(MandatoryCounterPartyAddressOrConnectorAddress::new)
.verify(PROTOCOL, MandatoryValue::new)
.verify(OFFER, MandatoryObject::new)
.verifyObject(OFFER, v -> v
Expand All @@ -40,4 +43,22 @@ public static Validator<JsonObject> instance() {
)
.build();
}

private record MandatoryCounterPartyAddressOrConnectorAddress(JsonLdPath path) implements Validator<JsonObject> {

@Override
public ValidationResult validate(JsonObject input) {
var counterPartyAddress = new MandatoryValue(path.append(CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS));
var connectorAddress = new MandatoryValue(path.append(CONNECTOR_ADDRESS));

var validateCounterParty = counterPartyAddress.validate(input);
var validateConnectorAddress = connectorAddress.validate(input);

if (validateCounterParty.succeeded() || validateConnectorAddress.succeeded()) {
return ValidationResult.success();
} else {
return validateCounterParty;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
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.edc.connector.contract.spi.types.negotiation.ContractRequest.CALLBACK_ADDRESSES;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.CONNECTOR_ADDRESS;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.OFFER;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.PROTOCOL;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.PROVIDER_ID;
Expand Down Expand Up @@ -68,7 +68,7 @@ void setUp() {
void transform() {
var jsonObject = Json.createObjectBuilder()
.add(TYPE, ContractRequest.CONTRACT_REQUEST_TYPE)
.add(CONNECTOR_ADDRESS, "test-address")
.add(CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS, "test-address")
.add(PROTOCOL, "test-protocol")
.add(PROVIDER_ID, "test-provider-id")
.add(CALLBACK_ADDRESSES, createCallbackAddress())
Expand Down Expand Up @@ -109,10 +109,10 @@ void transform() {
}

@Test
void transform_shouldSetProviderIdAsConnectorAddress_whenProviderIdNotDefined() {
void transform_shouldSetProviderIdAsCounterPartyAddress_whenProviderIdNotDefined() {
var jsonObject = Json.createObjectBuilder()
.add(TYPE, ContractRequest.CONTRACT_REQUEST_TYPE)
.add(CONNECTOR_ADDRESS, "test-address")
.add(CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS, "test-address")
.add(PROTOCOL, "test-protocol")
.add(OFFER, Json.createObjectBuilder()
.add(OFFER_ID, "test-offer-id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import static jakarta.json.Json.createObjectBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.list;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_PROTOCOL;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_PROVIDER_URL;
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.edc.connector.contract.spi.types.negotiation.ContractRequest.CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.CONNECTOR_ADDRESS;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.OFFER;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.PROTOCOL;
Expand All @@ -43,7 +46,7 @@ class ContractRequestValidatorTest {
@Test
void shouldSuccess_whenObjectIsValid() {
var input = Json.createObjectBuilder()
.add(CONNECTOR_ADDRESS, value("http://connector-address"))
.add(CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS, value("http://connector-address"))
.add(PROTOCOL, value("protocol"))
.add(PROVIDER_ID, value("connector-id"))
.add(OFFER, createArrayBuilder().add(createObjectBuilder()
Expand All @@ -66,15 +69,15 @@ void shouldFail_whenMandatoryPropertiesAreMissing() {

assertThat(result).isFailed().extracting(ValidationFailure::getViolations).asInstanceOf(list(Violation.class))
.isNotEmpty()
.anySatisfy(violation -> assertThat(violation.path()).isEqualTo(CONNECTOR_ADDRESS))
.anySatisfy(violation -> assertThat(violation.path()).isEqualTo(CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS))
.anySatisfy(violation -> assertThat(violation.path()).isEqualTo(PROTOCOL))
.anySatisfy(violation -> assertThat(violation.path()).isEqualTo(OFFER));
}

@Test
void shouldFail_whenOfferMandatoryPropertiesAreMissing() {
var input = Json.createObjectBuilder()
.add(CONNECTOR_ADDRESS, value("http://connector-address"))
.add(CONTRACT_REQUEST_COUNTER_PARTY_ADDRESS, value("http://connector-address"))
.add(PROTOCOL, value("protocol"))
.add(PROVIDER_ID, value("connector-id"))
.add(OFFER, createArrayBuilder().add(createObjectBuilder()))
Expand All @@ -89,6 +92,24 @@ void shouldFail_whenOfferMandatoryPropertiesAreMissing() {
.anySatisfy(violation -> assertThat(violation.path()).isEqualTo(OFFER + "/" + POLICY));
}

@Test
void shouldSucceed_whenDeprecatedConnectorAddressIsUsed() {
var input = Json.createObjectBuilder()
.add(CONNECTOR_ADDRESS, value("http://connector-address"))
.add(PROTOCOL, value("protocol"))
.add(PROVIDER_ID, value("connector-id"))
.add(OFFER, createArrayBuilder().add(createObjectBuilder()
.add(OFFER_ID, value("offerId"))
.add(ASSET_ID, value("offerId"))
.add(POLICY, createArrayBuilder().add(createObjectBuilder()))
))
.build();

var result = validator.validate(input);

assertThat(result).isSucceeded();
}

private JsonArrayBuilder value(String value) {
return createArrayBuilder().add(createObjectBuilder().add(VALUE, value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public String negotiateContract(Participant provider, String offerId, String ass
.add(CONTEXT, createObjectBuilder().add(VOCAB, EDC_NAMESPACE))
.add(TYPE, "ContractRequestDto")
.add("providerId", provider.id)
.add("connectorAddress", provider.protocolEndpoint.url.toString())
.add("counterPartyAddress", provider.protocolEndpoint.url.toString())
.add("protocol", DSP_PROTOCOL)
.add("offer", createObjectBuilder()
.add("offerId", offerId)
Expand Down Expand Up @@ -292,7 +292,7 @@ public String initiateTransfer(Participant provider, String contractAgreementId,
.add("assetId", assetId)
.add("contractId", contractAgreementId)
.add("connectorId", provider.id)
.add("connectorAddress", provider.protocolEndpoint.url.toString())
.add("counterPartyAddress", provider.protocolEndpoint.url.toString())
.add("privateProperties", privateProperties)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ record TransferRequestSchema(
String type,
@Schema(requiredMode = REQUIRED)
String protocol,
@Schema(requiredMode = REQUIRED)
@Deprecated(since = "0.3.2")
@Schema(deprecated = true, description = "please use counterPartyAddress instead")
String connectorAddress,
@Schema(requiredMode = REQUIRED)
String counterPartyAddress,
@Schema(requiredMode = REQUIRED)
String connectorId,
@Schema(requiredMode = REQUIRED)
String contractId,
Expand All @@ -149,7 +152,7 @@ record TransferRequestSchema(
"@context": { "@vocab": "https://w3id.org/edc/v0.0.1/ns/" },
"@type": "https://w3id.org/edc/v0.0.1/ns/TransferRequest",
"protocol": "dataspace-protocol-http",
"connectorAddress": "http://provider-address",
"counterPartyAddress": "http://provider-address",
"connectorId": "provider-id",
"contractId": "contract-id",
"assetId": "asset-id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void initialize(ServiceExtensionContext context) {
transformerRegistry.register(new JsonObjectFromTransferStateTransformer(builderFactory));

transformerRegistry.register(new JsonObjectToTerminateTransferTransformer());
transformerRegistry.register(new JsonObjectToTransferRequestTransformer());
transformerRegistry.register(new JsonObjectToTransferRequestTransformer(context.getMonitor()));

validatorRegistry.register(TRANSFER_REQUEST_TYPE, TransferRequestValidator.instance());
validatorRegistry.register(TERMINATE_TRANSFER_TYPE, TerminateTransferValidator.instance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jakarta.json.JsonValue;
import org.eclipse.edc.connector.transfer.spi.types.TransferRequest;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.spi.types.domain.callback.CallbackAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
Expand All @@ -32,20 +33,25 @@

import static jakarta.json.JsonValue.ValueType.ARRAY;
import static jakarta.json.JsonValue.ValueType.OBJECT;
import static java.lang.String.format;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_ASSET_ID;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_CALLBACK_ADDRESSES;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_CONNECTOR_ADDRESS;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_CONNECTOR_ID;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_CONTRACT_ID;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_COUNTER_PARTY_ADDRESS;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_DATA_DESTINATION;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_PRIVATE_PROPERTIES;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_PROPERTIES;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_PROTOCOL;
import static org.eclipse.edc.connector.transfer.spi.types.TransferRequest.TRANSFER_REQUEST_TYPE;

public class JsonObjectToTransferRequestTransformer extends AbstractJsonLdTransformer<JsonObject, TransferRequest> {
private final Monitor monitor;

public JsonObjectToTransferRequestTransformer() {
public JsonObjectToTransferRequestTransformer(Monitor monitor) {
super(JsonObject.class, TransferRequest.class);
this.monitor = monitor;
}

@Override
Expand All @@ -54,7 +60,12 @@ public JsonObjectToTransferRequestTransformer() {

builder.id(nodeId(input));
visitProperties(input, k -> switch (k) {
case TRANSFER_REQUEST_CONNECTOR_ADDRESS -> v -> builder.connectorAddress(transformString(v, context));
case TRANSFER_REQUEST_CONNECTOR_ADDRESS -> v -> {
monitor.warning(format("The attribute %s has been deprecated in type %s, please use %s",
TRANSFER_REQUEST_CONNECTOR_ADDRESS, TRANSFER_REQUEST_TYPE, TRANSFER_REQUEST_COUNTER_PARTY_ADDRESS));
builder.counterPartyAddress(transformString(v, context));
};
case TRANSFER_REQUEST_COUNTER_PARTY_ADDRESS -> v -> builder.counterPartyAddress(transformString(v, context));
case TRANSFER_REQUEST_CONTRACT_ID -> (v) -> builder.contractId(transformString(v, context));
case TRANSFER_REQUEST_DATA_DESTINATION ->
v -> builder.dataDestination(transformObject(v, DataAddress.class, context));
Expand Down
Loading

0 comments on commit 26ed509

Please sign in to comment.