Skip to content

Commit

Permalink
feat(impl):[#359] update registry impl for lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-ext-kmassalski committed Mar 22, 2024
1 parent da1d6a7 commit ff53d66
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@
********************************************************************************/
package org.eclipse.tractusx.irs.registryclient.decentral;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.github.resilience4j.retry.annotation.Retry;
import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference;
import org.eclipse.tractusx.irs.component.assetadministrationshell.AssetAdministrationShellDescriptor;
import org.eclipse.tractusx.irs.component.assetadministrationshell.IdentifierKeyValuePair;
import org.eclipse.tractusx.irs.data.StringMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand All @@ -49,6 +54,14 @@ public class DecentralDigitalTwinRegistryClient {
private static final String PLACEHOLDER_AAS_IDENTIFIER = "aasIdentifier";
private static final String PLACEHOLDER_ASSET_IDS = "assetIds";

private static final ObjectWriter WRITER;
static {
final ObjectMapper MAPPER = new ObjectMapper();
MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
WRITER = MAPPER.writer();
}

private final RestTemplate edcRestTemplate;
private final String shellDescriptorTemplate;
private final String lookupShellsTemplate;
Expand All @@ -61,10 +74,6 @@ public DecentralDigitalTwinRegistryClient(final RestTemplate edcRestTemplate,
this.lookupShellsTemplate = lookupShellsTemplate;
}

private static String encodeWithBase64(final String aasIdentifier) {
return Base64.getEncoder().encodeToString(aasIdentifier.getBytes(StandardCharsets.UTF_8));
}

@Retry(name = "registry")
public AssetAdministrationShellDescriptor getAssetAdministrationShellDescriptor(
final EndpointDataReference endpointDataReference, final String aasIdentifier) {
Expand All @@ -78,14 +87,32 @@ public AssetAdministrationShellDescriptor getAssetAdministrationShellDescriptor(

@Retry(name = "registry")
public LookupShellsResponse getAllAssetAdministrationShellIdsByAssetLink(
final EndpointDataReference endpointDataReference, final List<IdentifierKeyValuePair> assetIds) {
final EndpointDataReference endpointDataReference, final IdentifierKeyValuePair assetIds) {
final String shellLookupEndpoint = endpointDataReference.getEndpoint() + lookupShellsTemplate;
final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(shellLookupEndpoint);
uriBuilder.uriVariables(Map.of(PLACEHOLDER_ASSET_IDS, StringMapper.mapToString(assetIds)));
uriBuilder.uriVariables(Map.of(PLACEHOLDER_ASSET_IDS, encodeWithBase64(assetIds)));
return edcRestTemplate.exchange(uriBuilder.build().toUri(), HttpMethod.GET,
new HttpEntity<>(null, headers(endpointDataReference)), LookupShellsResponse.class).getBody();
}

private static String encodeWithBase64(final String aasIdentifier) {
return Base64.getEncoder().encodeToString(aasIdentifier.getBytes(StandardCharsets.UTF_8));
}

private static String encodeWithBase64(final IdentifierKeyValuePair assetIds) {
return Base64.getEncoder().encodeToString(serialize(assetIds));
}

private static byte[] serialize(final IdentifierKeyValuePair assetIds) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
WRITER.writeValue(os, assetIds);
return os.toByteArray();
} catch (final IOException e) {
return new byte[0];
}
}

private HttpHeaders headers(final EndpointDataReference dataReference) {
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private String mapToShellId(final EndpointDataReference endpointDataReference, f

// Try to map the provided ID to the corresponding asset administration shell ID
final var mappingResultStream = decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink(
endpointDataReference, List.of(identifierKeyValuePair)).getResult().stream();
endpointDataReference, identifierKeyValuePair).getResult().stream();

// Special scenario: Multiple DTs with the same globalAssetId in one DTR, see:
// docs/arc42/cross-cutting/discovery-DTR--multiple-DTs-with-the-same-globalAssedId-in-one-DTR.puml
Expand Down Expand Up @@ -340,7 +340,7 @@ private Collection<String> lookupShellIds(final String bpn, final EndpointDataRe
try {
return decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink(
endpointDataReference,
List.of(IdentifierKeyValuePair.builder().name("manufacturerId").value(bpn).build())).getResult();
IdentifierKeyValuePair.builder().name("manufacturerId").value(bpn).build()).getResult();
} finally {
watch.stop();
log.info(TOOK_MS, watch.getLastTaskName(), watch.getLastTaskTimeMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference;
import org.eclipse.tractusx.irs.component.assetadministrationshell.AssetAdministrationShellDescriptor;
import org.eclipse.tractusx.irs.component.assetadministrationshell.IdentifierKeyValuePair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -59,7 +60,7 @@ void shouldCallForAllAssetAdministrationShellIdsByAssetLink() {
ResponseEntity.of(Optional.of(LookupShellsResponse.builder().result(Collections.emptyList()).build())));

// when
client.getAllAssetAdministrationShellIdsByAssetLink(endpointDataReference, new ArrayList<>());
client.getAllAssetAdministrationShellIdsByAssetLink(endpointDataReference, IdentifierKeyValuePair.builder().build());

// then
verify(restTemplate).exchange(any(), eq(HttpMethod.GET), any(), eq(LookupShellsResponse.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void shouldReturnExpectedShell() throws RegistryServiceException {
endpointDataRefFutures);

when(decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink(any(),
anyList())).thenReturn(lookupShellsResponse);
any(IdentifierKeyValuePair.class))).thenReturn(lookupShellsResponse);
when(decentralDigitalTwinRegistryClient.getAssetAdministrationShellDescriptor(any(), any())).thenReturn(
expectedShell);

Expand Down Expand Up @@ -132,7 +132,7 @@ void whenInterruptedExceptionOccurs() throws ExecutionException, InterruptedExce
connectorEndpoints)).thenReturn(dataRefFutures);

when(decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink(any(),
anyList())).thenReturn(lookupShellsResponse);
any(IdentifierKeyValuePair.class))).thenReturn(lookupShellsResponse);
when(decentralDigitalTwinRegistryClient.getAssetAdministrationShellDescriptor(any(), any())).thenReturn(
shellDescriptor(emptyList()));

Expand Down Expand Up @@ -166,7 +166,7 @@ void whenExecutionExceptionOccurs() {
connectorEndpoints)).thenReturn(dataRefFutures);

when(decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink(any(),
anyList())).thenReturn(lookupShellsResponse);
any(IdentifierKeyValuePair.class))).thenReturn(lookupShellsResponse);
when(decentralDigitalTwinRegistryClient.getAssetAdministrationShellDescriptor(any(), any())).thenReturn(
shellDescriptor(emptyList()));

Expand Down Expand Up @@ -230,7 +230,7 @@ void shouldReturnTheExpectedGlobalAssetId() throws RegistryServiceException {
when(endpointDataForConnectorsService.createFindEndpointDataForConnectorsFutures(anyList())).thenReturn(
dataRefFutures);
when(decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink(any(),
anyList())).thenReturn(lookupShellsResponse);
any(IdentifierKeyValuePair.class))).thenReturn(lookupShellsResponse);
when(decentralDigitalTwinRegistryClient.getAssetAdministrationShellDescriptor(any(), any())).thenReturn(
expectedShell);

Expand Down

0 comments on commit ff53d66

Please sign in to comment.