Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make dpselector api jsonld aware #3310

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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering
* 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
Expand All @@ -8,11 +8,11 @@
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from;
package org.eclipse.edc.core.transform.transformer.from;

import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.edc.core.transform.transformer.from.JsonObjectFromAssetTransformer;
import org.eclipse.edc.core.transform.transformer.from.JsonObjectFromCatalogTransformer;
import org.eclipse.edc.core.transform.transformer.from.JsonObjectFromCriterionTransformer;
import org.eclipse.edc.core.transform.transformer.from.JsonObjectFromDataAddressTransformer;
import org.eclipse.edc.core.transform.transformer.from.JsonObjectFromDataServiceTransformer;
import org.eclipse.edc.core.transform.transformer.from.JsonObjectFromDatasetTransformer;
import org.eclipse.edc.core.transform.transformer.from.JsonObjectFromDistributionTransformer;
Expand All @@ -27,6 +28,7 @@
import org.eclipse.edc.core.transform.transformer.to.JsonObjectToAssetTransformer;
import org.eclipse.edc.core.transform.transformer.to.JsonObjectToCatalogTransformer;
import org.eclipse.edc.core.transform.transformer.to.JsonObjectToCriterionTransformer;
import org.eclipse.edc.core.transform.transformer.to.JsonObjectToDataAddressTransformer;
import org.eclipse.edc.core.transform.transformer.to.JsonObjectToDataServiceTransformer;
import org.eclipse.edc.core.transform.transformer.to.JsonObjectToDatasetTransformer;
import org.eclipse.edc.core.transform.transformer.to.JsonObjectToDistributionTransformer;
Expand Down Expand Up @@ -63,25 +65,25 @@
@Extension(value = DspApiConfigurationExtension.NAME)
@Provides({ DspApiConfiguration.class, ProtocolWebhook.class })
public class DspApiConfigurationExtension implements ServiceExtension {

public static final String NAME = "Dataspace Protocol API Configuration Extension";

public static final String CONTEXT_ALIAS = "protocol";

public static final String DEFAULT_DSP_CALLBACK_ADDRESS = "http://localhost:8282/api/v1/dsp";
public static final String DSP_CALLBACK_ADDRESS = "edc.dsp.callback.address";

public static final int DEFAULT_PROTOCOL_PORT = 8282;
public static final String DEFAULT_PROTOCOL_API_PATH = "/api/v1/dsp";

public static final WebServiceSettings SETTINGS = WebServiceSettings.Builder.newInstance()
.apiConfigKey("web.http.protocol")
.contextAlias(CONTEXT_ALIAS)
.defaultPath(DEFAULT_PROTOCOL_API_PATH)
.defaultPort(DEFAULT_PROTOCOL_PORT)
.name("Protocol API")
.build();

@Inject
private TypeManager typeManager;

Expand All @@ -99,20 +101,20 @@ public class DspApiConfigurationExtension implements ServiceExtension {

@Inject
private TypeTransformerRegistry transformerRegistry;

@Override
public String name() {
return NAME;
}

@Override
public void initialize(ServiceExtensionContext context) {
var config = configurator.configure(context, webServer, SETTINGS);
var dspWebhookAddress = context.getSetting(DSP_CALLBACK_ADDRESS, DEFAULT_DSP_CALLBACK_ADDRESS);
context.registerService(DspApiConfiguration.class, new DspApiConfiguration(config.getContextAlias(), dspWebhookAddress));

context.registerService(ProtocolWebhook.class, () -> dspWebhookAddress);

var jsonLdMapper = typeManager.getMapper(JSON_LD);
webService.registerResource(config.getContextAlias(), new ObjectMapperProvider(jsonLdMapper));
webService.registerResource(config.getContextAlias(), new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper));
Expand All @@ -133,6 +135,7 @@ private void registerTransformers() {
transformerRegistry.register(new JsonObjectFromDistributionTransformer(jsonBuilderFactory));
transformerRegistry.register(new JsonObjectFromDataServiceTransformer(jsonBuilderFactory));
transformerRegistry.register(new JsonObjectFromAssetTransformer(jsonBuilderFactory, mapper));
transformerRegistry.register(new JsonObjectFromDataAddressTransformer(jsonBuilderFactory));
transformerRegistry.register(new JsonObjectFromQuerySpecTransformer(jsonBuilderFactory));
transformerRegistry.register(new JsonObjectFromCriterionTransformer(jsonBuilderFactory, mapper));

Expand All @@ -150,6 +153,7 @@ private void registerTransformers() {
transformerRegistry.register(new JsonObjectToAssetTransformer());
transformerRegistry.register(new JsonObjectToQuerySpecTransformer());
transformerRegistry.register(new JsonObjectToCriterionTransformer());
transformerRegistry.register(new JsonObjectToDataAddressTransformer());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
package org.eclipse.edc.protocol.dsp.transferprocess.transformer;

import jakarta.json.Json;
import org.eclipse.edc.core.transform.transformer.to.JsonObjectToDataAddressTransformer;
import org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from.JsonObjectFromDataAddressTransformer;
import org.eclipse.edc.core.transform.transformer.from.JsonObjectFromDataAddressTransformer;
import org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from.JsonObjectFromTransferCompletionMessageTransformer;
import org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from.JsonObjectFromTransferProcessTransformer;
import org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from.JsonObjectFromTransferRequestMessageTransformer;
Expand Down Expand Up @@ -68,6 +67,5 @@ public void initialize(ServiceExtensionContext context) {
registry.register(new JsonObjectToTransferCompletionMessageTransformer());
registry.register(new JsonObjectToTransferStartMessageTransformer());
registry.register(new JsonObjectToTransferTerminationMessageTransformer());
registry.register(new JsonObjectToDataAddressTransformer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import jakarta.json.Json;
import jakarta.json.JsonBuilderFactory;
import org.eclipse.edc.protocol.dsp.transferprocess.transformer.type.from.JsonObjectFromDataAddressTransformer;
import org.eclipse.edc.core.transform.transformer.from.JsonObjectFromDataAddressTransformer;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.eclipse.edc.connector.api.management.configuration.transform.JsonObjectFromContractAgreementTransformer;
import org.eclipse.edc.connector.api.management.configuration.transform.ManagementApiTypeTransformerRegistry;
import org.eclipse.edc.connector.api.management.configuration.transform.ManagementApiTypeTransformerRegistryImpl;
import org.eclipse.edc.core.transform.transformer.to.JsonObjectToDataAddressTransformer;
import org.eclipse.edc.core.transform.transformer.to.JsonValueToGenericTypeTransformer;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
Expand All @@ -42,20 +44,16 @@

/**
* Tells all the Management API controllers under which context alias they need to register their resources: either `default` or `management`
*
*/
@Provides(ManagementApiConfiguration.class)
@Extension(value = ManagementApiConfigurationExtension.NAME)
public class ManagementApiConfigurationExtension implements ServiceExtension {

public static final String NAME = "Management API configuration";

public static final String WEB_SERVICE_NAME = "Management API";
private static final String MANAGEMENT_CONTEXT_ALIAS = "management";
private static final int DEFAULT_MANAGEMENT_API_PORT = 8181;
private static final String DEFAULT_MANAGEMENT_API_CONTEXT_PATH = "/api/v1/management";

public static final String WEB_SERVICE_NAME = "Management API";

public static final WebServiceSettings SETTINGS = WebServiceSettings.Builder.newInstance()
.apiConfigKey("web.http." + MANAGEMENT_CONTEXT_ALIAS)
.contextAlias(MANAGEMENT_CONTEXT_ALIAS)
Expand Down Expand Up @@ -109,6 +107,8 @@ public ManagementApiTypeTransformerRegistry managementApiTypeTransformerRegistry

var registry = new ManagementApiTypeTransformerRegistryImpl(this.transformerRegistry);
registry.register(new JsonObjectFromContractAgreementTransformer(factory));
registry.register(new JsonObjectToDataAddressTransformer());
registry.register(new JsonValueToGenericTypeTransformer(typeManager.getMapper(JSON_LD)));
return registry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@

import java.util.Optional;

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

public class ConsumerPullDataPlaneProxyResolver {

private static final String PUBLIC_API_URL_PROPERTY = "publicApiUrl";
private static final String PUBLIC_API_URL_PROPERTY_DEPRECATED = "publicApiUrl";
private static final String PUBLIC_API_URL_PROPERTY = EDC_NAMESPACE + "publicApiUrl";

private final DataEncrypter dataEncrypter;
private final TypeManager typeManager;
Expand All @@ -44,6 +47,11 @@ public ConsumerPullDataPlaneProxyResolver(DataEncrypter dataEncrypter, TypeManag
this.tokenGenerationService = tokenGenerationService;
}

private static Object getPublicApiUrl(DataPlaneInstance instance) {
return Optional.ofNullable(instance.getProperties().get(PUBLIC_API_URL_PROPERTY))
.orElseGet(() -> instance.getProperties().get(PUBLIC_API_URL_PROPERTY_DEPRECATED));
}

public Result<DataAddress> toDataAddress(DataRequest request, DataAddress address, DataPlaneInstance instance) {
return resolveProxyUrl(instance)
.compose(proxyUrl -> generateAccessToken(address, request.getContractId())
Expand All @@ -57,9 +65,9 @@ public Result<DataAddress> toDataAddress(DataRequest request, DataAddress addres
}

private Result<String> resolveProxyUrl(DataPlaneInstance instance) {
return Optional.ofNullable(instance.getProperties().get(PUBLIC_API_URL_PROPERTY))
return Optional.ofNullable(getPublicApiUrl(instance))
.map(url -> Result.success((String) url))
.orElse(Result.failure(String.format("Missing property `%s` in DataPlaneInstance", PUBLIC_API_URL_PROPERTY)));
.orElse(Result.failure(String.format("Missing property `%s` (deprecated: `%s`) in DataPlaneInstance", PUBLIC_API_URL_PROPERTY, PUBLIC_API_URL_PROPERTY_DEPRECATED)));
}

private Result<String> generateAccessToken(DataAddress source, String contractId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class ConsumerPullDataPlaneProxyResolverTest {

private final ConsumerPullDataPlaneProxyResolver resolver = new ConsumerPullDataPlaneProxyResolver(dataEncrypter, TYPE_MANAGER, tokenGenerationService, tokenExpirationDateFunction);

private static DataAddress dataAddress() {
return DataAddress.Builder.newInstance().type(UUID.randomUUID().toString()).build();
}

@Test
void verifyToDataAddressSuccess() {
var address = dataAddress();
Expand Down Expand Up @@ -99,7 +103,7 @@ void verifyToDataAddressReturnsFailureIfMissingPublicApiUrl() {
var result = resolver.toDataAddress(dataRequest(), dataAddress(), instance);

assertThat(result.failed()).isTrue();
assertThat(result.getFailureDetail()).isEqualTo("Missing property `publicApiUrl` in DataPlaneInstance");
assertThat(result.getFailureDetail()).isEqualTo("Missing property `https://w3id.org/edc/v0.0.1/ns/publicApiUrl` (deprecated: `publicApiUrl`) in DataPlaneInstance");
}

@Test
Expand Down Expand Up @@ -143,10 +147,6 @@ void verifyToDataAddressReturnsFailureIfTokenGenerationFails() {
assertThat(result.getFailureDetail()).contains(errorMsg);
}

private static DataAddress dataAddress() {
return DataAddress.Builder.newInstance().type(UUID.randomUUID().toString()).build();
}

private DataRequest dataRequest() {
return DataRequest.Builder.newInstance()
.id(UUID.randomUUID().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ dependencies {
api(project(":spi:data-plane-selector:data-plane-selector-spi"))
api(project(":spi:common:aggregate-service-spi"))
implementation(project(":core:common:util"))
implementation(project(":extensions:common:json-ld"))
implementation(project(":extensions:common:api:management-api-configuration"))
implementation(project(":extensions:common:api:api-core")) //for the exception mapper
implementation(libs.jakarta.rsApi)

testImplementation(project(":core:data-plane-selector:data-plane-selector-core"))
testImplementation(project(":extensions:common:http"))
testImplementation(project(":core:common:junit"))
// for the TypeTransformerRegistryImpl
testImplementation(project(":core:common:transform-core"))
// for the JsonObject-To-DataAddress transformer
paullatzelsperger marked this conversation as resolved.
Show resolved Hide resolved
testImplementation(project(":extensions:common:api:management-api-configuration"))

testImplementation(libs.restAssured)
}

edcBuild {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@
package org.eclipse.edc.connector.dataplane.selector;

import org.eclipse.edc.connector.api.management.configuration.ManagementApiConfiguration;
import org.eclipse.edc.connector.dataplane.selector.api.DataplaneSelectorApiController;
import org.eclipse.edc.connector.api.management.configuration.transform.ManagementApiTypeTransformerRegistry;
import org.eclipse.edc.connector.dataplane.selector.api.v2.DataplaneSelectorApiController;
import org.eclipse.edc.connector.dataplane.selector.spi.DataPlaneSelectorService;
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance;
import org.eclipse.edc.connector.dataplane.selector.transformer.JsonObjectFromDataPlaneInstanceTransformer;
import org.eclipse.edc.connector.dataplane.selector.transformer.JsonObjectToDataPlaneInstanceTransformer;
import org.eclipse.edc.connector.dataplane.selector.transformer.JsonObjectToSelectionRequestTransformer;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.web.spi.WebService;

import java.util.Map;

import static jakarta.json.Json.createBuilderFactory;
import static org.eclipse.edc.spi.CoreConstants.JSON_LD;

@Extension(value = "DataPlane selector API")
public class DataPlaneSelectorApiExtension implements ServiceExtension {

Expand All @@ -39,6 +48,8 @@ public class DataPlaneSelectorApiExtension implements ServiceExtension {

@Inject
private TypeManager typeManager;
@Inject
private ManagementApiTypeTransformerRegistry transformerRegistry;

@Override
public void initialize(ServiceExtensionContext context) {
Expand All @@ -47,7 +58,10 @@ public void initialize(ServiceExtensionContext context) {

typeManager.registerTypes(DataPlaneInstance.class);

var controller = new DataplaneSelectorApiController(selectionService);
transformerRegistry.register(new JsonObjectToSelectionRequestTransformer());
transformerRegistry.register(new JsonObjectToDataPlaneInstanceTransformer());
transformerRegistry.register(new JsonObjectFromDataPlaneInstanceTransformer(createBuilderFactory(Map.of()), typeManager.getMapper(JSON_LD)));
var controller = new DataplaneSelectorApiController(selectionService, transformerRegistry);

webservice.registerResource(managementApiConfiguration.getContextAlias(), controller);
}
Expand Down

This file was deleted.

This file was deleted.

Loading