Skip to content

Commit

Permalink
refactor(EdcAdapterService): remove storing of edr and expiry check
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-rm-meyer-ISST committed May 15, 2024
1 parent 517a2c7 commit 65178bc
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 93 deletions.
2 changes: 0 additions & 2 deletions DEPENDENCIES_BACKEND
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jdk8/2.15.3,
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.15.3, Apache-2.0, approved, #7930
maven/mavencentral/com.fasterxml.jackson.module/jackson-module-parameter-names/2.15.3, Apache-2.0, approved, #8803
maven/mavencentral/com.fasterxml/classmate/1.6.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.github.stephenc.jcip/jcip-annotations/1.0-1, Apache-2.0, approved, CQ21949
maven/mavencentral/com.jayway.jsonpath/json-path/2.8.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.37.3, Apache-2.0, approved, #11701
maven/mavencentral/com.squareup.okhttp3/okhttp/4.12.0, Apache-2.0, approved, #11156
maven/mavencentral/com.squareup.okio/okio-jvm/3.6.0, Apache-2.0, approved, #11158
maven/mavencentral/com.squareup.okio/okio/3.6.0, Apache-2.0, approved, #11155
Expand Down
2 changes: 0 additions & 2 deletions backend/DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jdk8/2.15.3,
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.15.3, Apache-2.0, approved, #7930
maven/mavencentral/com.fasterxml.jackson.module/jackson-module-parameter-names/2.15.3, Apache-2.0, approved, #8803
maven/mavencentral/com.fasterxml/classmate/1.6.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.github.stephenc.jcip/jcip-annotations/1.0-1, Apache-2.0, approved, CQ21949
maven/mavencentral/com.jayway.jsonpath/json-path/2.8.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.37.3, Apache-2.0, approved, #11701
maven/mavencentral/com.squareup.okhttp3/okhttp/4.12.0, Apache-2.0, approved, #11156
maven/mavencentral/com.squareup.okio/okio-jvm/3.6.0, Apache-2.0, approved, #11158
maven/mavencentral/com.squareup.okio/okio/3.6.0, Apache-2.0, approved, #11155
Expand Down
13 changes: 3 additions & 10 deletions backend/pom.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022,2023 Volkswagen AG
Copyright (c) 2022,2023 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V. (represented by Fraunhofer ISST)
Copyright (c) 2022,2023 Contributors to the Eclipse Foundation
Copyright (c) 2022,2024 Volkswagen AG
Copyright (c) 2022,2024 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V. (represented by Fraunhofer ISST)
Copyright (c) 2022,2024 Contributors to the Eclipse Foundation
See the NOTICE file(s) distributed with this work for additional
information regarding copyright ownership.
Expand Down Expand Up @@ -41,7 +41,6 @@
<springdoc.version>2.3.0</springdoc.version>
<hibernate-validator.version>8.0.1.Final</hibernate-validator.version>
<snakeyaml.version>2.2</snakeyaml.version>
<nimbus.version>9.37.3</nimbus.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -114,12 +113,6 @@
<artifactId>modelmapper</artifactId>
<version>3.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.nimbusds/nimbus-jose-jwt -->
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>${nimbus.version}</version>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -89,7 +88,7 @@ private ResponseEntity<String> authCodeReceivingEndpoint(@RequestBody JsonNode b
return ResponseEntity.status(400).build();
}

edrService.save(transferId, new EdrDto(authKey, authCode, endpoint, new Date()));
edrService.save(transferId, new EdrDto(authKey, authCode, endpoint));
log.debug("EDR endpoint stored authCode for " + transferId);
return ResponseEntity.status(200).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@

import org.eclipse.tractusx.puris.backend.common.edc.logic.service.EndpointDataReferenceService;

import java.util.Date;

/**
* An internal, immutable Dto class used by the {@link EndpointDataReferenceService}
* It contains an authKey, authCode and endpoint.
*
* @param authKey This defines the key, under which the
* authCode is to be sent to the data plane.
* For example: "Authorization"
* @param authCode This is the secret key to be sent
* to the data plane.
* @param endpoint The address of the data plane that has
* to handle the consumer pull.
* @param expiresAt The expiry data in ms till data
* @param authKey This defines the key, under which the
* authCode is to be sent to the data plane.
* For example: "Authorization"
* @param authCode This is the secret key to be sent
* to the data plane.
* @param endpoint The address of the data plane that has
* to handle the consumer pull.
*/
public record EdrDto(String authKey, String authCode, String endpoint, Date expiresAt) {
public record EdrDto(String authKey, String authCode, String endpoint) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nimbusds.jwt.JWT;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.JWTParser;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.eclipse.tractusx.puris.backend.common.edc.domain.model.SubmodelType;
Expand All @@ -36,12 +33,12 @@
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.itemstocksamm.DirectionCharacteristic;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.*;
import java.util.regex.Pattern;

Expand All @@ -58,8 +55,6 @@ public class EdcAdapterService {
private final ObjectMapper objectMapper;
@Autowired
private EdcRequestBodyBuilder edcRequestBodyBuilder;
@Autowired
private EndpointDataReferenceService edrService;

@Autowired
private EdcContractMappingService edcContractMappingService;
Expand Down Expand Up @@ -580,16 +575,8 @@ private JsonNode getSubmodelFromPartner(MaterialPartnerRelation mpr, SubmodelTyp
break;
}
}
EdrDto edrDto = null;
// Await arrival of edr
for (int i = 0; i < 100; i++) {
Thread.sleep(100);
edrDto = getEdrForTransferProcessId(transferId); //edrService.findByTransferId(transferId);
if (edrDto != null) {
log.info("Received EDR data for " + assetId + " with " + partner.getEdcUrl());
break;
}
}
EdrDto edrDto = getAndAwaitEdrDto(transferId);
log.info("Received EDR data for " + assetId + " with " + partner.getEdcUrl());
if (edrDto == null) {
log.error("Failed to obtain EDR data for " + assetId + " with " + partner.getEdcUrl());
return doSubmodelRequest(type, mpr, direction, --retries);
Expand Down Expand Up @@ -622,6 +609,26 @@ private JsonNode getSubmodelFromPartner(MaterialPartnerRelation mpr, SubmodelTyp
return getSubmodelFromPartner(mpr, type, direction, --retries);
}

/**
* get the EDR via edr api and retry multiple times in case the EDR has not yet been available
*
* @param transferProcessId to get the EDR for, not null
* @return edr received, or null if not yet available
* @throws InterruptedException if thread was not able to sleep
*/
private @Nullable EdrDto getAndAwaitEdrDto(String transferProcessId) throws InterruptedException {
EdrDto edrDto = null;
// retry, if Data Space Protocol / Data Plane Provisioning communication needs time to prepare
for (int i = 0; i < 100; i++) {
edrDto = getEdrForTransferProcessId(transferProcessId);
if (edrDto != null) {
break;
}
Thread.sleep(100);
}
return edrDto;
}

public JsonNode doSubmodelRequest(SubmodelType type, MaterialPartnerRelation mpr, DirectionCharacteristic direction, int retries) {
if (retries < 0) {
return null;
Expand Down Expand Up @@ -779,19 +786,12 @@ private JsonNode getAasSubmodelDescriptors(String manufacturerPartId, String man
break;
}
}
EdrDto edrDto = null;
// Await arrival of edr
for (int i = 0; i < 100; i++) {
edrDto = getEdrForTransferProcessId(transferId);//edrService.findByTransferId(transferId);
if (edrDto != null) {
log.info("Received EDR data for " + assetId + " with " + partner.getEdcUrl());
break;
}
Thread.sleep(100);
}
EdrDto edrDto = getAndAwaitEdrDto(transferId);
if (edrDto == null) {
log.error("Failed to obtain EDR data for " + assetId + " with " + partner.getEdcUrl());
return getAasSubmodelDescriptors(manufacturerPartId, manufacturerId, mpr, --retries);
} else {
log.info("Received EDR data for " + assetId + " with " + partner.getEdcUrl());
}
HttpUrl.Builder urlBuilder = HttpUrl.parse(edrDto.endpoint()).newBuilder()
.addPathSegment("api")
Expand Down Expand Up @@ -863,26 +863,6 @@ private JsonNode getAasSubmodelDescriptors(String manufacturerPartId, String man
*/
private EdrDto getEdrForTransferProcessId(String transferProcessId) {

// Note: If we decode the auth token, we could add the exp as expiresAt so that I can store the EDR and lookup
// again
EdrDto storedEdr = edrService.findByTransferId(transferProcessId);
log.debug("storedEdr: {}", storedEdr);

// got a stored edr that has an auth token that will not expire within 5 seconds
// Notes:
// - this might cause an issue if there are different time zones between EDC and us
// - we could also implement an ProxyRequestInterceptor that gets a fresh token for a TransferProcessId
if (storedEdr != null && storedEdr.expiresAt().before(new Date(System.currentTimeMillis() + 5000))) {
log.info("Reuse EDR directly as it will not expire within 5 seconds");
return storedEdr;
}

if (storedEdr != null) {
log.debug("Expiry edr: {} VS expiry within 5 sec {}", storedEdr.expiresAt(), new Date(System.currentTimeMillis() + 5000));
} else {
log.debug("No EDR token found");
}

try (Response response = sendGetRequest(
List.of("v2", "edrs", transferProcessId, "dataaddress"),
Map.of("auto_refresh", "true"))
Expand All @@ -892,18 +872,11 @@ private EdrDto getEdrForTransferProcessId(String transferProcessId) {
String dataPlaneEndpoint = responseObject.get("endpoint").asText();
String authToken = responseObject.get("authorization").asText();

JWT jwt = JWTParser.parse(authToken);
JWTClaimsSet claims = jwt.getJWTClaimsSet();
Date expirationTime = claims.getExpirationTime();

storedEdr = new EdrDto("Authorization", authToken, dataPlaneEndpoint, expirationTime);
edrService.save(transferProcessId, storedEdr);
log.debug("Requested EDR successfully: {}", storedEdr);
EdrDto edr = new EdrDto("Authorization", authToken, dataPlaneEndpoint);
log.debug("Requested EDR successfully: {}", edr);

return storedEdr;
return edr;

} catch (ParseException e) {
log.error("EDR token could not be parsed: {}", e);
} catch (IOException e) {
log.error("EDR token for transfer process with ID {} could not be obtained", transferProcessId);
}
Expand Down Expand Up @@ -937,7 +910,7 @@ private void terminateTransfer(String transferProcessId) {
log.info("Terminated transfer process with id {}.", transferProcessId);
}
} catch (IOException e) {
log.error("Error while trying to terminate transfer: {}", e);
log.error("Error while trying to terminate transfer: ", e);
}
}

Expand Down Expand Up @@ -1058,7 +1031,7 @@ private boolean testContractPolicyConstraints(JsonNode catalogEntry) {
}
boolean result = true;

if (constraint.isPresent() && constraint.get().isArray() && constraint.get().size() == 2) {
if (constraint.get().isArray() && constraint.get().size() == 2) {
Optional<JsonNode> frameworkAgreementConstraint = Optional.empty();
Optional<JsonNode> purposeConstraint = Optional.empty();

Expand Down Expand Up @@ -1106,7 +1079,7 @@ private boolean testContractPolicyConstraints(JsonNode catalogEntry) {

private boolean testSingleConstraint(Optional<JsonNode> constraintToTest, String targetLeftOperand, String targetOperator, String targetRightOperand) {

if (!constraintToTest.isPresent()) return false;
if (constraintToTest.isEmpty()) return false;

JsonNode con = constraintToTest.get();

Expand All @@ -1126,7 +1099,7 @@ private boolean testSingleConstraint(Optional<JsonNode> constraintToTest, String
}

JsonNode rightOperandNode = con.get("odrl:rightOperand");
if (operatorNode == null || !targetRightOperand.equals(rightOperandNode.asText())) {
if (rightOperandNode == null || !targetRightOperand.equals(rightOperandNode.asText())) {
String rightOperand = rightOperandNode == null ? "null" : rightOperandNode.asText();
log.debug("Right operand '{}' odes not equal expected value '{}'.", rightOperand, targetRightOperand);
return false;
Expand Down
4 changes: 1 addition & 3 deletions local/tractus-x-edc/config/customer/puris-backend.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ puris.frameworkagreement.credential=puris
puris.frameworkagreement.version=1.0
puris.purpose.name=cx.puris.base
puris.purpose.version=1
logging.level.org.eclipse.tractusx.puris.backend=DEBUG
logging.level.org.eclipse.tractusx.puris.backend=INFO
puris.api.key=${CUSTOMER_BACKEND_API_KEY}
puris.dtr.url=http://dtr-customer:4243
puris.generatematerialcatenaxid=true

edc.controlplane.key=${EDC_API_PW}
edc.controlplane.management.url=http://customer-control-plane:8181/management
edc.controlplane.protocol.url=http://customer-control-plane:8184/api/v1/dsp
edc.dataplane.public.url=http://customer-data-plane:8285/api/public/

own.bpnl=BPNL4444444444XX
own.name=Control Unit Creator Inc.
own.bpns=BPNS4444444444XX
Expand Down
4 changes: 1 addition & 3 deletions local/tractus-x-edc/config/supplier/puris-backend.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ puris.frameworkagreement.credential=puris
puris.frameworkagreement.version=1.0
puris.purpose.name=cx.puris.base
puris.purpose.version=1
logging.level.org.eclipse.tractusx.puris.backend=DEBUG
logging.level.org.eclipse.tractusx.puris.backend=INFO
puris.api.key=${SUPPLIER_BACKEND_API_KEY}
puris.dtr.url=http://dtr-supplier:4243
puris.generatematerialcatenaxid=true

edc.controlplane.key=${EDC_API_PW}
edc.controlplane.management.url=http://supplier-control-plane:9181/management
edc.controlplane.protocol.url=http://supplier-control-plane:9184/api/v1/dsp
edc.dataplane.public.url=http://supplier-data-plane:9285/api/public/

own.bpnl=BPNL1234567890ZZ
own.name=Semiconductor Supplier Inc.
own.bpns=BPNS1234567890ZZ
Expand Down

0 comments on commit 65178bc

Please sign in to comment.