Skip to content

Commit

Permalink
Merge pull request #1012 from catenax-ng/feat/412-cleanup-edc-logic
Browse files Browse the repository at this point in the history
chore(tx-backend): #412 cleanup EDC logic and use IRS lib implementation instead
  • Loading branch information
ds-ext-sceronik authored Feb 26, 2024
2 parents c656f30 + 8d3bae5 commit e50a8f1
Show file tree
Hide file tree
Showing 32 changed files with 182 additions and 1,232 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Added

### Changed
- sring-core bumped from 6.0.16 to 6.0.17

### Removed
- removed EDC notification asset classes and replaced with IRS lib implementation

## [10.5.0 - 22.02.2024]

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SPDX-License-Identifier: Apache-2.0

<properties>
<springboot.version>3.1.8</springboot.version>
<spring-core.version>6.0.16</spring-core.version>
<spring-core.version>6.0.17</spring-core.version>
<spring-security-config.version>6.1.3</spring-security-config.version>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.irs.edc.client.EdcConfiguration;
import org.eclipse.tractusx.irs.edc.client.asset.EdcAssetService;
import org.eclipse.tractusx.irs.edc.client.contract.service.EdcContractDefinitionService;
import org.eclipse.tractusx.irs.edc.client.policy.AcceptedPoliciesProvider;
import org.eclipse.tractusx.irs.edc.client.policy.AcceptedPolicy;
import org.eclipse.tractusx.irs.edc.client.policy.Constraint;
Expand All @@ -41,6 +44,8 @@
import org.eclipse.tractusx.irs.edc.client.policy.Permission;
import org.eclipse.tractusx.irs.edc.client.policy.Policy;
import org.eclipse.tractusx.irs.edc.client.policy.PolicyType;
import org.eclipse.tractusx.irs.edc.client.policy.service.EdcPolicyDefinitionService;
import org.eclipse.tractusx.irs.edc.client.transformer.EdcTransformer;
import org.eclipse.tractusx.traceability.assets.infrastructure.base.irs.IrsService;
import org.eclipse.tractusx.traceability.assets.infrastructure.base.irs.model.response.PolicyResponse;
import org.eclipse.tractusx.traceability.common.properties.TraceabilityProperties;
Expand All @@ -58,6 +63,7 @@
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecutor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring6.SpringTemplateEngine;
Expand Down Expand Up @@ -209,4 +215,19 @@ public void onEntryRemovedEvent(EntryRemovedEvent<Retry> entryRemoveEvent) {
}
};
}

@Bean
public EdcAssetService edcNotificationAssetService(EdcConfiguration edcConfiguration, EdcTransformer edcTransformer, RestTemplate edcNotificationAssetRestTemplate) {
return new EdcAssetService(edcTransformer, edcConfiguration, edcNotificationAssetRestTemplate);
}

@Bean
public EdcPolicyDefinitionService edcPolicyDefinitionService(EdcConfiguration edcConfiguration, RestTemplate edcNotificationAssetRestTemplate) {
return new EdcPolicyDefinitionService(edcConfiguration, edcNotificationAssetRestTemplate);
}

@Bean
public EdcContractDefinitionService edcContractDefinitionService(EdcConfiguration edcConfiguration, RestTemplate edcNotificationAssetRestTemplate) {
return new EdcContractDefinitionService(edcConfiguration, edcNotificationAssetRestTemplate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ public RestTemplate edcRestTemplate(@Autowired EdcProperties edcProperties) {
.build();
}

@Bean
public RestTemplate edcNotificationAssetRestTemplate(@Autowired EdcProperties edcProperties) {
return new RestTemplateBuilder()
.rootUri(edcProperties.getProviderEdcUrl())
.defaultHeader("Accept", MediaType.APPLICATION_JSON_VALUE)
.defaultHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(EDC_API_KEY_HEADER_NAME, edcProperties.getApiAuthKey())
.setConnectTimeout(Duration.ofSeconds(10L))
.setReadTimeout(Duration.ofSeconds(25L))
.build();
}

/* RestTemplate used by trace x for the notification transfer to the edc controlplane including edc api key*/
@Bean
public RestTemplate edcNotificationTemplate(@Autowired EdcProperties edcProperties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ private CatalogItem getCatalogItem(final QualityNotificationMessage notification
.build())
.build()
).stream()
.filter(catalogItem -> policyCheckerService.isValid(catalogItem.getPolicy()))
.filter(catalogItem -> {
log.info("-- catalog item check --");
log.info("Item {}: {}", catalogItem.getItemId(), catalogItem);
boolean isValid = policyCheckerService.isValid(catalogItem.getPolicy());
log.info("IsValid : {}", isValid);
return isValid;
})
.findFirst()
.orElseThrow();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,62 @@
********************************************************************************/
package org.eclipse.tractusx.traceability.qualitynotification.domain.contract;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.irs.edc.client.asset.EdcAssetService;
import org.eclipse.tractusx.irs.edc.client.asset.model.NotificationType;
import org.eclipse.tractusx.irs.edc.client.asset.model.exception.CreateEdcAssetException;
import org.eclipse.tractusx.irs.edc.client.asset.model.exception.DeleteEdcAssetException;
import org.eclipse.tractusx.irs.edc.client.contract.model.exception.CreateEdcContractDefinitionException;
import org.eclipse.tractusx.irs.edc.client.contract.service.EdcContractDefinitionService;
import org.eclipse.tractusx.irs.edc.client.policy.model.exception.CreateEdcPolicyDefinitionException;
import org.eclipse.tractusx.irs.edc.client.policy.model.exception.DeleteEdcPolicyDefinitionException;
import org.eclipse.tractusx.irs.edc.client.policy.service.EdcPolicyDefinitionService;
import org.eclipse.tractusx.traceability.common.properties.TraceabilityProperties;
import org.eclipse.tractusx.traceability.qualitynotification.application.contract.model.CreateNotificationContractException;
import org.eclipse.tractusx.traceability.qualitynotification.application.contract.model.CreateNotificationContractRequest;
import org.eclipse.tractusx.traceability.qualitynotification.application.contract.model.CreateNotificationContractResponse;
import org.eclipse.tractusx.traceability.qualitynotification.application.contract.model.NotificationMethod;
import org.eclipse.tractusx.traceability.qualitynotification.domain.contract.asset.model.CreateEdcAssetException;
import org.eclipse.tractusx.traceability.qualitynotification.domain.contract.asset.service.EdcNotificationAssetService;
import org.eclipse.tractusx.traceability.qualitynotification.domain.contract.contract.model.CreateEdcContractDefinitionException;
import org.eclipse.tractusx.traceability.qualitynotification.domain.contract.contract.service.EdcContractDefinitionService;
import org.eclipse.tractusx.traceability.qualitynotification.domain.contract.policy.model.CreateEdcPolicyDefinitionException;
import org.eclipse.tractusx.traceability.qualitynotification.domain.contract.policy.service.EdcPolicyDefinitionService;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@AllArgsConstructor
public class EdcNotificationContractService {

private final EdcNotificationAssetService edcNotificationAssetService;
private final EdcAssetService edcNotificationAssetService;
private final EdcPolicyDefinitionService edcPolicyDefinitionService;
private final EdcContractDefinitionService edcContractDefinitionService;
private final TraceabilityProperties traceabilityProperties;

private static final String TRACE_FOSS_QUALITY_NOTIFICATION_INVESTIGATION_URL_TEMPLATE = "/api/qualitynotifications/%s";
private static final String TRACE_FOSS_QUALITY_NOTIFICATION_ALERT_URL_TEMPLATE = "/api/qualityalerts/%s";

public CreateNotificationContractResponse handle(CreateNotificationContractRequest request) {

NotificationMethod notificationMethod = request.notificationMethod();

log.info("Creating EDC asset notification contract for {} method and {} notification type", notificationMethod.getValue(), request.notificationType().getValue());

String notificationAssetId = "";

String notificationAssetId;
try {
notificationAssetId = edcNotificationAssetService.createNotificationAsset(notificationMethod, request.notificationType());
notificationAssetId = edcNotificationAssetService.createNotificationAsset(
createBaseUrl(request.notificationType(), request.notificationMethod()),
request.notificationType().name() + " " + request.notificationMethod().name(),
org.eclipse.tractusx.irs.edc.client.asset.model.NotificationMethod.valueOf(request.notificationMethod().name()),
NotificationType.valueOf(request.notificationType().name()));
} catch (CreateEdcAssetException e) {
throw new CreateNotificationContractException(e);
} catch (JsonProcessingException e2) {
log.error(e2.toString());
}


String accessPolicyId = "";
try {
accessPolicyId = edcPolicyDefinitionService.createAccessPolicy();
accessPolicyId = edcPolicyDefinitionService.createAccessPolicy(traceabilityProperties.getRightOperand());
} catch (CreateEdcPolicyDefinitionException e) {
revertNotificationAsset(notificationAssetId);
throw new CreateNotificationContractException(e);
} catch (JsonProcessingException e2) {
log.error(e2.toString());
}

String contractDefinitionId = "";
Expand All @@ -78,11 +86,13 @@ public CreateNotificationContractResponse handle(CreateNotificationContractReque
revertNotificationAsset(notificationAssetId);

throw new CreateNotificationContractException(e);
} catch (JsonProcessingException e2) {
log.error(e2.toString());
}

log.info("Created notification contract for {} notification asset id, access policy id {} and contract definition id {}", notificationAssetId, accessPolicyId, contractDefinitionId);
log.info(
"Created notification contract for {} notification asset id, access policy id {} and contract definition id {}",
notificationAssetId,
accessPolicyId,
contractDefinitionId);

return new CreateNotificationContractResponse(
notificationAssetId,
Expand All @@ -94,12 +104,25 @@ public CreateNotificationContractResponse handle(CreateNotificationContractReque
private void revertAccessPolicy(String accessPolicyId) {
log.info("Removing {} access policy", accessPolicyId);

edcPolicyDefinitionService.deleteAccessPolicy(accessPolicyId);
try {
edcPolicyDefinitionService.deleteAccessPolicy(accessPolicyId);
} catch (DeleteEdcPolicyDefinitionException e) {
throw new CreateNotificationContractException(e);
}
}

private void revertNotificationAsset(String notificationAssetId) {
log.info("Removing {} notification asset", notificationAssetId);

edcNotificationAssetService.deleteNotificationAsset(notificationAssetId);
try {
edcNotificationAssetService.deleteAsset(notificationAssetId);
} catch (DeleteEdcAssetException e) {
throw new CreateNotificationContractException(e);
}
}

private String createBaseUrl(org.eclipse.tractusx.traceability.qualitynotification.application.contract.model.NotificationType notificationType, NotificationMethod notificationMethod) {
final String template = notificationType.equals(org.eclipse.tractusx.traceability.qualitynotification.application.contract.model.NotificationType.QUALITY_ALERT) ? TRACE_FOSS_QUALITY_NOTIFICATION_ALERT_URL_TEMPLATE : TRACE_FOSS_QUALITY_NOTIFICATION_INVESTIGATION_URL_TEMPLATE;
return traceabilityProperties.getUrl() + template.formatted(notificationMethod.getValue());
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit e50a8f1

Please sign in to comment.