Skip to content

Commit

Permalink
Merge pull request #860 from dsmf/fix/841-some-cleanup-first
Browse files Browse the repository at this point in the history
Fix/841 some cleanup first
  • Loading branch information
ds-jhartmann authored Jul 31, 2024
2 parents daf495c + aead813 commit 08811e3
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.tractusx.irs.common.ExceptionUtils;
import org.eclipse.tractusx.irs.component.JobParameter;
import org.eclipse.tractusx.irs.component.PartChainIdentificationKey;
import org.eclipse.tractusx.irs.component.ProcessingError;
import org.eclipse.tractusx.irs.component.Shell;
import org.eclipse.tractusx.irs.component.Tombstone;
import org.eclipse.tractusx.irs.component.enums.ProcessStep;
Expand Down Expand Up @@ -64,23 +65,19 @@ public ItemContainer process(final ItemContainer.ItemContainerBuilder itemContai
final PartChainIdentificationKey itemId) {

if (StringUtils.isBlank(itemId.getBpn())) {
log.warn("Could not process item with id {} because no BPN was provided. Creating Tombstone.",
itemId.getGlobalAssetId());
return itemContainerBuilder.tombstone(
Tombstone.from(itemId.getGlobalAssetId(), null, "Can't get relationship without a BPN", 0,
ProcessStep.DIGITAL_TWIN_REQUEST)).build();
return itemContainerBuilder.tombstone(createNoBpnProvidedTombstone(jobData, itemId)).build();
}

try {
final var dtrKeys = List.of(new DigitalTwinRegistryKey(itemId.getGlobalAssetId(), itemId.getBpn()));
final var eithers = digitalTwinRegistryService.fetchShells(dtrKeys);
final var shell = eithers.stream()
final var shells = digitalTwinRegistryService.fetchShells(dtrKeys);
final var shell = shells.stream()
// we use findFirst here, because we query only for one
// DigitalTwinRegistryKey here
.map(Either::getOrNull)
.filter(Objects::nonNull)
.findFirst()
.orElseThrow(() -> shellNotFound(eithers));
.orElseThrow(() -> shellNotFound(shells));

itemContainerBuilder.shell(
jobData.isAuditContractNegotiation() ? shell : shell.withoutContractAgreementId());
Expand All @@ -101,6 +98,23 @@ public ItemContainer process(final ItemContainer.ItemContainerBuilder itemContai
return itemContainerBuilder.build();
}

private Tombstone createNoBpnProvidedTombstone(final JobParameter jobData, final PartChainIdentificationKey itemId) {
log.warn("Could not process item with id {} because no BPN was provided. Creating Tombstone.",
itemId.getGlobalAssetId());

final ProcessingError error = ProcessingError.builder()
.withProcessStep(ProcessStep.DIGITAL_TWIN_REQUEST)
.withRetryCounterAndLastAttemptNow(0)
.withErrorDetail("Can't get relationship without a BPN")
.build();
return Tombstone.builder()
.endpointURL(null)
.catenaXId(itemId.getGlobalAssetId())
.processingError(error)
.businessPartnerNumber(jobData.getBpn())
.build();
}

private static RegistryServiceException shellNotFound(final Collection<Either<Exception, Shell>> eithers) {
final RegistryServiceException shellNotFound = new RegistryServiceException("Shell not found");
ExceptionUtils.addSuppressedExceptions(eithers, shellNotFound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,8 @@ void shouldUseExceptionMessageWhenSuppressedExceptionNotPresent() {
}

private String zonedDateTimeExcerpt(ZonedDateTime dateTime) {
return new StringBuilder().append(dateTime.getYear())
.append("-")
.append(dateTime.getMonth())
.append("-")
.append(dateTime.getDayOfMonth())
.append("T")
.append(dateTime.getHour())
.append(":")
.append(dateTime.getMinute())
.append(":")
.append(dateTime.getSecond())
.toString();
return "%d-%s-%dT%d:%d:%d".formatted(dateTime.getYear(), dateTime.getMonth(), dateTime.getDayOfMonth(),
dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public <T> CompletableFuture<T> getFastestResult(final List<CompletableFuture<T>

if (ex != null) {
log.warn("All failed: " + System.lineSeparator()
// TODO (#538) can we remove logging here and log only up in call hierarchy
+ exceptions.stream()
.map(ExceptionUtils::getStackTrace)
.collect(Collectors.joining(System.lineSeparator())), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@
********************************************************************************/
package org.eclipse.tractusx.irs.edc.client.exceptions;

import lombok.Getter;
import org.eclipse.edc.policy.model.Policy;

/**
* This interface provides get-methods for fine-grained policy exceptions
* Policy exception with detail information
*/
public interface PolicyException {
/**
* @return the corresponding bpn of the policy
*/
String getBusinessPartnerNumber();
@Getter
public class PolicyException extends EdcClientException {

/**
* @return the policy that caused the exception
*/
Policy getPolicy();
private final transient Policy policy;
private final String businessPartnerNumber;

public PolicyException(final String message, final Policy policy, final String businessPartnerNumber) {
super(message);
this.policy = policy;
this.businessPartnerNumber = businessPartnerNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@
* Usage Policy Expired Exception errors in the contract negotiation.
*/
@Getter
public class UsagePolicyExpiredException extends EdcClientException implements PolicyException {

private final transient Policy policy;
private final String businessPartnerNumber;
public class UsagePolicyExpiredException extends PolicyException {

public UsagePolicyExpiredException(final List<AcceptedPolicy> acceptedPolicies,
final Policy providedCatalogItemPolicy, final String businessPartnerNumber) {
super("Policy " + acceptedPolicies.stream().map(policy -> policy.policy().getPolicyId()).toList()
+ " has expired.");
this.policy = providedCatalogItemPolicy;
this.businessPartnerNumber = businessPartnerNumber;
+ " has expired.", providedCatalogItemPolicy, businessPartnerNumber);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,13 @@
* Usage Policy Permission Exception errors in the contract negotiation.
*/
@Getter
public class UsagePolicyPermissionException extends EdcClientException implements PolicyException {

private final transient Policy policy;
private final String businessPartnerNumber;
public class UsagePolicyPermissionException extends PolicyException {

public UsagePolicyPermissionException(final List<AcceptedPolicy> acceptedPolicies,
final Policy providedCatalogItemPolicy, final String businessPartnerNumber) {
super("Policies " + acceptedPolicies.stream().map(policy -> policy.policy().getPolicyId()).toList()
+ " did not match with policy from " + businessPartnerNumber + ".");

this.policy = providedCatalogItemPolicy;
this.businessPartnerNumber = businessPartnerNumber;
+ " did not match with policy from " + businessPartnerNumber + ".", providedCatalogItemPolicy,
businessPartnerNumber);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
********************************************************************************/
package org.eclipse.tractusx.irs.component;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;

Expand All @@ -42,6 +43,7 @@
@Builder(toBuilder = true, setterPrefix = "with")
@JsonDeserialize(builder = ProcessingError.ProcessingErrorBuilder.class)
public class ProcessingError {

private ProcessStep processStep;
private String errorDetail;
private ZonedDateTime lastAttempt;
Expand All @@ -58,5 +60,14 @@ public class ProcessingError {
@JsonPOJOBuilder()
public static class ProcessingErrorBuilder {

public ProcessingErrorBuilder withLastAttemptNow() {
return this.withLastAttempt(ZonedDateTime.now(ZoneOffset.UTC));
}

public ProcessingErrorBuilder withRetryCounterAndLastAttemptNow(final int retryCount) {
return this.withLastAttemptNow().withRetryCounter(retryCount);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
* Tombstone with information about request failure
*/
@Getter
@Builder
@Builder(toBuilder = true)
@Jacksonized
@Schema(description = "Tombstone with information about request failure")
public class Tombstone {
private static final NodeType NODE_TYPE = NodeType.TOMBSTONE;

public static final int CATENA_X_ID_LENGTH = 45;

private static final NodeType NODE_TYPE = NodeType.TOMBSTONE;

@Schema(description = "CATENA-X global asset id in the format urn:uuid:uuid4.",
example = "urn:uuid:6c311d29-5753-46d4-b32c-19b918ea93b0", minLength = CATENA_X_ID_LENGTH,
maxLength = CATENA_X_ID_LENGTH,
Expand Down Expand Up @@ -88,26 +90,20 @@ public static Tombstone from(final String catenaXId, final String endpointURL, f

public static Tombstone from(final String globalAssetId, final String endpointURL, final Throwable exception,
final Throwable[] suppressed, final int retryCount, final ProcessStep processStep) {
final ProcessingError processingError = withProcessingError(processStep, retryCount, exception.getMessage(),
suppressed);
return Tombstone.builder()
.endpointURL(endpointURL)
.catenaXId(globalAssetId)
.processingError(processingError)
.processingError(ProcessingError.builder()
.withProcessStep(processStep)
.withRetryCounterAndLastAttemptNow(retryCount)
.withErrorDetail(exception.getMessage())
.withRootCauses(getRootErrorMessages(suppressed))
.build())
.build();
}

private static ProcessingError withProcessingError(final ProcessStep processStep, final int retryCount,
final String message, final Throwable... suppressed) {
final List<String> rootCauses = Arrays.stream(suppressed).map(Tombstone::getRootErrorMessages).toList();

return ProcessingError.builder()
.withProcessStep(processStep)
.withRetryCounter(retryCount)
.withLastAttempt(ZonedDateTime.now(ZoneOffset.UTC))
.withErrorDetail(message)
.withRootCauses(rootCauses)
.build();
private static List<String> getRootErrorMessages(final Throwable... throwables) {
return Arrays.stream(throwables).map(Tombstone::getRootErrorMessages).toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private Collection<String> lookupShellIds(final String bpn,
"%s occurred while looking up shell ids for bpn '%s'".formatted(e.getClass().getSimpleName(), bpn),
e);
} catch (TimeoutException e) {
throw new RegistryServiceException("Timeout during shell ID lookup", e);
throw new RegistryServiceException("Timeout during shell ID lookup for bpn '%s'".formatted(bpn), e);
}
}

Expand Down

0 comments on commit 08811e3

Please sign in to comment.