From ff53d662d5022ea5f3125eb783dbfe06d06a0dd4 Mon Sep 17 00:00:00 2001 From: "Krzysztof Massalski (Extern)" Date: Fri, 22 Mar 2024 14:10:58 +0100 Subject: [PATCH] feat(impl):[#359] update registry impl for lookup --- .../DecentralDigitalTwinRegistryClient.java | 41 +++++++++++++++---- .../DecentralDigitalTwinRegistryService.java | 4 +- ...ecentralDigitalTwinRegistryClientTest.java | 3 +- ...centralDigitalTwinRegistryServiceTest.java | 8 ++-- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClient.java b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClient.java index 1c7a7ed5b3..3fdd35a78b 100644 --- a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClient.java +++ b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClient.java @@ -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; @@ -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; @@ -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) { @@ -78,14 +87,32 @@ public AssetAdministrationShellDescriptor getAssetAdministrationShellDescriptor( @Retry(name = "registry") public LookupShellsResponse getAllAssetAdministrationShellIdsByAssetLink( - final EndpointDataReference endpointDataReference, final List 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)); diff --git a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryService.java b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryService.java index ab5b05e50c..a627ce034a 100644 --- a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryService.java +++ b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryService.java @@ -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 @@ -340,7 +340,7 @@ private Collection 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()); diff --git a/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClientTest.java b/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClientTest.java index 823fa5a175..b9599b181f 100644 --- a/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClientTest.java +++ b/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClientTest.java @@ -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; @@ -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)); diff --git a/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryServiceTest.java b/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryServiceTest.java index a2c2dace14..7e48e9a91f 100644 --- a/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryServiceTest.java +++ b/irs-registry-client/src/test/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryServiceTest.java @@ -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); @@ -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())); @@ -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())); @@ -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);