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(irs-api): Fix DiscoveryResponse #359

Merged
merged 9 commits into from
Jun 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public AssetAdministrationShellDescriptor getAAShellDescriptor(final DigitalTwin
log.info("Retrieved AAS Identification for DigitalTwinRegistryKey: {}", key);
final DiscoveryFinderRequest onlyBpn = new DiscoveryFinderRequest(List.of("bpn"));
final List<String> providedBpn = List.of(key.bpn());
final List<DiscoveryEndpoint> discoveryEndpoints = discoveryFinderClient.findDiscoveryEndpoints(onlyBpn);
final List<DiscoveryEndpoint> discoveryEndpoints = discoveryFinderClient.findDiscoveryEndpoints(onlyBpn)
.endpoints();
final List<String> connectorEndpoints = discoveryEndpoints.stream()
.map(discoveryEndpoint -> discoveryFinderClient.findConnectorEndpoints(
discoveryEndpoint.endpointAddress(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package org.eclipse.tractusx.irs.aaswrapper.registry.domain;

/**
* A result of finding Discovery Endpoint
* A single Discovery Endpoint.
*/
public record DiscoveryEndpoint(String type, String description, String endpointAddress, String documentation,
String resourceId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
*/
public interface DiscoveryFinderClient {

List<DiscoveryEndpoint> findDiscoveryEndpoints(DiscoveryFinderRequest request);
DiscoveryResponse findDiscoveryEndpoints(DiscoveryFinderRequest request);

List<EdcDiscoveryResult> findConnectorEndpoints(String endpointAddress, List<String> bpns);

}
Expand All @@ -61,14 +62,16 @@ class DiscoveryFinderClientImpl implements DiscoveryFinderClient {

@Override
@Retry(name = "registry")
public List<DiscoveryEndpoint> findDiscoveryEndpoints(final DiscoveryFinderRequest request) {
return restTemplate.postForObject(discoveryFinderUrl, request, List.class);
public DiscoveryResponse findDiscoveryEndpoints(final DiscoveryFinderRequest request) {
return restTemplate.postForObject(discoveryFinderUrl, request, DiscoveryResponse.class);
}

@Override
@Retry(name = "registry")
public List<EdcDiscoveryResult> findConnectorEndpoints(final String endpointAddress,
final List<String> bpns) {
return restTemplate.postForObject(endpointAddress, bpns, List.class);
public List<EdcDiscoveryResult> findConnectorEndpoints(final String endpointAddress, final List<String> bpns) {
final EdcDiscoveryResult[] edcDiscoveryResults = restTemplate.postForObject(endpointAddress, bpns,
EdcDiscoveryResult[].class);

return edcDiscoveryResults == null ? List.of() : List.of(edcDiscoveryResults);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/********************************************************************************
* Copyright (c) 2021,2022,2023
* 2022: ZF Friedrichshafen AG
* 2022: ISTOS GmbH
* 2022,2023: Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* 2022,2023: BOSCH AG
* Copyright (c) 2021,2022,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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. *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
package org.eclipse.tractusx.irs.aaswrapper.registry.domain;

import java.util.List;

/**
* The result of searching Discovery Endpoints.
* @param endpoints the list of {@link DiscoveryEndpoint}s
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
*/
public record DiscoveryResponse(List<DiscoveryEndpoint> endpoints) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,28 @@ class DecentralDigitalTwinRegistryServiceTest {
private final DecentralDigitalTwinRegistryClient decentralDigitalTwinRegistryClient = mock(
DecentralDigitalTwinRegistryClient.class);

private final DecentralDigitalTwinRegistryService decentralDigitalTwinRegistryService = new DecentralDigitalTwinRegistryService(discoveryFinderClient,
endpointDataForConnectorsService, decentralDigitalTwinRegistryClient);
private final DecentralDigitalTwinRegistryService decentralDigitalTwinRegistryService = new DecentralDigitalTwinRegistryService(
discoveryFinderClient, endpointDataForConnectorsService, decentralDigitalTwinRegistryClient);

@Test
void shouldReturnExpectedShell() {
// given
final DigitalTwinRegistryKey digitalTwinRegistryKey = new DigitalTwinRegistryKey("urn:uuid:4132cd2b-cbe7-4881-a6b4-39fdc31cca2b", "bpn");
final DigitalTwinRegistryKey digitalTwinRegistryKey = new DigitalTwinRegistryKey(
"urn:uuid:4132cd2b-cbe7-4881-a6b4-39fdc31cca2b", "bpn");
final AssetAdministrationShellDescriptor expectedShell = shellDescriptor(Collections.emptyList());
EndpointDataReference endpointDataReference = EndpointDataReference.Builder.newInstance().endpoint("url.to.host").build();
EndpointDataReference endpointDataReference = EndpointDataReference.Builder.newInstance()
.endpoint("url.to.host")
.build();
final List<DiscoveryEndpoint> discoveryEndpoints = List.of(
new DiscoveryEndpoint("type", "desc", "address", "doc", "resId"));
when(discoveryFinderClient.findDiscoveryEndpoints(any(DiscoveryFinderRequest.class))).thenReturn(
Collections.singletonList(new DiscoveryEndpoint("type", "desc", "address", "doc", "resId")));
when(endpointDataForConnectorsService.findEndpointDataForConnectors(anyList())).thenReturn(List.of(endpointDataReference));
when(decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink(any(), anyList())).thenReturn(Collections.emptyList());
when(decentralDigitalTwinRegistryClient.getAssetAdministrationShellDescriptor(any(), any())).thenReturn(expectedShell);
new DiscoveryResponse(discoveryEndpoints));
when(endpointDataForConnectorsService.findEndpointDataForConnectors(anyList())).thenReturn(
List.of(endpointDataReference));
when(decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink(any(),
anyList())).thenReturn(Collections.emptyList());
when(decentralDigitalTwinRegistryClient.getAssetAdministrationShellDescriptor(any(), any())).thenReturn(
expectedShell);

// when
final AssetAdministrationShellDescriptor actualShell = decentralDigitalTwinRegistryService.getAAShellDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public class ContractNegotiationService {

private final EdcControlPlaneClient edcControlPlaneClient;

private final EdcConfiguration config;

private final PolicyCheckerService policyCheckerService;

public NegotiationResponse negotiate(final String providerConnectorUrl, final CatalogItem catalogItem)
Expand All @@ -72,8 +70,7 @@ public NegotiationResponse negotiate(final String providerConnectorUrl, final Ca

final NegotiationRequest negotiationRequest = NegotiationRequest.builder()
.connectorId(catalogItem.getConnectorId())
.connectorAddress(providerConnectorUrl
+ config.getControlplane().getProviderSuffix())
.connectorAddress(providerConnectorUrl)
.offer(contractOfferRequest)
.build();

Expand All @@ -94,7 +91,7 @@ public NegotiationResponse negotiate(final String providerConnectorUrl, final Ca
.managedResources(TransferProcessRequest.DEFAULT_MANAGED_RESOURCES)
.connectorId(catalogItem.getConnectorId())
.connectorAddress(
providerConnectorUrl + config.getControlplane().getProviderSuffix())
providerConnectorUrl)
.contractId(response.getContractAgreementId())
.assetId(catalogItem.getAssetPropId())
.dataDestination(destination)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ public class EdcControlPlaneClient {

/* package */ Catalog getCatalog(final String providerConnectorUrl, final int offset) {
final var catalogUrl = config.getControlplane().getEndpoint().getData() + "/catalog/request";
final var providerUrl = providerConnectorUrl + config.getControlplane().getProviderSuffix();
final var limit = config.getControlplane().getCatalogPageSize();

final CatalogRequest request = buildCatalogRequest(offset, providerUrl, limit);
final CatalogRequest request = buildCatalogRequest(offset, providerConnectorUrl, limit);
return edcRestTemplate.exchange(catalogUrl, HttpMethod.POST, new HttpEntity<>(request, headers()),
Catalog.class).getBody();
}
Expand All @@ -80,9 +79,9 @@ private CatalogRequest buildCatalogRequest(final int offset, final String provid
return CatalogRequest.builder().providerUrl(providerUrl).querySpec(querySpec.build()).build();
}

/* package */ Catalog getCatalogWithFilter(final String providerConnectorUrl, final String key,
final String value) {
/* package */ Catalog getCatalogWithFilter(final String providerConnectorUrl, final String key, final String value) {
final var catalogUrl = config.getControlplane().getEndpoint().getData() + "/catalog/request";

final var querySpec = QuerySpec.Builder.newInstance().filter(List.of(new Criterion(key, "=", value)));
final var catalogRequest = CatalogRequest.builder()
.providerUrl(providerConnectorUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
import org.apache.commons.validator.routines.UrlValidator;
import org.eclipse.dataspaceconnector.spi.types.domain.catalog.Catalog;
import org.eclipse.dataspaceconnector.spi.types.domain.edr.EndpointDataReference;
import org.eclipse.tractusx.irs.common.CxTestDataContainer;
import org.eclipse.tractusx.irs.common.Masker;
import org.eclipse.tractusx.irs.common.OutboundMeterRegistryService;
import org.eclipse.tractusx.irs.component.Relationship;
import org.eclipse.tractusx.irs.edc.client.exceptions.EdcClientException;
import org.eclipse.tractusx.irs.edc.client.model.CatalogItem;
import org.eclipse.tractusx.irs.edc.client.model.NegotiationResponse;
import org.eclipse.tractusx.irs.edc.client.model.notification.EdcNotification;
import org.eclipse.tractusx.irs.edc.client.model.notification.EdcNotificationResponse;
import org.eclipse.tractusx.irs.common.CxTestDataContainer;
import org.eclipse.tractusx.irs.common.Masker;
import org.eclipse.tractusx.irs.common.OutboundMeterRegistryService;
import org.eclipse.tractusx.irs.component.Relationship;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
Expand Down Expand Up @@ -136,6 +136,11 @@ class EdcSubmodelClientImpl implements EdcSubmodelClient {
private final EdcControlPlaneClient edcControlPlaneClient;
private final UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);

private static void stopWatchOnEdcTask(final StopWatch stopWatch) {
stopWatch.stop();
log.info("EDC Task '{}' took {} ms", stopWatch.getLastTaskName(), stopWatch.getLastTaskTimeMillis());
}

@Override
public CompletableFuture<List<Relationship>> getRelationships(final String submodelEndpointAddress,
final RelationshipAspect traversalAspectType) throws EdcClientException {
Expand Down Expand Up @@ -163,10 +168,12 @@ private NegotiationResponse fetchNegotiationResponse(final String submodelEndpoi
final String providerConnectorUrl = submodelEndpointAddress.substring(0, indexOfUrn);
final String target = submodelEndpointAddress.substring(indexOfUrn + 1, indexOfSubModel);
final String decodedTarget = URLDecoder.decode(target, StandardCharsets.UTF_8);
log.info("Starting contract negotiation with providerConnectorUrl {} and target {}", providerConnectorUrl,
final String providerWithSuffix = appendSuffix(providerConnectorUrl,
config.getControlplane().getProviderSuffix());
log.info("Starting contract negotiation with providerConnectorUrl {} and target {}", providerWithSuffix,
decodedTarget);
final CatalogItem catalogItem = catalogCache.getCatalogItem(providerConnectorUrl, decodedTarget).orElseThrow();
return contractNegotiationService.negotiate(providerConnectorUrl, catalogItem);
final CatalogItem catalogItem = catalogCache.getCatalogItem(providerWithSuffix, decodedTarget).orElseThrow();
return contractNegotiationService.negotiate(providerWithSuffix, catalogItem);
}

private CompletableFuture<List<Relationship>> startSubmodelDataRetrieval(
Expand Down Expand Up @@ -288,20 +295,22 @@ public CompletableFuture<EndpointDataReference> getEndpointReferenceForAsset(fin
return execute(endpointAddress, () -> {
final StopWatch stopWatch = new StopWatch();
stopWatch.start("Get EDC Submodel task for shell descriptor, endpoint " + endpointAddress);
final String providerWithSuffix = appendSuffix(endpointAddress, config.getControlplane().getProviderSuffix());

final Catalog catalog = edcControlPlaneClient.getCatalogWithFilter(endpointAddress, filterKey, filterValue);
final Catalog catalog = edcControlPlaneClient.getCatalogWithFilter(providerWithSuffix, filterKey, filterValue);

final List<CatalogItem> items = catalog.getContractOffers()
.stream()
.map(contractOffer -> CatalogItem.builder()
.itemId(contractOffer.getId())
.assetPropId(
contractOffer.getAsset().getId())
.connectorId(catalog.getId())
.policy(contractOffer.getPolicy())
.build())
.toList();
final NegotiationResponse response = contractNegotiationService.negotiate(endpointAddress,
.stream()
.map(contractOffer -> CatalogItem.builder()
.itemId(contractOffer.getId())
.assetPropId(
contractOffer.getAsset()
.getId())
.connectorId(catalog.getId())
.policy(contractOffer.getPolicy())
.build())
.toList();
final NegotiationResponse response = contractNegotiationService.negotiate(providerWithSuffix,
items.stream().findFirst().orElseThrow());

return pollingService.<EndpointDataReference>createJob()
Expand All @@ -314,17 +323,21 @@ public CompletableFuture<EndpointDataReference> getEndpointReferenceForAsset(fin
});
}

private String appendSuffix(final String endpointAddress, final String providerSuffix) {
String addressWithSuffix;
if (endpointAddress.endsWith("/") && providerSuffix.startsWith("/")) {
addressWithSuffix = endpointAddress.substring(0, endpointAddress.length() - 1) + providerSuffix;
} else {
addressWithSuffix = endpointAddress + providerSuffix;
}
return addressWithSuffix;
}

private Optional<EndpointDataReference> retrieveEndpointDataReference(final String contractAgreementId) {
log.info("Retrieving dataReference from storage for contractAgreementId {}", Masker.mask(contractAgreementId));
return endpointDataReferenceStorage.remove(contractAgreementId);
}

private static void stopWatchOnEdcTask(final StopWatch stopWatch) {
stopWatch.stop();
log.info("EDC Task '{}' took {} ms", stopWatch.getLastTaskName(), stopWatch.getLastTaskTimeMillis());
}

@SuppressWarnings({ "PMD.AvoidRethrowingException",
"PMD.AvoidCatchingGenericException"
})
Expand Down
Loading