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

Feature: Add config parameter for ProviderGatewayController context registration #796

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ When the proxy receive a request, it must contain the EDR, which will be decoded
## Configuration

| Key | Required | Default | Description |
|---------------------------------|----------|----------------------------------------------------------------------------------------|
|--------------------------------------------|----------------------------------------------------------------------------------------|
| tx.dpf.provider.proxy.thread.pool | | 10 | Thread pool size for the provider data plane proxy gateway |
| edc.dataplane.token.validation.endpoint | x | | URL of the token validation endpoint |
| web.http.gateway.context.path | | | Path to register the ProviderGatewayController to |
| web.http.gateway.context.port | | | Port to register the ProviderGatewayController to |
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ public class DataPlaneProxyProviderApiExtension implements ServiceExtension {
static final String NAME = "Data Plane Proxy Provider API";
@Setting(value = "Thread pool size for the provider data plane proxy gateway", type = "int")
private static final String THREAD_POOL_SIZE = "tx.dpf.provider.proxy.thread.pool";

@Setting(value = "Path to register the ProviderGatewayController to", type = "String")
private static final String WEB_HTTP_GATEWAY_PATH_SETTING = "web.http.gateway.path";

@Setting(value = "Port to register the ProviderGatewayController to", type = "int")
private static final String WEB_HTTP_GATEWAY_PORT_SETTING = "web.http.gateway.port";

private static final String GATEWAY_CONTEXT = "gateway";

@Setting
private static final String CONTROL_PLANE_VALIDATION_ENDPOINT = "edc.dataplane.token.validation.endpoint";

@Inject
private WebService webService;

Expand Down Expand Up @@ -74,7 +84,7 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
executorService = newFixedThreadPool(context.getSetting(THREAD_POOL_SIZE, DEFAULT_THREAD_POOL));
executorService = newFixedThreadPool(context.getConfig().getInteger(THREAD_POOL_SIZE, DEFAULT_THREAD_POOL));

var validationEndpoint = context.getConfig().getString(CONTROL_PLANE_VALIDATION_ENDPOINT);

Expand All @@ -87,7 +97,13 @@ public void initialize(ServiceExtensionContext context) {
executorService,
monitor);

webService.registerResource(controller);
// If a setting for the port mapping for a separate gateway context exists, we assume the context also exists and register into that
// Otherwise we use the default context
if (context.getConfig().hasKey(WEB_HTTP_GATEWAY_PATH_SETTING) && context.getConfig().hasKey(WEB_HTTP_GATEWAY_PORT_SETTING)) {
webService.registerResource(GATEWAY_CONTEXT, controller);
} else {
webService.registerResource(controller);
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2023 Mercedes Benz Tech Innovation GmbH
*
* 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:
* Mercedes Benz Tech Innovation GmbH - initial API and implementation
*
*/

package org.eclipse.tractusx.edc.dataplane.proxy.provider.api.response;

import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.configuration.ConfigFactory;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.tractusx.edc.dataplane.proxy.provider.api.DataPlaneProxyProviderApiExtension;
import org.eclipse.tractusx.edc.dataplane.proxy.provider.api.gateway.ProviderGatewayController;
import org.eclipse.tractusx.edc.dataplane.proxy.provider.api.validation.ProxyProviderDataAddressResolver;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;

import java.util.HashMap;
import java.util.Map;

@ExtendWith(DependencyInjectionExtension.class)
class DataPlaneProxyProviderApiExtensionTest {

private DataPlaneProxyProviderApiExtension extension;

private static final String CONFIG_THREAD_POOL_SIZE_KEY = "tx.dpf.provider.proxy.thread.pool";
private static final String CONFIG_THREAD_POOL_SIZE_VALUE = "10";

private static final String CONFIG_WEB_HTTP_GATEWAY_PATH_KEY = "web.http.gateway.path";
private static final String CONFIG_WEB_HTTP_GATEWAY_PATH_VALUE = "/api/v1/gateway";

private static final String CONFIG_WEB_HTTP_GATEWAY_PORT_KEY = "web.http.gateway.port";
private static final String CONFIG_WEB_HTTP_GATEWAY_PORT_VALUE = "11111";

private static final String CONFIG_CONTROL_PLANE_VALIDATION_ENDPOINT_KEY = "edc.dataplane.token.validation.endpoint";
private static final String CONFIG_CONTROL_PLANE_VALIDATION_ENDPOINT_VALUE = "http://example.com";

// mocks
private ServiceExtensionContext serviceExtensionContext;
private ProviderGatewayController providerGatewayController;
private Monitor monitor;
private WebService webService;
private ProxyProviderDataAddressResolver proxyProviderDataAddressResolver;
private TypeManager typeManager;


@BeforeEach
void setup(ObjectFactory factory, ServiceExtensionContext context) {
serviceExtensionContext = Mockito.mock(ServiceExtensionContext.class);
providerGatewayController = Mockito.mock(ProviderGatewayController.class);
monitor = Mockito.mock(Monitor.class);
webService = Mockito.mock(WebService.class);
proxyProviderDataAddressResolver = Mockito.mock(ProxyProviderDataAddressResolver.class);
typeManager = Mockito.mock(TypeManager.class);

Mockito.when(serviceExtensionContext.getService(ProviderGatewayController.class))
.thenReturn(providerGatewayController);
Mockito.when(serviceExtensionContext.getMonitor()).thenReturn(monitor);
context.registerService(TypeManager.class, typeManager);
context.registerService(WebService.class, webService);

extension = factory.constructInstance(DataPlaneProxyProviderApiExtension.class);
}

private Map<String, String> getConfig() {
return new HashMap<>() {
{
put(CONFIG_THREAD_POOL_SIZE_KEY, CONFIG_THREAD_POOL_SIZE_VALUE);
put(CONFIG_CONTROL_PLANE_VALIDATION_ENDPOINT_KEY, CONFIG_CONTROL_PLANE_VALIDATION_ENDPOINT_VALUE);
}
};
}

private Map<String, String> getConfigWithContext() {
var config = getConfig();
config.put(CONFIG_WEB_HTTP_GATEWAY_PATH_KEY, CONFIG_WEB_HTTP_GATEWAY_PATH_VALUE);
config.put(CONFIG_WEB_HTTP_GATEWAY_PORT_KEY, CONFIG_WEB_HTTP_GATEWAY_PORT_VALUE);
return config;
}

@Test
void testInitialize() {
var config = ConfigFactory.fromMap(getConfig());
Mockito.when(serviceExtensionContext.getConfig()).thenReturn(config);

extension.initialize(serviceExtensionContext);

Mockito.verify(webService, Mockito.times(1)).registerResource(Mockito.any(ProviderGatewayController.class));
}

@Test
void testInitializeWithContext() {
var config = ConfigFactory.fromMap(getConfigWithContext());
Mockito.when(serviceExtensionContext.getConfig()).thenReturn(config);

extension.initialize(serviceExtensionContext);

Mockito.verify(webService, Mockito.times(1)).registerResource(Mockito.any(String.class), Mockito.any(ProviderGatewayController.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public class TestRuntimeConfiguration {
public static final String PLATO_DSP_CALLBACK = "http://localhost:" + PLATO_DSP_API_PORT + DSP_PATH;
static final int SOKRATES_CONNECTOR_PORT = getFreePort();
static final int SOKRATES_MANAGEMENT_PORT = getFreePort();
static final int SOKRATES_GATEWAY_PORT = getFreePort();
static final String SOKRATES_CONNECTOR_PATH = "/api";
static final String SOKRATES_MANAGEMENT_PATH = "/api/v1/management";
static final String SOKRATES_GATEWAY_PATH = "/api/v1/gateway";
static final int SOKRATES_DSP_API_PORT = getFreePort();
public static final String SOKRATES_DSP_CALLBACK = "http://localhost:" + SOKRATES_DSP_API_PORT + DSP_PATH;
static final String SOKRATES_PUBLIC_API_PORT = String.valueOf(getFreePort());
Expand Down Expand Up @@ -96,6 +98,8 @@ public static Map<String, String> sokratesConfiguration() {
put("edc.api.auth.key", "testkey");
put("web.http.public.path", "/api/public");
put("web.http.public.port", SOKRATES_PUBLIC_API_PORT);
put("web.http.gateway.path", SOKRATES_GATEWAY_PATH);
put("web.http.gateway.port", String.valueOf(SOKRATES_GATEWAY_PORT));

put("edc.transfer.send.retry.limit", "1");
put("edc.transfer.send.retry.base-delay.ms", "100");
Expand Down