|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Metaform Systems, Inc. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Apache License, Version 2.0 which is available at |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: Apache-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Metaform Systems, Inc. - initial API and implementation |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package org.eclipse.edc.connector.controlplane.discovery; |
| 16 | + |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | +import okhttp3.Request; |
| 19 | +import okhttp3.Response; |
| 20 | +import org.eclipse.edc.http.spi.EdcHttpClient; |
| 21 | +import org.eclipse.edc.protocol.spi.DataspaceProfileContext; |
| 22 | +import org.eclipse.edc.protocol.spi.ParticipantProfileService; |
| 23 | +import org.eclipse.edc.protocol.spi.ProtocolVersion; |
| 24 | +import org.eclipse.edc.protocol.spi.ProtocolVersions; |
| 25 | +import org.eclipse.edc.protocol.spi.discovery.DiscoveryRequest; |
| 26 | +import org.eclipse.edc.protocol.spi.discovery.DiscoveryResponse; |
| 27 | +import org.eclipse.edc.protocol.spi.discovery.DiscoveryService; |
| 28 | +import org.eclipse.edc.protocol.spi.discovery.DiscoveryUrlResolver; |
| 29 | +import org.eclipse.edc.spi.result.Result; |
| 30 | +import org.eclipse.edc.spi.result.ServiceResult; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
| 35 | +import java.util.function.Function; |
| 36 | +import java.util.stream.Stream; |
| 37 | + |
| 38 | +public class DiscoveryServiceImpl implements DiscoveryService { |
| 39 | + |
| 40 | + private final EdcHttpClient httpClient; |
| 41 | + private final ParticipantProfileService participantProfileService; |
| 42 | + private final ObjectMapper objectMapper; |
| 43 | + private final List<DiscoveryUrlResolver> resolvers = new ArrayList<>(); |
| 44 | + |
| 45 | + public DiscoveryServiceImpl( |
| 46 | + EdcHttpClient httpClient, |
| 47 | + ParticipantProfileService participantProfileService, |
| 48 | + ObjectMapper objectMapper) { |
| 49 | + this.httpClient = httpClient; |
| 50 | + this.participantProfileService = participantProfileService; |
| 51 | + this.objectMapper = objectMapper; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + @Override |
| 56 | + public ServiceResult<List<DiscoveryResponse>> discover(String participantContextId, DiscoveryRequest request) { |
| 57 | + |
| 58 | + var resolver = resolvers.stream() |
| 59 | + .filter(r -> r.canResolve(request)) |
| 60 | + .findFirst() |
| 61 | + .orElse(null); |
| 62 | + |
| 63 | + if (resolver != null) { |
| 64 | + return resolveMatches(participantContextId, request, resolver); |
| 65 | + |
| 66 | + } else { |
| 67 | + return ServiceResult.badRequest("No resolver found for request: %s".formatted(request)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private ServiceResult<List<DiscoveryResponse>> resolveMatches(String participantContextId, DiscoveryRequest request, DiscoveryUrlResolver resolver) { |
| 72 | + |
| 73 | + var versionsResult = resolver.resolve(request).compose(this::fetchProtocolVersions); |
| 74 | + if (versionsResult.failed()) { |
| 75 | + return ServiceResult.badRequest(versionsResult.getFailureDetail()); |
| 76 | + } |
| 77 | + |
| 78 | + var localProfiles = participantProfileService.resolveAll(participantContextId); |
| 79 | + var matches = localProfiles.stream() |
| 80 | + .flatMap(profileMatcher(versionsResult.getContent())) |
| 81 | + .toList(); |
| 82 | + |
| 83 | + return ServiceResult.success(matches); |
| 84 | + } |
| 85 | + |
| 86 | + private Function<DataspaceProfileContext, Stream<? extends DiscoveryResponse>> profileMatcher(List<ProtocolVersion> versions) { |
| 87 | + return profile -> versions.stream() |
| 88 | + .filter(v -> v.version().equals(profile.protocolVersion().version()) && |
| 89 | + v.binding().equals(profile.protocolVersion().binding())) |
| 90 | + .map(v -> new DiscoveryResponse( |
| 91 | + profile.name(), |
| 92 | + v.version(), |
| 93 | + v.path(), |
| 94 | + v.binding())); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + private ServiceResult<List<ProtocolVersion>> fetchProtocolVersions(String url) { |
| 99 | + var request = new Request.Builder().url(url).get().build(); |
| 100 | + var result = httpClient.execute(request, response -> handleResponse(response, url)); |
| 101 | + |
| 102 | + if (result.failed()) { |
| 103 | + return ServiceResult.badRequest(result.getFailureDetail()); |
| 104 | + } |
| 105 | + return ServiceResult.success(result.getContent()); |
| 106 | + } |
| 107 | + |
| 108 | + private Result<List<ProtocolVersion>> handleResponse(Response response, String url) { |
| 109 | + if (!response.isSuccessful()) { |
| 110 | + return Result.failure("Unexpected status %d fetching '%s'".formatted(response.code(), url)); |
| 111 | + } |
| 112 | + try (var body = response.body()) { |
| 113 | + var versions = objectMapper.readValue(body.byteStream(), ProtocolVersions.class); |
| 114 | + return Result.success(versions.protocolVersions()); |
| 115 | + } catch (IOException e) { |
| 116 | + return Result.failure("Failed to parse '%s': %s".formatted(url, e.getMessage())); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void registerResolver(DiscoveryUrlResolver resolver) { |
| 122 | + resolvers.add(resolver); |
| 123 | + } |
| 124 | +} |
0 commit comments