diff --git a/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/configuration/ApiHeaderAuthFilter.java b/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/configuration/ApiHeaderAuthFilter.java index 6a1d5f512..648f9cabc 100644 --- a/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/configuration/ApiHeaderAuthFilter.java +++ b/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/configuration/ApiHeaderAuthFilter.java @@ -59,6 +59,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (url.contains("/public") && !apiKeyValue.equals(authHeaderValue)) { + log.error(url+"-"+apiKeyValue +"-"+authHeaderValue+"-"+apiKeyHeader); log.error("**** ApiHeaderAuthFilter genreated Unauthorized response for public api *****************"); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } else if(auth !=null && auth.getAuthorities()!=null && auth.getAuthorities().isEmpty()){ diff --git a/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/controller/ConsumerController.java b/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/controller/ConsumerController.java index 8dbcf87e6..e96c29283 100644 --- a/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/controller/ConsumerController.java +++ b/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/controller/ConsumerController.java @@ -68,4 +68,20 @@ public ResponseEntity subscribeDataOffers(@Valid @RequestBody ConsumerRe consumerControlPanelService.subscribeDataOffers(consumerRequest, processId); return ResponseEntity.ok().body(processId); } + + @PostMapping(value = "/subscribe-download-data") + @PreAuthorize("hasPermission('','consumer_download_data')") + public ResponseEntity subscribeAndDownloadDataOffers(@Valid @RequestBody ConsumerRequest consumerRequest) { + log.info("Request recevied : /api/subscribe-download-data-edr"); + return ResponseEntity.ok().body(consumerControlPanelService.subscribeAndDownloadDataOffers(consumerRequest)); + } + + @GetMapping(value = "/download-data") + @PreAuthorize("hasPermission('','consumer_download_data')") + public ResponseEntity downloadFileFromEDCUsingifAlreadyTransferStatusCompleted(@RequestParam String assetId) + throws Exception { + log.info("Request received : /api/download-data-using-edr"); + return ok().body(consumerControlPanelService.downloadFileFromEDCUsingifAlreadyTransferStatusCompleted(assetId)); + } + } diff --git a/modules/sde-core/src/main/resources/flyway/V22__add_new_role_for_download.sql b/modules/sde-core/src/main/resources/flyway/V22__add_new_role_for_download.sql new file mode 100644 index 000000000..27c4df696 --- /dev/null +++ b/modules/sde-core/src/main/resources/flyway/V22__add_new_role_for_download.sql @@ -0,0 +1,28 @@ +/******************************************************************************** + * Copyright (c) 2023 T-Systems International GmbH + * Copyright (c) 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 + ********************************************************************************/ +INSERT INTO sde_permission (sde_permission,description) + VALUES ('consumer_download_data','Allows consumer user to download data'); + +INSERT INTO sde_role_permission_mapping (sde_permission,sde_role) + VALUES ('consumer_download_data','User'); + +INSERT INTO sde_role_permission_mapping (sde_permission,sde_role) + VALUES ('consumer_download_data','Creator'); + diff --git a/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/service/ConsumerControlPanelServiceTest.java b/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/service/ConsumerControlPanelServiceTest.java index 6e45a1863..e16faa6eb 100644 --- a/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/service/ConsumerControlPanelServiceTest.java +++ b/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/service/ConsumerControlPanelServiceTest.java @@ -40,6 +40,7 @@ import org.eclipse.tractusx.sde.edc.entities.request.policies.Operator; import org.eclipse.tractusx.sde.edc.entities.request.policies.PolicyConstraintBuilderService; import org.eclipse.tractusx.sde.edc.facilitator.ContractNegotiateManagementHelper; +import org.eclipse.tractusx.sde.edc.facilitator.EDRRequestHelper; import org.eclipse.tractusx.sde.edc.gateways.database.ContractNegotiationInfoRepository; import org.eclipse.tractusx.sde.edc.model.contractoffers.ContractOfferRequestFactory; import org.eclipse.tractusx.sde.edc.model.request.ConsumerRequest; @@ -80,6 +81,9 @@ class ConsumerControlPanelServiceTest { @Autowired private ConsumerControlPanelService consumerControlPanelService; + + @MockBean + private EDRRequestHelper edrRequestHelper; @MockBean private ContractOfferCatalogApi contractOfferCatalogApi; diff --git a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/ContractOfferCatalogApi.java b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/ContractOfferCatalogApi.java index 4cd20502e..d3f510edd 100644 --- a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/ContractOfferCatalogApi.java +++ b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/ContractOfferCatalogApi.java @@ -26,7 +26,7 @@ import com.fasterxml.jackson.databind.JsonNode; -@FeignClient(value = "ContractOfferCatalogApi", url = "${edc.consumer.hostname}${edc.consumer.managementpath:/data/v2}", configuration = EDCDataConsumerConfiguration.class) +@FeignClient(value = "ContractOfferCatalogApi", url = "${edc.consumer.hostname}${edc.consumer.managementpath:/data}${edc.consumer.managementpath.apiversion:/v2}", configuration = EDCDataConsumerConfiguration.class) public interface ContractOfferCatalogApi { @PostMapping(value = "/catalog/request") diff --git a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/EDCFeignClientApi.java b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/EDCFeignClientApi.java index 91939c674..a24ab7bd5 100644 --- a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/EDCFeignClientApi.java +++ b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/EDCFeignClientApi.java @@ -31,7 +31,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; -@FeignClient(value = "EDCFeignClientApi", url = "${edc.hostname}${edc.managementpath:/data/v2}", configuration = EDCDataProviderConfiguration.class) +@FeignClient(value = "EDCFeignClientApi", url = "${edc.hostname}${edc.managementpath:/data}${edc.managementpath.apiversion:/v2}", configuration = EDCDataProviderConfiguration.class) public interface EDCFeignClientApi { @GetMapping(path = "/assets/{id}") diff --git a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/EDRApiProxy.java b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/EDRApiProxy.java new file mode 100644 index 000000000..4eb26e330 --- /dev/null +++ b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/api/EDRApiProxy.java @@ -0,0 +1,59 @@ +/******************************************************************************** + * Copyright (c) 2023 T-Systems International GmbH + * Copyright (c) 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.sde.edc.api; + +import java.net.URI; +import java.util.List; +import java.util.Map; + +import org.eclipse.tractusx.sde.edc.model.contractnegotiation.AcknowledgementId; +import org.eclipse.tractusx.sde.edc.model.contractnegotiation.ContractNegotiations; +import org.eclipse.tractusx.sde.edc.model.edr.EDRCachedByIdResponse; +import org.eclipse.tractusx.sde.edc.model.edr.EDRCachedResponse; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient(value = "EDRApiProxy", url = "placeholder") +public interface EDRApiProxy { + + @PostMapping(path = "/edrs", consumes = MediaType.APPLICATION_JSON_VALUE) + AcknowledgementId edrCacheCreate(URI url, @RequestBody ContractNegotiations requestBody, + @RequestHeader Map requestHeader); + + @GetMapping(path = "/edrs", consumes = MediaType.APPLICATION_JSON_VALUE) + List getEDRCachedByAsset(URI url, @RequestParam("assetId") String assetId, + @RequestHeader Map requestHeader); + + @GetMapping(path = "/edrs/{transferProcessId}") + EDRCachedByIdResponse getEDRCachedByTransferProcessId(URI url, + @PathVariable("transferProcessId") String transferProcessId, + @RequestHeader Map requestHeader); + + @GetMapping + Object getActualDataFromProviderDataPlane(URI url, @RequestHeader Map requestHeader); + +} diff --git a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/entities/request/asset/AssetEntryRequestFactory.java b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/entities/request/asset/AssetEntryRequestFactory.java index 43ede726d..d2c0fcacf 100644 --- a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/entities/request/asset/AssetEntryRequestFactory.java +++ b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/entities/request/asset/AssetEntryRequestFactory.java @@ -77,7 +77,7 @@ private AssetEntryRequest buildAsset(String submodel, String shellId, String sub private String subModelPayloadUrl(String submodel, String uuid) { return UriComponentsBuilder .fromHttpUrl(dftHostname) - .path("/api/"+submodel+"/public/") + .path(submodel+"/public/") .path(uuid) .toUriString(); } diff --git a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/facilitator/AbstractEDCStepsHelper.java b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/facilitator/AbstractEDCStepsHelper.java index 80d64c5f0..09c2fd240 100644 --- a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/facilitator/AbstractEDCStepsHelper.java +++ b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/facilitator/AbstractEDCStepsHelper.java @@ -27,9 +27,12 @@ public class AbstractEDCStepsHelper { - @Value("${edc.consumer.hostname}${edc.consumer.managementpath:/data/v2}") + @Value("${edc.consumer.hostname}${edc.consumer.managementpath:/data}${edc.consumer.managementpath.apiversion:/v2}") protected String consumerHost; - + + @Value("${edc.consumer.hostname}${edc.consumer.managementpath:/data}") + protected String consumerHostWithDataPath; + @Value("${edc.consumer.hostname}") protected String consumerHostWithoutDataPath; @@ -39,9 +42,12 @@ public class AbstractEDCStepsHelper { @Value("${edc.consumer.apikey}") private String edcApiKeyValue; - @Value("${edc.hostname}${edc.managementpath:/data/v2}") + @Value("${edc.hostname}${edc.managementpath:/data}${edc.managementpath.apiversion:/v2}") protected String providerHost; - + + @Value("${edc.hostname}${edc.managementpath:/data}") + protected String providerHostWithManagementPath; + @Value("${edc.hostname}") protected String providerHostWithoutDataPath; diff --git a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/facilitator/EDRRequestHelper.java b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/facilitator/EDRRequestHelper.java new file mode 100644 index 000000000..f67467161 --- /dev/null +++ b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/facilitator/EDRRequestHelper.java @@ -0,0 +1,76 @@ +/******************************************************************************** + * Copyright (c) 2023 T-Systems International GmbH + * Copyright (c) 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.sde.edc.facilitator; + +import java.net.URI; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.tractusx.sde.edc.api.EDRApiProxy; +import org.eclipse.tractusx.sde.edc.entities.request.policies.ActionRequest; +import org.eclipse.tractusx.sde.edc.mapper.ContractMapper; +import org.eclipse.tractusx.sde.edc.model.contractnegotiation.AcknowledgementId; +import org.eclipse.tractusx.sde.edc.model.contractnegotiation.ContractNegotiations; +import org.eclipse.tractusx.sde.edc.model.edr.EDRCachedByIdResponse; +import org.eclipse.tractusx.sde.edc.model.edr.EDRCachedResponse; +import org.springframework.stereotype.Service; + +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; + +@Service +@RequiredArgsConstructor +public class EDRRequestHelper extends AbstractEDCStepsHelper { + + private final EDRApiProxy edrApiProxy; + private final ContractMapper contractMapper; + + @SneakyThrows + public String edrRequestInitiate(String providerUrl, String providerId, String offerId, String assetId, + ActionRequest action, Map extensibleProperty) { + + ContractNegotiations contractNegotiations = contractMapper + .prepareContractNegotiations(providerUrl + protocolPath, offerId, assetId, providerId, action); + + AcknowledgementId acknowledgementId = edrApiProxy.edrCacheCreate(new URI(consumerHostWithDataPath), contractNegotiations, + getAuthHeader()); + return acknowledgementId.getId(); + } + + @SneakyThrows + public List getEDRCachedByAsset(String assetId) { + return edrApiProxy.getEDRCachedByAsset(new URI(consumerHostWithDataPath), assetId, getAuthHeader()); + } + + @SneakyThrows + public EDRCachedByIdResponse getEDRCachedByTransferProcessId(String transferProcessId) { + return edrApiProxy.getEDRCachedByTransferProcessId(new URI(consumerHostWithDataPath), transferProcessId, getAuthHeader()); + } + + @SneakyThrows + public Object getDataFromProvider(EDRCachedByIdResponse authorizationToken) { + Map authHeader = new HashMap<>(); + authHeader.put(authorizationToken.getAuthKey(), authorizationToken.getAuthCode()); + return edrApiProxy.getActualDataFromProviderDataPlane(new URI(authorizationToken.getEndpoint()), authHeader); + } + +} diff --git a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/model/edr/EDRCachedByIdResponse.java b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/model/edr/EDRCachedByIdResponse.java new file mode 100644 index 000000000..6a7e26ff0 --- /dev/null +++ b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/model/edr/EDRCachedByIdResponse.java @@ -0,0 +1,56 @@ +/******************************************************************************** + * Copyright (c) 2023 T-Systems International GmbH + * Copyright (c) 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.sde.edc.model.edr; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class EDRCachedByIdResponse { + + @JsonProperty("edc:type") + private String type; + + @JsonProperty("edc:authCode") + private String authCode; + + @JsonProperty("edc:endpoint") + private String endpoint; + + @JsonProperty("edc:id") + private String id; + + @JsonProperty("edc:authKey") + private String authKey; + +} diff --git a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/model/edr/EDRCachedResponse.java b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/model/edr/EDRCachedResponse.java new file mode 100644 index 000000000..635f83d70 --- /dev/null +++ b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/model/edr/EDRCachedResponse.java @@ -0,0 +1,65 @@ +/******************************************************************************** + * Copyright (c) 2023 T-Systems International GmbH + * Copyright (c) 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.sde.edc.model.edr; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class EDRCachedResponse { + + @JsonProperty("@type") + private String type; + + @JsonProperty("@context") + private Object context; + + @JsonProperty("edc:agreementId") + private String agreementId; + + @JsonProperty("edc:transferProcessId") + private String transferProcessId; + + @JsonProperty("edc:assetId") + private String assetId; + + @JsonProperty("edc:providerId") + private String providerId; + + @JsonProperty("tx:edrState") + private String edrState; + + @JsonProperty("tx:expirationDate") + private String expirationDate; + +} diff --git a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/services/ConsumerControlPanelService.java b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/services/ConsumerControlPanelService.java index 6a0682b6b..f452676af 100644 --- a/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/services/ConsumerControlPanelService.java +++ b/modules/sde-external-services/edc/src/main/java/org/eclipse/tractusx/sde/edc/services/ConsumerControlPanelService.java @@ -25,8 +25,10 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; import org.eclipse.tractusx.sde.common.entities.UsagePolicies; @@ -39,9 +41,12 @@ import org.eclipse.tractusx.sde.edc.entities.request.policies.PolicyConstraintBuilderService; import org.eclipse.tractusx.sde.edc.facilitator.AbstractEDCStepsHelper; import org.eclipse.tractusx.sde.edc.facilitator.ContractNegotiateManagementHelper; +import org.eclipse.tractusx.sde.edc.facilitator.EDRRequestHelper; import org.eclipse.tractusx.sde.edc.gateways.database.ContractNegotiationInfoRepository; import org.eclipse.tractusx.sde.edc.model.contractnegotiation.ContractNegotiationDto; import org.eclipse.tractusx.sde.edc.model.contractoffers.ContractOfferRequestFactory; +import org.eclipse.tractusx.sde.edc.model.edr.EDRCachedByIdResponse; +import org.eclipse.tractusx.sde.edc.model.edr.EDRCachedResponse; import org.eclipse.tractusx.sde.edc.model.request.ConsumerRequest; import org.eclipse.tractusx.sde.edc.model.response.QueryDataOfferModel; import org.eclipse.tractusx.sde.edc.util.UtilityFunctions; @@ -50,7 +55,9 @@ import com.fasterxml.jackson.databind.JsonNode; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -60,6 +67,7 @@ public class ConsumerControlPanelService extends AbstractEDCStepsHelper { private final ContractOfferCatalogApi contractOfferCatalogApiProxy; private final ContractNegotiateManagementHelper contractNegotiateManagement; + private final EDRRequestHelper edrRequestHelper; private final ContractNegotiationInfoRepository contractNegotiationInfoRepository; private final PolicyConstraintBuilderService policyConstraintBuilderService; @@ -95,7 +103,7 @@ private QueryDataOfferModel buildContractOffer(String sproviderUrl, JsonNode con JsonNode policy = offer.get("odrl:hasPolicy"); String edcstr = "edc:"; - + QueryDataOfferModel build = QueryDataOfferModel.builder() .assetId(getFieldFromJsonNode(offer, edcstr + EDCAssetConstant.ASSET_PROP_ID)) .connectorOfferUrl(sproviderUrl + File.separator + getFieldFromJsonNode(offer, "@id")) @@ -209,19 +217,15 @@ public void subscribeDataOffers(ConsumerRequest consumerRequest, String processI && !checkContractNegotiationStatus.get().getState().equals("FINALIZED") && !checkContractNegotiationStatus.get().getState().equals("TERMINATED") && counter <= retry); - } catch(InterruptedException ie) { + } catch (InterruptedException ie) { log.error("Exception in subscribeDataOffers" + ie.getMessage()); Thread.currentThread().interrupt(); } catch (Exception e) { log.error("Exception in subscribeDataOffers" + e.getMessage()); } finally { - - // Local DB entry ContractNegotiationInfoEntity contractNegotiationInfoEntity = ContractNegotiationInfoEntity.builder() - .id(UUID.randomUUID().toString()) - .processId(processId) - .connectorId(consumerRequest.getConnectorId()) - .offerId(offer.getOfferId()) + .id(UUID.randomUUID().toString()).processId(processId) + .connectorId(consumerRequest.getConnectorId()).offerId(offer.getOfferId()) .contractNegotiationId(negotiateContractId != null ? negotiateContractId.get() : null) .status(checkContractNegotiationStatus.get() != null ? checkContractNegotiationStatus.get().getState() @@ -234,4 +238,84 @@ public void subscribeDataOffers(ConsumerRequest consumerRequest, String processI } + public Object subscribeAndDownloadDataOffers(@Valid ConsumerRequest consumerRequest) { + + HashMap extensibleProperty = new HashMap<>(); + Map response = new ConcurrentHashMap<>(); + + var recipientURL = UtilityFunctions.removeLastSlashOfUrl(consumerRequest.getProviderUrl()); + + List policies = consumerRequest.getPolicies(); + + Optional findFirst = policies.stream() + .filter(type -> type.getType().equals(UsagePolicyEnum.CUSTOM)).findFirst(); + + if (findFirst.isPresent()) { + extensibleProperty.put(findFirst.get().getType().name(), findFirst.get().getValue()); + } + + ActionRequest action = policyConstraintBuilderService.getUsagePolicyConstraints(policies); + consumerRequest.getOffers().parallelStream().forEach(offer -> { + try { + edrRequestHelper.edrRequestInitiate(recipientURL, consumerRequest.getConnectorId(), offer.getOfferId(), + offer.getAssetId(), action, extensibleProperty); + + EDRCachedResponse checkContractNegotiationStatus = verifyEDRRequestStatus(offer.getAssetId()); + + response.put(offer.getAssetId(), downloadFile(checkContractNegotiationStatus)); + + } catch (Exception e) { + log.error("Exception in subscribeAndDownloadDataOffers" + e.getMessage()); + response.put(offer.getAssetId(), e.getMessage()); + } + }); + return response; + } + + @SneakyThrows + private EDRCachedResponse verifyEDRRequestStatus(String assetId) { + EDRCachedResponse eDRCachedResponse = null; + List eDRCachedResponseList = null; + try { + int retry = 5; + int counter = 1; + do { + Thread.sleep(5000); + eDRCachedResponseList = edrRequestHelper.getEDRCachedByAsset(assetId); + counter++; + } while (eDRCachedResponseList != null && !eDRCachedResponseList.isEmpty() + && !eDRCachedResponseList.get(0).getEdrState().equals("NEGOTIATED") && counter <= retry); + + if (eDRCachedResponseList != null && !eDRCachedResponseList.isEmpty()) + eDRCachedResponse = eDRCachedResponseList.get(0); + } catch (InterruptedException ie) { + log.error("Exception in verifyEDRRequestStatus" + ie.getMessage()); + Thread.currentThread().interrupt(); + throw ie; + } catch (Exception e) { + log.error("Exception in verifyEDRRequestStatus" + e.getMessage()); + throw e; + } + return eDRCachedResponse; + } + + @SneakyThrows + private EDRCachedByIdResponse getAuthorizationTokenForDataDownload(String transferProcessId) { + return edrRequestHelper.getEDRCachedByTransferProcessId(transferProcessId); + } + + @SneakyThrows + public Object downloadFileFromEDCUsingifAlreadyTransferStatusCompleted(String assetId) { + EDRCachedResponse verifyEDRRequestStatus = verifyEDRRequestStatus(assetId); + return downloadFile(verifyEDRRequestStatus); + } + + private Object downloadFile(EDRCachedResponse verifyEDRRequestStatus) { + if (verifyEDRRequestStatus != null) { + EDRCachedByIdResponse authorizationToken = getAuthorizationTokenForDataDownload( + verifyEDRRequestStatus.getTransferProcessId()); + return edrRequestHelper.getDataFromProvider(authorizationToken); + } + return null; + } }