Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tri 1658 show infected supply chain on first tier level #617

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.eclipse.tractusx.irs.component.Job;
import org.eclipse.tractusx.irs.component.Jobs;
import org.eclipse.tractusx.irs.component.Notification;
import org.eclipse.tractusx.irs.component.Submodel;
import org.eclipse.tractusx.irs.component.Summary;
import org.eclipse.tractusx.irs.component.enums.JobState;
import org.eclipse.tractusx.irs.edc.client.model.notification.EdcNotification;
import org.eclipse.tractusx.irs.edc.client.model.notification.ResponseNotificationContent;
import org.eclipse.tractusx.irs.util.JsonUtil;

/**
* Object to store in cache
Expand All @@ -51,20 +55,25 @@ public class BpnInvestigationJob {

private Jobs jobSnapshot;
private List<String> incidentBpns;
private List<String> unansweredNotifications;
private List<String> answeredNotifications;
//todo request notification content?
private List<Notification> unansweredNotifications;
private List<EdcNotification<ResponseNotificationContent>> answeredNotifications;
private JobState state;

public BpnInvestigationJob(final Jobs jobSnapshot, final List<String> incidentBpns) {
this(jobSnapshot, incidentBpns, new ArrayList<>(), new ArrayList<>(), JobState.RUNNING);
}

private static Jobs extendJobWithSupplyChainSubmodel(final Jobs irsJob,
final SupplyChainImpacted supplyChainImpacted) {
final SupplyChainImpacted supplyChainImpacted, final Integer hops,
final String bpnlOfFirstLevelSupplyChainImpacted) {
final SupplyChainImpactedAspect supplyChainImpactedAspect = SupplyChainImpactedAspect.builder()
.supplyChainImpacted(supplyChainImpacted)
.impactedSuppliersOnFirstTier(Set.of(new SupplyChainImpactedAspect.ImpactedSupplierFirstLevel(bpnlOfFirstLevelSupplyChainImpacted, hops)))
.build();
final Submodel supplyChainImpactedSubmodel = Submodel.builder()
.aspectType(SUPPLY_CHAIN_ASPECT_TYPE)
.payload(Map.of("supplyChainImpacted",
supplyChainImpacted.getDescription()))
.payload(new JsonUtil().asMap(supplyChainImpactedAspect))
.build();

return irsJob.toBuilder()
Expand All @@ -78,13 +87,14 @@ private static Jobs updateLastModified(final Jobs irsJob, final ZonedDateTime la
return irsJob.toBuilder().job(job).build();
}

public BpnInvestigationJob update(final Jobs jobSnapshot, final SupplyChainImpacted newSupplyChain) {
public BpnInvestigationJob update(final Jobs jobSnapshot, final SupplyChainImpacted newSupplyChain,
final Integer hops, final String bpnlOfFirstLevelSuppyChainImpacted) {
final Optional<SupplyChainImpacted> previousSupplyChain = getSupplyChainImpacted();

final SupplyChainImpacted supplyChainImpacted = previousSupplyChain.map(
prevSupplyChain -> prevSupplyChain.or(newSupplyChain)).orElse(newSupplyChain);

this.jobSnapshot = extendJobWithSupplyChainSubmodel(jobSnapshot, supplyChainImpacted);
this.jobSnapshot = extendJobWithSupplyChainSubmodel(jobSnapshot, supplyChainImpacted, hops, bpnlOfFirstLevelSuppyChainImpacted);
this.jobSnapshot = extendSummary(this.jobSnapshot);
this.jobSnapshot = updateLastModified(this.jobSnapshot, ZonedDateTime.now(ZoneOffset.UTC));
return this;
Expand All @@ -111,14 +121,15 @@ private Jobs extendSummary(final Jobs irsJob) {
return irsJob.toBuilder().job(job).build();
}

public BpnInvestigationJob withNotifications(final List<String> notifications) {
public BpnInvestigationJob withNotifications(final List<Notification> notifications) {
this.unansweredNotifications.addAll(notifications);
return this;
}

public BpnInvestigationJob withAnsweredNotification(final String notificationId) {
this.unansweredNotifications.remove(notificationId);
this.answeredNotifications.add(notificationId);
public BpnInvestigationJob withAnsweredNotification(final EdcNotification<ResponseNotificationContent> notification) {
this.unansweredNotifications.removeIf(unansweredNotification -> unansweredNotification.getNotificationId().equals(notification.getHeader().getOriginalNotificationId()));
notification.getContent().incrementHops();
this.answeredNotifications.add(notification);
return this;
}

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

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -33,6 +34,7 @@
import org.eclipse.tractusx.irs.common.auth.SecurityHelperService;
import org.eclipse.tractusx.irs.component.JobHandle;
import org.eclipse.tractusx.irs.component.Jobs;
import org.eclipse.tractusx.irs.component.Notification;
import org.eclipse.tractusx.irs.component.PartChainIdentificationKey;
import org.eclipse.tractusx.irs.component.RegisterBpnInvestigationJob;
import org.eclipse.tractusx.irs.component.RegisterJob;
Expand Down Expand Up @@ -109,12 +111,14 @@ public void handleNotificationCallback(final EdcNotification<ResponseNotificatio

investigationJob.ifPresent(job -> {
final String originalNotificationId = notification.getHeader().getOriginalNotificationId();
job.withAnsweredNotification(originalNotificationId);
job.withAnsweredNotification(notification);
final Optional<String> notificationResult = Optional.ofNullable(notification.getContent().getResult())
.map(Object::toString);

//todo tutaj update, jezeli jest NO to ma wjechać bpn przekazany wcześniej
final SupplyChainImpacted supplyChainImpacted = notificationResult.map(SupplyChainImpacted::fromString)
.orElse(SupplyChainImpacted.UNKNOWN);
final String bpnlOfFirstLevelSupplyChainImpacted = notification.getContent().getBpnl();
log.debug("Received answer for Notification with id '{}' and investigation result '{}'.",
originalNotificationId, supplyChainImpacted);
log.debug("Unanswered notifications left: '{}'", job.getUnansweredNotifications());
Expand All @@ -124,7 +128,14 @@ public void handleNotificationCallback(final EdcNotification<ResponseNotificatio
job = job.complete();
}

bpnInvestigationJobCache.store(jobId, job.update(job.getJobSnapshot(), supplyChainImpacted));
final Optional<ResponseNotificationContent> minHops = job.getAnsweredNotifications()
.stream()
.map(EdcNotification::getContent)
.min(Comparator.comparing(
ResponseNotificationContent::getHops));
final Integer hops = minHops.map(ResponseNotificationContent::getHops).orElse(0);

bpnInvestigationJobCache.store(jobId, job.update(job.getJobSnapshot(), supplyChainImpacted, hops, bpnlOfFirstLevelSupplyChainImpacted));
recursiveNotificationHandler.handleNotification(jobId, supplyChainImpacted);

});
Expand Down Expand Up @@ -160,8 +171,9 @@ private boolean hasUnansweredNotifications(final BpnInvestigationJob job) {

private Predicate<BpnInvestigationJob> investigationJobNotificationPredicate(
final EdcNotification<ResponseNotificationContent> notification) {
return investigationJob -> investigationJob.getUnansweredNotifications()
.contains(notification.getHeader().getOriginalNotificationId());
return investigationJob -> investigationJob.getUnansweredNotifications().stream()
.map(Notification::getNotificationId)
.anyMatch(notificationId -> notificationId.equals(notification.getHeader().getOriginalNotificationId()));
}

private RegisterJob bpnInvestigations(final PartChainIdentificationKey key, final BomLifecycle bomLifecycle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.irs.common.JobProcessingFinishedEvent;
import org.eclipse.tractusx.irs.component.Jobs;
import org.eclipse.tractusx.irs.component.Notification;
import org.eclipse.tractusx.irs.component.Relationship;
import org.eclipse.tractusx.irs.connector.job.JobStore;
import org.eclipse.tractusx.irs.connector.job.MultiTransferJob;
Expand Down Expand Up @@ -109,8 +110,9 @@ public void handleJobProcessingFinishedEvent(final JobProcessingFinishedEvent jo
final InvestigationResult investigationResult = IncidentValidation.getResult(investigationJob, completedJob,
completedJobId);

//todo przekazać id joba startującego
final BpnInvestigationJob investigationJobUpdate = investigationJob.update(
investigationResult.completedJob(), investigationResult.supplyChainImpacted());
investigationResult.completedJob(), investigationResult.supplyChainImpacted(), 0, null);

if (leafNodeIsReached(investigationResult.completedJob()) || supplyChainIsImpacted(
investigationResult.supplyChainImpacted())) {
Expand Down Expand Up @@ -153,7 +155,7 @@ private void triggerInvestigationOnNextLevel(final Jobs completedJob,
log.debug("Triggering investigation on the next level.");
if (anyBpnIsMissingFromRelationship(completedJob)) {
log.error("One or more Relationship items did not contain a BPN.");
investigationJobUpdate.update(completedJob, SupplyChainImpacted.UNKNOWN);
investigationJobUpdate.update(completedJob, SupplyChainImpacted.UNKNOWN, 0, null);
}
// Map<BPN, List<GlobalAssetID>>
final Map<String, List<String>> bpns = getBPNsFromRelationships(completedJob.getRelationships());
Expand All @@ -170,12 +172,12 @@ private void triggerInvestigationOnNextLevel(final Jobs completedJob,
log.debug("BPNs '{}' could not be resolved to an EDC address using DiscoveryService.", unresolvedBPNs);
log.info("Some EDC addresses could not be resolved with DiscoveryService. "
+ "Updating SupplyChainImpacted to {}", SupplyChainImpacted.UNKNOWN);
investigationJobUpdate.update(completedJob, SupplyChainImpacted.UNKNOWN);
investigationJobUpdate.update(completedJob, SupplyChainImpacted.UNKNOWN, 0, null);
recursiveNotificationHandler.handleNotification(investigationJobUpdate.getJobSnapshot().getJob().getId(),
SupplyChainImpacted.UNKNOWN);
} else if (resolvedBPNs.isEmpty()) {
log.info("No BPNs could not be found. Updating SupplyChainImpacted to {}", SupplyChainImpacted.UNKNOWN);
investigationJobUpdate.update(completedJob, SupplyChainImpacted.UNKNOWN);
investigationJobUpdate.update(completedJob, SupplyChainImpacted.UNKNOWN, 0, null);
recursiveNotificationHandler.handleNotification(investigationJobUpdate.getJobSnapshot().getJob().getId(),
SupplyChainImpacted.UNKNOWN);
} else {
Expand All @@ -191,16 +193,18 @@ private void sendNotifications(final Jobs completedJob, final BpnInvestigationJo
if (edcBaseUrl.isEmpty()) {
log.warn("No EDC URL found for BPN '{}'. Setting investigation result to '{}'", bpn,
SupplyChainImpacted.UNKNOWN);
investigationJobUpdate.update(completedJob, SupplyChainImpacted.UNKNOWN);
investigationJobUpdate.update(completedJob, SupplyChainImpacted.UNKNOWN, 0, null);
}
edcBaseUrl.forEach(url -> {
try {
final String notificationId = sendEdcNotification(bpn, url,
investigationJobUpdate.getIncidentBpns(), globalAssetIds);
investigationJobUpdate.withNotifications(Collections.singletonList(notificationId));
// investigationJobUpdate.withNotifications(Collections.singletonList(notificationId));
//todo store bpn here (if this is yes in another step (callback) we will use this bpn number)
investigationJobUpdate.withNotifications(Collections.singletonList(new Notification(notificationId, bpn)));
} catch (final EdcClientException e) {
log.error("Exception during sending EDC notification.", e);
investigationJobUpdate.update(completedJob, SupplyChainImpacted.UNKNOWN);
investigationJobUpdate.update(completedJob, SupplyChainImpacted.UNKNOWN, 0, null);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Locale;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

/**
Expand All @@ -36,6 +37,7 @@ public enum SupplyChainImpacted {
UNKNOWN("Unknown");

@Getter
@JsonValue
private final String description;

SupplyChainImpacted(final String description) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/********************************************************************************
* Copyright (c) 2021,2022,2023
* 2022: ZF Friedrichshafen AG
* 2022: ISTOS GmbH
* 2022,2023: Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* 2022,2023: BOSCH AG
* Copyright (c) 2021,2022,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.irs.ess.service;

import java.util.Set;

import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;

/**
* Supply Chain Impacted aspect representation
*/
@Value
@Builder(toBuilder = true)
@Jacksonized
public class SupplyChainImpactedAspect {
private final SupplyChainImpacted supplyChainImpacted;
private final Set<ImpactedSupplierFirstLevel> impactedSuppliersOnFirstTier;

/**
* BPNLs on first tier level where infection was detected
* @param bpnl
* @param hops
*/
@Builder(toBuilder = true)
@Jacksonized
record ImpactedSupplierFirstLevel(String bpnl, Integer hops) {
}
}
13 changes: 13 additions & 0 deletions irs-api/src/main/java/org/eclipse/tractusx/irs/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
********************************************************************************/
package org.eclipse.tractusx.irs.util;

import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
Expand Down Expand Up @@ -75,6 +78,16 @@ public String asString(final Object input) {
}
}

/**
* Serialize an object as a Map {@link Map}.
*
* @param input the object to serialize.
* @return the Map representation of the object
*/
public Map<String, Object> asMap(final Object input) {
return MAPPER.convertValue(input, new TypeReference<>() { });
}

/**
* Deserialize an object from a JSON {@link String}.
*
Expand Down
Loading